pub struct Html<'s> { /* private fields */ }Expand description
解析好的文档。借用源字符串(输入含 \r 或 NUL 时持有规范化后的副本)。
Implementations§
Source§impl<'s> Html<'s>
impl<'s> Html<'s>
Sourcepub fn parse_with(html: &'s str, opts: &ParseOptions<'_>) -> Html<'s>
pub fn parse_with(html: &'s str, opts: &ParseOptions<'_>) -> Html<'s>
按选项解析:关闭脚本、在给定上下文元素里解析片段。
Sourcepub fn parse(html: &'s str) -> Html<'s>
pub fn parse(html: &'s str) -> Html<'s>
解析整个 HTML 文档。永不失败:任何输入都会得到一棵树。
Examples found in repository?
examples/parse_bench.rs (line 30)
6fn main() {
7 let mut args = std::env::args().skip(1);
8 let manifest = args
9 .next()
10 .expect("usage: parse_bench <manifest.jsonl> [rounds]");
11 let rounds: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(5);
12 let pages: Vec<String> = std::fs::read_to_string(&manifest)
13 .unwrap()
14 .lines()
15 .filter_map(|l| {
16 let i = l.find("\"path\"")?;
17 let rest = &l[i + 6..];
18 let s = rest.find('"')? + 1;
19 let e = s + rest[s..].find('"')?;
20 std::fs::read(&rest[s..e]).ok()
21 })
22 .map(|b| String::from_utf8_lossy(&b).into_owned())
23 .collect();
24 let mut best = f64::MAX;
25 let mut nodes = 0;
26 for _ in 0..rounds {
27 let t = Instant::now();
28 nodes = 0;
29 for p in &pages {
30 nodes += std::hint::black_box(pagesift_html::Html::parse(p)).len();
31 }
32 let ms = t.elapsed().as_secs_f64() * 1e3;
33 best = best.min(ms);
34 print!("{ms:.1} ");
35 }
36 println!("| pages={} nodes={nodes} best_ms={best:.1}", pages.len());
37}Sourcepub fn parse_with_scratch(html: &'s str, scratch: &mut ParseScratch) -> Html<'s>
pub fn parse_with_scratch(html: &'s str, scratch: &mut ParseScratch) -> Html<'s>
Parse while reusing node, attribute, and decoded-text allocations from the preceding document handled by this worker.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
节点总数。节点 id 的范围是 0..len(),可以用来开按节点下标索引的数组。
Examples found in repository?
examples/parse_bench.rs (line 30)
6fn main() {
7 let mut args = std::env::args().skip(1);
8 let manifest = args
9 .next()
10 .expect("usage: parse_bench <manifest.jsonl> [rounds]");
11 let rounds: usize = args.next().and_then(|s| s.parse().ok()).unwrap_or(5);
12 let pages: Vec<String> = std::fs::read_to_string(&manifest)
13 .unwrap()
14 .lines()
15 .filter_map(|l| {
16 let i = l.find("\"path\"")?;
17 let rest = &l[i + 6..];
18 let s = rest.find('"')? + 1;
19 let e = s + rest[s..].find('"')?;
20 std::fs::read(&rest[s..e]).ok()
21 })
22 .map(|b| String::from_utf8_lossy(&b).into_owned())
23 .collect();
24 let mut best = f64::MAX;
25 let mut nodes = 0;
26 for _ in 0..rounds {
27 let t = Instant::now();
28 nodes = 0;
29 for p in &pages {
30 nodes += std::hint::black_box(pagesift_html::Html::parse(p)).len();
31 }
32 let ms = t.elapsed().as_secs_f64() * 1e3;
33 best = best.min(ms);
34 print!("{ms:.1} ");
35 }
36 println!("| pages={} nodes={nodes} best_ms={best:.1}", pages.len());
37}pub fn is_empty(&self) -> bool
pub fn get(&self, id: NodeId) -> NodeRef<'_>
Trait Implementations§
Auto Trait Implementations§
impl<'s> Freeze for Html<'s>
impl<'s> RefUnwindSafe for Html<'s>
impl<'s> Send for Html<'s>
impl<'s> Sync for Html<'s>
impl<'s> Unpin for Html<'s>
impl<'s> UnsafeUnpin for Html<'s>
impl<'s> UnwindSafe for Html<'s>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more