Skip to content

Commit

Permalink
Merge pull request #75 from niklak/feature/mini-selector-revise-2
Browse files Browse the repository at this point in the history
- renamed `snap_is` -> `mini_is`, `snap_match` to `mini_match`
  • Loading branch information
niklak authored Feb 27, 2025
2 parents 67b5bd4 + be8eff7 commit e324e5e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All notable changes to the `dom_query` crate will be documented in this file.
### Added
- Implemented the `markdown` feature, which allows serializing a `Document` or `NodeRef` into Markdown text using the `md()` method.
- Implemented the `mini_selector` feature, providing a lightweight and faster alternative for element matching with limited CSS selector support.
This includes `NodeRef` additional methods: `find_descendants`, `try_find_descendants`, `snap_is`, and `snap_match`.
This includes `NodeRef` additional methods: `find_descendants`, `try_find_descendants`, `mini_is`, and `mini_match`.

## [0.14.0] - 2025-02-16

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ assert_eq!(got.as_ref(), expected);
This allows `NodeData` and all ascending structures, including `Document`, to implement the `Send` trait;
- `markdown` — optional, enables the `Document::md` and `NodeRef::md` methods, allowing serialization of a document or node to `Markdown` text.
- `mini_selector` — optional, provides a lightweight and faster alternative for element matching with limited CSS selector support.
This includes additional `NodeRef` methods: `find_descendants`, `try_find_descendants`, `snap_is`, and `snap_match`.
This includes additional `NodeRef` methods: `find_descendants`, `try_find_descendants`, `mini_is`, and `mini_match`.

## Possible issues
* [wasm32 compilation](https://niklak.github.io/dom_query_by_example/WASM32-compilation.html)
Expand Down
46 changes: 23 additions & 23 deletions src/mini_selector/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn find_descendant_ids<'a>(
path: &'a str,
) -> Result<Vec<NodeId>, nom::Err<nom::error::Error<&'a str>>> {
// Start with the provided node ID as the initial working set
let mut tops = vec![id];
let mut stack = vec![id];
// Final collection of matching node IDs
let mut res = vec![];

Expand All @@ -22,7 +22,7 @@ pub fn find_descendant_ids<'a>(
let is_last = selectors.len() - 1 == idx;

// Process all current top-level nodes before moving to the next selector
while let Some(id) = tops.pop() {
while let Some(id) = stack.pop() {
// Collect immediate children that are elements (for potential matching)
let mut ops: Vec<NodeId> = child_nodes(Ref::clone(nodes), &id, is_last)
.filter(|id| nodes[id.value].is_element())
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn find_descendant_ids<'a>(
if is_last {
res.extend(candidates);
} else {
tops.extend(candidates);
stack.extend(candidates);

// Continue with the next selector since we've updated the tops
continue 'work_loop;
Expand Down Expand Up @@ -138,13 +138,13 @@ impl NodeRef<'_> {
/// # Returns
///
/// `true` if this node matches the given CSS selector, `false` otherwise.
pub fn snap_is(&self, css_sel: &str) -> bool {
MiniSelector::new(css_sel).map_or(false, |sel| self.snap_match(&sel))
pub fn mini_is(&self, css_sel: &str) -> bool {
MiniSelector::new(css_sel).map_or(false, |sel| self.mini_match(&sel))
}

/// Checks if this node matches the given CSS selector.
///
/// This method uses the given `MiniSelector` for matching elements.
/// This method uses the given [`MiniSelector`] for matching elements.
/// It is faster than [`NodeRef::is_match`] method but has limitations.
///
/// # Arguments
Expand All @@ -154,7 +154,7 @@ impl NodeRef<'_> {
/// # Returns
///
/// `true` if this node matches the given CSS selector, `false` otherwise.
pub fn snap_match(&self, matcher: &MiniSelector) -> bool {
pub fn mini_match(&self, matcher: &MiniSelector) -> bool {
matcher.match_node(self)
}
}
Expand Down Expand Up @@ -232,26 +232,26 @@ mod tests {
let doc = Document::fragment(contents);
let link_sel = doc.select_single(r#"a[id]"#);
let link_node = link_sel.nodes().first().unwrap();
assert!(!link_node.snap_is(r#"a[href="//example.com"]"#));
assert!(link_node.snap_is(r#"a[href^="https://"]"#));
assert!(link_node.snap_is(r#"a[href$="/"]"#));
assert!(link_node.snap_is(r#"a[href*="example.com"]"#));
assert!(link_node.snap_is(r#"a[id|="main"]"#));
assert!(link_node.snap_is(r#"a[class~="border"]"#));
assert!(link_node.snap_is(r#"[class *= "blue-400 bord"]"#));
assert!(!link_node.snap_is(r#"[class *= "glue-400 bord"]"#));
assert!(link_node.snap_is(r#"#main-link"#));
assert!(!link_node.snap_is(r#"#link"#));
assert!(!link_node.snap_is(r#"a[target="_blank"]"#));
assert!(link_node.snap_is(r#"a[target]"#));
assert!(!link_node.snap_is(r#"a[href^="https://"][href*="examplxe"][href$="/"]"#));
assert!(link_node.snap_is(r#"a[href^="https://"][href*="example"][href$="/"]"#));
assert!(!link_node.mini_is(r#"a[href="//example.com"]"#));
assert!(link_node.mini_is(r#"a[href^="https://"]"#));
assert!(link_node.mini_is(r#"a[href$="/"]"#));
assert!(link_node.mini_is(r#"a[href*="example.com"]"#));
assert!(link_node.mini_is(r#"a[id|="main"]"#));
assert!(link_node.mini_is(r#"a[class~="border"]"#));
assert!(link_node.mini_is(r#"[class *= "blue-400 bord"]"#));
assert!(!link_node.mini_is(r#"[class *= "glue-400 bord"]"#));
assert!(link_node.mini_is(r#"#main-link"#));
assert!(!link_node.mini_is(r#"#link"#));
assert!(!link_node.mini_is(r#"a[target="_blank"]"#));
assert!(link_node.mini_is(r#"a[target]"#));
assert!(!link_node.mini_is(r#"a[href^="https://"][href*="examplxe"][href$="/"]"#));
assert!(link_node.mini_is(r#"a[href^="https://"][href*="example"][href$="/"]"#));

let another_sel = doc.select_single(r#"a.other-link"#);
let another_link_node = another_sel.nodes().first().unwrap();
let text_node = another_link_node.first_child().unwrap();

assert!(!another_link_node.snap_is(r#"#main-link"#));
assert!(!text_node.snap_is(r#"#main-link"#));
assert!(!another_link_node.mini_is(r#"#main-link"#));
assert!(!text_node.mini_is(r#"#main-link"#));
}
}

0 comments on commit e324e5e

Please sign in to comment.