scrape_core/query/
mod.rs

1//! Query engine for finding elements in the DOM.
2//!
3//! This module provides various ways to query the DOM tree:
4//!
5//! - **CSS Selectors**: Standard CSS selector syntax via the `selectors` crate
6//! - **Filters**: BeautifulSoup-style attribute filtering
7//!
8//! # CSS Selectors
9//!
10//! Use [`find`] and [`find_all`] to query by CSS selector:
11//!
12//! ```rust
13//! use scrape_core::{
14//!     Html5everParser, Parser,
15//!     query::{find, find_all},
16//! };
17//!
18//! let parser = Html5everParser;
19//! let doc = parser.parse("<div class=\"item\"><span>A</span></div>").unwrap();
20//!
21//! // Find first element matching selector
22//! let span = find(&doc, "div.item span").unwrap();
23//!
24//! // Find all matching elements
25//! let items = find_all(&doc, ".item").unwrap();
26//! ```
27//!
28//! # Attribute Filters
29//!
30//! Use [`Filter`] for BeautifulSoup-style queries:
31//!
32//! ```rust
33//! use scrape_core::{
34//!     Html5everParser, Parser,
35//!     query::{Filter, find_by_filter},
36//! };
37//!
38//! let parser = Html5everParser;
39//! let doc = parser.parse("<div class=\"item\" data-id=\"123\">text</div>").unwrap();
40//!
41//! let filter = Filter::new().tag("div").class("item").attr("data-id", "123");
42//!
43//! let results = find_by_filter(&doc, &filter);
44//! ```
45//!
46//! # Supported CSS Selectors
47//!
48//! | Selector | Example | Description |
49//! |----------|---------|-------------|
50//! | Type | `div` | Matches elements by tag name |
51//! | Class | `.foo` | Matches elements with class |
52//! | ID | `#bar` | Matches element by ID |
53//! | Universal | `*` | Matches all elements |
54//! | Attribute | `[href]` | Matches elements with attribute |
55//! | Attribute value | `[type="text"]` | Matches attribute with value |
56//! | Descendant | `div span` | Matches descendants |
57//! | Child | `div > span` | Matches direct children |
58//! | Adjacent sibling | `h1 + p` | Matches adjacent sibling |
59//! | General sibling | `h1 ~ p` | Matches following siblings |
60//! | :first-child | `li:first-child` | First child element |
61//! | :last-child | `li:last-child` | Last child element |
62//! | :nth-child | `li:nth-child(2n)` | Nth child element |
63//! | :empty | `div:empty` | Elements with no children |
64//! | :not() | `div:not(.hidden)` | Negation |
65
66mod error;
67mod filter;
68mod find;
69mod selector;
70
71pub use error::{QueryError, QueryResult};
72pub use filter::{Filter, find_by_filter, find_first_by_filter};
73pub use find::{
74    find, find_all, find_all_with_selector, find_all_within, find_all_within_with_selector,
75    find_with_selector, find_within, find_within_with_selector,
76};
77pub use selector::{
78    ElementWrapper, NonTSPseudoClass, PseudoElement, ScrapeSelector, matches_selector,
79    matches_selector_with_caches, parse_selector,
80};