script: Allow reusing results from xpath queries (#39392)

This behaviour is optional, but observable. Other browsers implement it,
so we should do it too.

Testing: There are no WPT tests for this, which is fair since the spec
explicitly states implementors may choose to not reuse the result.
Fixes: Part of https://github.com/servo/servo/issues/34527

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
Simon Wülker 2025-09-19 19:19:04 +02:00 committed by GitHub
parent 2c3d580ef1
commit 84577c9fd4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 85 additions and 24 deletions

View file

@ -53,7 +53,7 @@ impl XPathExpression {
&self,
context_node: &Node,
result_type_num: u16,
_result: Option<&XPathResult>,
result: Option<&XPathResult>,
resolver: Option<Rc<XPathNSResolver>>,
can_gc: CanGc,
) -> Fallible<DomRoot<XPathResult>> {
@ -63,16 +63,23 @@ impl XPathExpression {
let global = self.global();
let window = global.as_window();
let result_value = evaluate_parsed_xpath(&self.parsed_expression, context_node, resolver)?;
let result_value =
evaluate_parsed_xpath(&self.parsed_expression, context_node, resolver)?.into();
// TODO(vlindhol): support putting results into mutable `_result` as per the spec
Ok(XPathResult::new(
window,
None,
can_gc,
result_type,
result_value.into(),
))
if let Some(result) = result {
// According to https://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathEvaluator-evaluate, reusing
// the provided result object is optional. We choose to do it here because thats what other browsers do.
result.reinitialize_with(result_type, result_value);
Ok(DomRoot::from_ref(result))
} else {
Ok(XPathResult::new(
window,
None,
can_gc,
result_type,
result_value,
))
}
}
}