1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

LibWeb: Implement CSSStyleSheet.replace()

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.
This commit is contained in:
Tim Ledbetter 2024-02-24 07:46:59 +00:00 committed by Andreas Kling
parent d209d5a84f
commit 81c67d34eb
5 changed files with 118 additions and 3 deletions

View file

@ -0,0 +1,50 @@
<!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>