Skip to content

Commit 7a37187

Browse files
committed
attr setter by with value callback
1 parent 11f501d commit 7a37187

File tree

9 files changed

+5481
-1839
lines changed

9 files changed

+5481
-1839
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"scripts": {
2525
"analyze": "cem analyze --litelement",
26-
"build": "bash bin/build.sh",
26+
"build": "bash types/types-generate.sh && bash bin/build.sh",
2727
"postinstall": "bash bin/postinstall.sh",
2828
"start": "wds",
2929
"test": "bash bin/test.sh",
@@ -36,8 +36,8 @@
3636
"devDependencies": {
3737
"@open-wc/testing": "3.2.0",
3838
"@web/dev-server": "0.3.0",
39-
"@web/test-runner": "0.17.0",
40-
"concurrently": "8.2.0",
39+
"@web/test-runner": "^0.13.29",
40+
"concurrently": "7.2.1",
4141
"esbuild": "0.18.17",
4242
"typescript": "5.1.6"
4343
},

src/CssChain.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ export interface CssChainCollection<T> extends Array<AnyElement&T>, AnyElement
2222
attr(name:string): CssChainCollection<T>;
2323
/** (alias for `setAttribute`) sets elements attribute, returns CssChain */
2424
attr(name:string, value:string): CssChainCollection<T>;
25+
/** (alias for `setAttribute`) sets elements attribute with value from callback, returns CssChain */
26+
attr(name:string, valueCallback:( (el:T, i:number, arr:CssChainCollection<T>)=>string) ): CssChainCollection<T>;
2527
/** (alias for `setAttribute`) sets `css`-defined sub-tree elements attribute, returns CssChain */
26-
attr(name:string, value:string, css:string): CssChainCollection<T>;
28+
attr(name:string, valueOrCallback:string | ( (el:T, i:number, arr:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
2729
/** returns 1st element property value or `undefined` for empty collection */
2830
prop(name:string): any;
2931
/** sets elements attribute, returns CssChain */

src/CssChain.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,24 @@ function assignedNodesLight(f)
8989
class
9090
CssChainT extends Array
9191
{
92-
attr(...args){ return args.length>1 ? (( args[2] ? this.$(args[2]) : this ).setAttribute(...args),this) : this.getAttribute(...args) }
93-
prop(...args){ return args.length>1 ? (( args[2] ? this.$(args[2]) : this ).forEach( el=>el[args[0]]=args[1]),this ): this[0][args[0]] }
92+
attr(...args)
93+
{ if( args.length < 2 )
94+
return this.getAttribute(...args);
95+
let [k,v,s] = args
96+
, $s = this.$(s);
97+
if(isFn(v))
98+
this.map(v).forEach((V,i)=>$s[i].setAttribute(k,V))
99+
else
100+
$s.setAttribute(...args);
101+
return this
102+
}
103+
prop(...args){ return args.length>1 ? (this.$(args[2]).forEach( el=>el[args[0]]=args[1]),this ): this[0][args[0]] }
94104
forEach( ...args){ Array.prototype.forEach.apply(this,args); return this }
95105
map( ...args){ return map(this,...args) }
96106
push(...args){ Array.prototype.push.apply(this,args); return this; }
97107
querySelector(css){ return new CssChainT().push( this.querySelectorAll(css)[0] ) }
98108
querySelectorAll(css){ return this.reduce( ($,el)=> $.push(...(el.shadowRoot||el).querySelectorAll(css) ), new CssChainT()) }
99-
$(...args){ return args.length ? this.querySelectorAll(...args) : this; }
109+
$(...args){ return args.length && args[0] ? this.querySelectorAll(...args) : this; }
100110
parent(css)
101111
{ const s = new Set()
102112
, add = n=> s.has(n) ? 0 : (s.add(n), n)

0 commit comments

Comments
 (0)