Skip to content

Commit 3d058ad

Browse files
author
Emily Stark
committed
Make <a> tags SVG elements when they have xlink:href.
Fixes meteor#2178.
1 parent ecb366f commit 3d058ad

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/spacebars-tests/template_tests.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,9 @@
822822
{{/with}}
823823
{{/each}}
824824
</template>
825+
826+
<template name="spacebars_test_svg_anchor">
827+
<svg>
828+
<a xlink:href="http://www.example.com">Foo</a>
829+
</svg>
830+
</template>

packages/spacebars-tests/template_tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,3 +2256,19 @@ Tinytest.add(
22562256
test.equal(canonicalizeHtml(div.innerHTML), "parent");
22572257
}
22582258
);
2259+
2260+
Tinytest.add(
2261+
"spacebars - SVG <a> elements",
2262+
function (test) {
2263+
if (! document.createElementNS) {
2264+
// IE 8
2265+
return;
2266+
}
2267+
2268+
var tmpl = Template.spacebars_test_svg_anchor;
2269+
var div = renderToDiv(tmpl);
2270+
2271+
var anchNamespace = $(div).find("a").get(0).namespaceURI;
2272+
test.equal(anchNamespace, "http://www.w3.org/2000/svg");
2273+
}
2274+
);

packages/ui/render.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,24 @@ UI.InTemplateScope.prototype.toText = function (textMode, parentComponent) {
302302
return HTML.toText(this.content, textMode, this.parentPtr);
303303
};
304304

305+
var isSVGAnchor = function (node) {
306+
// We generally aren't able to detect SVG <a> elements because
307+
// if "A" were in our list of known svg element names, then all
308+
// <a> nodes would be created using
309+
// `document.createElementNS`. But in the special case of <a
310+
// xlink:href="...">, we can at least detect that attribute and
311+
// create an SVG <a> tag in that case.
312+
//
313+
// However, we still have a general problem of knowing when to
314+
// use document.createElementNS and when to use
315+
// document.createElement; for example, font tags will always
316+
// be created as SVG elements which can cause other
317+
// problems. #1977
318+
return (node.tagName === "a" &&
319+
node.attrs &&
320+
node.attrs["xlink:href"] !== undefined);
321+
};
322+
305323
// Convert the pseudoDOM `node` into reactive DOM nodes and insert them
306324
// into the element or DomRange `parent`, before the node or id `before`.
307325
var materialize = function (node, parent, before, parentComponent) {
@@ -355,7 +373,9 @@ var materialize = function (node, parent, before, parentComponent) {
355373
} else if (node instanceof HTML.Tag) {
356374
var tagName = node.tagName;
357375
var elem;
358-
if (HTML.isKnownSVGElement(tagName) && document.createElementNS) {
376+
if ((HTML.isKnownSVGElement(tagName) ||
377+
isSVGAnchor(node)) &&
378+
document.createElementNS) {
359379
elem = document.createElementNS('http://www.w3.org/2000/svg', tagName);
360380
} else {
361381
elem = document.createElement(node.tagName);

0 commit comments

Comments
 (0)