Skip to content

Commit 16106d1

Browse files
committed
handle foreignObject correctly, default to svg namespace for top-level svg elements - fixes sveltejs#2298
1 parent d637211 commit 16106d1

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

src/compile/nodes/Element.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,11 @@ export default class Element extends Node {
9494
this.name = info.name;
9595

9696
const parent_element = parent.find_nearest(/^Element/);
97-
this.namespace = this.name === 'svg' ?
98-
namespaces.svg :
99-
parent_element ? parent_element.namespace : this.component.namespace;
100-
101-
if (!this.namespace && svg.test(this.name)) {
102-
this.component.warn(this, {
103-
code: `missing-namespace`,
104-
message: `<${this.name}> is an SVG element – did you forget to add <svelte:options namespace="svg"/> ?`
105-
});
106-
}
97+
this.namespace = this.name === 'svg' || (!parent_element && svg.test(this.name))
98+
? namespaces.svg
99+
: this.name === 'foreignObject'
100+
? namespaces.html
101+
: parent_element ? parent_element.namespace : this.component.namespace;
107102

108103
if (this.name === 'textarea') {
109104
if (info.children.length > 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
html: `
3+
<svg>
4+
<foreignObject x="0" y="0" width="100" height="100">
5+
<p>some text</p>
6+
</foreignObject>
7+
</svg>
8+
`,
9+
10+
test({ assert, target }) {
11+
const p = target.querySelector('p');
12+
assert.equal(p.namespaceURI, 'http://www.w3.org/1999/xhtml');
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<svg>
2+
<foreignObject x="0" y="0" width="100" height="100">
3+
<p>some text</p>
4+
</foreignObject>
5+
</svg>

test/validator/samples/svg-child-component-undeclared-namespace/input.svelte

-8
This file was deleted.

test/validator/samples/svg-child-component-undeclared-namespace/warnings.json

-15
This file was deleted.

0 commit comments

Comments
 (0)