Skip to content

Commit

Permalink
Properly update margin/padding properties when removing individual ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ska-kialo authored and domenic committed Dec 28, 2023
1 parent dd59760 commit 65b18d1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
54 changes: 54 additions & 0 deletions lib/CSSStyleDeclaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,60 @@ describe('CSSStyleDeclaration', () => {
}
});

test('removing and setting individual margin properties updates the combined property accordingly', () => {
var style = new CSSStyleDeclaration();
style.margin = '1px 2px 3px 4px';

style.marginTop = '';
expect(style.margin).toEqual('');
expect(style.marginRight).toEqual('2px');
expect(style.marginBottom).toEqual('3px');
expect(style.marginLeft).toEqual('4px');

style.marginBottom = '';
expect(style.margin).toEqual('');
expect(style.marginRight).toEqual('2px');
expect(style.marginLeft).toEqual('4px');

style.marginBottom = '5px';
expect(style.margin).toEqual('');
expect(style.marginRight).toEqual('2px');
expect(style.marginBottom).toEqual('5px');
expect(style.marginLeft).toEqual('4px');

style.marginTop = '6px';
expect(style.cssText).toEqual('margin: 6px 2px 5px 4px;');
});

test.each(['padding', 'margin'])(
'removing an individual %s property should remove the combined property and replace it with the remaining individual ones',
(property) => {
var style = new CSSStyleDeclaration();
var parts = ['Top', 'Right', 'Bottom', 'Left'];
var partValues = ['1px', '2px', '3px', '4px'];

for (var j = 0; j < parts.length; j++) {
var partToRemove = parts[j];
style[property] = partValues.join(' ');
style[property + partToRemove] = '';

// Main property should have been removed
expect(style[property]).toEqual('');

// Expect other parts to still be there
for (var k = 0; k < parts.length; k++) {
var propertyCss = property + '-' + parts[k].toLowerCase() + ': ' + partValues[k] + ';';
if (k === j) {
expect(style[property + parts[k]]).toEqual('');
expect(style.cssText).not.toContain(propertyCss);
} else {
expect(style[property + parts[k]]).toEqual(partValues[k]);
expect(style.cssText).toContain(propertyCss);
}
}
}
});

test('setting a value to 0 should return the string value', () => {
var style = new CSSStyleDeclaration();
style.setProperty('fill-opacity', 0);
Expand Down
18 changes: 9 additions & 9 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,19 +694,19 @@ exports.subImplicitSetter = function (prefix, part, isValid, parser) {
}
v = parser(v);
this._setProperty(property, v);
var parts = [];
for (var i = 0; i < 4; i++) {
if (this._values[subparts[i]] == null || this._values[subparts[i]] === '') {
break;
}
parts.push(this._values[subparts[i]]);
}
if (parts.length === 4) {
for (i = 0; i < 4; i++) {

var parts = subparts.map(subpart => this._values[subpart]);
if (parts.every(p => p !== '' && p != null)) {
for (var i = 0; i < subparts.length; i++) {
this.removeProperty(subparts[i]);
this._values[subparts[i]] = parts[i];
}
this._setProperty(prefix, parts.join(' '));
} else {
this.removeProperty(prefix);
for (var i = 0; i < subparts.length; i++) {
this._setProperty(subparts[i], parts[i]);
}
}
return v;
};
Expand Down

0 comments on commit 65b18d1

Please sign in to comment.