Skip to content

Commit 97f3d56

Browse files
committed
handle important declarations in inline styles - fixes #1834
1 parent d720f0b commit 97f3d56

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 {
@@ -51,15 +52,15 @@ export default class StyleAttributeWrapper extends AttributeWrapper {
5152

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

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

9899
const result = get_style_value(chunks);
99100

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

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

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

src/runtime/internal/dom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ export function set_input_type(input, type) {
177177
}
178178
}
179179

180-
export function set_style(node, key, value) {
181-
node.style.setProperty(key, value);
180+
export function set_style(node, key, value, important) {
181+
node.style.setProperty(key, value, important ? 'important' : '');
182182
}
183183

184184
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)