script: Implement deprecated CSSStyleSheet members (#36313)

Implements `rules`, `addRule()` and `removeRule()` for `CSSStyleSheet`.
https://drafts.csswg.org/cssom/#legacy-css-style-sheet-members

This is part of #36162

Testing:
- `/css/css-cascade/at-scope-parsing.html`
- `/css/css-conditional/at-supports-whitespace.html`
- `/css/css-nesting/invalidation-004.html`
- `/css/css-nesting/parsing.html`
- `/css/css-nesting/serialize-group-rules-with-decls.html`
- `/css/css-syntax/custom-property-rule-ambiguity.html`
- `/css/css-syntax/invalid-nested-rules.html`
- `/css/css-syntax/trailing-braces.html`
- `/css/css-syntax/var-with-blocks.html`
- `/css/css-transitions/parsing/starting-style-parsing.html`
- `/css/cssom/CSSStyleSheet.html`
- `/css/cssom/idlharness.html`
- `/css/cssom/insertRule-across-context.html`

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Oriol Brufau 2025-04-03 08:56:47 -07:00 committed by GitHub
parent 9d6e1f67fb
commit f29c182929
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 57 additions and 309 deletions

View file

@ -10,6 +10,7 @@ use style::shared_lock::SharedRwLock;
use style::stylesheets::{CssRuleTypes, Stylesheet as StyleStyleSheet};
use crate::dom::bindings::codegen::Bindings::CSSStyleSheetBinding::CSSStyleSheetMethods;
use crate::dom::bindings::codegen::GenericBindings::CSSRuleListBinding::CSSRuleList_Binding::CSSRuleListMethods;
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
@ -125,7 +126,7 @@ impl CSSStyleSheet {
}
impl CSSStyleSheetMethods<crate::DomTypeHolder> for CSSStyleSheet {
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-cssrules
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-cssrules>
fn GetCssRules(&self) -> Fallible<DomRoot<CSSRuleList>> {
if !self.origin_clean.get() {
return Err(Error::Security);
@ -133,7 +134,7 @@ impl CSSStyleSheetMethods<crate::DomTypeHolder> for CSSStyleSheet {
Ok(self.rulelist())
}
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule>
fn InsertRule(&self, rule: DOMString, index: u32) -> Fallible<u32> {
if !self.origin_clean.get() {
return Err(Error::Security);
@ -142,11 +143,53 @@ impl CSSStyleSheetMethods<crate::DomTypeHolder> for CSSStyleSheet {
.insert_rule(&rule, index, CssRuleTypes::default(), None, CanGc::note())
}
// https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule>
fn DeleteRule(&self, index: u32) -> ErrorResult {
if !self.origin_clean.get() {
return Err(Error::Security);
}
self.rulelist().remove_rule(index)
}
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-rules>
fn GetRules(&self) -> Fallible<DomRoot<CSSRuleList>> {
self.GetCssRules()
}
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-removerule>
fn RemoveRule(&self, index: u32) -> ErrorResult {
self.DeleteRule(index)
}
/// <https://drafts.csswg.org/cssom/#dom-cssstylesheet-addrule>
fn AddRule(
&self,
selector: DOMString,
block: DOMString,
optional_index: Option<u32>,
) -> Fallible<i32> {
// > 1. Let *rule* be an empty string.
// > 2. Append *selector* to *rule*.
let mut rule = selector;
// > 3. Append " { " to *rule*.
// > 4. If *block* is not empty, append *block*, followed by a space, to *rule*.
// > 5. Append "}" to *rule*.
if block.is_empty() {
rule.push_str(" { }");
} else {
rule.push_str(" { ");
rule.push_str(block.str());
rule.push_str(" } ");
};
// > 6. Let *index* be *optionalIndex* if provided, or the number of CSS rules in the stylesheet otherwise.
let index = optional_index.unwrap_or_else(|| self.rulelist().Length());
// > 7. Call `insertRule()`, with *rule* and *index* as arguments.
self.InsertRule(rule, index)?;
// > 8. Return -1.
Ok(-1)
}
}