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

@ -2,13 +2,16 @@
* 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 context::EvaluationCtx;
use eval::Evaluatable;
use eval::{Error as EvaluationError, Evaluatable};
pub(crate) use eval_value::{NodesetHelpers, Value};
use parser::OwnedParserError;
pub(crate) use parser::{Expr, parse as parse_impl};
use super::dom::node::Node;
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
use crate::dom::bindings::error::{Error as JsError, Error, Fallible};
mod context;
#[allow(dead_code)]
@ -19,58 +22,41 @@ mod eval_value;
#[allow(dead_code)]
mod parser;
/// The failure modes of executing an XPath.
#[derive(Debug, PartialEq)]
pub(crate) enum Error {
/// The XPath was syntactically invalid
Parsing { source: OwnedParserError },
/// The XPath could not be executed
Evaluating { source: eval::Error },
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::Parsing { source } => write!(f, "Unable to parse XPath: {}", source),
Error::Evaluating { source } => write!(f, "Unable to evaluate XPath: {}", source),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Parsing { source } => Some(source),
Error::Evaluating { source } => Some(source),
}
}
}
/// Parse an XPath expression from a string
pub(crate) fn parse(xpath: &str) -> Result<Expr, Error> {
pub(crate) fn parse(xpath: &str) -> Fallible<Expr> {
match parse_impl(xpath) {
Ok(expr) => {
debug!("Parsed XPath: {:?}", expr);
debug!("Parsed XPath: {expr:?}");
Ok(expr)
},
Err(e) => {
debug!("Unable to parse XPath: {}", e);
Err(Error::Parsing { source: e })
Err(error) => {
debug!("Unable to parse XPath: {error}");
Err(Error::Operation)
},
}
}
/// Evaluate an already-parsed XPath expression
pub(crate) fn evaluate_parsed_xpath(expr: &Expr, context_node: &Node) -> Result<Value, Error> {
let context = EvaluationCtx::new(context_node);
pub(crate) fn evaluate_parsed_xpath(
expr: &Expr,
context_node: &Node,
resolver: Option<Rc<XPathNSResolver>>,
) -> Fallible<Value> {
let context = EvaluationCtx::new(context_node, resolver);
match expr.evaluate(&context) {
Ok(v) => {
debug!("Evaluated XPath: {:?}", v);
Ok(v)
Ok(value) => {
debug!("Evaluated XPath: {value:?}");
Ok(value)
},
Err(e) => {
debug!("Unable to evaluate XPath: {}", e);
Err(Error::Evaluating { source: e })
Err(error) => {
debug!("Unable to evaluate XPath: {error}");
let error = match error {
EvaluationError::JsException(exception) => exception,
_ => JsError::Operation,
};
Err(error)
},
}
}