diff --git a/src/generators/dom/visitors/Element/StyleAttribute.ts b/src/generators/dom/visitors/Element/StyleAttribute.ts index 2ca0618eb2..b821ed4719 100644 --- a/src/generators/dom/visitors/Element/StyleAttribute.ts +++ b/src/generators/dom/visitors/Element/StyleAttribute.ts @@ -60,7 +60,7 @@ export default function visitStyleAttribute( block.builders.update.addConditional( condition, - `${node.var}.style.setProperty('${prop.key}', ${value});` + `@setStyle(${node.var}, '${prop.key}', ${value});` ); } } else { @@ -68,7 +68,7 @@ export default function visitStyleAttribute( } block.builders.hydrate.addLine( - `${node.var}.style.setProperty('${prop.key}', ${value});` + `@setStyle(${node.var}, '${prop.key}', ${value});` ); }); } diff --git a/src/shared/dom.js b/src/shared/dom.js index 0372d96fdc..e3b1676e1c 100644 --- a/src/shared/dom.js +++ b/src/shared/dom.js @@ -137,4 +137,8 @@ export function setInputType(input, type) { try { input.type = type; } catch (e) {} +} + +export function setStyle(node, key, value) { + node.style.setProperty(key, value); } \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index 37abe9d46f..c9f7b3729a 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -25,6 +25,10 @@ function createElement(name) { return document.createElement(name); } +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + function destroy(detach) { this.destroy = noop; this.fire('destroy'); @@ -163,8 +167,8 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('color', state.color); - div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); + setStyle(div, 'color', state.color); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); }, mount: function ( target, anchor ) { @@ -173,11 +177,11 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.color ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); } if ( changed.x || changed.y ) { - div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); } }, diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js index 49cd174c7e..3d48ec09a8 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected.js +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -1,4 +1,4 @@ -import { assign, createElement, detachNode, insertNode, noop, proto } from "svelte/shared.js"; +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment ( state, component ) { var div; @@ -10,8 +10,8 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('color', state.color); - div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); + setStyle(div, 'color', state.color); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); }, mount: function ( target, anchor ) { @@ -20,11 +20,11 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.color ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); } if ( changed.x || changed.y ) { - div.style.setProperty('transform', "translate(" + state.x + "px," + state.y + "px)"); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); } }, diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index 45eb18fb0a..e3793b4fe1 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -25,6 +25,10 @@ function createElement(name) { return document.createElement(name); } +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + function destroy(detach) { this.destroy = noop; this.fire('destroy'); @@ -163,7 +167,7 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('background', "url(data:image/png;base64," + state.data + ")"); + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); }, mount: function ( target, anchor ) { @@ -172,7 +176,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.data ) { - div.style.setProperty('background', "url(data:image/png;base64," + state.data + ")"); + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); } }, diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js index bd5439dbf6..b02e794bb8 100644 --- a/test/js/samples/inline-style-optimized-url/expected.js +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -1,4 +1,4 @@ -import { assign, createElement, detachNode, insertNode, noop, proto } from "svelte/shared.js"; +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment ( state, component ) { var div; @@ -10,7 +10,7 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('background', "url(data:image/png;base64," + state.data + ")"); + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); }, mount: function ( target, anchor ) { @@ -19,7 +19,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.data ) { - div.style.setProperty('background', "url(data:image/png;base64," + state.data + ")"); + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); } }, diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index 5ba7797e47..98713a9e1b 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -25,6 +25,10 @@ function createElement(name) { return document.createElement(name); } +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + function destroy(detach) { this.destroy = noop; this.fire('destroy'); @@ -163,7 +167,7 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); }, mount: function ( target, anchor ) { @@ -172,7 +176,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.color ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); } }, diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js index 5c9883775e..2de1d2b084 100644 --- a/test/js/samples/inline-style-optimized/expected.js +++ b/test/js/samples/inline-style-optimized/expected.js @@ -1,4 +1,4 @@ -import { assign, createElement, detachNode, insertNode, noop, proto } from "svelte/shared.js"; +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; function create_main_fragment ( state, component ) { var div; @@ -10,7 +10,7 @@ function create_main_fragment ( state, component ) { }, hydrate: function ( nodes ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); }, mount: function ( target, anchor ) { @@ -19,7 +19,7 @@ function create_main_fragment ( state, component ) { update: function ( changed, state ) { if ( changed.color ) { - div.style.setProperty('color', state.color); + setStyle(div, 'color', state.color); } },