Skip to content

Commit d58d3be

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 f2f5d2c commit d58d3be

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
@@ -95,8 +95,11 @@ util.convertToTspans = function(_context, _callback) {
9595
visibility: 'visible',
9696
'white-space': 'pre'
9797
});
98+
9899
result = _context.appendSVG(converted);
100+
99101
if(!result) _context.text(str);
102+
100103
if(_context.select('a').size()) {
101104
// at least in Chrome, pointer-events does not seem
102105
// to be honored in children of <text> elements
@@ -246,6 +249,7 @@ function convertToSVG(_str) {
246249
var match = d.match(/<(\/?)([^ >]*)\s*(.*)>/i),
247250
tag = match && match[2].toLowerCase(),
248251
style = TAG_STYLES[tag];
252+
249253
if(style !== undefined) {
250254
var close = match[1],
251255
extra = match[3],
@@ -263,12 +267,18 @@ function convertToSVG(_str) {
263267
if(close) return '</a>';
264268
else if(extra.substr(0, 4).toLowerCase() !== 'href') return '<a>';
265269
else {
266-
var dummyAnchor = document.createElement('a');
267-
dummyAnchor.href = extra.substr(4).replace(/["'=]/g, '');
270+
// remove quotes, leading '=', replace '&' with '&amp;'
271+
var href = extra.substr(4)
272+
.replace(/["']/g, '')
273+
.replace(/=/, '')
274+
.replace(/&/g, '&amp;');
268275

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

271-
return '<a xlink:show="new" xlink:href' + extra.substr(4) + '>';
281+
return '<a xlink:show="new" xlink:href="' + href + '">';
272282
}
273283
}
274284
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)