script: Implement DocumentOrShadowDOM.adoptedStylesheet with FrozenArray (#38163)

Spec:
https://drafts.csswg.org/cssom/#dom-documentorshadowroot-adoptedstylesheets

Implement `DocumentOrShadowDOM.adoptedStylesheet`. Due to
`ObservableArray` being a massive issue on its own, it will be as it was
a `FrozenArray` at first. This approach is similar to how Gecko
implement adopted stylesheet. See
https://phabricator.services.mozilla.com/D144547#change-IXyOzxxFn8sU.

All of the changes will be gated behind a preference
`dom_adoptedstylesheet_enabled`.

Adopted stylesheet is implemented by adding the setter and getter of it.
While the getter works like a normal attribute getter, the setter need
to consider the inner working of document and shadow root StylesheetSet,
specifically the ordering and the invalidations. Particularly for
setter, we will clear all of the adopted stylesheet within the
StylesheetSet and readd them. Possible optimization exist, but the focus
should be directed to implementing `ObservableArray`.

More context about the implementations
https://hackmd.io/vtJAn4UyS_O0Idvk5dCO_w.

Testing: Existing WPT Coverage
Fixes: https://github.com/servo/servo/issues/37561

---------

Signed-off-by: Jo Steven Novaryo <jo.steven.novaryo@huawei.com>
This commit is contained in:
Jo Steven Novaryo 2025-07-23 16:16:01 +08:00 committed by GitHub
parent d2e5137201
commit f523445fc3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 434 additions and 139 deletions

View file

@ -160,8 +160,7 @@ impl HTMLStyleElement {
}
*self.stylesheet.borrow_mut() = Some(s.clone());
self.clean_stylesheet_ownership();
stylesheets_owner
.add_stylesheet(StylesheetSource::Element(Dom::from_ref(self.upcast())), s);
stylesheets_owner.add_owned_stylesheet(self.upcast(), s);
}
pub(crate) fn get_stylesheet(&self) -> Option<Arc<Stylesheet>> {
@ -178,7 +177,7 @@ impl HTMLStyleElement {
None, // todo handle location
None, // todo handle title
sheet,
false, // is_constructed
None, // constructor_document
CanGc::note(),
)
})
@ -187,7 +186,7 @@ impl HTMLStyleElement {
fn clean_stylesheet_ownership(&self) {
if let Some(cssom_stylesheet) = self.cssom_stylesheet.get() {
cssom_stylesheet.set_owner(None);
cssom_stylesheet.set_owner_node(None);
}
self.cssom_stylesheet.set(None);
}