diff --git a/src/compiler/compile/nodes/StyleDirective.ts b/src/compiler/compile/nodes/StyleDirective.ts index ea340cf2dc..9e3b4981e6 100644 --- a/src/compiler/compile/nodes/StyleDirective.ts +++ b/src/compiler/compile/nodes/StyleDirective.ts @@ -10,6 +10,7 @@ export default class StyleDirective extends Node { name: string; expression: Expression; should_cache: boolean; + important: boolean = false; constructor(component: Component, parent: Node, scope: TemplateScope, info: TemplateNode) { super(component, parent, scope, info); @@ -30,10 +31,16 @@ export default class StyleDirective extends Node { this.expression = new Expression(component, this, scope, identifier); this.should_cache = false; } else { + const last_chunk = info.value[info.value.length - 1]; + if (last_chunk && last_chunk.type === 'Text' && /\s*!important\s*;?\s*$/.test(last_chunk.data)) { + this.important = true; + last_chunk.data = last_chunk.data.replace(/\s*!important\s*;?\s*$/, ''); + if (!last_chunk.data) info.value.pop(); + } + const raw_expression = nodes_to_template_literal(info.value); this.expression = new Expression(component, this, scope, raw_expression); this.should_cache = raw_expression.expressions.length > 0; } - } } diff --git a/src/compiler/compile/render_dom/wrappers/Element/index.ts b/src/compiler/compile/render_dom/wrappers/Element/index.ts index 5145db9fef..5f730dab6a 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/index.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/index.ts @@ -1072,7 +1072,7 @@ export default class ElementWrapper extends Wrapper { add_styles(block: Block) { const has_spread = this.node.attributes.some(attr => attr.is_spread); this.node.styles.forEach((style_directive) => { - const { name, expression, should_cache } = style_directive; + const { name, expression, should_cache, important } = style_directive; const snippet = expression.manipulate(block); let cached_snippet; @@ -1081,7 +1081,7 @@ export default class ElementWrapper extends Wrapper { block.add_variable(cached_snippet, snippet); } - const updater = b`@set_style(${this.var}, "${name}", ${should_cache ? cached_snippet : snippet}, false)`; + const updater = b`@set_style(${this.var}, "${name}", ${should_cache ? cached_snippet : snippet}, ${important ? 'true' : 'false'})`; block.chunks.hydrate.push(updater); diff --git a/src/compiler/compile/render_ssr/handlers/Element.ts b/src/compiler/compile/render_ssr/handlers/Element.ts index 5957e1db0d..31a7845403 100644 --- a/src/compiler/compile/render_ssr/handlers/Element.ts +++ b/src/compiler/compile/render_ssr/handlers/Element.ts @@ -40,7 +40,10 @@ export default function (node: Element, renderer: Renderer, options: RenderOptio class_expression_list.reduce((lhs, rhs) => x`${lhs} + ' ' + ${rhs}`); const style_expression_list = node.styles.map(style_directive => { - const { name, expression: { node: expression } } = style_directive; + let { name, important, expression: { node: expression } } = style_directive; + if (important) { + expression = x`${expression} + ' !important'`; + } return p`"${name}": ${expression}`; }); diff --git a/test/runtime-puppeteer/samples/inline-style-directive-important/_config.js b/test/runtime-puppeteer/samples/inline-style-directive-important/_config.js new file mode 100644 index 0000000000..e06a640767 --- /dev/null +++ b/test/runtime-puppeteer/samples/inline-style-directive-important/_config.js @@ -0,0 +1,34 @@ +export default { + html: ` +