script/dom/
xpathevaluator.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::rc::Rc;
6
7use dom_struct::dom_struct;
8use js::rust::HandleObject;
9use script_bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
10
11use super::bindings::error::Error;
12use crate::dom::bindings::codegen::Bindings::XPathEvaluatorBinding::XPathEvaluatorMethods;
13use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
14use crate::dom::bindings::error::Fallible;
15use crate::dom::bindings::reflector::{DomGlobal, Reflector, reflect_dom_object_with_proto};
16use crate::dom::bindings::root::{Dom, DomRoot};
17use crate::dom::bindings::str::DOMString;
18use crate::dom::node::Node;
19use crate::dom::window::Window;
20use crate::dom::xpathexpression::XPathExpression;
21use crate::dom::xpathresult::XPathResult;
22use crate::script_runtime::CanGc;
23
24#[dom_struct]
25pub(crate) struct XPathEvaluator {
26    reflector_: Reflector,
27    window: Dom<Window>,
28}
29
30impl XPathEvaluator {
31    fn new_inherited(window: &Window) -> XPathEvaluator {
32        XPathEvaluator {
33            reflector_: Reflector::new(),
34            window: Dom::from_ref(window),
35        }
36    }
37
38    pub(crate) fn new(
39        window: &Window,
40        proto: Option<HandleObject>,
41        can_gc: CanGc,
42    ) -> DomRoot<XPathEvaluator> {
43        reflect_dom_object_with_proto(
44            Box::new(XPathEvaluator::new_inherited(window)),
45            window,
46            proto,
47            can_gc,
48        )
49    }
50}
51
52impl XPathEvaluatorMethods<crate::DomTypeHolder> for XPathEvaluator {
53    /// <https://dom.spec.whatwg.org/#dom-xpathevaluator-xpathevaluator>
54    fn Constructor(
55        window: &Window,
56        proto: Option<HandleObject>,
57        can_gc: CanGc,
58    ) -> DomRoot<XPathEvaluator> {
59        XPathEvaluator::new(window, proto, can_gc)
60    }
61
62    /// <https://dom.spec.whatwg.org/#dom-xpathevaluatorbase-createexpression>
63    fn CreateExpression(
64        &self,
65        expression: DOMString,
66        _resolver: Option<Rc<XPathNSResolver>>,
67        can_gc: CanGc,
68    ) -> Fallible<DomRoot<XPathExpression>> {
69        let global = self.global();
70        let window = global.as_window();
71        // NB: this function is *not* Fallible according to the spec, so we swallow any parsing errors and
72        // just pass a None as the expression... it's not great.
73        let parsed_expression =
74            xpath::parse::<()>(&expression.str()).map_err(|_e| Error::Syntax(None))?;
75        Ok(XPathExpression::new(
76            window,
77            None,
78            can_gc,
79            parsed_expression,
80        ))
81    }
82
83    /// <https://dom.spec.whatwg.org/#dom-xpathevaluatorbase-creatensresolver>
84    fn CreateNSResolver(&self, node_resolver: &Node) -> DomRoot<Node> {
85        // Legacy: the spec tells us to just return `node_resolver` as-is
86        DomRoot::from_ref(node_resolver)
87    }
88
89    /// <https://dom.spec.whatwg.org/#dom-xpathevaluatorbase-evaluate>
90    fn Evaluate(
91        &self,
92        expression_str: DOMString,
93        context_node: &Node,
94        resolver: Option<Rc<XPathNSResolver>>,
95        result_type: u16,
96        result: Option<&XPathResult>,
97        can_gc: CanGc,
98    ) -> Fallible<DomRoot<XPathResult>> {
99        let is_allowed_context_node_type = matches!(
100            context_node.type_id(),
101            NodeTypeId::Attr |
102                NodeTypeId::CharacterData(
103                    CharacterDataTypeId::Comment |
104                        CharacterDataTypeId::Text(_) |
105                        CharacterDataTypeId::ProcessingInstruction
106                ) |
107                NodeTypeId::Document(_) |
108                NodeTypeId::Element(_)
109        );
110        if !is_allowed_context_node_type {
111            return Err(Error::NotSupported);
112        }
113
114        let global = self.global();
115        let window = global.as_window();
116
117        let parsed_expression =
118            xpath::parse::<()>(&expression_str.str()).map_err(|_| Error::Syntax(None))?;
119        let expression = XPathExpression::new(window, None, can_gc, parsed_expression);
120        expression.evaluate_internal(context_node, result_type, result, resolver, can_gc)
121    }
122}