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;
10
11use crate::dom::bindings::codegen::Bindings::XPathResultBinding::{
12    XPathResultConstants, XPathResultMethods,
13};
14use crate::dom::bindings::error::{Error, Fallible};
15use crate::dom::bindings::inheritance::Castable;
16use crate::dom::bindings::reflector::{Reflector, reflect_dom_object_with_proto};
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::node::Node;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
22use crate::xpath::{Value, XPathWrapper};
23
24#[repr(u16)]
25#[derive(Clone, Copy, Debug, Eq, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)]
26pub(crate) enum XPathResultType {
27    Any = XPathResultConstants::ANY_TYPE,
28    Number = XPathResultConstants::NUMBER_TYPE,
29    String = XPathResultConstants::STRING_TYPE,
30    Boolean = XPathResultConstants::BOOLEAN_TYPE,
31    UnorderedNodeIterator = XPathResultConstants::UNORDERED_NODE_ITERATOR_TYPE,
32    OrderedNodeIterator = XPathResultConstants::ORDERED_NODE_ITERATOR_TYPE,
33    UnorderedNodeSnapshot = XPathResultConstants::UNORDERED_NODE_SNAPSHOT_TYPE,
34    OrderedNodeSnapshot = XPathResultConstants::ORDERED_NODE_SNAPSHOT_TYPE,
35    AnyUnorderedNode = XPathResultConstants::ANY_UNORDERED_NODE_TYPE,
36    FirstOrderedNode = XPathResultConstants::FIRST_ORDERED_NODE_TYPE,
37}
38
39impl TryFrom<u16> for XPathResultType {
40    type Error = ();
41
42    fn try_from(value: u16) -> Result<Self, Self::Error> {
43        match value {
44            XPathResultConstants::ANY_TYPE => Ok(Self::Any),
45            XPathResultConstants::NUMBER_TYPE => Ok(Self::Number),
46            XPathResultConstants::STRING_TYPE => Ok(Self::String),
47            XPathResultConstants::BOOLEAN_TYPE => Ok(Self::Boolean),
48            XPathResultConstants::UNORDERED_NODE_ITERATOR_TYPE => Ok(Self::UnorderedNodeIterator),
49            XPathResultConstants::ORDERED_NODE_ITERATOR_TYPE => Ok(Self::OrderedNodeIterator),
50            XPathResultConstants::UNORDERED_NODE_SNAPSHOT_TYPE => Ok(Self::UnorderedNodeSnapshot),
51            XPathResultConstants::ORDERED_NODE_SNAPSHOT_TYPE => Ok(Self::OrderedNodeSnapshot),
52            XPathResultConstants::ANY_UNORDERED_NODE_TYPE => Ok(Self::AnyUnorderedNode),
53            XPathResultConstants::FIRST_ORDERED_NODE_TYPE => Ok(Self::FirstOrderedNode),
54            _ => Err(()),
55        }
56    }
57}
58
59#[derive(Debug, JSTraceable, MallocSizeOf)]
60pub(crate) enum XPathResultValue {
61    Boolean(bool),
62    /// A IEEE-754 double-precision floating point number
63    Number(f64),
64    String(DOMString),
65    /// A collection of unique nodes
66    Nodeset(Vec<DomRoot<Node>>),
67}
68
69impl From<Value> for XPathResultValue {
70    fn from(value: Value) -> Self {
71        match value {
72            Value::Boolean(b) => XPathResultValue::Boolean(b),
73            Value::Number(n) => XPathResultValue::Number(n),
74            Value::String(s) => XPathResultValue::String(s.into()),
75            Value::NodeSet(nodes) => {
76                XPathResultValue::Nodeset(nodes.into_iter().map(XPathWrapper::into_inner).collect())
77            },
78        }
79    }
80}
81
82#[dom_struct]
83pub(crate) struct XPathResult {
84    reflector_: Reflector,
85    window: Dom<Window>,
86    /// The revision of the owner document when this result was created. When iterating over the
87    /// values in the result, this is used to invalidate the iterator when the document is modified.
88    version: Cell<u64>,
89    result_type: Cell<XPathResultType>,
90    value: RefCell<XPathResultValue>,
91    iterator_pos: Cell<usize>,
92}
93
94impl XPathResult {
95    fn new_inherited(
96        window: &Window,
97        result_type: XPathResultType,
98        value: XPathResultValue,
99    ) -> XPathResult {
100        XPathResult {
101            reflector_: Reflector::new(),
102            window: Dom::from_ref(window),
103            version: Cell::new(
104                window
105                    .Document()
106                    .upcast::<Node>()
107                    .inclusive_descendants_version(),
108            ),
109            result_type: Cell::new(result_type),
110            iterator_pos: Cell::new(0),
111            value: RefCell::new(value),
112        }
113    }
114
115    fn document_changed_since_creation(&self) -> bool {
116        let current_document_version = self
117            .window
118            .Document()
119            .upcast::<Node>()
120            .inclusive_descendants_version();
121        current_document_version != self.version.get()
122    }
123
124    /// NB: Blindly trusts `result_type` and constructs an object regardless of the contents
125    /// of `value`. The exception is `XPathResultType::Any`, for which we look at the value
126    /// to determine the type.
127    pub(crate) fn new(
128        window: &Window,
129        proto: Option<HandleObject>,
130        can_gc: CanGc,
131        result_type: XPathResultType,
132        value: XPathResultValue,
133    ) -> DomRoot<XPathResult> {
134        reflect_dom_object_with_proto(
135            Box::new(XPathResult::new_inherited(window, result_type, value)),
136            window,
137            proto,
138            can_gc,
139        )
140    }
141
142    pub(crate) fn reinitialize_with(&self, result_type: XPathResultType, value: XPathResultValue) {
143        self.result_type.set(result_type);
144        *self.value.borrow_mut() = value;
145        self.version.set(
146            self.window
147                .Document()
148                .upcast::<Node>()
149                .inclusive_descendants_version(),
150        );
151        self.iterator_pos.set(0);
152    }
153}
154
155impl XPathResultMethods<crate::DomTypeHolder> for XPathResult {
156    /// <https://dom.spec.whatwg.org/#dom-xpathresult-resulttype>
157    fn ResultType(&self) -> u16 {
158        self.result_type.get() as u16
159    }
160
161    /// <https://dom.spec.whatwg.org/#dom-xpathresult-numbervalue>
162    fn GetNumberValue(&self) -> Fallible<f64> {
163        match (&*self.value.borrow(), self.result_type.get()) {
164            (XPathResultValue::Number(n), XPathResultType::Number) => Ok(*n),
165            _ => Err(Error::Type(
166                "Can't get number value for non-number XPathResult".to_string(),
167            )),
168        }
169    }
170
171    /// <https://dom.spec.whatwg.org/#dom-xpathresult-stringvalue>
172    fn GetStringValue(&self) -> Fallible<DOMString> {
173        match (&*self.value.borrow(), self.result_type.get()) {
174            (XPathResultValue::String(s), XPathResultType::String) => Ok(s.clone()),
175            _ => Err(Error::Type(
176                "Can't get string value for non-string XPathResult".to_string(),
177            )),
178        }
179    }
180
181    /// <https://dom.spec.whatwg.org/#dom-xpathresult-booleanvalue>
182    fn GetBooleanValue(&self) -> Fallible<bool> {
183        match (&*self.value.borrow(), self.result_type.get()) {
184            (XPathResultValue::Boolean(b), XPathResultType::Boolean) => Ok(*b),
185            _ => Err(Error::Type(
186                "Can't get boolean value for non-boolean XPathResult".to_string(),
187            )),
188        }
189    }
190
191    /// <https://dom.spec.whatwg.org/#dom-xpathresult-iteratenext>
192    fn IterateNext(&self) -> Fallible<Option<DomRoot<Node>>> {
193        if !matches!(
194            self.result_type.get(),
195            XPathResultType::OrderedNodeIterator | XPathResultType::UnorderedNodeIterator
196        ) {
197            return Err(Error::Type("Result is not an iterator".into()));
198        }
199
200        if self.document_changed_since_creation() {
201            return Err(Error::InvalidState(None));
202        }
203
204        let XPathResultValue::Nodeset(nodes) = &*self.value.borrow() else {
205            return Err(Error::Type(
206                "Can't iterate on XPathResult that is not a node-set".to_string(),
207            ));
208        };
209
210        let position = self.iterator_pos.get();
211        if position >= nodes.len() {
212            Ok(None)
213        } else {
214            let node = nodes[position].clone();
215            self.iterator_pos.set(position + 1);
216            Ok(Some(node))
217        }
218    }
219
220    /// <https://dom.spec.whatwg.org/#dom-xpathresult-invaliditeratorstate>
221    fn InvalidIteratorState(&self) -> bool {
222        let is_iterable = matches!(
223            self.result_type.get(),
224            XPathResultType::OrderedNodeIterator | XPathResultType::UnorderedNodeIterator
225        );
226
227        is_iterable && self.document_changed_since_creation()
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}