Implement CSSStyleSheet::replaceSync (#36586)

Implements the `replaceSync` method on CSSStyleSheet

Testing: Covered by wpt tests. Expectations are updated.

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-04-23 08:29:01 -07:00 committed by GitHub
parent 5d3cbc67ee
commit 30fdf48ca6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 47 additions and 162 deletions

View file

@ -24,7 +24,7 @@ use crate::dom::bindings::reflector::{
DomGlobal, reflect_dom_object, reflect_dom_object_with_proto,
};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::cssrulelist::{CSSRuleList, RulesSource};
use crate::dom::element::Element;
use crate::dom::medialist::MediaList;
@ -290,4 +290,32 @@ impl CSSStyleSheetMethods<crate::DomTypeHolder> for CSSStyleSheet {
// > 8. Return -1.
Ok(-1)
}
/// <https://drafts.csswg.org/cssom/#synchronously-replace-the-rules-of-a-cssstylesheet>
fn ReplaceSync(&self, text: USVString) -> Result<(), Error> {
// Step 1. If the constructed flag is not set throw a NotAllowedError
if !self.is_constructed {
return Err(Error::NotAllowed);
}
// Step 2. Let rules be the result of running parse a stylesheets contents from text.
let global = self.global();
let window = global.as_window();
StyleStyleSheet::update_from_str(
&self.style_stylesheet,
&text,
UrlExtraData(window.get_url().get_arc()),
None,
window.css_error_reporter(),
AllowImportRules::No, // Step 3.If rules contains one or more @import rules, remove those rules from rules.
);
// Step 4. Set sheets CSS rules to rules.
// We reset our rule list, which will be initialized properly
// at the next getter access.
self.rulelist.set(None);
Ok(())
}
}