script/dom/
xpathresult.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::cell::{Cell, RefCell};
6
7use dom_struct::dom_struct;
8use js::rust::HandleObject;
9use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
10use xpath::NodesetHelpers;
11
12use crate::dom::bindings::codegen::Bindings::XPathResultBinding::{
13    XPathResultConstants, XPathResultMethods,
14};
15use crate::dom::bindings::error::{Error, Fallible};
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::node::Node;
21use crate::dom::window::Window;
22use crate::script_runtime::CanGc;
23use crate::xpath::{Value, XPathWrapper};
24
25#[repr(u16)]
26#[derive(Clone, Copy, Debug, Eq, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)]
27pub(crate) enum XPathResultType {
28    Any = XPathResultConstants::ANY_TYPE,
29    Number = XPathResultConstants::NUMBER_TYPE,
30    String = XPathResultConstants::STRING_TYPE,
31    Boolean = XPathResultConstants::BOOLEAN_TYPE,
32    UnorderedNodeIterator = XPathResultConstants::UNORDERED_NODE_ITERATOR_TYPE,
33    OrderedNodeIterator = XPathResultConstants::ORDERED_NODE_ITERATOR_TYPE,
34    UnorderedNodeSnapshot = XPathResultConstants::UNORDERED_NODE_SNAPSHOT_TYPE,
35    OrderedNodeSnapshot = XPathResultConstants::ORDERED_NODE_SNAPSHOT_TYPE,
36    AnyUnorderedNode = XPathResultConstants::ANY_UNORDERED_NODE_TYPE,
37    FirstOrderedNode = XPathResultConstants::FIRST_ORDERED_NODE_TYPE,
38}
39
40impl TryFrom<u16> for XPathResultType {
41    type Error = ();
42
43    fn try_from(value: u16) -> Result<Self, Self::Error> {
44        match value {
45            XPathResultConstants::ANY_TYPE => Ok(Self::Any),
46            XPathResultConstants::NUMBER_TYPE => Ok(Self::Number),
47            XPathResultConstants::STRING_TYPE => Ok(Self::String),
48            XPathResultConstants::BOOLEAN_TYPE => Ok(Self::Boolean),
49            XPathResultConstants::UNORDERED_NODE_ITERATOR_TYPE => Ok(Self::UnorderedNodeIterator),
50            XPathResultConstants::ORDERED_NODE_ITERATOR_TYPE => Ok(Self::OrderedNodeIterator),
51            XPathResultConstants::UNORDERED_NODE_SNAPSHOT_TYPE => Ok(Self::UnorderedNodeSnapshot),
52            XPathResultConstants::ORDERED_NODE_SNAPSHOT_TYPE => Ok(Self::OrderedNodeSnapshot),
53            XPathResultConstants::ANY_UNORDERED_NODE_TYPE => Ok(Self::AnyUnorderedNode),
54            XPathResultConstants::FIRST_ORDERED_NODE_TYPE => Ok(Self::FirstOrderedNode),
55            _ => Err(()),
56        }
57    }
58}
59
60#[derive(Debug, JSTraceable, MallocSizeOf)]
61pub(crate) enum XPathResultValue {
62    Boolean(bool),
63    /// A IEEE-754 double-precision floating point number
64    Number(f64),
65    String(DOMString),
66    /// A collection of unique nodes
67    Nodeset(Vec<DomRoot<Node>>),
68}
69
70impl From<Value> for XPathResultValue {
71    fn from(value: Value) -> Self {
72        match value {
73            Value::Boolean(b) => XPathResultValue::Boolean(b),
74            Value::Number(n) => XPathResultValue::Number(n),
75            Value::String(s) => XPathResultValue::String(s.into()),
76            Value::Nodeset(nodes) => {
77                // Put the evaluation result into (unique) document order. This also re-roots them
78                // so that we are sure we can hold them for the lifetime of this XPathResult.
79                let rooted_nodes = nodes.document_order_unique();
80                XPathResultValue::Nodeset(
81                    rooted_nodes
82                        .into_iter()
83                        .map(XPathWrapper::into_inner)
84                        .collect(),
85                )
86            },
87        }
88    }
89}
90
91#[dom_struct]
92pub(crate) struct XPathResult {
93    reflector_: Reflector,
94    window: Dom<Window>,
95    /// The revision of the owner document when this result was created. When iterating over the
96    /// values in the result, this is used to invalidate the iterator when the document is modified.
97    version: Cell<u64>,
98    result_type: Cell<XPathResultType>,
99    value: RefCell<XPathResultValue>,
100    iterator_pos: Cell<usize>,
101}
102
103impl XPathResult {
104    fn new_inherited(
105        window: &Window,
106        result_type: XPathResultType,
107        value: XPathResultValue,
108    ) -> XPathResult {
109        // TODO(vlindhol): if the wanted result type is AnyUnorderedNode | FirstOrderedNode,
110        // we could drop all nodes except one to save memory.
111        let inferred_result_type = if result_type == XPathResultType::Any {
112            match value {
113                XPathResultValue::Boolean(_) => XPathResultType::Boolean,
114                XPathResultValue::Number(_) => XPathResultType::Number,
115                XPathResultValue::String(_) => XPathResultType::String,
116                XPathResultValue::Nodeset(_) => XPathResultType::UnorderedNodeIterator,
117            }
118        } else {
119            result_type
120        };
121
122        XPathResult {
123            reflector_: Reflector::new(),
124            window: Dom::from_ref(window),
125            version: Cell::new(
126                window
127                    .Document()
128                    .upcast::<Node>()
129                    .inclusive_descendants_version(),
130            ),
131            result_type: Cell::new(inferred_result_type),
132            iterator_pos: Cell::new(0),
133            value: RefCell::new(value),
134        }
135    }
136
137    fn document_changed_since_creation(&self) -> bool {
138        let current_document_version = self
139            .window
140            .Document()
141            .upcast::<Node>()
142            .inclusive_descendants_version();
143        current_document_version != self.version.get()
144    }
145
146    /// NB: Blindly trusts `result_type` and constructs an object regardless of the contents
147    /// of `value`. The exception is `XPathResultType::Any`, for which we look at the value
148    /// to determine the type.
149    pub(crate) fn new(
150        window: &Window,
151        proto: Option<HandleObject>,
152        can_gc: CanGc,
153        result_type: XPathResultType,
154        value: XPathResultValue,
155    ) -> DomRoot<XPathResult> {
156        reflect_dom_object_with_proto(
157            Box::new(XPathResult::new_inherited(window, result_type, value)),
158            window,
159            proto,
160            can_gc,
161        )
162    }
163
164    pub(crate) fn reinitialize_with(&self, result_type: XPathResultType, value: XPathResultValue) {
165        self.result_type.set(result_type);
166        *self.value.borrow_mut() = value;
167        self.version.set(
168            self.window
169                .Document()
170                .upcast::<Node>()
171                .inclusive_descendants_version(),
172        );
173        self.iterator_pos.set(0);
174    }
175}
176
177impl XPathResultMethods<crate::DomTypeHolder> for XPathResult {
178    /// <https://dom.spec.whatwg.org/#dom-xpathresult-resulttype>
179    fn ResultType(&self) -> u16 {
180        self.result_type.get() as u16
181    }
182
183    /// <https://dom.spec.whatwg.org/#dom-xpathresult-numbervalue>
184    fn GetNumberValue(&self) -> Fallible<f64> {
185        match (&*self.value.borrow(), self.result_type.get()) {
186            (XPathResultValue::Number(n), XPathResultType::Number) => Ok(*n),
187            _ => Err(Error::Type(
188                "Can't get number value for non-number XPathResult".to_string(),
189            )),
190        }
191    }
192
193    /// <https://dom.spec.whatwg.org/#dom-xpathresult-stringvalue>
194    fn GetStringValue(&self) -> Fallible<DOMString> {
195        match (&*self.value.borrow(), self.result_type.get()) {
196            (XPathResultValue::String(s), XPathResultType::String) => Ok(s.clone()),
197            _ => Err(Error::Type(
198                "Can't get string value for non-string XPathResult".to_string(),
199            )),
200        }
201    }
202
203    /// <https://dom.spec.whatwg.org/#dom-xpathresult-booleanvalue>
204    fn GetBooleanValue(&self) -> Fallible<bool> {
205        match (&*self.value.borrow(), self.result_type.get()) {
206            (XPathResultValue::Boolean(b), XPathResultType::Boolean) => Ok(*b),
207            _ => Err(Error::Type(
208                "Can't get boolean value for non-boolean XPathResult".to_string(),
209            )),
210        }
211    }
212
213    /// <https://dom.spec.whatwg.org/#dom-xpathresult-iteratenext>
214    fn IterateNext(&self) -> Fallible<Option<DomRoot<Node>>> {
215        if !matches!(
216            self.result_type.get(),
217            XPathResultType::OrderedNodeIterator | XPathResultType::UnorderedNodeIterator
218        ) {
219            return Err(Error::Type("Result is not an iterator".into()));
220        }
221
222        if self.document_changed_since_creation() {
223            return Err(Error::InvalidState(None));
224        }
225
226        let XPathResultValue::Nodeset(nodes) = &*self.value.borrow() else {
227            return Err(Error::Type(
228                "Can't iterate on XPathResult that is not a node-set".to_string(),
229            ));
230        };
231
232        let position = self.iterator_pos.get();
233        if position >= nodes.len() {
234            Ok(None)
235        } else {
236            let node = nodes[position].clone();
237            self.iterator_pos.set(position + 1);
238            Ok(Some(node))
239        }
240    }
241
242    /// <https://dom.spec.whatwg.org/#dom-xpathresult-invaliditeratorstate>
243    fn InvalidIteratorState(&self) -> bool {
244        let is_iterable = matches!(
245            self.result_type.get(),
246            XPathResultType::OrderedNodeIterator | XPathResultType::UnorderedNodeIterator
247        );
248
249        is_iterable && self.document_changed_since_creation()
250    }
251
252    /// <https://dom.spec.whatwg.org/#dom-xpathresult-snapshotlength>
253    fn GetSnapshotLength(&self) -> Fallible<u32> {
254        match (&*self.value.borrow(), self.result_type.get()) {
255            (
256                XPathResultValue::Nodeset(nodes),
257                XPathResultType::OrderedNodeSnapshot | XPathResultType::UnorderedNodeSnapshot,
258            ) => Ok(nodes.len() as u32),
259            _ => Err(Error::Type(
260                "Can't get snapshot length of XPathResult that is not a snapshot".to_string(),
261            )),
262        }
263    }
264
265    /// <https://dom.spec.whatwg.org/#dom-xpathresult-snapshotitem>
266    fn SnapshotItem(&self, index: u32) -> Fallible<Option<DomRoot<Node>>> {
267        match (&*self.value.borrow(), self.result_type.get()) {
268            (
269                XPathResultValue::Nodeset(nodes),
270                XPathResultType::OrderedNodeSnapshot | XPathResultType::UnorderedNodeSnapshot,
271            ) => Ok(nodes.get(index as usize).cloned()),
272            _ => Err(Error::Type(
273                "Can't get snapshot item of XPathResult that is not a snapshot".to_string(),
274            )),
275        }
276    }
277
278    /// <https://dom.spec.whatwg.org/#dom-xpathresult-singlenodevalue>
279    fn GetSingleNodeValue(&self) -> Fallible<Option<DomRoot<Node>>> {
280        match (&*self.value.borrow(), self.result_type.get()) {
281            (
282                XPathResultValue::Nodeset(nodes),
283                XPathResultType::AnyUnorderedNode | XPathResultType::FirstOrderedNode,
284            ) => Ok(nodes.first().cloned()),
285            _ => Err(Error::Type(
286                "Getting single value requires result type 'any unordered node' or 'first ordered node'".to_string(),
287            )),
288        }
289    }
290}