Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Specify svg namespace if {@html} is used in svg #7464

Merged
merged 3 commits into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/compiler/compile/render_dom/wrappers/RawMustacheTag.ts
@@ -1,8 +1,10 @@
import { namespaces } from './../../../utils/namespaces';
import { b, x } from 'code-red';
import Renderer from '../Renderer';
import Block from '../Block';
import Tag from './shared/Tag';
import Wrapper from './shared/Wrapper';
import Element from '../../nodes/Element';
import MustacheTag from '../../nodes/MustacheTag';
import RawMustacheTag from '../../nodes/RawMustacheTag';
import { is_head } from './shared/is_head';
Expand Down Expand Up @@ -51,9 +53,12 @@ export default class RawMustacheTagWrapper extends Tag {

const update_anchor = needs_anchor ? html_anchor : this.next ? this.next.var : 'null';

block.chunks.create.push(b`${html_tag} = new @HtmlTag();`);
const parent_element = this.node.find_nearest(/^Element/) as Element;
const is_svg = parent_element && parent_element.namespace === namespaces.svg;
block.chunks.create.push(b`${html_tag} = new @HtmlTag(${is_svg ? 'true' : 'false'});`);

if (this.renderer.options.hydratable) {
block.chunks.claim.push(b`${html_tag} = @claim_html_tag(${_parent_nodes});`);
block.chunks.claim.push(b`${html_tag} = @claim_html_tag(${_parent_nodes}, ${is_svg ? 'true' : 'false'});`);
}
block.chunks.hydrate.push(b`${html_tag}.a = ${update_anchor};`);
block.chunks.mount.push(b`${html_tag}.m(${init}, ${parent_node || '#target'}, ${parent_node ? null : '#anchor'});`);
Expand Down
30 changes: 19 additions & 11 deletions src/runtime/internal/dom.ts
Expand Up @@ -492,12 +492,13 @@ function find_comment(nodes, text, start) {
return nodes.length;
}

export function claim_html_tag(nodes) {

export function claim_html_tag(nodes, is_svg: boolean) {
// find html opening tag
const start_index = find_comment(nodes, 'HTML_TAG_START', 0);
const end_index = find_comment(nodes, 'HTML_TAG_END', start_index);
if (start_index === end_index) {
return new HtmlTagHydration();
return new HtmlTagHydration(undefined, is_svg);
}

init_claim_info(nodes);
Expand All @@ -509,7 +510,7 @@ export function claim_html_tag(nodes) {
n.claim_order = nodes.claim_info.total_claimed;
nodes.claim_info.total_claimed += 1;
}
return new HtmlTagHydration(claimed_nodes);
return new HtmlTagHydration(claimed_nodes, is_svg);
}

export function set_data(text, data) {
Expand Down Expand Up @@ -645,26 +646,33 @@ export function query_selector_all(selector: string, parent: HTMLElement = docum
}

export class HtmlTag {
private is_svg = false;
// parent for creating node
e: HTMLElement;
e: HTMLElement | SVGElement;
// html tag nodes
n: ChildNode[];
// target
t: HTMLElement;
t: HTMLElement | SVGElement;
// anchor
a: HTMLElement;
a: HTMLElement | SVGElement;

constructor() {
constructor(is_svg: boolean = false) {
this.is_svg = is_svg;
this.e = this.n = null;
}

c(html: string) {
this.h(html);
}

m(html: string, target: HTMLElement, anchor: HTMLElement = null) {
m(
html: string,
target: HTMLElement | SVGElement,
anchor: HTMLElement | SVGElement = null
) {
if (!this.e) {
this.e = element(target.nodeName as keyof HTMLElementTagNameMap);
if (this.is_svg) this.e = svg_element(target.nodeName as keyof SVGElementTagNameMap);
else this.e = element(target.nodeName as keyof HTMLElementTagNameMap);
this.t = target;
this.c(html);
}
Expand Down Expand Up @@ -698,8 +706,8 @@ export class HtmlTagHydration extends HtmlTag {
// hydration claimed nodes
l: ChildNode[] | void;

constructor(claimed_nodes?: ChildNode[]) {
super();
constructor(claimed_nodes?: ChildNode[], is_svg: boolean = false) {
super(is_svg);
this.e = this.n = null;
this.l = claimed_nodes;
}
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/each-block-changed-check/expected.js
Expand Up @@ -52,7 +52,7 @@ function create_each_block(ctx) {
t4 = text(t4_value);
t5 = text(" ago:");
t6 = space();
html_tag = new HtmlTag();
html_tag = new HtmlTag(false);
attr(span, "class", "meta");
html_tag.a = null;
attr(div, "class", "comment");
Expand Down Expand Up @@ -170,4 +170,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
35 changes: 35 additions & 0 deletions test/runtime/samples/svg-html-tag/_config.js
@@ -0,0 +1,35 @@
export default {
html: `
<svg width="100" height="60">
<circle cx="25" cy="30" r="24" fill="#FFD166"></circle>
<circle cx="75" cy="30" r="24" fill="#118AB2"></circle>
</svg>
`,
test({ assert, target, component }) {

let svg = target.querySelector('svg');
let circles = target.querySelectorAll('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(2, circles.length);
assert.equal(circles[0].namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circles[1].namespaceURI, 'http://www.w3.org/2000/svg');

component.width = 200;
component.height = 120;
assert.htmlEqual(
target.innerHTML,
`
<svg width="200" height="120">
<circle cx="50" cy="60" r="24" fill="#FFD166"></circle>
<circle cx="150" cy="60" r="24" fill="#118AB2"></circle>
</svg>
`
);
svg = target.querySelector('svg');
circles = target.querySelectorAll('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(2, circles.length);
assert.equal(circles[0].namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circles[1].namespaceURI, 'http://www.w3.org/2000/svg');
}
};
10 changes: 10 additions & 0 deletions test/runtime/samples/svg-html-tag/main.svelte
@@ -0,0 +1,10 @@
<script>
export let width = 100
export let height = 60
$: circle = `<circle cx="${width/4}" cy="${height/2}" r="24" fill="#FFD166"/>`
</script>

<svg width="{width}" height="{height}">
{@html circle}
<circle cx="{width/4*3}" cy="{height/2}" r="24" fill="#118AB2"/>
</svg>
39 changes: 39 additions & 0 deletions test/runtime/samples/svg-html-tag2/_config.js
@@ -0,0 +1,39 @@
export default {
html: `
<svg width="100" height="60">
<rect>
<circle cx="25" cy="30" r="24" fill="#FFD166"></circle>
<circle cx="75" cy="30" r="24" fill="#118AB2"></circle>
</rect>
</svg>
`,
test({ assert, target, component }) {

let svg = target.querySelector('svg');
let circles = target.querySelectorAll('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(2, circles.length);
assert.equal(circles[0].namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circles[1].namespaceURI, 'http://www.w3.org/2000/svg');

component.width = 200;
component.height = 120;
assert.htmlEqual(
target.innerHTML,
`
<svg width="200" height="120">
<rect>
<circle cx="50" cy="60" r="24" fill="#FFD166"></circle>
<circle cx="150" cy="60" r="24" fill="#118AB2"></circle>
</rect>
</svg>
`
);
svg = target.querySelector('svg');
circles = target.querySelectorAll('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(2, circles.length);
assert.equal(circles[0].namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circles[1].namespaceURI, 'http://www.w3.org/2000/svg');
}
};
12 changes: 12 additions & 0 deletions test/runtime/samples/svg-html-tag2/main.svelte
@@ -0,0 +1,12 @@
<script>
export let width = 100
export let height = 60
$: circle = `<circle cx="${width/4}" cy="${height/2}" r="24" fill="#FFD166"/>`
</script>

<svg width="{width}" height="{height}">
<rect>
{@html circle}
<circle cx="{width/4*3}" cy="{height/2}" r="24" fill="#118AB2"/>
</rect>
</svg>
33 changes: 33 additions & 0 deletions test/runtime/samples/svg-html-tag3/_config.js
@@ -0,0 +1,33 @@
export default {
html: `
<svg>
<foreignObject>
<circle cx="25" cy="30" r="24" fill="#FFD166"></circle>
</foreignObject>
</svg>
`,
test({ assert, target, component }) {

let svg = target.querySelector('svg');
let circle = target.querySelector('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circle.namespaceURI, 'http://www.w3.org/1999/xhtml');

component.width = 200;
component.height = 120;
assert.htmlEqual(
target.innerHTML,
`
<svg>
<foreignObject>
<circle cx="50" cy="60" r="24" fill="#FFD166"></circle>
</foreignObject>
</svg>
`
);
svg = target.querySelector('svg');
circle = target.querySelector('circle');
assert.equal(svg.namespaceURI, 'http://www.w3.org/2000/svg');
assert.equal(circle.namespaceURI, 'http://www.w3.org/1999/xhtml');
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/svg-html-tag3/main.svelte
@@ -0,0 +1,11 @@
<script>
export let width = 100
export let height = 60
$: circle = `<circle cx="${width/4}" cy="${height/2}" r="24" fill="#FFD166"/>`
</script>

<svg>
<foreignObject>
{@html circle}
</foreignObject>
</svg>