Skip to content

Commit d6c3738

Browse files
committed
avoid wrappers for <svelte:element>
1 parent 04415e9 commit d6c3738

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { filename, locator } from '../../../state.js';
3838

3939
export const block_open = t_string(`<!--${HYDRATION_START}-->`);
4040
export const block_close = t_string(`<!--${HYDRATION_END}-->`);
41+
export const block_anchor = t_string(`<!---->`);
4142

4243
/**
4344
* @param {string} value
@@ -1470,8 +1471,6 @@ const template_visitors = {
14701471
}
14711472
};
14721473

1473-
context.state.template.push(block_open);
1474-
14751474
const main = create_block(node, node.fragment.nodes, {
14761475
...context,
14771476
state: { ...context.state, metadata }
@@ -1506,7 +1505,7 @@ const template_visitors = {
15061505
)
15071506
)
15081507
),
1509-
block_close
1508+
block_anchor
15101509
);
15111510
if (context.state.options.dev) {
15121511
context.state.template.push(t_statement(b.stmt(b.call('$.pop_element'))));

packages/svelte/src/internal/client/dom/blocks/svelte-element.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { namespace_svg } from '../../../../constants.js';
2-
import { hydrate_anchor, hydrate_start, hydrating } from '../hydration.js';
2+
import { hydrate_anchor, hydrating } from '../hydration.js';
33
import { empty } from '../operations.js';
44
import {
55
block,
@@ -14,15 +14,15 @@ import { current_component_context } from '../../runtime.js';
1414
import { DEV } from 'esm-env';
1515

1616
/**
17-
* @param {Comment} anchor
17+
* @param {Comment | Element} node
1818
* @param {() => string} get_tag
1919
* @param {boolean} is_svg
2020
* @param {undefined | ((element: Element, anchor: Node | null) => void)} render_fn,
2121
* @param {undefined | (() => string)} get_namespace
2222
* @param {undefined | [number, number]} location
2323
* @returns {void}
2424
*/
25-
export function element(anchor, get_tag, is_svg, render_fn, get_namespace, location) {
25+
export function element(node, get_tag, is_svg, render_fn, get_namespace, location) {
2626
const filename = DEV && location && current_component_context?.function.filename;
2727

2828
/** @type {string | null} */
@@ -32,7 +32,9 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
3232
let current_tag;
3333

3434
/** @type {null | Element} */
35-
let element = null;
35+
let element = hydrating && node.nodeType === 1 ? /** @type {Element} */ (node) : null;
36+
37+
let anchor = /** @type {Comment} */ (hydrating && element ? element.nextSibling : node);
3638

3739
/** @type {import('#client').Effect | null} */
3840
let effect;
@@ -51,6 +53,7 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
5153
: is_svg || next_tag === 'svg'
5254
? namespace_svg
5355
: null;
56+
5457
// Assumption: Noone changes the namespace but not the tag (what would that even mean?)
5558
if (next_tag === tag) return;
5659

@@ -79,7 +82,7 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
7982
if (next_tag && next_tag !== current_tag) {
8083
effect = branch(() => {
8184
element = hydrating
82-
? /** @type {Element} */ (hydrate_start)
85+
? /** @type {Element} */ (element)
8386
: ns
8487
? document.createElementNS(ns, next_tag)
8588
: document.createElement(next_tag);

packages/svelte/src/internal/client/dom/operations.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { hydrate_anchor, hydrate_start, hydrating } from './hydration.js';
22
import { DEV } from 'esm-env';
33
import { init_array_prototype_warnings } from '../dev/equality.js';
4+
import { HYDRATION_END } from '../../../constants.js';
45

56
// export these for reference in the compiled code, making global name deduplication unnecessary
67
/** @type {Window} */
@@ -98,12 +99,16 @@ export function first_child(fragment, is_text) {
9899
*/
99100
/*#__NO_SIDE_EFFECTS__*/
100101
export function sibling(node, is_text = false) {
101-
const next_sibling = node.nextSibling;
102+
var next_sibling = /** @type {import('#client').TemplateNode} */ (node.nextSibling);
102103

103104
if (!hydrating) {
104105
return next_sibling;
105106
}
106107

108+
if (next_sibling.nodeType === 8 && /** @type {Comment} */ (next_sibling).data === '') {
109+
return sibling(next_sibling, is_text);
110+
}
111+
107112
// if a sibling {expression} is empty during SSR, there might be no
108113
// text node to hydrate — we must therefore create one
109114
if (is_text && next_sibling?.nodeType !== 3) {
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<!--[-->
2-
<!--[-->
32
<title>lorem</title>
4-
<!--]-->
5-
<!--[-->
3+
<!---->
64
<style>
75
.ipsum {
86
display: block;
97
}
108
</style>
11-
<!--]-->
12-
<!--[-->
9+
<!---->
1310
<script>
1411
console.log(true);
1512
</script>
16-
<!--]--><!--]-->
13+
<!----><!--]-->

packages/svelte/tests/snapshot/samples/svelte-element/_expected/server/index.svelte.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ export default function Svelte_element($$payload, $$props) {
55

66
let { tag = 'hr' } = $$props;
77

8-
$$payload.out += `<!--[-->`;
98
if (tag) $.element($$payload, tag, () => {}, () => {});
10-
$$payload.out += `<!--]-->`;
9+
$$payload.out += `<!---->`;
1110
$.pop();
12-
}
11+
}

0 commit comments

Comments
 (0)