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

add optimization of static <svelte:element> and fix a bug #1

Merged
merged 1 commit into from
Dec 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export default class Element extends Node {
this.tag_expr = new Expression(component, this, scope, info.tag);
} else {
this.tag_expr = new Expression(component, this, scope, string_literal(info.tag) as Literal);
this.name = info.tag;
}
} else {
this.tag_expr = new Expression(component, this, scope, string_literal(this.name) as Literal);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export default class ElementWrapper extends Wrapper {
node.styles.length > 0 ||
this.node.name === 'option' ||
node.tag_expr.dynamic_dependencies().length ||
node.is_dynamic_element ||
renderer.options.dev
) {
this.parent.cannot_use_innerhtml(); // need to use add_location
Expand Down Expand Up @@ -1176,7 +1177,7 @@ function to_html(wrappers: Array<ElementWrapper | TextWrapper | MustacheTagWrapp
} else if (wrapper.node.name === 'noscript') {
// do nothing
} else {
const nodeName = wrapper.node.name === 'svelte:element' && wrapper.node.tag_expr.node.type === 'Literal' ? wrapper.node.tag_expr.node.value : wrapper.node.name;
const nodeName = wrapper.node.name;

// element
state.quasi.value.raw += `<${nodeName}`;
Expand Down
23 changes: 14 additions & 9 deletions test/js/samples/svelte-element-svg/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function create_dynamic_element_1(ctx) {
return { c: noop, m: noop, p: noop, d: noop };
}

// (1:0) <svelte:element this="svg" xmlns="http://www.w3.org/2000/svg">
// (5:0) <svelte:element this={tag.svg} xmlns="http://www.w3.org/2000/svg">
function create_dynamic_element(ctx) {
let svelte_element1;
let svelte_element0;
Expand All @@ -38,8 +38,8 @@ function create_dynamic_element(ctx) {

return {
c() {
svelte_element1 = svg_element("svg");
svelte_element0 = svg_element("path");
svelte_element1 = svg_element(/*tag*/ ctx[0].svg);
svelte_element0 = svg_element(/*tag*/ ctx[0].path);
set_svg_attributes(svelte_element0, svelte_element0_data);
set_svg_attributes(svelte_element1, svelte_element1_data);
},
Expand All @@ -60,9 +60,9 @@ function create_dynamic_element(ctx) {
}

function create_fragment(ctx) {
let previous_tag = "svg";
let previous_tag = /*tag*/ ctx[0].svg;
let svelte_element1_anchor;
let svelte_element1 = "svg" && create_dynamic_element(ctx);
let svelte_element1 = /*tag*/ ctx[0].svg && create_dynamic_element(ctx);

return {
c() {
Expand All @@ -74,12 +74,12 @@ function create_fragment(ctx) {
insert(target, svelte_element1_anchor, anchor);
},
p(ctx, [dirty]) {
if ("svg") {
if (/*tag*/ ctx[0].svg) {
if (!previous_tag) {
svelte_element1 = create_dynamic_element(ctx);
svelte_element1.c();
svelte_element1.m(svelte_element1_anchor.parentNode, svelte_element1_anchor);
} else if (safe_not_equal(previous_tag, "svg")) {
} else if (safe_not_equal(previous_tag, /*tag*/ ctx[0].svg)) {
svelte_element1.d(1);
svelte_element1 = create_dynamic_element(ctx);
svelte_element1.c();
Expand All @@ -92,7 +92,7 @@ function create_fragment(ctx) {
svelte_element1 = null;
}

previous_tag = "svg";
previous_tag = /*tag*/ ctx[0].svg;
},
i: noop,
o: noop,
Expand All @@ -103,10 +103,15 @@ function create_fragment(ctx) {
};
}

function instance($$self) {
const tag = { svg: 'svg', path: 'path' };
return [tag];
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, null, create_fragment, safe_not_equal, {});
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}

Expand Down
10 changes: 7 additions & 3 deletions test/js/samples/svelte-element-svg/input.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<svelte:element this="svg" xmlns="http://www.w3.org/2000/svg">
<svelte:element this="path" xmlns="http://www.w3.org/2000/svg"></svelte:element>
</svelte:element>
<script>
const tag = { svg: 'svg', path: 'path' };
</script>

<svelte:element this={tag.svg} xmlns="http://www.w3.org/2000/svg">
<svelte:element this={tag.path} xmlns="http://www.w3.org/2000/svg" />
</svelte:element>
13 changes: 13 additions & 0 deletions test/runtime/samples/static-svelte-element2/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
html: `
<div>
<p></p>
</div>
`,

test({ assert, target }) {
const p = target.querySelector('p');

assert.notEqual(p, undefined);
}
};
7 changes: 7 additions & 0 deletions test/runtime/samples/static-svelte-element2/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
const p = 'p';
</script>

<div>
<svelte:element this={p} />
</div>