From e0d69e4e11bd39df063bff9f0f4406d555739577 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 27 Sep 2024 19:36:19 +0400 Subject: [PATCH 1/2] use new css api when available --- .eslintrc | 2 +- demo/csp-simple.html | 34 ++++++++++++++++++++++++++++++++++ demo/csp.html | 2 +- src/lib/dom.js | 25 ++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 demo/csp-simple.html diff --git a/.eslintrc b/.eslintrc index e2728a18a1d..e396864f776 100644 --- a/.eslintrc +++ b/.eslintrc @@ -26,8 +26,8 @@ clearTimeout: true, setInterval: true, clearInterval: true, + CSSStyleSheet: true, Blob: true, - cvox: true, alert: true, prompt: true, XMLHttpRequest: true, diff --git a/demo/csp-simple.html b/demo/csp-simple.html new file mode 100644 index 00000000000..e1d66f7cabd --- /dev/null +++ b/demo/csp-simple.html @@ -0,0 +1,34 @@ + + + + + + + Editor + + +

+
+
+
+
+
+
+
+
+
diff --git a/demo/csp.html b/demo/csp.html
index a03f0fcae37..77f08a2ebf8 100644
--- a/demo/csp.html
+++ b/demo/csp.html
@@ -6,7 +6,7 @@
   
   Editor
diff --git a/src/lib/dom.js b/src/lib/dom.js
index a83b0a6f3bf..1f193b7bf8d 100644
--- a/src/lib/dom.js
+++ b/src/lib/dom.js
@@ -271,15 +271,38 @@ function importCssString(cssText, id, target) {
     if (id)
         cssText += "\n/*# sourceURL=ace/css/" + id + " */";
     
+    if (!USE_STYLE_TAG) {
+        try {
+            var stylesheet = styles[id];
+            if (!stylesheet) {
+                stylesheet = styles[id] = new CSSStyleSheet();
+                stylesheet.replaceSync(cssText);
+            }
+            container.adoptedStyleSheets.push(stylesheet);
+            USE_STYLE_TAG = false;
+            return;
+        } catch(e) {
+            if (USE_STYLE_TAG === null) {
+                USE_STYLE_TAG = true;
+            } else {
+                setTimeout(function() {
+                    throw e;
+                });
+            }
+        }
+    }
+    
     var style = exports.createElement("style");
     style.appendChild(doc.createTextNode(cssText));
     if (id)
         style.id = id;
-
     if (container == doc)
         container = exports.getDocumentHead(doc);
     container.insertBefore(style, container.firstChild);
 }
+var USE_STYLE_TAG = null;
+var styles = Object.create(null);
+
 exports.importCssString = importCssString;
 
 /**

From 7235a2dcf92d3679708070b32c825199bc006725 Mon Sep 17 00:00:00 2001
From: nightwing 
Date: Mon, 28 Jul 2025 12:43:38 +0400
Subject: [PATCH 2/2] fix test

---
 src/ace_test.js | 24 +++++++++++++++++++++++-
 src/lib/dom.js  |  8 ++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/ace_test.js b/src/ace_test.js
index 2f1921fe1cf..6552245f758 100644
--- a/src/ace_test.js
+++ b/src/ace_test.js
@@ -89,7 +89,9 @@ module.exports = {
     "test: useStrictCSP": function() {
         ace.config.set("useStrictCSP", undefined);
         function getStyleNode() {
-            return document.getElementById("test.css");
+            return document.adoptedStyleSheets ?
+                document.adoptedStyleSheets.find(sheet => sheet.$id === "test.css") :
+                document.getElementById("test.css");
         }
         assert.ok(!getStyleNode());
         dom.importCssString("test{}", "test.css", false);
@@ -170,6 +172,26 @@ module.exports = {
         var editor = ace.edit(el);
         assert.equal(editor.container, el);
         editor.destroy();
+    },
+    "test: use adoptedStyleSheets": function() {
+        dom.$resetCssModeForTests();
+        var div = document.createElement("div");
+        div.adoptedStyleSheets = [];
+        div.getRootNode = function() {
+            return div;
+        };
+        if (!window.CSSStyleSheet) {
+            window.CSSStyleSheet = function() {
+                this.cssRules = [];
+                this.replaceSync = function() {};
+            };
+        }
+
+        var editor = ace.edit(div);
+        console.log(div.adoptedStyleSheets);
+
+        editor.destroy();
+        dom.$resetCssModeForTests();
     }
 };
 
diff --git a/src/lib/dom.js b/src/lib/dom.js
index 1f193b7bf8d..350cbfbeab5 100644
--- a/src/lib/dom.js
+++ b/src/lib/dom.js
@@ -276,6 +276,7 @@ function importCssString(cssText, id, target) {
             var stylesheet = styles[id];
             if (!stylesheet) {
                 stylesheet = styles[id] = new CSSStyleSheet();
+                stylesheet.$id = id;
                 stylesheet.replaceSync(cssText);
             }
             container.adoptedStyleSheets.push(stylesheet);
@@ -305,6 +306,13 @@ var styles = Object.create(null);
 
 exports.importCssString = importCssString;
 
+exports.$resetCssModeForTests = function(useStyleTag) {
+    USE_STYLE_TAG = useStyleTag == undefined ? null : useStyleTag;
+    styles = Object.create(null);
+    strictCSP = undefined;
+    cssCache = [];
+};
+
 /**
  * @param {string} uri
  * @param {Document} [doc]