Skip to content

Commit

Permalink
fix: convert value to String if toString method exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cdoublev committed Apr 25, 2021
1 parent b527ed7 commit c0ec266
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,38 @@ describe('CSSStyleDeclaration', () => {
expect(style.fillOpacity).toEqual('0');
});

test('setting a value to empty string or null should remove the value', () => {
var style = new CSSStyleDeclaration();
style.setProperty('opacity', 0);
expect(style.opacity).toEqual('0');
style.setProperty('opacity', null);
expect(style.opacity).toEqual('');
style.setProperty('opacity', 0);
expect(style.opacity).toEqual('0');
style.setProperty('opacity', '');
expect(style.opacity).toEqual('');
});

test('setting a value to an object implementing toString() should work', () => {
var style = new CSSStyleDeclaration();
var object = {
toString: function() {
return 1;
},
};
style.setProperty('opacity', object);
expect(style.opacity).toEqual('1');
style.setProperty('opacity', [0]);
expect(style.opacity).toEqual('0');
object = {
toString: function() {
return [1]; // Not recursive: [1].toString() === '1'
},
};
style.setProperty('opacity', object);
expect(style.opacity).toEqual('0');
});

test('onchange callback should be called when the csstext changes', () => {
var style = new CSSStyleDeclaration(function(cssText) {
expect(cssText).toEqual('opacity: 0;');
Expand Down
3 changes: 3 additions & 0 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ exports.valueType = function valueType(val) {
if (val === '' || val === null) {
return exports.TYPES.NULL_OR_EMPTY_STR;
}
if (typeof val.toString === 'function') {
val = val.toString();
}
if (typeof val === 'number') {
val = val.toString();
}
Expand Down

0 comments on commit c0ec266

Please sign in to comment.