dom_query 0.27.0

HTML querying and manipulation with CSS selectors
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::cell::Ref;

use tendril::StrTendril;

use super::helpers::normalized_char_count;
use super::Tree;

use crate::entities::{into_tendril, wrap_tendril, StrWrap};
use crate::node::{child_nodes, descendant_nodes};
use crate::node::{NodeData, NodeId, TreeNode};
pub struct TreeNodeOps {}

/// Number of leading scaffold nodes to skip when merging parsed fragments
const SKIP_NODES_ON_MERGE: usize = 3;

// property
impl TreeNodeOps {
    /// Collects all text content of a node and its descendants.
    ///
    /// # Arguments
    ///
    /// - `nodes`: a reference to a vector of `TreeNode`s.
    /// - `id`: `NodeId` of the element to get the text content from.
    ///
    /// This function will traverse the tree and collect all text content
    /// from the node and its descendants. It will ignore any nodes that
    /// are not `Element`s or `Text`s.
    ///
    /// The function returns a `StrTendril` containing all collected text content.
    pub fn text_of(nodes: Ref<Vec<TreeNode>>, id: NodeId) -> StrTendril {
        let node_ids = std::iter::once(id).chain(descendant_nodes(Ref::clone(&nodes), &id));

        let text = node_ids
            .filter_map(|node_id| nodes.get(node_id.value))
            .filter_map(|node| match &node.data {
                NodeData::Text { ref contents } => Some(contents),
                _ => None,
            })
            .fold(StrWrap::new(), |mut acc, contents| {
                acc.push_tendril(contents);
                acc
            });

        into_tendril(text)
    }

    /// Traverses the tree and counts all text content of a node and its descendants,
    /// but only counting each sequence of whitespace as a single character.
    ///
    /// # Arguments
    ///
    /// - `nodes`: a reference to a vector of `TreeNode`s.
    /// - `id`: `NodeId` of the element to get the text content from.
    ///
    /// This function will traverse the tree and count all text content
    /// from the node and its descendants.
    ///
    /// It has an advantage over `node.text().split_whitespace().count()`
    /// because it doesn't need to collect and consume the text content.
    ///
    /// # Returns
    /// The number of characters that would be in the text content if it were normalized,
    /// where normalization means treating any sequence of whitespace characters as a single space.
    pub fn normalized_char_count(nodes: Ref<Vec<TreeNode>>, id: NodeId) -> usize {
        let mut c: usize = 0;
        let mut last_was_whitespace = true;

        let node_ids = std::iter::once(id).chain(descendant_nodes(Ref::clone(&nodes), &id));
        for node in node_ids.filter_map(|node_id| nodes.get(node_id.value)) {
            if let NodeData::Text { ref contents } = node.data {
                c += normalized_char_count(contents, last_was_whitespace);
                last_was_whitespace = contents.ends_with(char::is_whitespace);
            }
        }

        if last_was_whitespace && c > 0 {
            c -= 1;
        }
        c
    }

    /// Returns the text of the node without its descendants.
    pub fn immediate_text_of(nodes: Ref<Vec<TreeNode>>, id: NodeId) -> StrTendril {
        let mut text = StrWrap::new();

        let node_ids = std::iter::once(id)
            .chain(child_nodes(Ref::clone(&nodes), &id, false));

        node_ids
            .filter_map(|id| nodes.get(id.value))
            .for_each(|tree_node| {
                if let NodeData::Text { ref contents } = tree_node.data {
                    text.push_tendril(contents)
                }
            });

        into_tendril(text)
    }

    /// Gets the last sibling node of a node by id.
    ///
    /// This function walks through sibling nodes from the given node until there are no more sibling nodes.
    /// It returns the last sibling node id it found.
    pub fn last_sibling_of(nodes: &[TreeNode], id: &NodeId) -> Option<NodeId> {
        let mut last_id = None;
        let mut current_id = nodes.get(id.value)?.next_sibling;
        while let Some(curr) = current_id.and_then(|id| nodes.get(id.value)) {
            last_id = Some(curr.id);
            current_id = curr.next_sibling;
        }
        last_id
    }

    /// Returns the next sibling id, that is an [`NodeData::Element`] of the selected node.
    pub fn next_element_sibling_of(nodes: &[TreeNode], id: &NodeId) -> Option<NodeId> {
        let mut node = nodes.get(id.value)?;

        while let Some(id) = node.next_sibling {
            node = nodes.get(id.value)?;
            if node.is_element() {
                return Some(node.id);
            }
        }
        None
    }

    /// Returns the previous sibling id, that is an [`NodeData::Element`] of the selected node.
    pub fn prev_element_sibling_of(nodes: &[TreeNode], id: &NodeId) -> Option<NodeId> {
        let mut node = nodes.get(id.value)?;

        while let Some(id) = node.prev_sibling {
            node = nodes.get(id.value)?;
            if node.is_element() {
                return Some(node.id);
            }
        }
        None
    }

    /// Returns the first child id, that is an [`NodeData::Element`] of the selected node.
    pub fn first_element_child_of(nodes: &[TreeNode], id: &NodeId) -> Option<NodeId> {
        let node = nodes.get(id.value)?;
        let mut next_child_id = node.first_child;

        while let Some(node_id) = next_child_id {
            let child_node = nodes.get(node_id.value)?;
            if child_node.is_element() {
                return Some(node_id);
            }
            next_child_id = child_node.next_sibling;
        }
        None
    }
    /// Checks if the given node id is valid in the tree.
    pub fn is_valid_node_id(nodes: &[TreeNode], id: &NodeId) -> bool {
        nodes.get(id.value).is_some_and(|node| node.id == *id)
    }
}

// manipulation
impl TreeNodeOps {
    /// Creates a new node with the given data.
    pub fn create_node(nodes: &mut Vec<TreeNode>, data: NodeData) -> NodeId {
        let new_child_id = NodeId::new(nodes.len());
        nodes.push(TreeNode::new(new_child_id, data));
        new_child_id
    }

    /// Creates a new element from data  and appends it to a node by id
    pub fn append_child_data_of(nodes: &mut Vec<TreeNode>, id: &NodeId, data: NodeData) {
        let last_child_id = nodes.get(id.value).and_then(|node| node.last_child);

        let new_child_id = NodeId::new(nodes.len());
        let mut child = TreeNode::new(new_child_id, data);
        child.prev_sibling = last_child_id;
        child.parent = Some(*id);
        nodes.push(child);
        let new_child_id_opt = Some(new_child_id);

        if let Some(node) = last_child_id.and_then(|id| nodes.get_mut(id.value)) {
            node.next_sibling = new_child_id_opt
        }

        if let Some(parent) = nodes.get_mut(id.value) {
            if parent.first_child.is_none() {
                parent.first_child = new_child_id_opt
            }
            parent.last_child = new_child_id_opt;
        }
    }

    /// Appends a child node by `new_child_id` to a node by `id`. `new_child_id` must exist in the tree.
    pub fn append_child_of(nodes: &mut [TreeNode], id: &NodeId, new_child_id: &NodeId) {
        Self::remove_from_parent(nodes, new_child_id);
        let Some(parent) = nodes.get_mut(id.value) else {
            return;
        };

        let last_child_id = parent.last_child;

        if last_child_id.is_none() {
            parent.first_child = Some(*new_child_id);
        }

        parent.last_child = Some(*new_child_id);

        if let Some(last_child) = last_child_id.and_then(|id| nodes.get_mut(id.value)) {
            last_child.next_sibling = Some(*new_child_id);
        }

        if let Some(child) = nodes.get_mut(new_child_id.value) {
            child.prev_sibling = last_child_id;
            child.parent = Some(*id);
        }
    }

    /// Prepend a child node by `new_child_id` to a node by `id`. `new_child_id` must exist in the tree.
    pub fn prepend_child_of(nodes: &mut [TreeNode], id: &NodeId, new_child_id: &NodeId) {
        Self::remove_from_parent(nodes, new_child_id);
        let Some(parent) = nodes.get_mut(id.value) else {
            return;
        };
        let first_child_id = parent.first_child;

        if first_child_id.is_none() {
            parent.last_child = Some(*new_child_id);
        }

        parent.first_child = Some(*new_child_id);

        if let Some(first_child) = first_child_id.and_then(|id| nodes.get_mut(id.value)) {
            first_child.prev_sibling = Some(*new_child_id);
        }

        if let Some(child) = nodes.get_mut(new_child_id.value) {
            child.next_sibling = first_child_id;
            child.parent = Some(*id);
            child.prev_sibling = None;
        }
    }

    /// Append a sibling node in the tree before the given node.
    pub fn insert_before_of(nodes: &mut [TreeNode], id: &NodeId, new_sibling_id: &NodeId) {
        Self::remove_from_parent(nodes, new_sibling_id);
        let Some(node) = nodes.get_mut(id.value) else {
            return;
        };

        let parent_id = node.parent;
        let prev_sibling_id = node.prev_sibling;

        node.prev_sibling = Some(*new_sibling_id);

        if let Some(new_sibling) = nodes.get_mut(new_sibling_id.value) {
            new_sibling.parent = parent_id;
            new_sibling.prev_sibling = prev_sibling_id;
            new_sibling.next_sibling = Some(*id);
        };

        if let Some(parent) = parent_id.and_then(|id| nodes.get_mut(id.value)) {
            if parent.first_child == Some(*id) {
                parent.first_child = Some(*new_sibling_id);
            }
        }

        if let Some(prev_sibling) = prev_sibling_id.and_then(|id| nodes.get_mut(id.value)) {
            prev_sibling.next_sibling = Some(*new_sibling_id);
        }
    }

    /// Append a sibling node in the tree after the given node.
    pub fn insert_after_of(nodes: &mut [TreeNode], id: &NodeId, new_sibling_id: &NodeId) {
        Self::remove_from_parent(nodes, new_sibling_id);
        let Some(node) = nodes.get_mut(id.value) else {
            return;
        };

        let parent_id = node.parent;
        let next_sibling_id = node.next_sibling;

        node.next_sibling = Some(*new_sibling_id);

        if let Some(new_sibling) = nodes.get_mut(new_sibling_id.value) {
            new_sibling.parent = parent_id;
            new_sibling.prev_sibling = Some(*id);
            new_sibling.next_sibling = next_sibling_id;
        };

        if let Some(parent) = parent_id.and_then(|id| nodes.get_mut(id.value)) {
            if parent.last_child == Some(*id) {
                parent.last_child = Some(*new_sibling_id);
            }
        }

        if let Some(next_sibling) = next_sibling_id.and_then(|id| nodes.get_mut(id.value)) {
            next_sibling.prev_sibling = Some(*new_sibling_id);
        }
    }

    /// Inserts a node and it's siblings by id before the selected node.
    pub fn insert_siblings_before(nodes: &mut [TreeNode], id: &NodeId, new_node_id: &NodeId) {
        let mut next_node_id = Some(*new_node_id);

        while let Some(node_id) = next_node_id {
            next_node_id = nodes.get(node_id.value).and_then(|n| n.next_sibling);
            Self::insert_before_of(nodes, id, &node_id);
        }
    }

    /// Inserts a node and it's siblings by id after the selected node.
    pub fn insert_siblings_after(nodes: &mut [TreeNode], id: &NodeId, new_node_id: &NodeId) {
        let mut next_node_id = Some(*new_node_id);
        let mut target_id = *id;

        while let Some(node_id) = next_node_id {
            next_node_id = nodes.get(node_id.value).and_then(|n| n.next_sibling);
            Self::insert_after_of(nodes, &target_id, &node_id);
            target_id = node_id;
        }
    }

    /// Appends another node and it's siblings to the selected node.
    pub fn append_children_of(nodes: &mut [TreeNode], id: &NodeId, new_child_id: &NodeId) {
        let mut next_node_id = Some(new_child_id).copied();

        while let Some(node_id) = next_node_id {
            next_node_id = nodes.get(node_id.value).and_then(|n| n.next_sibling);
            Self::append_child_of(nodes, id, &node_id);
        }
    }

    /// Prepend another node and it's siblings to the selected node.
    pub fn prepend_children_of(nodes: &mut [TreeNode], id: &NodeId, new_child_id: &NodeId) {
        // avoiding call borrow
        let mut prev_node_id = Self::last_sibling_of(nodes, new_child_id);

        if prev_node_id.is_none() {
            prev_node_id = Some(*new_child_id)
        }
        while let Some(node_id) = prev_node_id {
            prev_node_id = nodes.get(node_id.value).and_then(|n| n.prev_sibling);
            Self::remove_from_parent(nodes, &node_id);
            Self::prepend_child_of(nodes, id, &node_id);
        }
    }

    /// Remove a node from the its parent by id. The node remains in the tree.
    /// It is possible to assign it to another node in the tree after this operation.
    pub fn remove_from_parent(nodes: &mut [TreeNode], id: &NodeId) {
        let Some(node) = nodes.get_mut(id.value) else {
            return;
        };
        let parent_id = node.parent;
        let prev_sibling_id = node.prev_sibling;
        let next_sibling_id = node.next_sibling;

        if parent_id.is_none() && prev_sibling_id.is_none() && next_sibling_id.is_none() {
            return;
        }

        node.parent = None;
        node.next_sibling = None;
        node.prev_sibling = None;

        if let Some(parent) = parent_id.and_then(|id| nodes.get_mut(id.value)) {
            if parent.first_child == Some(*id) {
                parent.first_child = next_sibling_id;
            }

            if parent.last_child == Some(*id) {
                parent.last_child = prev_sibling_id;
            }
        }

        if let Some(prev_sibling) = prev_sibling_id.and_then(|id| nodes.get_mut(id.value)) {
            prev_sibling.next_sibling = next_sibling_id;
        }

        if let Some(next_sibling) = next_sibling_id.and_then(|id| nodes.get_mut(id.value)) {
            next_sibling.prev_sibling = prev_sibling_id;
        }
    }

    /// Changes the parent of children nodes of a node.
    pub fn reparent_children_of(
        nodes: &mut [TreeNode],
        id: &NodeId,
        new_parent_id: Option<NodeId>,
    ) {
        let Some(node) = nodes.get_mut(id.value) else {
            return;
        };

        let first_child_id = node.first_child;
        let last_child_id = node.last_child;
        node.first_child = None;
        node.last_child = None;

        if let Some(new_parent) = new_parent_id.and_then(|id| nodes.get_mut(id.value)) {
            new_parent.first_child = first_child_id;
            new_parent.last_child = last_child_id;
        }
        let mut next_child_id = first_child_id;
        while let Some(child_id) = next_child_id {
            if let Some(child) = nodes.get_mut(child_id.value) {
                child.parent = new_parent_id;
                next_child_id = child.next_sibling;
            }
        }
    }

    /// Parses given text and sets its contents to the selected node.
    /// This operation replaces any contents of the selected node with the given text.
    pub fn set_text<T>(nodes: &mut Vec<TreeNode>, id: &NodeId, text: T)
    where
        T: Into<StrTendril>,
    {
        let Some(node) = nodes.get_mut(id.value) else {
            return;
        };
        match node.data {
            NodeData::Element(_) => {
                let text_node_id = Self::create_node(
                    nodes,
                    NodeData::Text {
                        contents: wrap_tendril(text.into()),
                    },
                );
                Self::reparent_children_of(nodes, id, None);
                Self::append_child_of(nodes, id, &text_node_id);
            }
            NodeData::Text { ref mut contents } => {
                *contents = wrap_tendril(text.into());
            }
            _ => (),
        }
    }
}

impl TreeNodeOps {
    /// Adds nodes from another tree to the current tree
    pub(crate) fn merge(nodes: &mut Vec<TreeNode>, mut other_nodes: Vec<TreeNode>) {
        // `parse_fragment` returns a document that looks like:
        // <:root>                 id -> 0
        //     <html>              id -> 2
        //        things we need.
        //     </html>
        // <:root>
        // <body></body>           id -> 1

        let offset = nodes.len();
        let id_offset = offset - SKIP_NODES_ON_MERGE;

        for node in other_nodes.iter_mut().skip(SKIP_NODES_ON_MERGE) {
            node.adjust(id_offset);
        }
        nodes.extend(other_nodes.into_iter().skip(SKIP_NODES_ON_MERGE));
    }

    /// Adds nodes from another tree to the current tree and
    /// then applies a function to the first non-document merged node.
    pub(crate) fn merge_with_fn<F>(nodes: &mut Vec<TreeNode>, other: Tree, f: F)
    where
        F: FnOnce(&mut Vec<TreeNode>, NodeId),
    {
        let anchor = nodes.len();
        if anchor < SKIP_NODES_ON_MERGE {
            return;
        }
        let other_nodes = other.nodes.into_inner();
        Self::merge(nodes, other_nodes);
        let new_node_id = NodeId::new(anchor);
        f(nodes, new_node_id);
    }
}