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:
Simon Wülker 2025-06-24 14:45:50 +02:00 committed by GitHub
parent 9487f66eaa
commit 2dc62c504f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 151 additions and 76 deletions

View file

@ -37,7 +37,9 @@ use crate::dom::htmlscriptelement::HTMLScriptElement;
use crate::dom::htmltemplateelement::HTMLTemplateElement;
use crate::dom::node::Node;
use crate::dom::processinginstruction::ProcessingInstruction;
use crate::dom::servoparser::{ElementAttribute, ParsingAlgorithm, create_element_for_token};
use crate::dom::servoparser::{
ElementAttribute, ParsingAlgorithm, attach_declarative_shadow_inner, create_element_for_token,
};
use crate::dom::virtualmethods::vtable_for;
use crate::script_runtime::CanGc;
@ -139,6 +141,15 @@ enum ParseOperation {
#[no_trace]
mode: ServoQuirksMode,
},
AttachDeclarativeShadowRoot {
location: ParseNodeId,
template: ParseNodeId,
attributes: Vec<Attribute>,
/// Used to notify the parser thread whether or not attaching the shadow root succeeded
#[no_trace]
sender: Sender<bool>,
},
}
#[derive(MallocSizeOf)]
@ -562,6 +573,26 @@ impl Tokenizer {
ParseOperation::SetQuirksMode { mode } => {
document.set_quirks_mode(mode);
},
ParseOperation::AttachDeclarativeShadowRoot {
location,
template,
attributes,
sender,
} => {
let location = self.get_node(&location);
let template = self.get_node(&template);
let attributes = attributes
.into_iter()
.map(|attribute| HtmlAttribute {
name: attribute.name,
value: StrTendril::from(attribute.value),
})
.collect();
let did_succeed =
attach_declarative_shadow_inner(&location, &template, attributes).is_ok();
sender.send(did_succeed).unwrap();
},
}
}
}
@ -920,4 +951,40 @@ impl TreeSink for Sink {
fn pop(&self, node: &Self::Handle) {
self.send_op(ParseOperation::Pop { node: node.id });
}
/// Attach declarative shadow
fn attach_declarative_shadow(
&self,
location: &Self::Handle,
template: &Self::Handle,
attributes: Vec<HtmlAttribute>,
) -> Result<(), String> {
let attributes = attributes
.into_iter()
.map(|attribute| Attribute {
name: attribute.name,
value: String::from(attribute.value),
})
.collect();
// Unfortunately the parser can only proceed after it knows whether attaching the shadow root
// succeeded or failed. Attaching a shadow root can fail for many different reasons,
// and so we need to block until the script thread has processed this operation.
let (sender, receiver) = unbounded();
self.send_op(ParseOperation::AttachDeclarativeShadowRoot {
location: location.id,
template: template.id,
attributes,
sender,
});
let did_succeed = receiver.recv().unwrap();
// TODO: This api is silly, we shouldn't have to return a string here
if did_succeed {
Ok(())
} else {
Err("Attaching declarative shadow root failed".to_owned())
}
}
}