Skip to content

Commit b567eb2

Browse files
authored
Merge pull request #3435 from sveltejs/gh-1834
Handle !important in inline styles
2 parents 38001ce + fa222e7 commit b567eb2

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Text from '../../../nodes/Text';
1010
export interface StyleProp {
1111
key: string;
1212
value: Array<Text|Expression>;
13+
important: boolean;
1314
}
1415

1516
export default class StyleAttributeWrapper extends AttributeWrapper {
@@ -50,15 +51,15 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
5051

5152
block.builders.update.add_conditional(
5253
condition,
53-
`@set_style(${this.parent.var}, "${prop.key}", ${value});`
54+
`@set_style(${this.parent.var}, "${prop.key}", ${value}${prop.important ? ', 1' : ''});`
5455
);
5556
}
5657
} else {
5758
value = stringify((prop.value[0] as Text).data);
5859
}
5960

6061
block.builders.hydrate.add_line(
61-
`@set_style(${this.parent.var}, "${prop.key}", ${value});`
62+
`@set_style(${this.parent.var}, "${prop.key}", ${value}${prop.important ? ', 1' : ''});`
6263
);
6364
});
6465
}
@@ -96,7 +97,7 @@ function optimize_style(value: Array<Text|Expression>) {
9697

9798
const result = get_style_value(chunks);
9899

99-
props.push({ key, value: result.value });
100+
props.push({ key, value: result.value, important: result.important });
100101
chunks = result.chunks;
101102
}
102103

@@ -168,9 +169,19 @@ function get_style_value(chunks: Array<Text | Expression>) {
168169
}
169170
}
170171

172+
let important = false;
173+
174+
const last_chunk = value[value.length - 1];
175+
if (last_chunk && last_chunk.type === 'Text' && /\s*!important\s*$/.test(last_chunk.data)) {
176+
important = true;
177+
last_chunk.data = last_chunk.data.replace(/\s*!important\s*$/, '');
178+
if (!last_chunk.data) value.pop();
179+
}
180+
171181
return {
172182
chunks,
173-
value
183+
value,
184+
important
174185
};
175186
}
176187

src/runtime/internal/dom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ export function set_input_type(input, type) {
183183
}
184184
}
185185

186-
export function set_style(node, key, value) {
187-
node.style.setProperty(key, value);
186+
export function set_style(node, key, value, important) {
187+
node.style.setProperty(key, value, important ? 'important' : '');
188188
}
189189

190190
export function select_option(select, value) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default {
2+
html: `
3+
<p class="svelte-y94hdy" style="color: red !important; font-size: 20px !important; opacity: 1;">red</p>
4+
`,
5+
6+
test({ assert, component, target, window }) {
7+
const p = target.querySelector('p');
8+
9+
let styles = window.getComputedStyle(p);
10+
assert.equal(styles.color, 'red');
11+
assert.equal(styles.fontSize, '20px');
12+
13+
component.color = 'green';
14+
15+
styles = window.getComputedStyle(p);
16+
assert.equal(styles.color, 'green');
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
export let color = `red`;
3+
</script>
4+
5+
<p style="color: {color} !important; font-size: 20px !important; opacity: 1;">{color}</p>
6+
7+
<style>
8+
p {
9+
color: blue !important;
10+
font-size: 10px !important;
11+
}
12+
</style>

0 commit comments

Comments
 (0)