servo-script 0.4.0

A component of the servo web-engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* 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/. */

//! Bindings to the `xpath` crate

use std::cell::Ref;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::rc::Rc;

use html5ever::{LocalName, Namespace, Prefix};
use js::context::JSContext;
use script_bindings::callback::ExceptionHandling;
use script_bindings::codegen::GenericBindings::AttrBinding::AttrMethods;
use script_bindings::codegen::GenericBindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
use script_bindings::root::Dom;
use script_bindings::str::DOMString;
use style::Atom;
use style::dom::OpaqueNode;

use crate::dom::attr::Attr;
use crate::dom::bindings::codegen::Bindings::XPathNSResolverBinding::XPathNSResolver;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::comment::Comment;
use crate::dom::document::Document;
use crate::dom::element::Element;
use crate::dom::element::attributes::storage::AttributeStorage;
use crate::dom::iterators::{PrecedingNodeIterator, ShadowIncluding};
use crate::dom::node::{Node, NodeTraits};
use crate::dom::processinginstruction::ProcessingInstruction;
use crate::dom::text::Text;

pub(crate) type Value = xpath::Value<XPathWrapper<DomRoot<Node>>>;

/// Wrapper type that allows us to define xpath traits on the relevant types,
/// since they're not defined in `script`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct XPathWrapper<T>(pub T);

pub(crate) struct XPathImplementation;

impl xpath::Dom for XPathImplementation {
    type Context = JSContext;
    type Node = XPathWrapper<DomRoot<Node>>;
    type NamespaceResolver = XPathWrapper<Rc<XPathNSResolver>>;
}

impl xpath::Node for XPathWrapper<DomRoot<Node>> {
    type Context = JSContext;
    type ProcessingInstruction = XPathWrapper<DomRoot<ProcessingInstruction>>;
    type Document = XPathWrapper<DomRoot<Document>>;
    type Attribute = XPathWrapper<DomRoot<Attr>>;
    type Element = XPathWrapper<DomRoot<Element>>;
    /// A opaque handle to a node with the sole purpose of comparing one node with another.
    type Opaque = OpaqueNode;

    fn is_comment(&self) -> bool {
        self.0.is::<Comment>()
    }

    fn is_text(&self) -> bool {
        self.0.is::<Text>()
    }

    fn text_content(&self) -> String {
        self.0.GetTextContent().unwrap_or_default().into()
    }

    fn language(&self) -> Option<String> {
        self.0.get_lang()
    }

    fn parent(&self) -> Option<Self> {
        // The parent of an attribute node is its owner, see
        // https://www.w3.org/TR/1999/REC-xpath-19991116/#attribute-nodes
        if let Some(attribute) = self.0.downcast::<Attr>() {
            return attribute
                .GetOwnerElement()
                .map(DomRoot::upcast)
                .map(XPathWrapper);
        }

        self.0.GetParentNode().map(XPathWrapper)
    }

    fn children(&self) -> impl Iterator<Item = Self> {
        self.0.children().map(XPathWrapper)
    }

    fn compare_tree_order(&self, other: &Self) -> Ordering {
        if self == other {
            Ordering::Equal
        } else if self.0.is_before(&other.0) {
            Ordering::Less
        } else {
            Ordering::Greater
        }
    }

    fn traverse_preorder(&self) -> impl Iterator<Item = Self> {
        self.0
            .traverse_preorder(ShadowIncluding::No)
            .map(XPathWrapper)
    }

    fn inclusive_ancestors(&self) -> impl Iterator<Item = Self> {
        self.0
            .inclusive_ancestors(ShadowIncluding::No)
            .map(XPathWrapper)
    }

    fn preceding_nodes(&self) -> impl Iterator<Item = Self> {
        PrecedingNodeIteratorWithoutAncestors::new(&self.0).map(XPathWrapper)
    }

    fn following_nodes(&self) -> impl Iterator<Item = Self> {
        let owner_document = self.0.owner_document();
        let next_non_descendant_node = self
            .0
            .following_nodes(owner_document.upcast(), ShadowIncluding::No)
            .next_skipping_children();
        let following_nodes = next_non_descendant_node
            .clone()
            .map(|node| node.following_nodes(owner_document.upcast(), ShadowIncluding::No))
            .into_iter()
            .flatten();
        next_non_descendant_node
            .into_iter()
            .chain(following_nodes)
            .map(XPathWrapper)
    }

    fn preceding_siblings(&self) -> impl Iterator<Item = Self> {
        self.0.preceding_siblings().map(XPathWrapper)
    }

    fn following_siblings(&self) -> impl Iterator<Item = Self> {
        self.0.following_siblings().map(XPathWrapper)
    }

    fn owner_document(&self) -> Self::Document {
        XPathWrapper(self.0.owner_document())
    }

    fn to_opaque(&self) -> Self::Opaque {
        self.0.to_opaque()
    }

    fn as_processing_instruction(&self) -> Option<Self::ProcessingInstruction> {
        self.0
            .downcast::<ProcessingInstruction>()
            .map(DomRoot::from_ref)
            .map(XPathWrapper)
    }

    fn as_attribute(&self) -> Option<Self::Attribute> {
        self.0
            .downcast::<Attr>()
            .map(DomRoot::from_ref)
            .map(XPathWrapper)
    }

    fn as_element(&self) -> Option<Self::Element> {
        self.0
            .downcast::<Element>()
            .map(DomRoot::from_ref)
            .map(XPathWrapper)
    }

    fn get_root_node(&self) -> Self {
        XPathWrapper(self.0.GetRootNode(&GetRootNodeOptions::empty()))
    }
}

impl xpath::Document for XPathWrapper<DomRoot<Document>> {
    type Node = XPathWrapper<DomRoot<Node>>;

    fn get_elements_with_id(
        &self,
        cx: &mut JSContext,
        id: &str,
    ) -> impl Iterator<Item = XPathWrapper<DomRoot<Element>>> {
        struct ElementIterator<'a> {
            elements: Ref<'a, [Dom<Element>]>,
            position: usize,
        }

        impl<'a> Iterator for ElementIterator<'a> {
            type Item = XPathWrapper<DomRoot<Element>>;

            fn next(&mut self) -> Option<Self::Item> {
                let element = self.elements.get(self.position)?;
                self.position += 1;
                Some(element.as_rooted().into())
            }
        }

        ElementIterator {
            elements: self.0.get_elements_with_id(cx, &Atom::from(id)),
            position: 0,
        }
    }
}

impl xpath::Element for XPathWrapper<DomRoot<Element>> {
    type Context = JSContext;
    type Node = XPathWrapper<DomRoot<Node>>;
    type Attribute = XPathWrapper<DomRoot<Attr>>;

    fn as_node(&self) -> Self::Node {
        DomRoot::from_ref(self.0.upcast::<Node>()).into()
    }

    fn attributes(&self, cx: &mut JSContext) -> impl Iterator<Item = Self::Attribute> {
        struct AttributeIterator<'a> {
            attributes: &'a AttributeStorage,
            position: usize,
        }

        impl<'a> Iterator for AttributeIterator<'a> {
            type Item = XPathWrapper<DomRoot<Attr>>;

            fn next(&mut self) -> Option<Self::Item> {
                let entries = self.attributes.borrow();
                let entry = entries.get(self.position)?;
                self.position += 1;
                Some(DomRoot::from_ref(entry.as_attr().unwrap()).into())
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                let exact_length = self.attributes.borrow().len() - self.position;
                (exact_length, Some(exact_length))
            }
        }

        // XPath needs full DOM attribute nodes.
        AttributeIterator {
            attributes: self.0.dom_attrs(cx),
            position: 0,
        }
    }

    fn prefix(&self) -> Option<Prefix> {
        self.0.prefix().clone()
    }

    fn namespace(&self) -> Namespace {
        self.0.namespace().clone()
    }

    fn local_name(&self) -> LocalName {
        self.0.local_name().clone()
    }

    fn is_html_element_in_html_document(&self) -> bool {
        self.0.is_html_element() && self.0.owner_document().is_html_document()
    }
}

impl xpath::Attribute for XPathWrapper<DomRoot<Attr>> {
    type Node = XPathWrapper<DomRoot<Node>>;

    fn as_node(&self) -> Self::Node {
        XPathWrapper(DomRoot::from_ref(self.0.upcast::<Node>()))
    }

    fn prefix(&self) -> Option<Prefix> {
        self.0.prefix().cloned()
    }

    fn namespace(&self) -> Namespace {
        self.0.namespace().clone()
    }

    fn local_name(&self) -> LocalName {
        self.0.local_name().clone()
    }
}

impl xpath::NamespaceResolver for XPathWrapper<Rc<XPathNSResolver>> {
    type Context = JSContext;

    fn resolve_namespace_prefix(&self, cx: &mut JSContext, prefix: &str) -> Option<String> {
        self.0
            .LookupNamespaceURI__(cx, Some(DOMString::from(prefix)), ExceptionHandling::Report)
            .ok()
            .flatten()
            .map(String::from)
    }
}

impl xpath::ProcessingInstruction for XPathWrapper<DomRoot<ProcessingInstruction>> {
    fn target(&self) -> String {
        self.0.target().to_owned().into()
    }
}

impl<T> From<T> for XPathWrapper<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl<T> XPathWrapper<T> {
    pub(crate) fn into_inner(self) -> T {
        self.0
    }
}

pub(crate) fn parse_expression(
    cx: &mut JSContext,
    expression: &str,
    resolver: Option<Rc<XPathNSResolver>>,
    is_in_html_document: bool,
) -> Fallible<xpath::Expression> {
    xpath::parse(
        cx,
        expression,
        resolver.map(XPathWrapper),
        is_in_html_document,
    )
    .map_err(|error| match error {
        xpath::ParserError::FailedToResolveNamespacePrefix => Error::Namespace(None),
        _ => Error::Syntax(Some(format!("Failed to parse XPath expression: {error:?}"))),
    })
}

enum PrecedingNodeIteratorWithoutAncestors {
    Done,
    NotDone {
        current: DomRoot<Node>,
        /// When we're currently walking over the subtree of a node in reverse tree order
        /// then this is the iterator for doing that.
        subtree_iterator: Option<PrecedingNodeIterator>,
    },
}

/// Returns the previous element (in tree order) that is not an ancestor of `node`.
fn previous_non_ancestor_node(node: &Node) -> Option<DomRoot<Node>> {
    let mut current = DomRoot::from_ref(node);
    loop {
        if let Some(previous_sibling) = current.GetPreviousSibling() {
            return Some(previous_sibling);
        }

        current = current.GetParentNode()?;
    }
}

impl PrecedingNodeIteratorWithoutAncestors {
    fn new(node: &Node) -> Self {
        let Some(current) = previous_non_ancestor_node(node) else {
            return Self::Done;
        };

        Self::NotDone {
            subtree_iterator: current
                .descending_last_children()
                .last()
                .map(|node| node.preceding_nodes(&current)),
            current,
        }
    }
}

impl Iterator for PrecedingNodeIteratorWithoutAncestors {
    type Item = DomRoot<Node>;

    fn next(&mut self) -> Option<Self::Item> {
        let Self::NotDone {
            current,
            subtree_iterator,
        } = self
        else {
            return None;
        };

        if let Some(next_node) = subtree_iterator
            .as_mut()
            .and_then(|iterator| iterator.next())
        {
            return Some(next_node);
        }

        // Our current subtree is exhausted. Return the root of the subtree and move on to the next one
        // in inverse tree order.
        let Some(next_subtree) = previous_non_ancestor_node(current) else {
            *self = Self::Done;
            return None;
        };

        *current = next_subtree;
        *subtree_iterator = current
            .descending_last_children()
            .last()
            .map(|node| node.preceding_nodes(current));

        self.next()
    }
}