Skip to content

Commit

Permalink
Handle empty string and null for margin and padding subparts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins authored Dec 28, 2023
1 parent f8cf3e6 commit 792877d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,43 @@ describe('CSSStyleDeclaration', () => {
expect(style.marginTop).toEqual('0.5ex');
});

test('setting empty string and null to a padding or margin works', () => {
var style = new CSSStyleDeclaration();
var parts = ['Top', 'Right', 'Bottom', 'Left'];
function testParts(base, nullValue) {
var props = [base].concat(parts.map((part) => base + part));
for (let prop of props) {
expect(style[prop]).toEqual('');
style[prop] = '10px';
expect(style[prop]).toEqual('10px');
style[prop] = nullValue;
expect(style[prop]).toEqual('');
}
}

testParts('margin', '');
testParts('margin', null);
testParts('padding', '');
testParts('padding', null);
});

test('setting undefined to a padding or margin does nothing', () => {
var style = new CSSStyleDeclaration();
var parts = ['Top', 'Right', 'Bottom', 'Left'];
function testParts(base) {
var props = [base].concat(parts.map((part) => base + part));
for (let prop of props) {
style[prop] = '10px';
expect(style[prop]).toEqual('10px');
style[prop] = undefined;
expect(style[prop]).toEqual('10px');
}
}

testParts('margin');
testParts('padding');
});

test('setting null to background works', () => {
var style = new CSSStyleDeclaration();
style.background = 'red';
Expand Down
3 changes: 3 additions & 0 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,9 @@ exports.subImplicitSetter = function (prefix, part, isValid, parser) {
if (typeof v === 'number') {
v = v.toString();
}
if (v === null) {
v = '';
}
if (typeof v !== 'string') {
return undefined;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/properties/margin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var isValid = function (v) {
}
var type = parsers.valueType(v);
return (
type === TYPES.NULL_OR_EMPTY_STR ||
type === TYPES.LENGTH ||
type === TYPES.PERCENT ||
(type === TYPES.INTEGER && (v === '0' || v === 0))
Expand Down Expand Up @@ -40,6 +41,9 @@ module.exports.definition = {
if (typeof v === 'number') {
v = String(v);
}
if (v === null) {
v = '';
}
if (typeof v !== 'string') {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/properties/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var TYPES = parsers.TYPES;
var isValid = function (v) {
var type = parsers.valueType(v);
return (
type === TYPES.NULL_OR_EMPTY_STR ||
type === TYPES.LENGTH ||
type === TYPES.PERCENT ||
(type === TYPES.INTEGER && (v === '0' || v === 0))
Expand Down Expand Up @@ -33,6 +34,9 @@ module.exports.definition = {
if (typeof v === 'number') {
v = String(v);
}
if (v === null) {
v = '';
}
if (typeof v !== 'string') {
return;
}
Expand Down

0 comments on commit 792877d

Please sign in to comment.