Skip to content

Commit b8189bc

Browse files
committed
bug #3363 [LiveComponent] Fix preservation of !important inline style changes (xDeSwa)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Fix preservation of !important inline style changes | Q | A | -------------- | --- | Bug fix? | yes | New feature? | no | Deprecations? | no | Documentation? | no | Issues | Fix #3317 | License | MIT Commits ------- 8ced30c fix properly apply inline styles with important priority
2 parents d6c9184 + 8ced30c commit b8189bc

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,8 @@ var ElementChanges = class {
10481048
element.classList.add(...this.addedClasses);
10491049
element.classList.remove(...this.removedClasses);
10501050
this.styleChanges.getChangedItems().forEach((change) => {
1051-
element.style.setProperty(change.name, change.value);
1051+
if (/!\s*important/i.test(change.value)) element.style.setProperty(change.name, change.value.replace(/!\s*important/i, "").trim(), "important");
1052+
else element.style.setProperty(change.name, change.value);
10521053
});
10531054
this.styleChanges.getRemovedItems().forEach((styleName) => {
10541055
element.style.removeProperty(styleName);

src/LiveComponent/assets/src/Rendering/ElementChanges.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ export default class ElementChanges {
6767
element.classList.remove(...this.removedClasses);
6868

6969
this.styleChanges.getChangedItems().forEach((change) => {
70-
element.style.setProperty(change.name, change.value);
71-
return;
70+
// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty
71+
if (/!\s*important/i.test(change.value)) {
72+
element.style.setProperty(change.name, change.value.replace(/!\s*important/i, '').trim(), 'important');
73+
} else {
74+
element.style.setProperty(change.name, change.value);
75+
}
7276
});
7377

7478
this.styleChanges.getRemovedItems().forEach((styleName) => {

src/LiveComponent/assets/test/unit/Rendering/ElementChanges.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ describe('ElementChanges', () => {
7474
expect(element.style.color).toBe('blue');
7575
expect(element.getAttribute('data-foo')).toBe('baz');
7676
});
77+
78+
it('applies styles correctly when !important is used', async () => {
79+
const element = document.createElement('div');
80+
const changes = new ElementChanges();
81+
changes.addStyle('z-index', '999 !important', null);
82+
changes.addStyle('color', 'red ! IMPORTANT', null);
83+
changes.applyToElement(element);
84+
expect(element.style.getPropertyValue('z-index')).toBe('999');
85+
expect(element.style.getPropertyPriority('z-index')).toBe('important');
86+
expect(element.style.getPropertyValue('color')).toBe('red');
87+
expect(element.style.getPropertyPriority('color')).toBe('important');
88+
});
7789
});

0 commit comments

Comments
 (0)