Skip to content

use new css api when available #5646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
clearTimeout: true,
setInterval: true,
clearInterval: true,
CSSStyleSheet: true,
Blob: true,
cvox: true,
alert: true,
prompt: true,
XMLHttpRequest: true,
Expand Down
34 changes: 34 additions & 0 deletions demo/csp-simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Content-Security-Policy" content="
style-src 'self' ;
img-src 'self' data: ;
script-src 'self' 'nonce-bootstrap-script' https://mkslanc.github.io;
worker-src 'self' blob:
">
<title>Editor</title>
</head>
<body>
<pre id="editor"></pre>

<script src="../build/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>

<script nonce="bootstrap-script">

var editor = ace.edit("editor", {
theme: "ace/theme/tomorrow_night_eighties",
mode: "ace/mode/html",
maxLines: 30,
wrap: true,
autoScrollEditorIntoView: true
});

</script>

<script src="./show_own_source.js"></script>

</body>
</html>
2 changes: 1 addition & 1 deletion demo/csp.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="Content-Security-Policy" content="
style-src 'self' ;
img-src 'self' ;
script-src 'self' 'nonce-bootstrap-script';
script-src 'self' 'nonce-bootstrap-script' https://mkslanc.github.io;
worker-src 'self' blob:
">
<title>Editor</title>
Expand Down
24 changes: 23 additions & 1 deletion src/ace_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
};

Expand Down
33 changes: 32 additions & 1 deletion src/lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,48 @@ 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.$id = id;
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;

exports.$resetCssModeForTests = function(useStyleTag) {
USE_STYLE_TAG = useStyleTag == undefined ? null : useStyleTag;
styles = Object.create(null);
strictCSP = undefined;
cssCache = [];
};

/**
* @param {string} uri
* @param {Document} [doc]
Expand Down