From 3e8792ee2fbc5be003f7c6502b06bbc456314c68 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 27 Sep 2024 19:36:19 +0400 Subject: [PATCH] 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 a4ac3f0748d..69b7b803c2b 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 6a53ef8ae11..c1ffbda13e9 100644
--- a/src/lib/dom.js
+++ b/src/lib/dom.js
@@ -260,15 +260,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;
 
 /**