From c1fc1de77263bbb717f16309e54f3233a6f194f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Yves=20G=C3=A9rardy?= Date: Tue, 17 Jan 2023 15:04:19 +0100 Subject: [PATCH] Bypass css text (#2811) * [DOM mocks] enable setting element.style * Use the style setter directly --- render/render.js | 4 ++-- test-utils/domMock.js | 5 ++--- test-utils/tests/test-domMock.js | 13 +++++-------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/render/render.js b/render/render.js index 75a950b27..530313809 100644 --- a/render/render.js +++ b/render/render.js @@ -800,10 +800,10 @@ module.exports = function($window) { // Styles are equivalent, do nothing. } else if (style == null) { // New style is missing, just clear it. - element.style.cssText = "" + element.style = "" } else if (typeof style !== "object") { // New style is a string, let engine deal with patching. - element.style.cssText = style + element.style = style } else if (old == null || typeof old !== "object") { // `old` is missing or a string, `style` is an object. element.style.cssText = "" diff --git a/test-utils/domMock.js b/test-utils/domMock.js index f645595e0..883b2cb1e 100644 --- a/test-utils/domMock.js +++ b/test-utils/domMock.js @@ -346,9 +346,8 @@ module.exports = function(options) { get style() { return style }, - set style(_){ - // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style#Setting_style - throw new Error("setting element.style is not portable") + set style(value){ + this.style.cssText = value }, get className() { return this.attributes["class"] ? this.attributes["class"].value : "" diff --git a/test-utils/tests/test-domMock.js b/test-utils/tests/test-domMock.js index 269b641b7..36d3364cb 100644 --- a/test-utils/tests/test-domMock.js +++ b/test-utils/tests/test-domMock.js @@ -665,16 +665,13 @@ o.spec("domMock", function() { o(div.style.background).equals("url('/*foo*/')") }) - o("setting style throws", function () { + o("setting style updates style.cssText", function () { var div = $document.createElement("div") - var err = false - try { - div.style = "" - } catch (e) { - err = e - } + div.style = "background: red;" + + o(div.style.background).equals("red") + o(div.style.cssText).equals("background: red;") - o(err instanceof Error).equals(true) }) }) o.spec("events", function() {