Skip to content

Commit 5088024

Browse files
authored
Merge pull request #7 from sashafirsov/develop
1.1.1
2 parents 760cabd + ecb9991 commit 5088024

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

CssChain.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ export const map = (arr, ...args ) => Array.prototype.map.apply( arr, args );
33
export const csv = (arr, ...args ) => map( arr, ...args ).join(',');
44

55
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; }
68

79
const nop = ()=>''
8-
, isArr = a => Array.isArray(a)
10+
, isArr = a => Array.isArray(a) || (a && 'function' === typeof a.forEach)
911
, isT = (a,t) => typeof a === t
1012
, isStr = a => isT(a, 'string')
1113
, isNum = a => isT(a, 'number')
@@ -16,7 +18,8 @@ const nop = ()=>''
1618
, each = (arr, cb )=> (arr.forEach(cb),arr)
1719
, clear = n => hasAssigned(n)
1820
? n.assignedNodes().forEach( a => a.remove() )
19-
: n.innerHTML='' ;
21+
: n.innerHTML=''
22+
;
2023

2124
const node2text = { 1: n=>n.assignedNodes
2225
? collectionText(n.assignedNodes()) || collectionText(n.childNodes)
@@ -39,12 +42,12 @@ export const collectionHtml = arr => map( arr, n=>n.assignedElements
3942
).join('');
4043

4144
export const html2NodeArr = html =>
42-
{ const n = document.createElement('div');
45+
{ const n = createEl('div');
4346
n.innerHTML = html;
4447
const wrapIfText = e=>{
4548
if( e.nodeType !== 3 )
4649
return e;
47-
const n = document.createElement('span');
50+
const n = createEl('span');
4851
n.append(e);
4952
return n;
5053
};
@@ -98,7 +101,17 @@ CssChainT extends Array
98101
}
99102
erase(){ return this.forEach(n=>clear(n)) }
100103
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
102115
? csv( arr[0].split(',')
103116
, n=> ['""',"''"].includes(n) || !n
104117
? `slot:not([name])`
@@ -109,27 +122,30 @@ CssChainT extends Array
109122
{ ret.html( arr[ 1 ] );
110123
return this
111124
}
112-
return ret;
125+
return CssChain([...ss,...ret]);
113126
}
114127
template(n)
115128
{
116129
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) )
122130
{
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 );
124140
n.remove();
125141
}
126-
const c = CssChain(n.content||n).clone(this);
142+
const c = CssChain( n.content ? n.content.childNodes : n ).clone(this);
127143
c.slots().forEach( s =>
128144
{ const v = this.children.filter( n=>n.slot===s.name );
129145
v.length && setNodeHtml(s,v)
130146
});
131147
this.children.remove();
132-
this.forEach( (n,i)=> n.appendChild(c[i]))
148+
this.append(c);
133149
return this;
134150
}
135151
get innerText(){ return this.txt() }
@@ -199,7 +215,7 @@ const appliedTypes = new Set()
199215

200216
export function
201217
applyPrototype( nodeOrTag, ApiChain )
202-
{ const node = isStr(nodeOrTag) ? document.createElement(nodeOrTag) : nodeOrTag;
218+
{ const node = isStr(nodeOrTag) ? createEl(nodeOrTag) : nodeOrTag;
203219
if( appliedTypes.has(node.tagName) )
204220
return;
205221
appliedTypes.add( node.tagName );
@@ -218,7 +234,7 @@ applyPrototype( nodeOrTag, ApiChain )
218234
Object.getOwnPropertyNames(window)
219235
.filter(key => key.startsWith('HTML') && key.endsWith('Element')&& key.length > 11 )
220236
.map( key=>key.substring(4,key.length-7).toLowerCase() )
221-
.forEach( tag=>applyPrototype( document.createElement(tag), CssChainT ) );
237+
.forEach( tag=>applyPrototype( createEl(tag), CssChainT ) );
222238

223239
export function
224240
CssChain( css, el=document, protoArr=[] )

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _HTML template/slot and DOM manipulation library_
44
_Collection API inherits the element API and Array._
55

66
[![git][github-image] GitHub](https://github.com/sashafirsov/css-chain)
7-
| Demo: [css-chain](https://unpkg.com/[email protected].0/dist/demo.html)
7+
| Demo: [css-chain](https://unpkg.com/[email protected].1/dist/demo.html)
88
| [tests project](https://github.com/sashafirsov/css-chain-test)
99

1010
[![NPM version][npm-image]][npm-url] [![coverage][coverage-image]][coverage-url]
@@ -212,10 +212,10 @@ ApiChain( [a,b] ).f1().f2() // would reuse API generated in previous call
212212
[github-image]: https://cdnjs.cloudflare.com/ajax/libs/octicons/8.5.0/svg/mark-github.svg
213213
[npm-image]: https://img.shields.io/npm/v/css-chain.svg
214214
[npm-url]: https://npmjs.org/package/css-chain
215-
[coverage-image]: https://unpkg.com/[email protected].0/coverage/coverage.svg
216-
[coverage-url]: https://unpkg.com/[email protected].0/coverage/lcov-report/index.html
217-
[PokeApi-explorer-image]: https://unpkg.com/[email protected].0/src/PokeApi-Explorer.png
218-
[PokeApi-explorer-url]: https://unpkg.com/[email protected].0/src/PokeApi-Explorer.html
215+
[coverage-image]: https://unpkg.com/[email protected].1/coverage/coverage.svg
216+
[coverage-url]: https://unpkg.com/[email protected].1/coverage/lcov-report/index.html
217+
[PokeApi-explorer-image]: https://unpkg.com/[email protected].1/src/PokeApi-Explorer.png
218+
[PokeApi-explorer-url]: https://unpkg.com/[email protected].1/src/PokeApi-Explorer.html
219219
[css-chain-image]: ChainedCalls.png
220220
[css-chain-link]: https://github.com/sashafirsov/css-chain-test/blob/9edc6edac6bc6c22c078e2fd987b37a7721947ee/src/PokeApi-Explorer.js#L140
221221
[element-api-image]: ElementAPI.png

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "css-chain",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "ApiChain and CssChain JS. Collection API inherits the Array and element API.",
55
"browser": "CssChain.js",
66
"module": "CssChain.js",

0 commit comments

Comments
 (0)