mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 18:48:13 +00:00

This method asynchronously replaces the content of the given stylesheet with the content passed to it. An exception is thrown if this method is used by a stylesheet not created with the `CSSStyleSheet()` constructor.
50 lines
1.4 KiB
HTML
50 lines
1.4 KiB
HTML
<!DOCTYPE html>
|
|
<style>
|
|
.test {
|
|
font-size: 12px;
|
|
}
|
|
</style>
|
|
<script src="../include.js"></script>
|
|
<script>
|
|
asyncTest(async done => {
|
|
const newStyle = `
|
|
.test {
|
|
font-size: 14px;
|
|
}
|
|
.test2 {
|
|
font-size: 16px;
|
|
}`;
|
|
const newStyle2 = `.test {
|
|
padding: 100px;
|
|
}`;
|
|
|
|
const nonConstructedSheet = document.styleSheets[0];
|
|
try {
|
|
await nonConstructedSheet.replace(newStyle);
|
|
println("FAIL");
|
|
} catch (e) {
|
|
println(`Exception thrown when calling replace() on non-constructed stylesheet: ${e.name}`);
|
|
}
|
|
|
|
const sheet = new CSSStyleSheet();
|
|
const cssRules = sheet.cssRules;
|
|
|
|
await sheet.replace(newStyle);
|
|
println(`Number of CSS rules after replace(): ${sheet.cssRules.length}`);
|
|
for (const rule of sheet.cssRules) {
|
|
println(`Rule: ${rule.cssText}`);
|
|
}
|
|
|
|
const cssRulesAfterReplace = sheet.cssRules;
|
|
println(`cssRules returns the same object before and after replace(): ${cssRules === cssRulesAfterReplace}`);
|
|
|
|
const importRule = `@import url("test.css");`;
|
|
await sheet.replace(`${newStyle2} ${importRule}`);
|
|
|
|
println(`@import rule should be not appear below:`);
|
|
for (const rule of sheet.cssRules) {
|
|
println(`Rule: ${rule.cssText}`);
|
|
}
|
|
done();
|
|
});
|
|
</script>
|