Skip to content

Commit 26fbcec

Browse files
authored
Merge pull request #111 from eps1lon/feat/custom-property-access
feat: Implement read/write of custom properties
2 parents 935939b + 9a3a612 commit 26fbcec

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/CSSStyleDeclaration.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ CSSStyleDeclaration.prototype = {
5656
this.removeProperty(name);
5757
return;
5858
}
59+
var isCustomProperty = name.indexOf('--') === 0;
60+
if (isCustomProperty) {
61+
this._setProperty(name, value, priority);
62+
return;
63+
}
5964
var lowercaseName = name.toLowerCase();
6065
if (!allProperties.has(lowercaseName) && !allExtraProperties.has(lowercaseName)) {
6166
return;

lib/CSSStyleDeclaration.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,33 @@ describe('CSSStyleDeclaration', () => {
518518
expect(0).toEqual(style.length);
519519
expect(undefined).toEqual(style[0]);
520520
});
521+
522+
test('getPropertyValue for custom properties in cssText', () => {
523+
const style = new CSSStyleDeclaration();
524+
style.cssText = '--foo: red';
525+
526+
expect(style.getPropertyValue('--foo')).toEqual('red');
527+
});
528+
529+
test('getPropertyValue for custom properties with setProperty', () => {
530+
const style = new CSSStyleDeclaration();
531+
style.setProperty('--bar', 'blue');
532+
533+
expect(style.getPropertyValue('--bar')).toEqual('blue');
534+
});
535+
536+
test('getPropertyValue for custom properties with object setter', () => {
537+
const style = new CSSStyleDeclaration();
538+
style['--baz'] = 'yellow';
539+
540+
expect(style.getPropertyValue('--baz')).toEqual('');
541+
});
542+
543+
test('custom properties are case-sensitive', () => {
544+
const style = new CSSStyleDeclaration();
545+
style.cssText = '--fOo: purple';
546+
547+
expect(style.getPropertyValue('--foo')).toEqual('');
548+
expect(style.getPropertyValue('--fOo')).toEqual('purple');
549+
});
521550
});

0 commit comments

Comments
 (0)