simdsel 0.1.1

A blazing fast HTML selector powered by SIMD.
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
//! The parsel-shaped [`Selector`].

use std::borrow::Cow;
use std::rc::Rc;

use simdhtml::document::Document;
use simdhtml::document::id::NodeId;
use simdhtml::parse::{EncodingMode, ParseOptions};

use crate::css::ast::SelectorList;
use crate::css::cache::QueryCache;
use crate::css::eval::root_element;
use crate::css::parser::parse_selector;
use crate::css::select::{Count, Projection, extract, select_as_within_cache, select_within_cache};
use crate::error::Result;
use crate::record::{MatchKind, MatchRecord};
use crate::xpath::XPath;
use crate::xpath::value::{NodeRef, RefKind, Value};

/// One result of a selector chain: an element/text/attribute match or a
/// scalar XPath string.
#[derive(Clone, Debug)]
enum Item {
    Record(MatchRecord),
    Text(String),
}

/// An owned document or a result set, like parsel's `Selector`/`SelectorList`.
///
/// ```
/// use simdsel::Selector;
///
/// let sel = Selector::from_text(r#"<div class=item><b>hi</b></div>"#)?;
/// assert_eq!(sel.css(".item b::text")?.get(), Some("hi".into()));
/// assert_eq!(sel.xpath("//b/text()")?.get(), Some("hi".into()));
/// # Ok::<(), simdsel::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct Selector<'a> {
    doc: Rc<Document<'a>>,
    roots: Vec<NodeId>,
    items: Vec<Item>,
    /// Derived query data (masks, child index) shared by every query from this
    /// selector.  A function of the document, not of `roots`, so it stays valid
    /// as queries narrow; built on demand.
    cache: Rc<QueryCache>,
}

impl<'a> Selector<'a> {
    /// Wraps an already parsed document and selects its root element.
    ///
    /// ```
    /// use simdsel::{Document, Selector};
    ///
    /// let doc = Document::parse(b"<p>hi</p>")?;
    /// let sel = Selector::new(doc);
    /// assert_eq!(sel.css("p::text")?.get(), Some("hi".into()));
    /// # Ok::<(), simdsel::Error>(())
    /// ```
    pub fn new(doc: Document<'a>) -> Self {
        let root = Item::Record(document_record(&doc));
        Self {
            doc: Rc::new(doc),
            roots: vec![NodeId::ROOT],
            items: vec![root],
            cache: Rc::new(QueryCache::root()),
        }
    }

    /// Parses `input` as bytes (parsel's `body=`) and selects from its root.
    /// The charset is sniffed from a BOM or `<meta charset>`.
    pub fn from_bytes(input: &'a [u8]) -> Result<Self> {
        Ok(Self::new(Document::parse(input)?))
    }

    /// [`Self::from_bytes`] with explicit [`ParseOptions`].
    pub fn from_bytes_with(input: &'a [u8], options: ParseOptions) -> Result<Self> {
        Ok(Self::new(Document::parse_with(input, options)?))
    }

    /// Parses already decoded `text` (parsel's `text=`) and selects from its
    /// root. No BOM or `<meta charset>` sniffing: a `&str` is already text.
    pub fn from_text(text: &'a str) -> Result<Self> {
        Self::from_bytes_with(
            text.as_bytes(),
            ParseOptions {
                encoding: EncodingMode::Utf8,
                ..Default::default()
            },
        )
    }

    /// Splits a result set into one single-result selector per item; the
    /// derived selectors share the document and the query cache.
    ///
    /// ```
    /// use simdsel::Selector;
    ///
    /// let sel = Selector::from_text("<b>a</b><b>b</b>")?;
    /// let items = sel.css("b")?.split();
    /// let texts = items.iter().filter_map(|b| b.text()).collect::<Vec<_>>();
    /// assert_eq!(texts, vec!["a", "b"]);
    /// # Ok::<(), simdsel::Error>(())
    /// ```
    pub fn split(&self) -> Vec<Self> {
        self.items
            .iter()
            .map(|item| Self {
                doc: Rc::clone(&self.doc),
                roots: match item {
                    Item::Record(record) => vec![record.node],
                    Item::Text(_) => Vec::new(),
                },
                items: vec![item.clone()],
                cache: Rc::clone(&self.cache),
            })
            .collect()
    }

    /// The document this selector owns.
    pub fn doc(&self) -> &Document<'a> {
        &self.doc
    }

    /// Current search roots.
    pub fn roots(&self) -> &[NodeId] {
        &self.roots
    }

    /// Whether every result is a string: an XPath scalar or `::text`/`::attr`
    /// output rather than an element.
    pub fn is_text(&self) -> bool {
        !self.items.is_empty()
            && self.items.iter().all(|item| match item {
                Item::Text(_) => true,
                Item::Record(record) => record.kind != MatchKind::Element,
            })
    }

    /// Number of results.
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Whether the selection is empty.
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    /// Per-thread compiled-selector intern table: `css*` takes selector text on
    /// every call, and parsing it costs ~11% of a warm query.  Bounded, so a
    /// query-generating crawler clears it instead of growing it.
    fn compiled(query: &str) -> Result<Rc<SelectorList>> {
        use std::cell::RefCell;
        use std::collections::HashMap;

        thread_local! {
            static CACHE: RefCell<HashMap<Box<str>, Rc<SelectorList>>> = RefCell::new(HashMap::new());
        }
        // A few hundred selectors bound the memory.
        const MAX_ENTRIES: usize = 256;

        CACHE.with(|cache| {
            if let Some(parsed) = cache.borrow().get(query) {
                return Ok(Rc::clone(parsed));
            }
            let parsed = Rc::new(parse_selector(query)?);
            let mut cache = cache.borrow_mut();
            if cache.len() >= MAX_ENTRIES {
                cache.clear();
            }
            cache.insert(query.into(), Rc::clone(&parsed));
            Ok(parsed)
        })
    }

    /// Compiles and evaluates a CSS selector inside the current results.
    ///
    /// The current results are not part of the search (use `:scope` to include
    /// them). `::text` and `::attr(name)` select text and attribute values.
    pub fn css(&self, query: &str) -> Result<Self> {
        let parsed = Self::compiled(query)?;
        let records = select_within_cache(&self.doc, &parsed, &self.roots, &self.cache)?;
        let roots = records.iter().map(|record| record.node).collect::<Vec<_>>();
        Ok(Self {
            doc: Rc::clone(&self.doc),
            roots,
            items: records.into_iter().map(Item::Record).collect(),
            cache: Rc::clone(&self.cache),
        })
    }

    /// Evaluates an XPath 1.0 expression at every element result, flattening the
    /// node-sets into parsel-shaped items: elements stay elements, text and
    /// attribute nodes become strings, scalars become one string.
    ///
    /// Text results cannot be queried further, as in parsel.
    pub fn xpath(&self, expr: &str) -> Result<Self> {
        let path = XPath::new(expr)?;
        let mut items = Vec::new();
        let mut roots = Vec::new();
        for item in &self.items {
            let Item::Record(record) = item else {
                continue;
            };
            if record.kind != MatchKind::Element {
                continue;
            }
            let context = NodeRef::element(record.node);
            match path.eval_at(&self.doc, context)? {
                Value::Nodes(nodes) => {
                    for node in nodes {
                        match node.kind() {
                            RefKind::Element => {
                                let record = element_record(&self.doc, node.node_id());
                                roots.push(record.node);
                                items.push(Item::Record(record));
                            }
                            RefKind::Text | RefKind::Attribute => {
                                items.push(Item::Text(node.string_value(&self.doc).into_owned()))
                            }
                            // parsel never sees the document node (lxml has none).
                            RefKind::Document => {}
                        }
                    }
                }
                Value::Str(text) => items.push(Item::Text(text)),
                Value::Number(number) => items.push(Item::Text(format_number(number))),
                Value::Bool(value) => {
                    items.push(Item::Text(if value { "1" } else { "0" }.to_string()));
                }
            }
        }
        Ok(Self {
            doc: Rc::clone(&self.doc),
            roots,
            items,
            cache: Rc::clone(&self.cache),
        })
    }

    /// Evaluates CSS and projects the result: `Count` avoids materializing
    /// records, `Texts` borrows from the document input.
    pub fn css_as<'b, P: Projection>(&'b self, query: &str) -> Result<P::Output<'b>> {
        let parsed = Self::compiled(query)?;
        select_as_within_cache::<P>(&self.doc, &parsed, &self.roots, &self.cache)
    }

    /// Counts CSS matches without materializing records.
    pub fn css_count(&self, query: &str) -> Result<usize> {
        self.css_as::<Count>(query)
    }

    /// First result's extracted string.  Element results are the original source
    /// slice, not a re-serialization; text and attribute results are decoded.
    pub fn get(&self) -> Option<Cow<'_, str>> {
        self.items.first().map(|item| self.item_value(item))
    }

    /// All result strings.
    pub fn getall(&self) -> Vec<Cow<'_, str>> {
        self.items
            .iter()
            .map(|item| self.item_value(item))
            .collect()
    }

    /// First result's text content.
    pub fn text(&self) -> Option<Cow<'_, str>> {
        match self.items.first()? {
            Item::Record(record) => record_text(&self.doc, record),
            Item::Text(text) => Some(Cow::Borrowed(text)),
        }
    }

    /// All result text contents.
    pub fn texts(&self) -> Vec<Cow<'_, str>> {
        self.items
            .iter()
            .filter_map(|item| match item {
                Item::Record(record) => record_text(&self.doc, record),
                Item::Text(text) => Some(Cow::Borrowed(text.as_str())),
            })
            .collect()
    }

    /// Attributes of the first element result.
    pub fn attrib(&self) -> Option<Vec<(String, String)>> {
        match self.items.first()? {
            Item::Record(record) => record_attrib(&self.doc, record),
            Item::Text(_) => None,
        }
    }

    fn item_value<'s>(&'s self, item: &'s Item) -> Cow<'s, str> {
        match item {
            Item::Record(record) => extract(&self.doc, record),
            Item::Text(text) => Cow::Borrowed(text),
        }
    }
}

impl Selector<'static> {
    /// Parses an owned buffer and selects from its root: the bytes move into the
    /// document and the selector carries no lifetime.
    ///
    /// ```
    /// use simdsel::Selector;
    ///
    /// let body = b"<p>hi</p>".to_vec();
    /// let sel = Selector::from_vec(body)?;
    /// assert_eq!(sel.css("p::text")?.get(), Some("hi".into()));
    /// # Ok::<(), simdsel::Error>(())
    /// ```
    pub fn from_vec(input: Vec<u8>) -> Result<Self> {
        Ok(Self::new(Document::parse_owned(input)?))
    }

    /// [`Self::from_vec`] with explicit [`ParseOptions`].
    pub fn from_vec_with(input: Vec<u8>, options: ParseOptions) -> Result<Self> {
        Ok(Self::new(Document::parse_owned_with(input, options)?))
    }

    /// [`Selector::from_text`] for an owned string: it neither borrows nor
    /// copies, and no charset sniffing runs.
    pub fn from_string(text: String) -> Result<Self> {
        Self::from_vec_with(
            text.into_bytes(),
            ParseOptions {
                encoding: EncodingMode::Utf8,
                ..Default::default()
            },
        )
    }
}

impl<'a> From<Document<'a>> for Selector<'a> {
    fn from(doc: Document<'a>) -> Self {
        Self::new(doc)
    }
}

/// One match record for an element node.
fn element_record(doc: &Document<'_>, id: NodeId) -> MatchRecord {
    let node = doc.node(id);
    MatchRecord {
        node: id,
        kind: MatchKind::Element,
        span: node.span(),
        outer_end: node.outer_end(),
        attr: None,
    }
}

/// The record a document selector starts with: the root element, or the
/// document node when the parse produced no element.
fn document_record(doc: &Document<'_>) -> MatchRecord {
    match root_element(doc) {
        Some(id) => element_record(doc, id),
        None => MatchRecord {
            node: NodeId::ROOT,
            kind: MatchKind::Element,
            span: 0..0,
            outer_end: 0,
            attr: None,
        },
    }
}

/// Python's `repr(float)`, which is what parsel stringifies XPath numbers with.
fn format_number(value: f64) -> String {
    if value.is_nan() {
        return "nan".to_string();
    }
    if value.is_infinite() {
        return if value > 0.0 { "inf" } else { "-inf" }.to_string();
    }
    format!("{value:?}")
}

/// Text content of one match record: subtree text for elements, the decoded
/// node for text records, nothing for attributes.
pub(crate) fn record_text<'a>(doc: &'a Document<'_>, record: &MatchRecord) -> Option<Cow<'a, str>> {
    match record.kind {
        MatchKind::Element => Some(Cow::Owned(crate::access::text_content(doc, record.node))),
        MatchKind::Text => Some(doc.text(record.node)),
        MatchKind::Attr => None,
    }
}

/// Attributes of one element match record, deduplicated first-wins.  `None` for
/// text and attribute records.  Names are lowercased like parsel/lxml: HTML
/// attribute names are case-insensitive.
pub(crate) fn record_attrib(
    doc: &Document<'_>,
    record: &MatchRecord,
) -> Option<Vec<(String, String)>> {
    if record.kind != MatchKind::Element {
        return None;
    }
    let mut out = Vec::new();
    let mut seen: Vec<String> = Vec::new();
    for attr in crate::access::attr_range(doc, record.node) {
        let name = crate::access::attr_name(doc, attr);
        if seen
            .iter()
            .any(|s| s.eq_ignore_ascii_case(std::str::from_utf8(name).unwrap_or("")))
        {
            continue;
        }
        let name = String::from_utf8_lossy(name).to_ascii_lowercase();
        seen.push(name.clone());
        out.push((name, crate::access::attr_value(doc, attr).into_owned()));
    }
    Some(out)
}