script: make Node::xml_serialize fallible. (#38532)

Testing: [Try run][1] did not reveal any test failures. There doesn't
seem to be any straightforward failure scenarios that can be triggered
in `xml5ever` that are not IO errors and the xml_serialize method simply
serializes to a String buffer.

[1]:
https://github.com/servo/servo/actions/runs/16824267959/job/47657275606l

Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Mukilan Thiyagarajan 2025-08-08 17:22:59 +05:30 committed by GitHub
parent 8f52e28225
commit 23c0947072
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 8 deletions

View file

@ -3830,7 +3830,7 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
.html_serialize(ChildrenOnly(Some(qname)), false, vec![], can_gc)
} else {
self.upcast::<Node>()
.xml_serialize(XmlChildrenOnly(Some(qname)))
.xml_serialize(XmlChildrenOnly(Some(qname)))?
};
Ok(TrustedHTMLOrNullIsEmptyString::NullIsEmptyString(result))
@ -3888,7 +3888,7 @@ impl ElementMethods<crate::DomTypeHolder> for Element {
self.upcast::<Node>()
.html_serialize(IncludeNode, false, vec![], can_gc)
} else {
self.upcast::<Node>().xml_serialize(XmlIncludeNode)
self.upcast::<Node>().xml_serialize(XmlIncludeNode)?
};
Ok(TrustedHTMLOrNullIsEmptyString::NullIsEmptyString(result))

View file

@ -3150,17 +3150,25 @@ impl Node {
pub(crate) fn xml_serialize(
&self,
traversal_scope: xml_serialize::TraversalScope,
) -> DOMString {
) -> Fallible<DOMString> {
let mut writer = vec![];
xml_serialize::serialize(
&mut writer,
&self,
xml_serialize::SerializeOpts { traversal_scope },
)
.expect("Cannot serialize node");
.map_err(|error| {
error!("Cannot serialize node: {error}");
Error::InvalidState
})?;
// FIXME(ajeffrey): Directly convert UTF8 to DOMString
DOMString::from(String::from_utf8(writer).unwrap())
let string = DOMString::from(String::from_utf8(writer).map_err(|error| {
error!("Cannot serialize node: {error}");
Error::InvalidState
})?);
Ok(string)
}
/// <https://html.spec.whatwg.org/multipage/#fragment-serializing-algorithm-steps>
@ -3168,19 +3176,19 @@ impl Node {
&self,
require_well_formed: bool,
can_gc: CanGc,
) -> DOMString {
) -> Fallible<DOMString> {
// Step 1. Let context document be node's node document.
let context_document = self.owner_document();
// Step 2. If context document is an HTML document, return the result of HTML fragment serialization algorithm
// with node, false, and « ».
if context_document.is_html_document() {
return self.html_serialize(
return Ok(self.html_serialize(
html_serialize::TraversalScope::ChildrenOnly(None),
false,
vec![],
can_gc,
);
));
}
// Step 3. Return the XML serialization of node given require well-formed.

View file

@ -464,6 +464,8 @@ impl ShadowRootMethods<crate::DomTypeHolder> for ShadowRoot {
// algorithm steps with this and true.
self.upcast::<Node>()
.fragment_serialization_algorithm(true, can_gc)
.inspect_err(|error| warn!("fragment serialization failed: {error:?}"))
.unwrap_or_default()
}
/// <https://html.spec.whatwg.org/multipage/#dom-shadowroot-innerhtml>