From bbed6cddcd3971be9cb74ebc927e6ae57135ea28 Mon Sep 17 00:00:00 2001 From: Jo Steven Novaryo <65610990+stevennovaryo@users.noreply.github.com> Date: Fri, 18 Jul 2025 13:39:09 +0800 Subject: [PATCH] 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` - for stylesheet parsed from an element. - `Dom` - for constructed stylesheet. Testing: No WPT regression. Fixes: #38133 --------- Signed-off-by: Jo Steven Novaryo --- components/script/dom/document.rs | 36 ++++++---- components/script/dom/documentorshadowroot.rs | 70 +++++++++++++------ components/script/dom/htmllinkelement.rs | 10 ++- components/script/dom/htmlstyleelement.rs | 10 ++- components/script/dom/shadowroot.rs | 35 ++++++---- components/script/dom/stylesheetlist.rs | 8 ++- 6 files changed, 112 insertions(+), 57 deletions(-) diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 5b572df39ad..89f323983eb 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -134,7 +134,9 @@ use crate::dom::customelementregistry::CustomElementDefinition; use crate::dom::customevent::CustomEvent; use crate::dom::datatransfer::DataTransfer; use crate::dom::documentfragment::DocumentFragment; -use crate::dom::documentorshadowroot::{DocumentOrShadowRoot, StyleSheetInDocument}; +use crate::dom::documentorshadowroot::{ + DocumentOrShadowRoot, ServoStylesheetInDocument, StylesheetSource, +}; use crate::dom::documenttype::DocumentType; use crate::dom::domimplementation::DOMImplementation; use crate::dom::element::{ @@ -332,7 +334,7 @@ pub(crate) struct Document { style_shared_lock: StyleSharedRwLock, /// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed. #[custom_trace] - stylesheets: DomRefCell>, + stylesheets: DomRefCell>, stylesheet_list: MutNullableDom, ready_state: Cell, /// Whether the DOMContentLoaded event has already been dispatched. @@ -4889,23 +4891,29 @@ impl Document { stylesheets .get(Origin::Author, index) - .and_then(|s| s.owner.upcast::().get_cssom_stylesheet()) + .and_then(|s| s.owner.get_cssom_object()) } /// Add a stylesheet owned by `owner` to the list of document 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) { + pub(crate) fn add_stylesheet(&self, owner: StylesheetSource, sheet: Arc) { let stylesheets = &mut *self.stylesheets.borrow_mut(); - let insertion_point = stylesheets - .iter() - .map(|(sheet, _origin)| sheet) - .find(|sheet_in_doc| { - owner - .upcast::() - .is_before(sheet_in_doc.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() + .map(|(sheet, _origin)| sheet) + .find(|sheet_in_doc| match sheet_in_doc.owner { + StylesheetSource::Element(ref other_elem) => { + owner_elem.upcast::().is_before(other_elem.upcast()) + }, + StylesheetSource::Constructed(_) => unreachable!(), + }) + .cloned(), + StylesheetSource::Constructed(_) => unreachable!(), + }; if self.has_browsing_context() { self.window.layout_mut().add_stylesheet( @@ -4932,7 +4940,7 @@ impl Document { /// Remove a stylesheet owned by `owner` from the list of document sheets. #[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily. - pub(crate) fn remove_stylesheet(&self, owner: &Element, stylesheet: &Arc) { + pub(crate) fn remove_stylesheet(&self, owner: StylesheetSource, stylesheet: &Arc) { if self.has_browsing_context() { self.window .layout_mut() diff --git a/components/script/dom/documentorshadowroot.rs b/components/script/dom/documentorshadowroot.rs index d46cfacd76d..0173be600c4 100644 --- a/components/script/dom/documentorshadowroot.rs +++ b/components/script/dom/documentorshadowroot.rs @@ -26,46 +26,77 @@ use crate::dom::element::Element; use crate::dom::htmlelement::HTMLElement; use crate::dom::node::{self, Node, VecPreOrderInsertionHelper}; use crate::dom::shadowroot::ShadowRoot; +use crate::dom::types::CSSStyleSheet; use crate::dom::window::Window; use crate::script_runtime::CanGc; use crate::stylesheet_set::StylesheetSetRef; +/// Stylesheet could be constructed by a CSSOM object CSSStylesheet or parsed +/// from HTML element such as `