mirror of
https://github.com/servo/servo.git
synced 2025-09-30 00:29:14 +01:00
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:
parent
f3d5617349
commit
1898a740a8
9 changed files with 164 additions and 132 deletions
|
@ -2,26 +2,33 @@
|
|||
* 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::fmt;
|
||||
use std::iter::Enumerate;
|
||||
use std::rc::Rc;
|
||||
use std::vec::IntoIter;
|
||||
|
||||
use script_bindings::error::Fallible;
|
||||
use script_bindings::script_runtime::CanGc;
|
||||
use script_bindings::str::DOMString;
|
||||
|
||||
use super::Node;
|
||||
use crate::dom::bindings::callback::ExceptionHandling;
|
||||
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
|
||||
use crate::dom::bindings::root::DomRoot;
|
||||
|
||||
/// The context during evaluation of an XPath expression.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct EvaluationCtx {
|
||||
/// Where we started at
|
||||
/// Where we started at.
|
||||
pub(crate) starting_node: DomRoot<Node>,
|
||||
/// The "current" node in the evaluation
|
||||
/// The "current" node in the evaluation.
|
||||
pub(crate) context_node: DomRoot<Node>,
|
||||
/// Details needed for evaluating a predicate list
|
||||
/// Details needed for evaluating a predicate list.
|
||||
pub(crate) predicate_ctx: Option<PredicateCtx>,
|
||||
/// The nodes we're currently matching against
|
||||
/// The nodes we're currently matching against.
|
||||
pub(crate) predicate_nodes: Option<Vec<DomRoot<Node>>>,
|
||||
/// A list of known namespace prefixes.
|
||||
pub(crate) resolver: Option<Rc<XPathNSResolver>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
@ -32,12 +39,13 @@ pub(crate) struct PredicateCtx {
|
|||
|
||||
impl EvaluationCtx {
|
||||
/// Prepares the context used while evaluating the XPath expression
|
||||
pub(crate) fn new(context_node: &Node) -> EvaluationCtx {
|
||||
pub(crate) fn new(context_node: &Node, resolver: Option<Rc<XPathNSResolver>>) -> EvaluationCtx {
|
||||
EvaluationCtx {
|
||||
starting_node: DomRoot::from_ref(context_node),
|
||||
context_node: DomRoot::from_ref(context_node),
|
||||
predicate_ctx: None,
|
||||
predicate_nodes: None,
|
||||
resolver,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,6 +56,7 @@ impl EvaluationCtx {
|
|||
context_node: DomRoot::from_ref(node),
|
||||
predicate_ctx: self.predicate_ctx,
|
||||
predicate_nodes: self.predicate_nodes.clone(),
|
||||
resolver: self.resolver.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,6 +66,7 @@ impl EvaluationCtx {
|
|||
context_node: self.context_node.clone(),
|
||||
predicate_ctx: None,
|
||||
predicate_nodes: Some(nodes.into_iter().map(DomRoot::from_ref).collect()),
|
||||
resolver: self.resolver.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,9 +84,26 @@ impl EvaluationCtx {
|
|||
}
|
||||
|
||||
/// Resolve a namespace prefix using the context node's document
|
||||
pub(crate) fn resolve_namespace(&self, prefix: Option<&str>) -> Option<DOMString> {
|
||||
self.context_node
|
||||
.LookupNamespaceURI(prefix.map(DOMString::from))
|
||||
pub(crate) fn resolve_namespace(
|
||||
&self,
|
||||
prefix: Option<&str>,
|
||||
can_gc: CanGc,
|
||||
) -> Fallible<Option<DOMString>> {
|
||||
// First check if the prefix is known by our resolver function
|
||||
if let Some(resolver) = self.resolver.as_ref() {
|
||||
if let Some(namespace_uri) = resolver.LookupNamespaceURI__(
|
||||
prefix.map(DOMString::from),
|
||||
ExceptionHandling::Rethrow,
|
||||
can_gc,
|
||||
)? {
|
||||
return Ok(Some(namespace_uri));
|
||||
}
|
||||
}
|
||||
|
||||
// Then, see if it's defined on the context node
|
||||
Ok(self
|
||||
.context_node
|
||||
.LookupNamespaceURI(prefix.map(DOMString::from)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,6 +127,19 @@ impl Iterator for EvalNodesetIter<'_> {
|
|||
index: idx + 1,
|
||||
size: self.size,
|
||||
}),
|
||||
resolver: self.ctx.resolver.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for EvaluationCtx {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("EvaluationCtx")
|
||||
.field("starting_node", &self.starting_node)
|
||||
.field("context_node", &self.context_node)
|
||||
.field("predicate_ctx", &self.predicate_ctx)
|
||||
.field("predicate_nodes", &self.predicate_nodes)
|
||||
.field("resolver", &"<callback function>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue