Skip to content

Commit 3c83588

Browse files
committed
assignedNodes() and CssChainAssignedNodes for light DOM
1 parent e92fa3a commit 3c83588

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

src/CssChain.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export const addNodeHtml = ( n, val ) =>
7676
: append(val);
7777
}
7878
export const setNodeHtml = ( n, val ) => { clear(n); addNodeHtml(n,val) };
79+
function assignedNodesLight(f)
80+
{ if(f?.flatten)
81+
{ const r = [];
82+
const flatten = n=> n.CssChainAssignedNodes ? each(n.CssChainAssignedNodes,flatten): r.push(n);
83+
each(this.CssChainAssignedNodes, flatten );
84+
return r;
85+
}
86+
return this.CssChainAssignedNodes;
87+
}
7988

8089
class
8190
CssChainT extends Array
@@ -131,42 +140,47 @@ CssChainT extends Array
131140
}
132141
template(n)
133142
{ if( n === undefined )
134-
{
135-
const $s = this.$('[slot]')
136-
.forEach( n => this.$(n.slot ? `slot[name="${n.slot}"]`:'slot:not([name])').length
137-
|| n.parentNode.insertBefore( nodeProp('slot','name',n.slot), n ));
138-
$s.remove();
139-
n = createEl('template');
140-
this.childNodes.forEach( c=>n.content.append(c) );
141-
this.append($s);
142-
}else if( isStr(n) )
143-
{ n = this.$( n );
144-
n.remove();
145-
}
143+
{
144+
const $s = this.$('[slot]')
145+
.forEach( n => this.$(n.slot ? `slot[name="${n.slot}"]`:'slot:not([name])').length
146+
|| n.parentNode.insertBefore( nodeProp('slot','name',n.slot), n ));
147+
$s.remove();
148+
n = createEl('template');
149+
this.childNodes.forEach( c=>n.content.append(c) );
150+
this.append($s);
151+
}else if( isStr(n) )
152+
{ n = this.$( n );
153+
n.remove();
154+
}
146155
n = n.cloneNode(true);
147156
const content = n.content ? n.content.childNodes : n;
148157
const c = CssChain( content );
149158
c.slots().forEach( s =>
150159
{ const v = this.children.filter( n=>n.slot===s.name );
151160
const p = s.parentNode;
161+
s.CssChainAssignedNodes = [];
162+
s.assignedNodes = s.assignedElements = assignedNodesLight;
163+
const insert = (n,r)=>
164+
{ const k = p.insertBefore( n, r);
165+
n.CssChainAssignedSlot || s.CssChainAssignedNodes.push(n);
166+
n.CssChainAssignedSlot = s;
167+
return k;
168+
};
152169
each(v, e=>e.cssChainSlot = s);
153-
if(s.id ==='s3'){
154-
debugger;
155-
}
156170
const injectInSlot = (e,s)=>
157171
{
158172
if( e.tagName === 'SLOT' )
159173
{ const q = e.parentNode;
160-
const r = p.insertBefore( e, s);
174+
const r = insert(e,s);
161175
each( q.querySelectorAll(`[slot="${e.name}"]`), n=> p.insertBefore( n, r) );
162176
}else
163-
p.insertBefore( e, s);
177+
insert(e,s);
164178
}
165179
v.length ? each( v, e=>injectInSlot( e, s) )
166180
: s.name==="" &&
167181
each ( [...(n.content ? n.content.childNodes : n.childNodes)]
168182
.filter(e=>!e.hasArttribute ||e.hasArttribute('slot'))
169-
, e=>p.insertBefore( e, s) );
183+
, e=>insert(e,s) );
170184
if( v.length ) s.hidden=true;
171185
});
172186
this.children.filter(e=>!e.cssChainSlot).remove();

test/CssChain-template.test.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,19 @@ describe( 'CssChain template() variations', () =>
6060
|| null
6161
)
6262
);
63-
expect(light.$('#'+k).parentElement || null).to.equal(s && light.$('#host1').slots().find( e=>e.id===s));
64-
// node.assignedSlot in light DOM equals the parent slot
63+
64+
expect(light.$('#'+k)[0]?.CssChainAssignedSlot || null)
65+
.to.equal( s && light.$('#'+k)[0].CssChainAssignedSlot );
66+
67+
// has a sibling slot w/ name
68+
// slots
69+
const slotName = light.$('#'+k).slot;
70+
const seekedSlot = s && light.$('#host1').slots().find( e=>e.id===s);
71+
72+
if( slotName === undefined )
73+
expect( null ).to.equal( seekedSlot );
74+
else
75+
expect( light.$('#'+k).parent().slots(slotName) ).to.contain( seekedSlot );
6576
};
6677
const assert_assignedNodes = (k,arr)=>
6778
{ assert_array_equals( n[k].assignedNodes(), arr.map( e=>n[e] ) );
@@ -71,8 +82,7 @@ describe( 'CssChain template() variations', () =>
7182
).map( e=>e.id )
7283
).to.eql( arr );
7384

74-
expect( light.$('#'+k).children.map( e=>e.id ) ).to.eql( arr );
75-
// node.assignedNodes in light DOM equals the child nodes
85+
expect( light.$('#'+k).assignedNodes().map( e=>e.id ) ).to.eql( arr );
7686
};
7787
const assert_assignedNodesF = (k,arr)=>
7888
{ const F = { flatten: true };
@@ -83,8 +93,8 @@ describe( 'CssChain template() variations', () =>
8393
).map( e=>e.id )
8494
).to.eql( arr );
8595

86-
expect( light.$('#'+k).children.map( e=>e.tagName==='SLOT'?e.firstChild:e ).map( e=>e.id ) ).to.eql( arr );
87-
// node.assignedNodes in light DOM equals the child nodes
96+
// node.assignedNodes in light DOM do not need shadow root penetration
97+
expect( light.$('#'+k).assignedNodes(F).map( e=>e.id ) ).to.eql( arr );
8898
};
8999

90100
// same sequence as in prev test but testing using CssChain of shadow and light dom

0 commit comments

Comments
 (0)