Skip to content

Commit 4776c41

Browse files
authored
Merge pull request #835 from sveltejs/gh-822
use anchors for slotted content
2 parents 5255957 + 975a974 commit 4776c41

File tree

7 files changed

+51
-11
lines changed

7 files changed

+51
-11
lines changed

src/generators/dom/preprocess.ts

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ function preprocessChildren(
412412
lastChild = null;
413413

414414
cleaned.forEach((child: Node, i: number) => {
415+
child.parent = node;
416+
415417
const preprocessor = preprocessors[child.type];
416418
if (preprocessor) preprocessor(generator, block, state, child, inEachBlock, elementStack, componentStack, stripWhitespace, cleaned[i + 1] || nextSibling);
417419

src/generators/dom/visitors/EachBlock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function visitEachBlock(
2121
const iterations = block.getUniqueName(`${each_block}_iterations`);
2222
const params = block.params.join(', ');
2323

24-
const needsAnchor = node.next ? !isDomNode(node.next) : !state.parentNode;
24+
const needsAnchor = node.next ? !isDomNode(node.next, generator) : !state.parentNode;
2525
const anchor = needsAnchor
2626
? block.getUniqueName(`${each_block}_anchor`)
2727
: (node.next && node.next.var) || 'null';

src/generators/dom/visitors/IfBlock.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function visitIfBlock(
8080
) {
8181
const name = node.var;
8282

83-
const needsAnchor = node.next ? !isDomNode(node.next) : !state.parentNode;
83+
const needsAnchor = node.next ? !isDomNode(node.next, generator) : !state.parentNode || !isDomNode(node.parent, generator);
8484
const anchor = needsAnchor
8585
? block.getUniqueName(`${name}_anchor`)
8686
: (node.next && node.next.var) || 'null';
@@ -94,7 +94,7 @@ export default function visitIfBlock(
9494
const dynamic = branches[0].hasUpdateMethod; // can use [0] as proxy for all, since they necessarily have the same value
9595
const hasOutros = branches[0].hasOutroMethod;
9696

97-
const vars = { name, anchor, params, if_name, hasElse };
97+
const vars = { name, needsAnchor, anchor, params, if_name, hasElse };
9898

9999
if (node.else) {
100100
if (hasOutros) {
@@ -137,7 +137,7 @@ function simple(
137137
node: Node,
138138
branch,
139139
dynamic,
140-
{ name, anchor, params, if_name }
140+
{ name, needsAnchor, anchor, params, if_name }
141141
) {
142142
block.builders.init.addBlock(deindent`
143143
var ${name} = (${branch.condition}) && ${branch.block}(${params}, #component);
@@ -152,7 +152,7 @@ function simple(
152152
`if (${name}) ${name}.${mountOrIntro}(${targetNode}, ${anchorNode});`
153153
);
154154

155-
const parentNode = state.parentNode || `${anchor}.parentNode`;
155+
const parentNode = (state.parentNode && !needsAnchor) ? state.parentNode : `${anchor}.parentNode`;
156156

157157
const enter = dynamic
158158
? branch.hasIntroMethod
@@ -227,7 +227,7 @@ function compound(
227227
node: Node,
228228
branches,
229229
dynamic,
230-
{ name, anchor, params, hasElse, if_name }
230+
{ name, needsAnchor, anchor, params, hasElse, if_name }
231231
) {
232232
const select_block_type = generator.getUniqueName(`select_block_type`);
233233
const current_block_type = block.getUniqueName(`current_block_type`);
@@ -255,7 +255,7 @@ function compound(
255255
`${if_name}${name}.${mountOrIntro}(${targetNode}, ${anchorNode});`
256256
);
257257

258-
const parentNode = state.parentNode || `${anchor}.parentNode`;
258+
const parentNode = (state.parentNode && !needsAnchor) ? state.parentNode : `${anchor}.parentNode`;
259259

260260
const changeBlock = deindent`
261261
${hasElse
@@ -303,7 +303,7 @@ function compoundWithOutros(
303303
node: Node,
304304
branches,
305305
dynamic,
306-
{ name, anchor, params, hasElse }
306+
{ name, needsAnchor, anchor, params, hasElse }
307307
) {
308308
const select_block_type = block.getUniqueName(`select_block_type`);
309309
const current_block_type_index = block.getUniqueName(`current_block_type_index`);
@@ -354,7 +354,7 @@ function compoundWithOutros(
354354
`${if_current_block_type_index}${if_blocks}[${current_block_type_index}].${mountOrIntro}(${targetNode}, ${anchorNode});`
355355
);
356356

357-
const parentNode = state.parentNode || `${anchor}.parentNode`;
357+
const parentNode = (state.parentNode && !needsAnchor) ? state.parentNode : `${anchor}.parentNode`;
358358

359359
const destroyOldBlock = deindent`
360360
${name}.outro(function() {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { DomGenerator } from '../../index';
12
import { Node } from '../../../../interfaces';
23

3-
export default function isDomNode(node: Node) {
4-
return node.type === 'Element' || node.type === 'Text' || node.type === 'MustacheTag';
4+
export default function isDomNode(node: Node, generator: DomGenerator) {
5+
if (node.type === 'Element') return !generator.components.has(node.name);
6+
return node.type === 'Text' || node.type === 'MustacheTag';
57
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<slot/>
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
html: `
3+
<div>
4+
<p>unconditional</p>
5+
</div>`,
6+
7+
test(assert, component, target) {
8+
component.set({ foo: true });
9+
assert.htmlEqual(target.innerHTML, `
10+
<div>
11+
<p>unconditional</p>
12+
<p>conditional</p>
13+
</div>
14+
`);
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Nested>
2+
<p>unconditional</p>
3+
4+
{{#if foo}}
5+
<p>conditional</p>
6+
{{/if}}
7+
</Nested>
8+
9+
<script>
10+
import Nested from './Nested.html';
11+
12+
export default {
13+
components: {
14+
Nested
15+
}
16+
};
17+
</script>

0 commit comments

Comments
 (0)