Skip to content

Commit

Permalink
fix: skip onchange callback if serialized value did not change
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Dec 12, 2021
1 parent b527ed7 commit cbdc83a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var CSSStyleDeclaration = function CSSStyleDeclaration(onChangeCallback) {
this._values = {};
this._importants = {};
this._length = 0;
this._setInProgress = false;
this._onChange =
onChangeCallback ||
function() {
Expand Down Expand Up @@ -77,6 +78,7 @@ CSSStyleDeclaration.prototype = {
this.removeProperty(name);
return;
}
var originalText = this.cssText;
if (this._values[name]) {
// Property already exist. Overwrite it.
var index = Array.prototype.indexOf.call(this, name);
Expand All @@ -91,7 +93,9 @@ CSSStyleDeclaration.prototype = {
}
this._values[name] = value;
this._importants[name] = priority;
this._onChange(this.cssText);
if (this.cssText !== originalText && !this._setInProgress) {
this._onChange(this.cssText);
}
},

/**
Expand Down Expand Up @@ -196,6 +200,7 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
// malformed css, just return
return;
}
this._setInProgress = true;
var rule_length = dummyRule.length;
var name;
for (i = 0; i < rule_length; ++i) {
Expand All @@ -206,6 +211,7 @@ Object.defineProperties(CSSStyleDeclaration.prototype, {
dummyRule.getPropertyPriority(name)
);
}
this._setInProgress = false;
this._onChange(this.cssText);
},
enumerable: true,
Expand Down
36 changes: 36 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,46 @@ describe('CSSStyleDeclaration', () => {
});

test('onchange callback should be called when the csstext changes', () => {
var called = 0;
var style = new CSSStyleDeclaration(function(cssText) {
called++;
expect(cssText).toEqual('opacity: 0;');
});
style.cssText = 'opacity: 0;';
expect(called).toEqual(1);
style.cssText = 'opacity: 0;';
expect(called).toEqual(2);
});

test('onchange callback should be called only once when multiple properties were added', () => {
var called = 0;
var style = new CSSStyleDeclaration(function(cssText) {
called++;
expect(cssText).toEqual('width: 100px; height: 100px;');
});
style.cssText = 'width: 100px;height:100px;';
expect(called).toEqual(1);
});

test('onchange callback should not be called when property is set to the same value', () => {
var called = 0;
var style = new CSSStyleDeclaration(function() {
called++;
});

style.setProperty('opacity', 0);
expect(called).toEqual(1);
style.setProperty('opacity', 0);
expect(called).toEqual(1);
});

test('onchange callback should not be called when removeProperty was called on non-existing property', () => {
var called = 0;
var style = new CSSStyleDeclaration(function() {
called++;
});
style.removeProperty('opacity');
expect(called).toEqual(0);
});

test('setting float should work the same as cssfloat', () => {
Expand Down

0 comments on commit cbdc83a

Please sign in to comment.