@@ -3,9 +3,11 @@ export const map = (arr, ...args ) => Array.prototype.map.apply( arr, args );
3
3
export const csv = ( arr , ...args ) => map ( arr , ...args ) . join ( ',' ) ;
4
4
5
5
export const collectionText = arr => map ( arr , e => getNodeText ( e ) ) . join ( '' )
6
+ const createEl = tag => document . createElement ( tag ) ;
7
+ function nodeProp ( tag , prop , val ) { const el = createEl ( tag ) ; el [ prop ] = val ; return el ; }
6
8
7
9
const nop = ( ) => ''
8
- , isArr = a => Array . isArray ( a )
10
+ , isArr = a => Array . isArray ( a ) || ( a && 'function' === typeof a . forEach )
9
11
, isT = ( a , t ) => typeof a === t
10
12
, isStr = a => isT ( a , 'string' )
11
13
, isNum = a => isT ( a , 'number' )
@@ -16,7 +18,8 @@ const nop = ()=>''
16
18
, each = ( arr , cb ) => ( arr . forEach ( cb ) , arr )
17
19
, clear = n => hasAssigned ( n )
18
20
? n . assignedNodes ( ) . forEach ( a => a . remove ( ) )
19
- : n . innerHTML = '' ;
21
+ : n . innerHTML = ''
22
+ ;
20
23
21
24
const node2text = { 1 : n => n . assignedNodes
22
25
? collectionText ( n . assignedNodes ( ) ) || collectionText ( n . childNodes )
@@ -39,12 +42,12 @@ export const collectionHtml = arr => map( arr, n=>n.assignedElements
39
42
) . join ( '' ) ;
40
43
41
44
export const html2NodeArr = html =>
42
- { const n = document . createElement ( 'div' ) ;
45
+ { const n = createEl ( 'div' ) ;
43
46
n . innerHTML = html ;
44
47
const wrapIfText = e => {
45
48
if ( e . nodeType !== 3 )
46
49
return e ;
47
- const n = document . createElement ( 'span' ) ;
50
+ const n = createEl ( 'span' ) ;
48
51
n . append ( e ) ;
49
52
return n ;
50
53
} ;
@@ -98,7 +101,17 @@ CssChainT extends Array
98
101
}
99
102
erase ( ) { return this . forEach ( n => clear ( n ) ) }
100
103
slots ( ...arr )
101
- { const ret = this . map ( n => n . shadowRoot || n ) . $ ( arr . length
104
+ {
105
+ const selector = arr . length
106
+ ? csv ( arr [ 0 ] . split ( ',' )
107
+ , n => [ '""' , "''" ] . includes ( n ) || ! n
108
+ ? `slot:not([name])`
109
+ : `slot[name="${ n } "]`
110
+ )
111
+ : 'slot' ;
112
+ const ss = this . filter ( el => el . matches && el . matches ( selector ) ) ;
113
+ const ret = this . filter ( n => ! ss . includes ( n ) && n . querySelector )
114
+ . map ( n => n . shadowRoot || n ) . $ ( arr . length
102
115
? csv ( arr [ 0 ] . split ( ',' )
103
116
, n => [ '""' , "''" ] . includes ( n ) || ! n
104
117
? `slot:not([name])`
@@ -109,27 +122,30 @@ CssChainT extends Array
109
122
{ ret . html ( arr [ 1 ] ) ;
110
123
return this
111
124
}
112
- return ret ;
125
+ return CssChain ( [ ... ss , ... ret ] ) ;
113
126
}
114
127
template ( n )
115
128
{
116
129
if ( n === undefined )
117
- { const x = this . $ ( '[slot]' ) . forEach ( n => n . remove ( ) ) ;
118
- n = this . splice ( 0 , this . length ) ;
119
- this . push ( document . createElement ( 'span' ) ) ;
120
- this . append ( x ) ;
121
- } else if ( isStr ( n ) )
122
130
{
123
- n = this . $ ( n ) ;
131
+ const $s = this . $ ( '[slot]' )
132
+ . forEach ( n => this . $ ( n . slot ? `slot[name="${ n . slot } "]` :'slot:not([name])' ) . length
133
+ || n . parentNode . insertBefore ( nodeProp ( 'slot' , 'name' , n . slot ) , n ) ) ;
134
+ $s . remove ( ) ;
135
+ n = createEl ( 'template' ) ;
136
+ this . childNodes . forEach ( c => n . content . append ( c ) ) ;
137
+ this . append ( $s ) ;
138
+ } else if ( isStr ( n ) )
139
+ { n = this . $ ( n ) ;
124
140
n . remove ( ) ;
125
141
}
126
- const c = CssChain ( n . content || n ) . clone ( this ) ;
142
+ const c = CssChain ( n . content ? n . content . childNodes : n ) . clone ( this ) ;
127
143
c . slots ( ) . forEach ( s =>
128
144
{ const v = this . children . filter ( n => n . slot === s . name ) ;
129
145
v . length && setNodeHtml ( s , v )
130
146
} ) ;
131
147
this . children . remove ( ) ;
132
- this . forEach ( ( n , i ) => n . appendChild ( c [ i ] ) )
148
+ this . append ( c ) ;
133
149
return this ;
134
150
}
135
151
get innerText ( ) { return this . txt ( ) }
@@ -199,7 +215,7 @@ const appliedTypes = new Set()
199
215
200
216
export function
201
217
applyPrototype ( nodeOrTag , ApiChain )
202
- { const node = isStr ( nodeOrTag ) ? document . createElement ( nodeOrTag ) : nodeOrTag ;
218
+ { const node = isStr ( nodeOrTag ) ? createEl ( nodeOrTag ) : nodeOrTag ;
203
219
if ( appliedTypes . has ( node . tagName ) )
204
220
return ;
205
221
appliedTypes . add ( node . tagName ) ;
@@ -218,7 +234,7 @@ applyPrototype( nodeOrTag, ApiChain )
218
234
Object . getOwnPropertyNames ( window )
219
235
. filter ( key => key . startsWith ( 'HTML' ) && key . endsWith ( 'Element' ) && key . length > 11 )
220
236
. map ( key => key . substring ( 4 , key . length - 7 ) . toLowerCase ( ) )
221
- . forEach ( tag => applyPrototype ( document . createElement ( tag ) , CssChainT ) ) ;
237
+ . forEach ( tag => applyPrototype ( createEl ( tag ) , CssChainT ) ) ;
222
238
223
239
export function
224
240
CssChain ( css , el = document , protoArr = [ ] )
0 commit comments