Skip to content

Commit

Permalink
Merge pull request #3435 from sveltejs/gh-1834
Browse files Browse the repository at this point in the history
Handle !important in inline styles
  • Loading branch information
Rich-Harris committed Aug 22, 2019
2 parents 38001ce + fa222e7 commit b567eb2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/compiler/compile/render_dom/wrappers/Element/StyleAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Text from '../../../nodes/Text';
export interface StyleProp {
key: string;
value: Array<Text|Expression>;
important: boolean;
}

export default class StyleAttributeWrapper extends AttributeWrapper {
Expand Down Expand Up @@ -50,15 +51,15 @@ export default class StyleAttributeWrapper extends AttributeWrapper {

block.builders.update.add_conditional(
condition,
`@set_style(${this.parent.var}, "${prop.key}", ${value});`
`@set_style(${this.parent.var}, "${prop.key}", ${value}${prop.important ? ', 1' : ''});`
);
}
} else {
value = stringify((prop.value[0] as Text).data);
}

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

const result = get_style_value(chunks);

props.push({ key, value: result.value });
props.push({ key, value: result.value, important: result.important });
chunks = result.chunks;
}

Expand Down Expand Up @@ -168,9 +169,19 @@ function get_style_value(chunks: Array<Text | Expression>) {
}
}

let important = false;

const last_chunk = value[value.length - 1];
if (last_chunk && last_chunk.type === 'Text' && /\s*!important\s*$/.test(last_chunk.data)) {
important = true;
last_chunk.data = last_chunk.data.replace(/\s*!important\s*$/, '');
if (!last_chunk.data) value.pop();
}

return {
chunks,
value
value,
important
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export function set_input_type(input, type) {
}
}

export function set_style(node, key, value) {
node.style.setProperty(key, value);
export function set_style(node, key, value, important) {
node.style.setProperty(key, value, important ? 'important' : '');
}

export function select_option(select, value) {
Expand Down
18 changes: 18 additions & 0 deletions test/runtime/samples/inline-style-important/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
html: `
<p class="svelte-y94hdy" style="color: red !important; font-size: 20px !important; opacity: 1;">red</p>
`,

test({ assert, component, target, window }) {
const p = target.querySelector('p');

let styles = window.getComputedStyle(p);
assert.equal(styles.color, 'red');
assert.equal(styles.fontSize, '20px');

component.color = 'green';

styles = window.getComputedStyle(p);
assert.equal(styles.color, 'green');
}
}
12 changes: 12 additions & 0 deletions test/runtime/samples/inline-style-important/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
export let color = `red`;
</script>

<p style="color: {color} !important; font-size: 20px !important; opacity: 1;">{color}</p>

<style>
p {
color: blue !important;
font-size: 10px !important;
}
</style>

0 comments on commit b567eb2

Please sign in to comment.