css: Refactor StyleSheetInDocument owner (#38136)

Refactor `documentotshadowroot::StyleSheetInDocument`, renaming it into
`ServoStylesheetInDocument` to avoid confusion with Stylo's
`StylesheetInDocument` trait.

To support constructed stylesheet. The `ServoStylesheetInDocument.owner`
would contains enum of:
- `Dom<Element>` - for stylesheet parsed from an element.
- `Dom<CSSStylesheet>` - for constructed stylesheet.

Testing: No WPT regression.
Fixes: #38133

---------

Signed-off-by: Jo Steven Novaryo <jo.steven.novaryo@huawei.com>
This commit is contained in:
Jo Steven Novaryo 2025-07-18 13:39:09 +08:00 committed by GitHub
parent d671f58078
commit bbed6cddcd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 112 additions and 57 deletions

View file

@ -32,7 +32,9 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::cssstylesheet::CSSStyleSheet;
use crate::dom::document::Document;
use crate::dom::documentfragment::DocumentFragment;
use crate::dom::documentorshadowroot::{DocumentOrShadowRoot, StyleSheetInDocument};
use crate::dom::documentorshadowroot::{
DocumentOrShadowRoot, ServoStylesheetInDocument, StylesheetSource,
};
use crate::dom::element::Element;
use crate::dom::htmlslotelement::HTMLSlotElement;
use crate::dom::node::{
@ -62,7 +64,7 @@ pub(crate) struct ShadowRoot {
host: MutNullableDom<Element>,
/// List of author styles associated with nodes in this shadow tree.
#[custom_trace]
author_styles: DomRefCell<AuthorStyles<StyleSheetInDocument>>,
author_styles: DomRefCell<AuthorStyles<ServoStylesheetInDocument>>,
stylesheet_list: MutNullableDom<StyleSheetList>,
window: Dom<Window>,
@ -175,22 +177,29 @@ impl ShadowRoot {
stylesheets
.get(index)
.and_then(|s| s.owner.upcast::<Node>().get_cssom_stylesheet())
.and_then(|s| s.owner.get_cssom_object())
}
/// Add a stylesheet owned by `owner` to the list of shadow root sheets, in the
/// correct tree position.
#[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
pub(crate) fn add_stylesheet(&self, owner: &Element, sheet: Arc<Stylesheet>) {
pub(crate) fn add_stylesheet(&self, owner: StylesheetSource, sheet: Arc<Stylesheet>) {
let stylesheets = &mut self.author_styles.borrow_mut().stylesheets;
let insertion_point = stylesheets
.iter()
.find(|sheet_in_shadow| {
owner
.upcast::<Node>()
.is_before(sheet_in_shadow.owner.upcast())
})
.cloned();
// TODO(stevennovayo): support constructed stylesheet for adopted stylesheet and its ordering
let insertion_point = match &owner {
StylesheetSource::Element(owner_elem) => stylesheets
.iter()
.find(|sheet_in_shadow| match sheet_in_shadow.owner {
StylesheetSource::Element(ref other_elem) => {
owner_elem.upcast::<Node>().is_before(other_elem.upcast())
},
StylesheetSource::Constructed(_) => unreachable!(),
})
.cloned(),
StylesheetSource::Constructed(_) => unreachable!(),
};
DocumentOrShadowRoot::add_stylesheet(
owner,
StylesheetSetRef::Author(stylesheets),
@ -202,7 +211,7 @@ impl ShadowRoot {
/// Remove a stylesheet owned by `owner` from the list of shadow root sheets.
#[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
pub(crate) fn remove_stylesheet(&self, owner: &Element, s: &Arc<Stylesheet>) {
pub(crate) fn remove_stylesheet(&self, owner: StylesheetSource, s: &Arc<Stylesheet>) {
DocumentOrShadowRoot::remove_stylesheet(
owner,
s,