Skip to content

Commit

Permalink
Merge pull request #111 from eps1lon/feat/custom-property-access
Browse files Browse the repository at this point in the history
feat: Implement read/write of custom properties
  • Loading branch information
jsakas authored Jan 23, 2020
2 parents 935939b + 9a3a612 commit 26fbcec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ CSSStyleDeclaration.prototype = {
this.removeProperty(name);
return;
}
var isCustomProperty = name.indexOf('--') === 0;
if (isCustomProperty) {
this._setProperty(name, value, priority);
return;
}
var lowercaseName = name.toLowerCase();
if (!allProperties.has(lowercaseName) && !allExtraProperties.has(lowercaseName)) {
return;
Expand Down
29 changes: 29 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,33 @@ describe('CSSStyleDeclaration', () => {
expect(0).toEqual(style.length);
expect(undefined).toEqual(style[0]);
});

test('getPropertyValue for custom properties in cssText', () => {
const style = new CSSStyleDeclaration();
style.cssText = '--foo: red';

expect(style.getPropertyValue('--foo')).toEqual('red');
});

test('getPropertyValue for custom properties with setProperty', () => {
const style = new CSSStyleDeclaration();
style.setProperty('--bar', 'blue');

expect(style.getPropertyValue('--bar')).toEqual('blue');
});

test('getPropertyValue for custom properties with object setter', () => {
const style = new CSSStyleDeclaration();
style['--baz'] = 'yellow';

expect(style.getPropertyValue('--baz')).toEqual('');
});

test('custom properties are case-sensitive', () => {
const style = new CSSStyleDeclaration();
style.cssText = '--fOo: purple';

expect(style.getPropertyValue('--foo')).toEqual('');
expect(style.getPropertyValue('--fOo')).toEqual('purple');
});
});

0 comments on commit 26fbcec

Please sign in to comment.