Skip to content

Commit 03950d8

Browse files
authored
Merge pull request #2471 from plotly/allow-encoded-uris-in-svg-text
Handle HTML links with encoded URIs correctly in svg text labels #2239
2 parents cea4fd4 + e40c795 commit 03950d8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/lib/svg_text_utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,10 @@ function buildSVGText(containerNode, str) {
469469
var dummyAnchor = document.createElement('a');
470470
dummyAnchor.href = href;
471471
if(PROTOCOLS.indexOf(dummyAnchor.protocol) !== -1) {
472-
nodeSpec.href = encodeURI(href);
472+
// Decode href to allow both already encoded and not encoded
473+
// URIs. Without decoding prior encoding, an already encoded
474+
// URI would be encoded twice producing a semantically different URI.
475+
nodeSpec.href = encodeURI(decodeURI(href));
473476
nodeSpec.target = getQuotedMatch(extra, TARGETMATCH) || '_blank';
474477
nodeSpec.popup = getQuotedMatch(extra, POPUPMATCH);
475478
}

test/jasmine/tests/svg_text_utils_test.js

+10
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ describe('svg+text utils', function() {
173173
});
174174
});
175175

176+
it('allows encoded URIs in href', function() {
177+
var node = mockTextSVGElement(
178+
'<a href="https://example.com/?q=date%20%3E=%202018-01-01">click</a>'
179+
);
180+
181+
expect(node.text()).toEqual('click');
182+
assertAnchorAttrs(node);
183+
assertAnchorLink(node, 'https://example.com/?q=date%20%3E=%202018-01-01');
184+
});
185+
176186
it('accepts `target` with links and tries to translate it to `xlink:show`', function() {
177187
var specs = [
178188
{target: '_blank', show: 'new'},

0 commit comments

Comments
 (0)