mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Implement declarative shadow root support for async HTML parser (#37443)
Implements the [`TreeSink::attach_declarative_shadow`](https://docs.rs/html5ever/latest/html5ever/interface/trait.TreeSink.html#method.attach_declarative_shadow) method for the async html parser. Try run with the async html parser: https://github.com/simonwuelker/servo/actions/runs/15634240606/job/44046871826. (There are far fewer failures than in the initial try run from https://github.com/servo/servo/issues/37418) Testing: We don't run tests with the async html parser Part of https://github.com/servo/servo/issues/37418 Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
9487f66eaa
commit
2dc62c504f
2 changed files with 151 additions and 76 deletions
|
@ -1441,81 +1441,7 @@ impl TreeSink for Sink {
|
|||
template: &Dom<Node>,
|
||||
attrs: Vec<Attribute>,
|
||||
) -> Result<(), String> {
|
||||
let host_element = host.downcast::<Element>().unwrap();
|
||||
|
||||
if host_element.shadow_root().is_some() {
|
||||
return Err(String::from("Already in a shadow host"));
|
||||
}
|
||||
|
||||
let template_element = template.downcast::<HTMLTemplateElement>().unwrap();
|
||||
|
||||
// Step 3. Let mode be template start tag's shadowrootmode attribute's value.
|
||||
// Step 4. Let clonable be true if template start tag has a shadowrootclonable attribute; otherwise false.
|
||||
// Step 5. Let delegatesfocus be true if template start tag
|
||||
// has a shadowrootdelegatesfocus attribute; otherwise false.
|
||||
// Step 6. Let serializable be true if template start tag
|
||||
// has a shadowrootserializable attribute; otherwise false.
|
||||
let mut shadow_root_mode = ShadowRootMode::Open;
|
||||
let mut clonable = false;
|
||||
let mut delegatesfocus = false;
|
||||
let mut serializable = false;
|
||||
|
||||
let attrs: Vec<ElementAttribute> = attrs
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|attr| ElementAttribute::new(attr.name, DOMString::from(String::from(attr.value))))
|
||||
.collect();
|
||||
|
||||
attrs
|
||||
.iter()
|
||||
.for_each(|attr: &ElementAttribute| match attr.name.local {
|
||||
local_name!("shadowrootmode") => {
|
||||
if attr.value.str().eq_ignore_ascii_case("open") {
|
||||
shadow_root_mode = ShadowRootMode::Open;
|
||||
} else if attr.value.str().eq_ignore_ascii_case("closed") {
|
||||
shadow_root_mode = ShadowRootMode::Closed;
|
||||
} else {
|
||||
unreachable!("shadowrootmode value is not open nor closed");
|
||||
}
|
||||
},
|
||||
local_name!("shadowrootclonable") => {
|
||||
clonable = true;
|
||||
},
|
||||
local_name!("shadowrootdelegatesfocus") => {
|
||||
delegatesfocus = true;
|
||||
},
|
||||
local_name!("shadowrootserializable") => {
|
||||
serializable = true;
|
||||
},
|
||||
_ => {},
|
||||
});
|
||||
|
||||
// Step 8.1. Attach a shadow root with declarative shadow host element,
|
||||
// mode, clonable, serializable, delegatesFocus, and "named".
|
||||
match host_element.attach_shadow(
|
||||
IsUserAgentWidget::No,
|
||||
shadow_root_mode,
|
||||
clonable,
|
||||
serializable,
|
||||
delegatesfocus,
|
||||
SlotAssignmentMode::Named,
|
||||
CanGc::note(),
|
||||
) {
|
||||
Ok(shadow_root) => {
|
||||
// Step 8.3. Set shadow's declarative to true.
|
||||
shadow_root.set_declarative(true);
|
||||
|
||||
// Set 8.4. Set template's template contents property to shadow.
|
||||
let shadow = shadow_root.upcast::<DocumentFragment>();
|
||||
template_element.set_contents(Some(shadow));
|
||||
|
||||
// Step 8.5. Set shadow’s available to element internals to true.
|
||||
shadow_root.set_available_to_element_internals(true);
|
||||
|
||||
Ok(())
|
||||
},
|
||||
Err(_) => Err(String::from("Attaching shadow fails")),
|
||||
}
|
||||
attach_declarative_shadow_inner(host, template, attrs)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1652,3 +1578,85 @@ impl TendrilSink<UTF8> for NetworkSink {
|
|||
self.output
|
||||
}
|
||||
}
|
||||
|
||||
fn attach_declarative_shadow_inner(
|
||||
host: &Node,
|
||||
template: &Node,
|
||||
attrs: Vec<Attribute>,
|
||||
) -> Result<(), String> {
|
||||
let host_element = host.downcast::<Element>().unwrap();
|
||||
|
||||
if host_element.shadow_root().is_some() {
|
||||
return Err(String::from("Already in a shadow host"));
|
||||
}
|
||||
|
||||
let template_element = template.downcast::<HTMLTemplateElement>().unwrap();
|
||||
|
||||
// Step 3. Let mode be template start tag's shadowrootmode attribute's value.
|
||||
// Step 4. Let clonable be true if template start tag has a shadowrootclonable attribute; otherwise false.
|
||||
// Step 5. Let delegatesfocus be true if template start tag
|
||||
// has a shadowrootdelegatesfocus attribute; otherwise false.
|
||||
// Step 6. Let serializable be true if template start tag
|
||||
// has a shadowrootserializable attribute; otherwise false.
|
||||
let mut shadow_root_mode = ShadowRootMode::Open;
|
||||
let mut clonable = false;
|
||||
let mut delegatesfocus = false;
|
||||
let mut serializable = false;
|
||||
|
||||
let attrs: Vec<ElementAttribute> = attrs
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(|attr| ElementAttribute::new(attr.name, DOMString::from(String::from(attr.value))))
|
||||
.collect();
|
||||
|
||||
attrs
|
||||
.iter()
|
||||
.for_each(|attr: &ElementAttribute| match attr.name.local {
|
||||
local_name!("shadowrootmode") => {
|
||||
if attr.value.str().eq_ignore_ascii_case("open") {
|
||||
shadow_root_mode = ShadowRootMode::Open;
|
||||
} else if attr.value.str().eq_ignore_ascii_case("closed") {
|
||||
shadow_root_mode = ShadowRootMode::Closed;
|
||||
} else {
|
||||
unreachable!("shadowrootmode value is not open nor closed");
|
||||
}
|
||||
},
|
||||
local_name!("shadowrootclonable") => {
|
||||
clonable = true;
|
||||
},
|
||||
local_name!("shadowrootdelegatesfocus") => {
|
||||
delegatesfocus = true;
|
||||
},
|
||||
local_name!("shadowrootserializable") => {
|
||||
serializable = true;
|
||||
},
|
||||
_ => {},
|
||||
});
|
||||
|
||||
// Step 8.1. Attach a shadow root with declarative shadow host element,
|
||||
// mode, clonable, serializable, delegatesFocus, and "named".
|
||||
match host_element.attach_shadow(
|
||||
IsUserAgentWidget::No,
|
||||
shadow_root_mode,
|
||||
clonable,
|
||||
serializable,
|
||||
delegatesfocus,
|
||||
SlotAssignmentMode::Named,
|
||||
CanGc::note(),
|
||||
) {
|
||||
Ok(shadow_root) => {
|
||||
// Step 8.3. Set shadow's declarative to true.
|
||||
shadow_root.set_declarative(true);
|
||||
|
||||
// Set 8.4. Set template's template contents property to shadow.
|
||||
let shadow = shadow_root.upcast::<DocumentFragment>();
|
||||
template_element.set_contents(Some(shadow));
|
||||
|
||||
// Step 8.5. Set shadow’s available to element internals to true.
|
||||
shadow_root.set_available_to_element_internals(true);
|
||||
|
||||
Ok(())
|
||||
},
|
||||
Err(_) => Err(String::from("Attaching shadow fails")),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue