node_html_parser/css_select/
mod.rs

1pub mod attributes;
2pub mod compile;
3pub mod convert;
4pub mod general;
5pub mod helpers;
6pub mod legacy;
7pub mod types;
8
9use crate::{css_select::compile::compile_token as compile_token_impl, dom::element::HTMLElement};
10use types::{Adapter, CompiledQuery, Options};
11
12pub fn select_all<'a>(selector: &str, root: &'a HTMLElement) -> Vec<&'a HTMLElement> {
13	legacy::query_selector_all(root, selector)
14}
15
16pub fn select_one<'a>(selector: &str, root: &'a HTMLElement) -> Option<&'a HTMLElement> {
17	select_all(selector, root).into_iter().next()
18}
19
20pub fn is<'a>(elem: &'a HTMLElement, selector: &str, root: &'a HTMLElement) -> bool {
21	// naive: run full selection and pointer compare
22	let ptr = elem as *const HTMLElement;
23	select_all(selector, root)
24		.into_iter()
25		.any(|e| std::ptr::eq(e as *const HTMLElement, ptr))
26}
27
28pub fn prepare_context<'a>(root: &'a HTMLElement) -> Vec<&'a HTMLElement> {
29	vec![root]
30}
31
32// Placeholder compile that currently always returns false predicate; will be replaced
33// by real compileToken + general selector pipeline.
34pub fn compile<'a, A: Adapter>(
35	selector: &str,
36	options: &Options<A>,
37	root: &'a HTMLElement,
38) -> CompiledQuery<'a, A> {
39	compile_token_impl::<A>(selector, options, root)
40}
41
42pub fn compile_token<'a, A: Adapter>(
43	selector: &str,
44	options: &Options<A>,
45	root: &'a HTMLElement,
46) -> CompiledQuery<'a, A> {
47	compile_token_impl::<A>(selector, options, root)
48}
49
50pub fn compile_unsafe<'a, A: Adapter>(
51	selector: &str,
52	options: &Options<A>,
53	root: &'a HTMLElement,
54) -> CompiledQuery<'a, A> {
55	compile_token_impl::<A>(selector, options, root)
56}
57
58// 新实验性内部编译(convert + general),暂不替换对外 API:
59pub fn compile_experimental<'a, A: Adapter + 'a>(
60	selector: &str,
61	_options: &Options<A>,
62	adapter: &'a A,
63) -> CompiledQuery<'a, A> {
64	// 直接调用新管线;调用方需自行提供 adapter 实例 (例如 HtmlAdapter)。
65	crate::css_select::compile::compile_internal_new::<A>(selector, adapter)
66}