Skip to content

Commit 0a1526d

Browse files
committed
lib: handle '=' and '&' in anchor href
- fixes #642 - strip only the leading '=' - replace & with '&' to make DOMParser happy - wrap resulting href in ""
1 parent 16e0fc2 commit 0a1526d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/lib/svg_text_utils.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ util.convertToTspans = function(_context, _callback) {
9494
visibility: 'visible',
9595
'white-space': 'pre'
9696
});
97+
9798
result = _context.appendSVG(converted);
99+
98100
if(!result) _context.text(str);
101+
99102
if(_context.select('a').size()) {
100103
// at least in Chrome, pointer-events does not seem
101104
// to be honored in children of <text> elements
@@ -245,6 +248,7 @@ function convertToSVG(_str) {
245248
var match = d.match(/<(\/?)([^ >]*)\s*(.*)>/i),
246249
tag = match && match[2].toLowerCase(),
247250
style = TAG_STYLES[tag];
251+
248252
if(style !== undefined) {
249253
var close = match[1],
250254
extra = match[3],
@@ -262,12 +266,18 @@ function convertToSVG(_str) {
262266
if(close) return '</a>';
263267
else if(extra.substr(0,4).toLowerCase() !== 'href') return '<a>';
264268
else {
265-
var dummyAnchor = document.createElement('a');
266-
dummyAnchor.href = extra.substr(4).replace(/["'=]/g, '');
269+
// remove quotes, leading '=', replace '&' with '&amp;'
270+
var href = extra.substr(4)
271+
.replace(/["']/g, '')
272+
.replace(/=/, '')
273+
.replace(/&/g, '&amp;');
267274

275+
// check protocol
276+
var dummyAnchor = document.createElement('a');
277+
dummyAnchor.href = href;
268278
if(PROTOCOLS.indexOf(dummyAnchor.protocol) === -1) return '<a>';
269279

270-
return '<a xlink:show="new" xlink:href' + extra.substr(4) + '>';
280+
return '<a xlink:show="new" xlink:href="' + href + '">';
271281
}
272282
}
273283
else if(tag === 'br') return '<br>';

test/jasmine/tests/svg_text_utils_test.js

+15
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,20 @@ describe('svg+text utils', function() {
104104
assertAnchorAttrs(node);
105105
assertAnchorLink(node, 'mailto:[email protected]');
106106
});
107+
108+
it('should keep query parameters in href', function() {
109+
var textCases = [
110+
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>',
111+
'<a href="https://abc.com/myFeature.jsp?name=abc&amp;pwd=def">abc.com?shared-key</a>'
112+
];
113+
114+
textCases.forEach(function(textCase) {
115+
var node = mockTextSVGElement(textCase);
116+
117+
assertAnchorAttrs(node);
118+
expect(node.text()).toEqual('abc.com?shared-key');
119+
assertAnchorLink(node, 'https://abc.com/myFeature.jsp?name=abc&pwd=def');
120+
});
121+
});
107122
});
108123
});

0 commit comments

Comments
 (0)