Skip to content

Commit 1b454ba

Browse files
authored
Merge pull request #3884 from sveltejs/gh-3882
neaten up hydration code
2 parents ca6c01d + 11b030b commit 1b454ba

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

src/compiler/compile/render_dom/wrappers/Element/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export default class ElementWrapper extends Wrapper {
121121
select_binding_dependencies?: Set<string>;
122122

123123
var: any;
124+
void: boolean;
124125

125126
constructor(
126127
renderer: Renderer,
@@ -136,6 +137,8 @@ export default class ElementWrapper extends Wrapper {
136137
name: node.name.replace(/[^a-zA-Z0-9_$]/g, '_')
137138
};
138139

140+
this.void = is_void(node.name);
141+
139142
this.class_dependencies = [];
140143

141144
this.attributes = this.node.attributes.map(attribute => {
@@ -258,6 +261,7 @@ export default class ElementWrapper extends Wrapper {
258261

259262
const node = this.var;
260263
const nodes = parent_nodes && block.get_unique_name(`${this.var.name}_nodes`); // if we're in unclaimable territory, i.e. <head>, parent_nodes is null
264+
const children = x`@children(${this.node.name === 'template' ? x`${node}.content` : node})`;
261265

262266
block.add_variable(node);
263267
const render_statement = this.get_render_statement();
@@ -269,8 +273,13 @@ export default class ElementWrapper extends Wrapper {
269273
if (parent_nodes) {
270274
block.chunks.claim.push(b`
271275
${node} = ${this.get_claim_statement(parent_nodes)};
272-
var ${nodes} = @children(${this.node.name === 'template' ? x`${node}.content` : node});
273276
`);
277+
278+
if (!this.void && this.node.children.length > 0) {
279+
block.chunks.claim.push(b`
280+
var ${nodes} = ${children};
281+
`);
282+
}
274283
} else {
275284
block.chunks.claim.push(
276285
b`${node} = ${render_statement};`
@@ -351,9 +360,9 @@ export default class ElementWrapper extends Wrapper {
351360
this.add_classes(block);
352361
this.add_manual_style_scoping(block);
353362

354-
if (nodes && this.renderer.options.hydratable) {
363+
if (nodes && this.renderer.options.hydratable && !this.void) {
355364
block.chunks.claim.push(
356-
b`${nodes}.forEach(@detach);`
365+
b`${this.node.children.length > 0 ? nodes : children}.forEach(@detach);`
357366
);
358367
}
359368

@@ -915,7 +924,7 @@ function to_html(wrappers: Array<ElementWrapper | TextWrapper | TagWrapper>, blo
915924

916925
state.quasi.value.raw += '>';
917926

918-
if (!is_void(wrapper.node.name)) {
927+
if (!(wrapper as ElementWrapper).void) {
919928
to_html((wrapper as ElementWrapper).fragment.nodes as Array<ElementWrapper | TextWrapper>, block, literal, state);
920929

921930
state.quasi.value.raw += `</${wrapper.node.name}>`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
options: {
3+
hydratable: true
4+
}
5+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* generated by Svelte vX.Y.Z */
2+
import {
3+
SvelteComponent,
4+
attr,
5+
children,
6+
claim_element,
7+
claim_space,
8+
detach,
9+
element,
10+
init,
11+
insert,
12+
noop,
13+
safe_not_equal,
14+
space
15+
} from "svelte/internal";
16+
17+
function create_fragment(ctx) {
18+
let img;
19+
let t;
20+
let div;
21+
22+
return {
23+
c() {
24+
img = element("img");
25+
t = space();
26+
div = element("div");
27+
this.h();
28+
},
29+
l(nodes) {
30+
img = claim_element(nodes, "IMG", { src: true, alt: true });
31+
t = claim_space(nodes);
32+
div = claim_element(nodes, "DIV", {});
33+
children(div).forEach(detach);
34+
this.h();
35+
},
36+
h() {
37+
attr(img, "src", "donuts.jpg");
38+
attr(img, "alt", "donuts");
39+
},
40+
m(target, anchor) {
41+
insert(target, img, anchor);
42+
insert(target, t, anchor);
43+
insert(target, div, anchor);
44+
},
45+
p: noop,
46+
i: noop,
47+
o: noop,
48+
d(detaching) {
49+
if (detaching) detach(img);
50+
if (detaching) detach(t);
51+
if (detaching) detach(div);
52+
}
53+
};
54+
}
55+
56+
class Component extends SvelteComponent {
57+
constructor(options) {
58+
super();
59+
init(this, options, null, create_fragment, safe_not_equal, {});
60+
}
61+
}
62+
63+
export default Component;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<img src="donuts.jpg" alt="donuts">
2+
<div></div>

0 commit comments

Comments
 (0)