File tree 5 files changed +28
-4
lines changed
5 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import Block from './Block';
2
2
import { trimStart , trimEnd } from '../../utils/trim' ;
3
3
import { assign } from '../../shared/index.js' ;
4
4
import getStaticAttributeValue from '../../utils/getStaticAttributeValue' ;
5
+ import isChildOfComponent from '../shared/utils/isChildOfComponent' ;
5
6
import { DomGenerator } from './index' ;
6
7
import { Node } from '../../interfaces' ;
7
8
import { State } from './interfaces' ;
@@ -340,7 +341,8 @@ const preprocessors = {
340
341
} ) ;
341
342
} else {
342
343
const slot = getStaticAttributeValue ( node , 'slot' ) ;
343
- if ( slot ) {
344
+ if ( slot && isChildOfComponent ( node , generator ) ) {
345
+ node . slotted = true ;
344
346
// TODO validate slots — no nesting, no dynamic names...
345
347
const component = componentStack [ componentStack . length - 1 ] ;
346
348
component . _slots . add ( slot ) ;
Original file line number Diff line number Diff line change @@ -63,7 +63,7 @@ export default function visitElement(
63
63
const name = childState . parentNode ;
64
64
65
65
const slot = node . attributes . find ( ( attribute : Node ) => attribute . name === 'slot' ) ;
66
- const parentNode = slot ?
66
+ const parentNode = node . slotted ?
67
67
`${ componentStack [ componentStack . length - 1 ] . var } ._slotted.${ slot . value [ 0 ] . data } ` : // TODO this looks bonkers
68
68
state . parentNode ;
69
69
Original file line number Diff line number Diff line change
1
+ import getStaticAttributeValue from '../../utils/getStaticAttributeValue' ;
2
+ import isChildOfComponent from '../shared/utils/isChildOfComponent' ;
1
3
import { SsrGenerator } from './index' ;
2
4
import { Node } from '../../interfaces' ;
3
5
@@ -62,6 +64,11 @@ const preprocessors = {
62
64
63
65
if ( ! isComponent ) {
64
66
generator . stylesheet . apply ( node , elementStack ) ;
67
+
68
+ const slot = getStaticAttributeValue ( node , 'slot' ) ;
69
+ if ( slot && isChildOfComponent ( node , generator ) ) {
70
+ node . slotted = true ;
71
+ }
65
72
}
66
73
67
74
if ( node . children . length ) {
@@ -80,6 +87,8 @@ function preprocessChildren(
80
87
elementStack : Node [ ]
81
88
) {
82
89
node . children . forEach ( ( child : Node , i : number ) => {
90
+ child . parent = node ;
91
+
83
92
const preprocessor = preprocessors [ child . type ] ;
84
93
if ( preprocessor ) preprocessor ( generator , child , elementStack ) ;
85
94
} ) ;
Original file line number Diff line number Diff line change @@ -47,8 +47,8 @@ export default function visitElement(
47
47
let openingTag = `<${ node . name } ` ;
48
48
let textareaContents ; // awkward special case
49
49
50
- const slot = node . attributes . find ( ( attribute : Node ) => attribute . name === 'slot' ) ;
51
- if ( slot ) {
50
+ if ( node . slotted ) {
51
+ const slot = node . attributes . find ( ( attribute : Node ) => attribute . name === 'slot' ) ;
52
52
const slotName = slot . value [ 0 ] . data ;
53
53
const appendTarget = generator . appendTargets [ generator . appendTargets . length - 1 ] ;
54
54
appendTarget . slotStack . push ( slotName ) ;
Original file line number Diff line number Diff line change
1
+ import { Node } from '../../../interfaces' ;
2
+ import Generator from '../../Generator' ;
3
+
4
+ export default function isChildOfComponent ( node : Node , generator : Generator ) {
5
+ while ( node = node . parent ) {
6
+ if ( node . type !== 'Element' ) continue ;
7
+ if ( generator . components . has ( node . name ) ) return true ;
8
+ if ( / - / . test ( node . name ) ) return false ;
9
+ }
10
+
11
+ // TODO do this in validation
12
+ throw new Error ( `Element with a slot='...' attribute must be a descendant of a component or custom element` ) ;
13
+ }
You can’t perform that action at this time.
0 commit comments