Skip to content

Commit 04b74e5

Browse files
committed
prop/attr setter by with value callback 4th parameter
1 parent 7a37187 commit 04b74e5

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

src/CssChain.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export interface CssChainCollection<T> extends Array<AnyElement&T>, AnyElement
2525
/** (alias for `setAttribute`) sets elements attribute with value from callback, returns CssChain */
2626
attr(name:string, valueCallback:( (el:T, i:number, arr:CssChainCollection<T>)=>string) ): CssChainCollection<T>;
2727
/** (alias for `setAttribute`) sets `css`-defined sub-tree elements attribute, returns CssChain */
28-
attr(name:string, valueOrCallback:string | ( (el:T, i:number, arr:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
28+
attr(name:string, valueOrCallback:string | ( (el:T, i:number, arrCss:CssChainCollection<T>, arrThis:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
2929
/** returns 1st element property value or `undefined` for empty collection */
3030
prop(name:string): any;
31-
/** sets elements attribute, returns CssChain */
32-
prop(name:string, value:any): CssChainCollection<T>;
33-
/** sets `css`-defined sub-tree elements attribute, returns CssChain */
34-
prop(name:string, value:any, css:string): CssChainCollection<T>;
31+
/** sets elements property, returns CssChain */
32+
prop(name:string, valueOrCallback:any | ( (el:T, i:number, arr:CssChainCollection<T>)=>string)): CssChainCollection<T>;
33+
/** sets `css`-defined sub-tree elements property, returns CssChain */
34+
prop(name:string, valueOrCallback:any | ( (el:T, i:number, arrCss:CssChainCollection<T>, arrThis:CssChainCollection<T>)=>string), css:string): CssChainCollection<T>;
3535
/** selects 1st elements by @param css string from each collection element, returns CssChain */
3636
querySelector(css: string): CssChainT;
3737
/** selects child elements by @param css string, returns CssChain */

src/CssChain.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,22 @@ CssChainT extends Array
9595
let [k,v,s] = args
9696
, $s = this.$(s);
9797
if(isFn(v))
98-
this.map(v).forEach((V,i)=>$s[i].setAttribute(k,V))
98+
$s.map((n,i,arr)=>v(n,i,arr,this)).forEach((V,i)=>$s[i].setAttribute(k,V))
9999
else
100100
$s.setAttribute(...args);
101101
return this
102102
}
103-
prop(...args){ return args.length>1 ? (this.$(args[2]).forEach( el=>el[args[0]]=args[1]),this ): this[0][args[0]] }
103+
prop(...args)
104+
{ if( args.length < 2 )
105+
return this[0][args[0]];
106+
let [k,v,s] = args
107+
, $s = this.$(s);
108+
if(isFn(v))
109+
$s.map((n,i,arr)=>v(n,i,arr,this)).forEach((V,i)=>$s[i][k]=V)
110+
else
111+
$s.forEach( el=>el[k]=v);
112+
return this
113+
}
104114
forEach( ...args){ Array.prototype.forEach.apply(this,args); return this }
105115
map( ...args){ return map(this,...args) }
106116
push(...args){ Array.prototype.push.apply(this,args); return this; }

test/CssChain-methods.test.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ describe( 'CssChain own methods', () =>
4848
it( 'attr(name,cb,css)', async ()=>
4949
{
5050
const el = await fixture(html`<div><a id="a1"><b>B1</b></a><a id="a2"><b>B2</b></a></div>`);
51-
const $X = $$('a',el)
52-
const css ='b'
53-
expect( $X.attr('title',(el,i)=>el.innerHTML+'-'+i, css).length ).to.equal(2);
54-
expect( $X.$(css).getAttribute('title') ).to.equal('<b>B1</b>-0');
55-
expect( $X.$(css)[0].getAttribute('title') ).to.equal('<b>B1</b>-0');
56-
expect( $X.$(css)[1].getAttribute('title') ).to.equal('<b>B2</b>-1');
57-
expect( $X.$(css).attr('title') ).to.equal('<b>B1</b>-0');
51+
const $X = $$('a',el);
52+
const css ='b';
53+
const cb = (n,i,arr,$)=>arr[i].innerText+'-'+$[i].id+'-'+i+'-'+n.innerText;
54+
expect( $X.attr('title',cb, css).length ).to.equal(2);
55+
expect( $X.$(css).getAttribute('title') ).to.equal('B1-a1-0-B1');
56+
expect( $X.$(css)[0].getAttribute('title') ).to.equal('B1-a1-0-B1');
57+
expect( $X.$(css)[1].getAttribute('title') ).to.equal('B2-a2-1-B2');
58+
expect( $X.$(css).attr('title') ).to.equal('B1-a1-0-B1');
5859
} );
5960

6061
it( 'prop(name), prop(name,val)', async ()=>
@@ -72,6 +73,18 @@ describe( 'CssChain own methods', () =>
7273

7374
expect( $X.attr('id') ).to.equal('AZ');
7475
} );
76+
it( 'prop(name,valCallback)', async ()=>
77+
{
78+
const el = await fixture(html`<div><a id="a1"></a><a id="a2"></a></div>`);
79+
const $X = $$('a',el);
80+
const cb = (n,i,arr,$) => `${ n.id }-${ i }-${arr[i].id}-${ $[i].tagName }`;
81+
expect( $X.prop('id', cb ).length ).to.equal(2);
82+
expect( $X.prop('id') ).to.equal('a1-0-a1-A');
83+
expect( $X[0].id ).to.equal('a1-0-a1-A');
84+
expect( $X[1].id ).to.equal('a2-1-a2-A');
85+
86+
expect( $X.attr('id') ).to.equal('a1-0-a1-A');
87+
} );
7588
it( 'prop(name,val,css)', async ()=>
7689
{
7790
const el = await fixture(html`<div><a id="a1"></a><a id="a2"></a></div>`);
@@ -84,6 +97,19 @@ describe( 'CssChain own methods', () =>
8497

8598
expect( $X.$('a').attr('id') ).to.equal('AZ');
8699
} );
100+
it( 'prop(name,valCb,css)', async ()=>
101+
{
102+
const el = await fixture(html`<div><a id="a1"></a><a id="a2"></a></div>`);
103+
const $X = $$(el)
104+
const cb = (n,i,arr,$) => `${ n.id }-${ i }-${arr[i].id}-${ $.tagName }`;
105+
106+
expect( $X.prop('id', cb, 'a').length ).to.equal(1);
107+
expect( $X.$('a').prop('id') ).to.equal('a1-0-a1-DIV');
108+
expect( $X.$('a')[0].id ).to.equal('a1-0-a1-DIV');
109+
expect( $X.$('a')[1].id ).to.equal('a2-1-a2-DIV');
110+
111+
expect( $X.$('a').attr('id') ).to.equal('a1-0-a1-DIV');
112+
} );
87113
it( 'attr(name,val,css)', async ()=>
88114
{
89115
const el = await fixture(html`<div><a id="a1"></a><a id="a2"></a></div>`);

0 commit comments

Comments
 (0)