From f84c00edb9aab75c939963332da57af0e82894f3 Mon Sep 17 00:00:00 2001 From: Ramon Snir Date: Sun, 1 Jan 2023 03:41:00 -0500 Subject: [PATCH] [fix] static svelte:element not replaced with tag in production mode (#7938) * [fix] static svelte:element not replaced with tag in production mode * [fix] static svelte:element not replaced with tag in production mode * add optimization of static and fix a bug Co-authored-by: Yuichiro Yamashita --- src/compiler/compile/nodes/Element.ts | 1 + .../render_dom/wrappers/Element/index.ts | 11 +++++---- .../js/samples/svelte-element-svg/expected.js | 23 +++++++++++-------- .../samples/svelte-element-svg/input.svelte | 10 +++++--- .../samples/static-svelte-element/_config.js | 13 +++++++++++ .../samples/static-svelte-element/main.svelte | 3 +++ .../samples/static-svelte-element2/_config.js | 13 +++++++++++ .../static-svelte-element2/main.svelte | 7 ++++++ 8 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 test/runtime/samples/static-svelte-element/_config.js create mode 100644 test/runtime/samples/static-svelte-element/main.svelte create mode 100644 test/runtime/samples/static-svelte-element2/_config.js create mode 100644 test/runtime/samples/static-svelte-element2/main.svelte diff --git a/src/compiler/compile/nodes/Element.ts b/src/compiler/compile/nodes/Element.ts index a499d013e35e..06ef1ba9c1a9 100644 --- a/src/compiler/compile/nodes/Element.ts +++ b/src/compiler/compile/nodes/Element.ts @@ -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); diff --git a/src/compiler/compile/render_dom/wrappers/Element/index.ts b/src/compiler/compile/render_dom/wrappers/Element/index.ts index 8d0429879e4b..cef758b89eca 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/index.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/index.ts @@ -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 @@ -1176,10 +1177,12 @@ function to_html(wrappers: Array { if (is_empty_textarea && attr.node.name === 'value') { @@ -1196,7 +1199,7 @@ function to_html(wrappers: Array`. // see https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element const first = wrapper.fragment.nodes[0]; @@ -1221,7 +1224,7 @@ function to_html(wrappers: Array, block, literal, state); - state.quasi.value.raw += ``; + state.quasi.value.raw += ``; } else { state.quasi.value.raw += '/>'; } diff --git a/test/js/samples/svelte-element-svg/expected.js b/test/js/samples/svelte-element-svg/expected.js index 36ce583ef10a..263f6c842bd8 100644 --- a/test/js/samples/svelte-element-svg/expected.js +++ b/test/js/samples/svelte-element-svg/expected.js @@ -18,7 +18,7 @@ function create_dynamic_element_1(ctx) { return { c: noop, m: noop, p: noop, d: noop }; } -// (1:0) +// (5:0) function create_dynamic_element(ctx) { let svelte_element1; let svelte_element0; @@ -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); }, @@ -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() { @@ -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(); @@ -92,7 +92,7 @@ function create_fragment(ctx) { svelte_element1 = null; } - previous_tag = "svg"; + previous_tag = /*tag*/ ctx[0].svg; }, i: noop, o: noop, @@ -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, {}); } } diff --git a/test/js/samples/svelte-element-svg/input.svelte b/test/js/samples/svelte-element-svg/input.svelte index 2dc4d548141e..111c0183c674 100644 --- a/test/js/samples/svelte-element-svg/input.svelte +++ b/test/js/samples/svelte-element-svg/input.svelte @@ -1,3 +1,7 @@ - - - \ No newline at end of file + + + + + diff --git a/test/runtime/samples/static-svelte-element/_config.js b/test/runtime/samples/static-svelte-element/_config.js new file mode 100644 index 000000000000..a26de6133802 --- /dev/null +++ b/test/runtime/samples/static-svelte-element/_config.js @@ -0,0 +1,13 @@ +export default { + html: ` +
+

+
+ `, + + test({ assert, target }) { + const p = target.querySelector('p'); + + assert.notEqual(p, undefined); + } +}; diff --git a/test/runtime/samples/static-svelte-element/main.svelte b/test/runtime/samples/static-svelte-element/main.svelte new file mode 100644 index 000000000000..3bdf853a5afa --- /dev/null +++ b/test/runtime/samples/static-svelte-element/main.svelte @@ -0,0 +1,3 @@ +
+ +
diff --git a/test/runtime/samples/static-svelte-element2/_config.js b/test/runtime/samples/static-svelte-element2/_config.js new file mode 100644 index 000000000000..a26de6133802 --- /dev/null +++ b/test/runtime/samples/static-svelte-element2/_config.js @@ -0,0 +1,13 @@ +export default { + html: ` +
+

+
+ `, + + test({ assert, target }) { + const p = target.querySelector('p'); + + assert.notEqual(p, undefined); + } +}; diff --git a/test/runtime/samples/static-svelte-element2/main.svelte b/test/runtime/samples/static-svelte-element2/main.svelte new file mode 100644 index 000000000000..a723b955d4b6 --- /dev/null +++ b/test/runtime/samples/static-svelte-element2/main.svelte @@ -0,0 +1,7 @@ + + +
+ +