dom_query 0.29.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
use std::fmt;

use bit_set::BitSet;
use cssparser::{CowRcStr, ParseError, ToCss};
use html5ever::{Namespace, ns};
use selectors::context::SelectorCaches;
use selectors::parser::{self, Component, LocalName, SelectorList, SelectorParseErrorKind};
use selectors::{Element, context, matching};

use crate::Tree;
use crate::css::{CssLocalName, CssString};
use crate::node::{DescendantNodes, NodeData, NodeRef, TreeNode};
/// CSS selector.
#[derive(Clone, Debug)]
pub struct Matcher {
    selector_list: SelectorList<InnerSelector>,
    // set when every selector in the list is a bare type selector (`p`, `ul,ol`),
    // so an element matches iff its local name is one of these
    local_names: Option<Vec<LocalName<InnerSelector>>>,
}

impl Matcher {
    /// creates a new CSS matcher.
    ///
    /// # Errors
    /// - [`cssparser::ParseError`] if the selector string is invalid.
    pub fn new(sel: &str) -> Result<Self, ParseError<SelectorParseErrorKind>> {
        let mut parser = cssparser::Parser::new(sel);
        selectors::parser::SelectorList::parse(
            &InnerSelectorParser,
            &mut parser,
            parser::ParseRelative::No,
        )
        .map(|selector_list| Self {
            local_names: type_selector_names(&selector_list),
            selector_list,
        })
    }

    /// Checks if an element matches Matcher's selection.
    pub fn match_element<E>(&self, element: &E) -> bool
    where
        E: Element<Impl = InnerSelector>,
    {
        let mut caches = context::SelectorCaches::default();
        self.match_element_with_caches(element, &mut caches)
    }

    /// Checks if an element matches Matcher's selection.
    pub fn match_element_with_caches<E>(&self, element: &E, caches: &mut SelectorCaches) -> bool
    where
        E: Element<Impl = InnerSelector>,
    {
        if let Some(names) = &self.local_names {
            return names.iter().any(|n| {
                element.has_local_name(matching::select_name(element, &n.name, &n.lower_name))
            });
        }
        let mut ctx = get_matching_context(caches);
        matching::matches_selector_list(&self.selector_list, element, &mut ctx)
    }

    /// The type-selector fast path on a tree node: `None` when the list needs the full matcher.
    fn match_tree_node(&self, node: &TreeNode) -> Option<bool> {
        let names = self.local_names.as_ref()?;
        let NodeData::Element(el) = &node.data else {
            return Some(false);
        };
        let is_html = el.name.ns == ns!(html);
        // the same rule as `selectors::matching::select_name`
        Some(names.iter().any(|n| {
            let name = if is_html || n.name == n.lower_name {
                &n.lower_name
            } else {
                &n.name
            };
            el.name.local == **name
        }))
    }
}

fn type_selector_names(
    list: &SelectorList<InnerSelector>,
) -> Option<Vec<LocalName<InnerSelector>>> {
    list.slice()
        .iter()
        .map(
            |selector| match selector.iter_raw_match_order().as_slice() {
                [Component::LocalName(name)] => Some(name.clone()),
                _ => None,
            },
        )
        .collect()
}

pub struct DescendantMatches<'a, 'b> {
    iter: DescendantNodes<'a>,
    matcher: &'b Matcher,
    caches: SelectorCaches,
    tree: &'a Tree,
}

impl<'a, 'b> DescendantMatches<'a, 'b> {
    pub fn new(root_node: NodeRef<'a>, matcher: &'b Matcher) -> Self {
        // Optimized for single-root node scenario - no duplicate checking needed
        let tree = root_node.tree;
        Self {
            iter: tree.descendant_ids_of_it(&root_node.id),
            matcher,
            caches: SelectorCaches::default(),
            tree,
        }
    }
}

impl<'a> Iterator for DescendantMatches<'a, '_> {
    type Item = NodeRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(node_id) = self.iter.next() {
            let Some(tree_node) = self.iter.node(node_id) else {
                continue;
            };
            let node = NodeRef::new(node_id, self.tree);
            let matched = self.matcher.match_tree_node(tree_node).unwrap_or_else(|| {
                tree_node.is_element()
                    && self
                        .matcher
                        .match_element_with_caches(&node, &mut self.caches)
            });
            if matched {
                return Some(node);
            }
        }
        None
    }
}

pub struct Matches<'a, 'b> {
    nodes: Vec<NodeRef<'a>>,
    matcher: &'b Matcher,
    seen: BitSet,
    caches: SelectorCaches,
}

impl<'a, 'b> Matches<'a, 'b> {
    pub fn new<I: Iterator<Item = NodeRef<'a>>>(root_nodes: I, matcher: &'b Matcher) -> Self {
        // Used for multiple root nodes where duplicate checking is necessary
        let nodes: Vec<NodeRef<'a>> = root_nodes
            .flat_map(|node| node.children_it(true).filter(|n| n.is_element()))
            .collect();
        // every visited node is marked, so size the set for the whole tree once
        let tree_len = nodes.first().map_or(0, |n| n.tree.len() + 1);

        Self {
            nodes,
            matcher,
            seen: BitSet::with_capacity(tree_len),
            caches: SelectorCaches::default(),
        }
    }
}

impl<'a> Iterator for Matches<'a, '_> {
    type Item = NodeRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(node) = self.nodes.pop() {
            // a node reached again through a nested root was already visited with its subtree
            if !self.seen.insert(node.id.value()) {
                continue;
            }
            self.nodes
                .extend(node.children_it(true).filter(|n| n.is_element()));

            if self
                .matcher
                .match_element_with_caches(&node, &mut self.caches)
            {
                return Some(node);
            }
        }
        None
    }
}

pub(crate) struct InnerSelectorParser;

impl<'i> parser::Parser<'i> for InnerSelectorParser {
    type Impl = InnerSelector;
    type Error = parser::SelectorParseErrorKind;

    fn parse_is_and_where(&self) -> bool {
        true
    }

    fn parse_has(&self) -> bool {
        true
    }

    fn parse_non_ts_pseudo_class(
        &self,
        name: CowRcStr<'i>,
    ) -> Result<NonTSPseudoClass, ParseError<Self::Error>> {
        if name.eq_ignore_ascii_case("any-link") {
            Ok(NonTSPseudoClass::AnyLink)
        } else if name.eq_ignore_ascii_case("link") {
            Ok(NonTSPseudoClass::Link)
        } else if name.eq_ignore_ascii_case("visited") {
            Ok(NonTSPseudoClass::Visited)
        } else if name.eq_ignore_ascii_case("active") {
            Ok(NonTSPseudoClass::Active)
        } else if name.eq_ignore_ascii_case("focus") {
            Ok(NonTSPseudoClass::Focus)
        } else if name.eq_ignore_ascii_case("hover") {
            Ok(NonTSPseudoClass::Hover)
        } else if name.eq_ignore_ascii_case("enabled") {
            Ok(NonTSPseudoClass::Enabled)
        } else if name.eq_ignore_ascii_case("disabled") {
            Ok(NonTSPseudoClass::Disabled)
        } else if name.eq_ignore_ascii_case("checked") {
            Ok(NonTSPseudoClass::Checked)
        } else if name.eq_ignore_ascii_case("indeterminate") {
            Ok(NonTSPseudoClass::Indeterminate)
        } else if name.eq_ignore_ascii_case("only-text") {
            Ok(NonTSPseudoClass::OnlyText)
        } else {
            Err(ParseError::custom(
                SelectorParseErrorKind::UnsupportedPseudoClassOrElement,
            ))
        }
    }

    fn parse_non_ts_functional_pseudo_class(
        &self,
        name: CowRcStr<'i>,
        parser: &mut cssparser::Parser<'i>,
        _after_part: bool,
    ) -> Result<<Self::Impl as parser::SelectorImpl>::NonTSPseudoClass, ParseError<Self::Error>>
    {
        if name.eq_ignore_ascii_case("has-text") {
            let s = parser.expect_string()?.as_ref();
            Ok(NonTSPseudoClass::HasText(CssString::from(s)))
        } else if name.eq_ignore_ascii_case("contains") {
            {
                let s = parser.expect_string()?.as_ref();
                Ok(NonTSPseudoClass::Contains(CssString::from(s)))
            }
        } else {
            Err(ParseError::custom(
                SelectorParseErrorKind::UnsupportedPseudoClassOrElement,
            ))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InnerSelector;

impl parser::SelectorImpl for InnerSelector {
    type ExtraMatchingData<'a> = ();
    type AttrValue = CssString;
    type Identifier = CssLocalName;
    type LocalName = CssLocalName;
    type NamespaceUrl = Namespace;
    type NamespacePrefix = CssLocalName;
    type BorrowedLocalName = CssLocalName;
    type BorrowedNamespaceUrl = Namespace;

    type NonTSPseudoClass = NonTSPseudoClass;
    type PseudoElement = PseudoElement;
}

/// Non-tree-structural pseudo-classes.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum NonTSPseudoClass {
    /// `:any-link` means one of the a, area, or link elements that has an href attribute.
    AnyLink,
    /// `:link` means same as `:any-link`
    Link,
    Visited,
    Active,
    Focus,
    Hover,
    Enabled,
    Disabled,
    Checked,
    Indeterminate,
    /// `:only-text` pseudo-class allows selecting a node with no child elements except a single **text** child node.
    OnlyText,
    /// `:has-text` pseudo-class represents a selection for the element or one of its descendant element that contains the specified text.
    HasText(CssString),
    /// `:contains` pseudo-class represents a selection for the element that contains the specified text (it's own text and text of all his descendant elements).
    Contains(CssString),
}

impl ToCss for NonTSPseudoClass {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
    where
        W: fmt::Write,
    {
        match self {
            Self::AnyLink => dest.write_str(":any-link"),
            Self::Link => dest.write_str(":link"),
            Self::Visited => dest.write_str(":visited"),
            Self::Active => dest.write_str(":active"),
            Self::Focus => dest.write_str(":focus"),
            Self::Hover => dest.write_str(":hover"),
            Self::Enabled => dest.write_str(":enabled"),
            Self::Disabled => dest.write_str(":disabled"),
            Self::Checked => dest.write_str(":checked"),
            Self::Indeterminate => dest.write_str(":indeterminate"),
            Self::OnlyText => dest.write_str(":only-text"),
            Self::HasText(s) => {
                dest.write_str(":has-text(")?;
                s.to_css(dest)?;
                dest.write_str(")")
            }
            Self::Contains(s) => {
                dest.write_str(":contains(")?;
                s.to_css(dest)?;
                dest.write_str(")")
            }
        }
    }
}

impl parser::NonTSPseudoClass for NonTSPseudoClass {
    fn is_active_or_hover(&self) -> bool {
        false
    }

    fn is_user_action_state(&self) -> bool {
        false
    }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub struct PseudoElement;

impl ToCss for PseudoElement {
    fn to_css<W>(&self, dest: &mut W) -> fmt::Result
    where
        W: fmt::Write,
    {
        dest.write_str("")
    }
}

impl parser::PseudoElement for PseudoElement {
    fn accepts_state_pseudo_classes(&self) -> bool {
        false
    }

    fn valid_after_slotted(&self) -> bool {
        false
    }
}

fn get_matching_context(
    caches: &mut context::SelectorCaches,
) -> matching::MatchingContext<'_, InnerSelector> {
    matching::MatchingContext::new(
        matching::MatchingMode::Normal,
        None,
        caches,
        matching::QuirksMode::NoQuirks,
        matching::NeedsSelectorFlags::No,
        context::MatchingForInvalidation::No,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Document, NodeId};

    #[test]
    fn type_selector_fast_path_agrees_with_the_selectors_crate() {
        let doc = Document::from(
            r##"<div class="a"><p id="x">t<a href="#">l</a></p><UL><li>1</li><li>2</li></UL>
            <svg><foreignObject><p>f</p></foreignObject><circle/></svg>
            <TABLE><tr><td>c</td><Td>d</Td></tr></TABLE><img><iframe></iframe></div><p>outside</p>"##,
        );
        let selectors = [
            "p",
            "P",
            "ul,ol",
            "img,object,embed,iframe",
            "circle",
            "foreignObject",
            "FOREIGNOBJECT",
            "svg, circle",
            "*",
            "p.a",
            "div p",
            "p:first-child",
            "div, p.a",
            ":not(p)",
        ];
        for sel in selectors {
            let fast = Matcher::new(sel).unwrap();
            let slow = Matcher {
                selector_list: fast.selector_list.clone(),
                local_names: None,
            };
            let mut caches = SelectorCaches::default();
            let walked: Vec<NodeId> = DescendantMatches::new(doc.root(), &fast)
                .map(|n| n.id)
                .collect();
            let expected: Vec<NodeId> = DescendantMatches::new(doc.root(), &slow)
                .map(|n| n.id)
                .collect();
            assert_eq!(walked, expected, "{sel}");
            // the full matcher must only ever see elements: text nodes would match `:not(p)`
            assert!(
                DescendantMatches::new(doc.root(), &slow).all(|n| n.is_element()),
                "{sel}"
            );
            for node in doc.root().descendants_it().filter(NodeRef::is_element) {
                assert_eq!(
                    fast.match_element_with_caches(&node, &mut caches),
                    slow.match_element_with_caches(&node, &mut caches),
                    "{sel} on {:?}",
                    node.node_name()
                );
            }
        }
        for sel in ["p", "ul,ol", "foreignObject", "svg, circle"] {
            assert!(Matcher::new(sel).unwrap().local_names.is_some(), "{sel}");
        }
        for sel in ["*", "p.a", "div p", "p:first-child", "#x", "div, p.a"] {
            assert!(Matcher::new(sel).unwrap().local_names.is_none(), "{sel}");
        }
    }
}