Skip to content

Commit

Permalink
fix: ensure priority is set before calling the onchange callback
Browse files Browse the repository at this point in the history
  • Loading branch information
LeahHirst committed Aug 25, 2022
1 parent b527ed7 commit 475c294
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ CSSStyleDeclaration.prototype = {
var isCustomProperty = name.indexOf('--') === 0;
if (isCustomProperty) {
this._setProperty(name, value, priority);
this._onChange(this.cssText);
return;
}
var lowercaseName = name.toLowerCase();
Expand All @@ -68,6 +69,7 @@ CSSStyleDeclaration.prototype = {

this[lowercaseName] = value;
this._importants[lowercaseName] = priority;
this._onChange(this.cssText);
},
_setProperty: function(name, value, priority) {
if (value === undefined) {
Expand All @@ -91,7 +93,6 @@ CSSStyleDeclaration.prototype = {
}
this._values[name] = value;
this._importants[name] = priority;
this._onChange(this.cssText);
},

/**
Expand Down
14 changes: 14 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ describe('CSSStyleDeclaration', () => {
style.setProperty('opacity', 0);
});

test('onchange callback should be called when priority is specified on a setProperty call', () => {
var style = new CSSStyleDeclaration(function(cssText) {
expect(cssText).toEqual('opacity: 0 !important;');
});
style.setProperty('opacity', 0, 'important');
});

test('onchange callback should be called when custom property is set', () => {
var style = new CSSStyleDeclaration(function(cssText) {
expect(cssText).toEqual('--test-prop: 0;');
});
style.setProperty('--test-prop', 0);
});

test('setting float should work the same as cssfloat', () => {
var style = new CSSStyleDeclaration();
style.float = 'left';
Expand Down

0 comments on commit 475c294

Please sign in to comment.