Skip to content

Commit c0ec266

Browse files
committed
fix: convert value to String if toString method exist
1 parent b527ed7 commit c0ec266

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/CSSStyleDeclaration.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,38 @@ describe('CSSStyleDeclaration', () => {
352352
expect(style.fillOpacity).toEqual('0');
353353
});
354354

355+
test('setting a value to empty string or null should remove the value', () => {
356+
var style = new CSSStyleDeclaration();
357+
style.setProperty('opacity', 0);
358+
expect(style.opacity).toEqual('0');
359+
style.setProperty('opacity', null);
360+
expect(style.opacity).toEqual('');
361+
style.setProperty('opacity', 0);
362+
expect(style.opacity).toEqual('0');
363+
style.setProperty('opacity', '');
364+
expect(style.opacity).toEqual('');
365+
});
366+
367+
test('setting a value to an object implementing toString() should work', () => {
368+
var style = new CSSStyleDeclaration();
369+
var object = {
370+
toString: function() {
371+
return 1;
372+
},
373+
};
374+
style.setProperty('opacity', object);
375+
expect(style.opacity).toEqual('1');
376+
style.setProperty('opacity', [0]);
377+
expect(style.opacity).toEqual('0');
378+
object = {
379+
toString: function() {
380+
return [1]; // Not recursive: [1].toString() === '1'
381+
},
382+
};
383+
style.setProperty('opacity', object);
384+
expect(style.opacity).toEqual('0');
385+
});
386+
355387
test('onchange callback should be called when the csstext changes', () => {
356388
var style = new CSSStyleDeclaration(function(cssText) {
357389
expect(cssText).toEqual('opacity: 0;');

lib/parsers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ exports.valueType = function valueType(val) {
4040
if (val === '' || val === null) {
4141
return exports.TYPES.NULL_OR_EMPTY_STR;
4242
}
43+
if (typeof val.toString === 'function') {
44+
val = val.toString();
45+
}
4346
if (typeof val === 'number') {
4447
val = val.toString();
4548
}

0 commit comments

Comments
 (0)