servo/components/script/xpath/context.rs
Ville Lindholm 475a3dfa38
script: Fix two issues in the XPath parser to pass all xml_xpath_tests.xml tests (#37279)
1. Better handling of namespaces for element and attribute names in XML
mode (read: non-HTML mode)
2. While parsing, pass along context on whether we are in an absolute
(`/`) or descendant (`//`) part of the query, and use it to correctly
enumerate descendants according to where we are in the evaluation of the
AST.

Testing: All 1024 tests in `xml_xpath_tests.xml` (actually
`xml_xpath_runner.html`) pass, as well as some random tests in
`text-html-attributes.html`.
Fixes: #37278

---------

Signed-off-by: Ville Lindholm <ville@lindholm.dev>
2025-06-06 07:16:42 +00:00

105 lines
3.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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::iter::Enumerate;
use std::vec::IntoIter;
use script_bindings::str::DOMString;
use super::Node;
use crate::dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use crate::dom::bindings::root::DomRoot;
/// The context during evaluation of an XPath expression.
#[derive(Debug)]
pub(crate) struct EvaluationCtx {
/// Where we started at
pub(crate) starting_node: DomRoot<Node>,
/// The "current" node in the evaluation
pub(crate) context_node: DomRoot<Node>,
/// Details needed for evaluating a predicate list
pub(crate) predicate_ctx: Option<PredicateCtx>,
/// The nodes we're currently matching against
pub(crate) predicate_nodes: Option<Vec<DomRoot<Node>>>,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct PredicateCtx {
pub(crate) index: usize,
pub(crate) size: usize,
}
impl EvaluationCtx {
/// Prepares the context used while evaluating the XPath expression
pub(crate) fn new(context_node: &Node) -> EvaluationCtx {
EvaluationCtx {
starting_node: DomRoot::from_ref(context_node),
context_node: DomRoot::from_ref(context_node),
predicate_ctx: None,
predicate_nodes: None,
}
}
/// Creates a new context using the provided node as the context node
pub(crate) fn subcontext_for_node(&self, node: &Node) -> EvaluationCtx {
EvaluationCtx {
starting_node: self.starting_node.clone(),
context_node: DomRoot::from_ref(node),
predicate_ctx: self.predicate_ctx,
predicate_nodes: self.predicate_nodes.clone(),
}
}
pub(crate) fn update_predicate_nodes(&self, nodes: Vec<&Node>) -> EvaluationCtx {
EvaluationCtx {
starting_node: self.starting_node.clone(),
context_node: self.context_node.clone(),
predicate_ctx: None,
predicate_nodes: Some(nodes.into_iter().map(DomRoot::from_ref).collect()),
}
}
pub(crate) fn subcontext_iter_for_nodes(&self) -> EvalNodesetIter {
let size = self.predicate_nodes.as_ref().map_or(0, |v| v.len());
EvalNodesetIter {
ctx: self,
nodes_iter: self
.predicate_nodes
.as_ref()
.map_or_else(|| Vec::new().into_iter(), |v| v.clone().into_iter())
.enumerate(),
size,
}
}
/// 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))
}
}
/// When evaluating predicates, we need to keep track of the current node being evaluated and
/// the index of that node in the nodeset we're operating on.
pub(crate) struct EvalNodesetIter<'a> {
ctx: &'a EvaluationCtx,
nodes_iter: Enumerate<IntoIter<DomRoot<Node>>>,
size: usize,
}
impl Iterator for EvalNodesetIter<'_> {
type Item = EvaluationCtx;
fn next(&mut self) -> Option<EvaluationCtx> {
self.nodes_iter.next().map(|(idx, node)| EvaluationCtx {
starting_node: self.ctx.starting_node.clone(),
context_node: node.clone(),
predicate_nodes: self.ctx.predicate_nodes.clone(),
predicate_ctx: Some(PredicateCtx {
index: idx + 1,
size: self.size,
}),
})
}
}