Skip to content

Commit

Permalink
lib: handle '=' and '&' in anchor href
Browse files Browse the repository at this point in the history
- fixes #642
- strip only the leading '='
- replace & with '&' to make DOMParser happy
- wrap resulting href in ""
  • Loading branch information
etpinard committed Jul 29, 2016
1 parent 16e0fc2 commit 0a1526d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ util.convertToTspans = function(_context, _callback) {
visibility: 'visible',
'white-space': 'pre'
});

result = _context.appendSVG(converted);

if(!result) _context.text(str);

if(_context.select('a').size()) {
// at least in Chrome, pointer-events does not seem
// to be honored in children of <text> elements
Expand Down Expand Up @@ -245,6 +248,7 @@ function convertToSVG(_str) {
var match = d.match(/<(\/?)([^ >]*)\s*(.*)>/i),
tag = match && match[2].toLowerCase(),
style = TAG_STYLES[tag];

if(style !== undefined) {
var close = match[1],
extra = match[3],
Expand All @@ -262,12 +266,18 @@ function convertToSVG(_str) {
if(close) return '</a>';
else if(extra.substr(0,4).toLowerCase() !== 'href') return '<a>';
else {
var dummyAnchor = document.createElement('a');
dummyAnchor.href = extra.substr(4).replace(/["'=]/g, '');
// remove quotes, leading '=', replace '&' with '&amp;'
var href = extra.substr(4)
.replace(/["']/g, '')
.replace(/=/, '')
.replace(/&/g, '&amp;');

// check protocol
var dummyAnchor = document.createElement('a');
dummyAnchor.href = href;
if(PROTOCOLS.indexOf(dummyAnchor.protocol) === -1) return '<a>';

return '<a xlink:show="new" xlink:href' + extra.substr(4) + '>';
return '<a xlink:show="new" xlink:href="' + href + '">';
}
}
else if(tag === 'br') return '<br>';
Expand Down
15 changes: 15 additions & 0 deletions test/jasmine/tests/svg_text_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,20 @@ describe('svg+text utils', function() {
assertAnchorAttrs(node);
assertAnchorLink(node, 'mailto:support@plot.ly');
});

it('should keep query parameters in href', function() {
var textCases = [
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>',
'<a href="https://abc.com/myFeature.jsp?name=abc&amp;pwd=def">abc.com?shared-key</a>'
];

textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase);

assertAnchorAttrs(node);
expect(node.text()).toEqual('abc.com?shared-key');
assertAnchorLink(node, 'https://abc.com/myFeature.jsp?name=abc&pwd=def');
});
});
});
});

0 comments on commit 0a1526d

Please sign in to comment.