Support ::part selector (#37307)

This is pretty much just wiring up the necessary stylo methods. Note
that the `exportparts` attribute is not yet supported, I'll do that in a
followup change

Testing: Covered by existing web platform tests.
This is the first half of https://github.com/servo/servo/issues/35349

Fixes https://github.com/servo/servo/issues/37325

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-06-09 12:17:28 +02:00 committed by GitHub
parent 73ee36be00
commit 0fa3de3937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 119 additions and 348 deletions

View file

@ -861,8 +861,14 @@ pub(crate) fn get_attr_for_layout<'dom>(
pub(crate) trait LayoutElementHelpers<'dom> {
fn attrs(self) -> &'dom [LayoutDom<'dom, Attr>];
fn has_class_for_layout(self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool;
fn has_class_or_part_for_layout(
self,
name: &AtomIdent,
attr_name: &LocalName,
case_sensitivity: CaseSensitivity,
) -> bool;
fn get_classes_for_layout(self) -> Option<&'dom [Atom]>;
fn get_parts_for_layout(self) -> Option<&'dom [Atom]>;
fn synthesize_presentational_hints_for_legacy_attributes<V>(self, hints: &mut V)
where
@ -905,8 +911,13 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
}
#[inline]
fn has_class_for_layout(self, name: &AtomIdent, case_sensitivity: CaseSensitivity) -> bool {
get_attr_for_layout(self, &ns!(), &local_name!("class")).is_some_and(|attr| {
fn has_class_or_part_for_layout(
self,
name: &AtomIdent,
attr_name: &LocalName,
case_sensitivity: CaseSensitivity,
) -> bool {
get_attr_for_layout(self, &ns!(), attr_name).is_some_and(|attr| {
attr.to_tokens()
.unwrap()
.iter()
@ -920,6 +931,11 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
.map(|attr| attr.to_tokens().unwrap())
}
fn get_parts_for_layout(self) -> Option<&'dom [Atom]> {
get_attr_for_layout(self, &ns!(), &local_name!("part"))
.map(|attr| attr.to_tokens().unwrap())
}
fn synthesize_presentational_hints_for_legacy_attributes<V>(self, hints: &mut V)
where
V: Push<ApplicableDeclarationBlock>,
@ -1995,6 +2011,16 @@ impl Element {
})
}
pub(crate) fn is_part(&self, name: &Atom, case_sensitivity: CaseSensitivity) -> bool {
self.get_attribute(&ns!(), &LocalName::from("part"))
.is_some_and(|attr| {
attr.value()
.as_tokens()
.iter()
.any(|atom| case_sensitivity.eq_atom(name, atom))
})
}
pub(crate) fn set_atomic_attribute(
&self,
local_name: &LocalName,
@ -4051,6 +4077,13 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
rooted!(in(*cx) let slottable = Slottable(Dom::from_ref(self.upcast::<Node>())));
slottable.find_a_slot(true)
}
/// <https://drafts.csswg.org/css-shadow-parts/#dom-element-part>
fn Part(&self) -> DomRoot<DOMTokenList> {
self.ensure_rare_data()
.part
.or_init(|| DOMTokenList::new(self, &local_name!("part"), None, CanGc::note()))
}
}
impl VirtualMethods for Element {
@ -4190,7 +4223,9 @@ impl VirtualMethods for Element {
match *name {
local_name!("id") => AttrValue::from_atomic(value.into()),
local_name!("name") => AttrValue::from_atomic(value.into()),
local_name!("class") => AttrValue::from_serialized_tokenlist(value.into()),
local_name!("class") | local_name!("part") => {
AttrValue::from_serialized_tokenlist(value.into())
},
_ => self
.super_type()
.unwrap()
@ -4564,8 +4599,8 @@ impl SelectorsElement for SelectorWrapper<'_> {
.is_some_and(|atom| case_sensitivity.eq_atom(id, atom))
}
fn is_part(&self, _name: &AtomIdent) -> bool {
false
fn is_part(&self, name: &AtomIdent) -> bool {
Element::is_part(self, name, CaseSensitivity::CaseSensitive)
}
fn imported_part(&self, _: &AtomIdent) -> Option<AtomIdent> {