From 9f25ea9210bfbcaba94d4df0c0f029ed97bae42a Mon Sep 17 00:00:00 2001 From: Elie Bariche <33458222+ebariche@users.noreply.github.com> Date: Thu, 10 Aug 2023 11:29:03 -0400 Subject: [PATCH] fix(csp): Make setCursor CSP compliant --- src/Uno.UI/ts/WindowManager.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/Uno.UI/ts/WindowManager.ts b/src/Uno.UI/ts/WindowManager.ts index 262af9b7f6e8..6f73947e066e 100644 --- a/src/Uno.UI/ts/WindowManager.ts +++ b/src/Uno.UI/ts/WindowManager.ts @@ -127,7 +127,7 @@ namespace Uno.UI { private containerElement: HTMLDivElement; private rootElement: HTMLElement; - private cursorStyleElement: HTMLElement; + private cursorStyleRule: CSSStyleRule; private allActiveElementsById: { [id: string]: HTMLElement | SVGElement } = {}; private uiElementRegistrations: { @@ -1731,22 +1731,16 @@ namespace Uno.UI { if (unoBody) { - //always cleanup - if (this.cursorStyleElement != undefined) { - this.cursorStyleElement.remove(); - this.cursorStyleElement = undefined - } - - //only add custom overriding style if not auto - if (cssCursor != "auto") { + if (this.cursorStyleRule === undefined) { + const styleSheet = document.styleSheets[document.styleSheets.length - 1]; - // this part is only to override default css: .uno-buttonbase {cursor: pointer;} + const ruleId = styleSheet.insertRule(".uno-buttonbase { }", styleSheet.cssRules.length); - this.cursorStyleElement = document.createElement("style"); - this.cursorStyleElement.innerHTML = ".uno-buttonbase { cursor: " + cssCursor + "; }"; - document.body.appendChild(this.cursorStyleElement); + this.cursorStyleRule = styleSheet.cssRules[ruleId]; } + this.cursorStyleRule.style.cursor = cssCursor !== "auto" ? cssCursor : null; + unoBody.style.cursor = cssCursor; } return "ok";