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