script: Use xpath ns resolver to resolve namespace prefixes (#39321)

The xpath resolver is a function provided by the user to resolve
namespace prefixes. Previously, we were ignoring the argument.

Testing: New web platform tests start to pass
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-16 19:25:45 +02:00 committed by GitHub
parent f3d5617349
commit 1898a740a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 164 additions and 132 deletions

View file

@ -1812,16 +1812,16 @@ impl Element {
Ref::map(self.attrs.borrow(), |attrs| &**attrs)
}
// Element branch of https://dom.spec.whatwg.org/#locate-a-namespace
/// Element branch of <https://dom.spec.whatwg.org/#locate-a-namespace>
pub(crate) fn locate_namespace(&self, prefix: Option<DOMString>) -> Namespace {
let namespace_prefix = prefix.clone().map(|s| Prefix::from(&*s));
// "1. If prefix is "xml", then return the XML namespace."
// Step 1. If prefix is "xml", then return the XML namespace.
if namespace_prefix == Some(namespace_prefix!("xml")) {
return ns!(xml);
}
// "2. If prefix is "xmlns", then return the XMLNS namespace."
// Step 2. If prefix is "xmlns", then return the XMLNS namespace.
if namespace_prefix == Some(namespace_prefix!("xmlns")) {
return ns!(xmlns);
}
@ -1833,21 +1833,20 @@ impl Element {
.inclusive_ancestors(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<Self>);
// "5. If its parent element is null, then return null."
// "6. Return the result of running locate a namespace on its parent element using prefix."
// Step 5. If its parent element is null, then return null.
// Step 6. Return the result of running locate a namespace on its parent element using prefix.
for element in inclusive_ancestor_elements {
// "3. If its namespace is non-null and its namespace prefix is prefix, then return
// namespace."
// Step 3. If its namespace is non-null and its namespace prefix is prefix, then return namespace.
if element.namespace() != &ns!() &&
element.prefix().as_ref().map(|p| &**p) == prefix.as_deref()
{
return element.namespace().clone();
}
// "4. If it has an attribute whose namespace is the XMLNS namespace, namespace prefix
// Step 4. If it has an attribute whose namespace is the XMLNS namespace, namespace prefix
// is "xmlns", and local name is prefix, or if prefix is null and it has an attribute
// whose namespace is the XMLNS namespace, namespace prefix is null, and local name is
// "xmlns", then return its value if it is not the empty string, and null otherwise."
// "xmlns", then return its value if it is not the empty string, and null otherwise.
let attr = Ref::filter_map(self.attrs(), |attrs| {
attrs.iter().find(|attr| {
if attr.namespace() != &ns!(xmlns) {
@ -3090,18 +3089,18 @@ impl Element {
#[allow(non_snake_case)]
impl ElementMethods<crate::DomTypeHolder> for Element {
// https://dom.spec.whatwg.org/#dom-element-namespaceuri
/// <https://dom.spec.whatwg.org/#dom-element-namespaceuri>
fn GetNamespaceURI(&self) -> Option<DOMString> {
Node::namespace_to_string(self.namespace.clone())
}
// https://dom.spec.whatwg.org/#dom-element-localname
/// <https://dom.spec.whatwg.org/#dom-element-localname>
fn LocalName(&self) -> DOMString {
// FIXME(ajeffrey): Convert directly from LocalName to DOMString
DOMString::from(&*self.local_name)
}
// https://dom.spec.whatwg.org/#dom-element-prefix
/// <https://dom.spec.whatwg.org/#dom-element-prefix>
fn GetPrefix(&self) -> Option<DOMString> {
self.prefix.borrow().as_ref().map(|p| DOMString::from(&**p))
}

View file

@ -3988,13 +3988,10 @@ impl NodeMethods<crate::DomTypeHolder> for Node {
/// <https://dom.spec.whatwg.org/#dom-node-lookupnamespaceuri>
fn LookupNamespaceURI(&self, prefix: Option<DOMString>) -> Option<DOMString> {
// Step 1.
let prefix = match prefix {
Some(ref p) if p.is_empty() => None,
pre => pre,
};
// Step 1. If prefix is the empty string, then set it to null.
let prefix = prefix.filter(|prefix| !prefix.is_empty());
// Step 2.
// Step 2. Return the result of running locate a namespace for this using prefix.
Node::namespace_to_string(Node::locate_namespace(self, prefix))
}

View file

@ -9,7 +9,6 @@ use js::rust::HandleObject;
use super::bindings::error::Error;
use crate::dom::bindings::codegen::Bindings::XPathEvaluatorBinding::XPathEvaluatorMethods;
use crate::dom::bindings::codegen::Bindings::XPathExpressionBinding::XPathExpression_Binding::XPathExpressionMethods;
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
@ -91,7 +90,7 @@ impl XPathEvaluatorMethods<crate::DomTypeHolder> for XPathEvaluator {
&self,
expression_str: DOMString,
context_node: &Node,
_resolver: Option<Rc<XPathNSResolver>>,
resolver: Option<Rc<XPathNSResolver>>,
result_type: u16,
result: Option<&XPathResult>,
can_gc: CanGc,
@ -101,12 +100,6 @@ impl XPathEvaluatorMethods<crate::DomTypeHolder> for XPathEvaluator {
let parsed_expression =
crate::xpath::parse(&expression_str).map_err(|_| Error::Syntax(None))?;
let expression = XPathExpression::new(window, None, can_gc, parsed_expression);
XPathExpressionMethods::<crate::DomTypeHolder>::Evaluate(
&*expression,
context_node,
result_type,
result,
can_gc,
)
expression.evaluate_internal(context_node, result_type, result, resolver, can_gc)
}
}

View file

@ -2,10 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::rc::Rc;
use dom_struct::dom_struct;
use js::rust::HandleObject;
use crate::dom::bindings::codegen::Bindings::XPathExpressionBinding::XPathExpressionMethods;
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
use crate::dom::bindings::root::{Dom, DomRoot};
@ -45,15 +48,13 @@ impl XPathExpression {
can_gc,
)
}
}
impl XPathExpressionMethods<crate::DomTypeHolder> for XPathExpression {
/// <https://dom.spec.whatwg.org/#dom-xpathexpression-evaluate>
fn Evaluate(
pub(crate) fn evaluate_internal(
&self,
context_node: &Node,
result_type_num: u16,
_result: Option<&XPathResult>,
resolver: Option<Rc<XPathNSResolver>>,
can_gc: CanGc,
) -> Fallible<DomRoot<XPathResult>> {
let result_type = XPathResultType::try_from(result_type_num)
@ -62,8 +63,7 @@ impl XPathExpressionMethods<crate::DomTypeHolder> for XPathExpression {
let global = self.global();
let window = global.as_window();
let result_value = evaluate_parsed_xpath(&self.parsed_expression, context_node)
.map_err(|_e| Error::Operation)?;
let result_value = evaluate_parsed_xpath(&self.parsed_expression, context_node, resolver)?;
// TODO(vlindhol): support putting results into mutable `_result` as per the spec
Ok(XPathResult::new(
@ -75,3 +75,16 @@ impl XPathExpressionMethods<crate::DomTypeHolder> for XPathExpression {
))
}
}
impl XPathExpressionMethods<crate::DomTypeHolder> for XPathExpression {
/// <https://dom.spec.whatwg.org/#dom-xpathexpression-evaluate>
fn Evaluate(
&self,
context_node: &Node,
result_type_num: u16,
result: Option<&XPathResult>,
can_gc: CanGc,
) -> Fallible<DomRoot<XPathResult>> {
self.evaluate_internal(context_node, result_type_num, result, None, can_gc)
}
}