#parser #html #tokenizer

no-std htmlparser

Pull-based, zero-allocation HTML parser

6 releases

0.2.1 Nov 3, 2024
0.2.0 Nov 1, 2024
0.1.3 Oct 25, 2024
0.1.1 Jun 27, 2023

#533 in Parser implementations

Download history 13/week @ 2024-08-19 51/week @ 2024-08-26 17/week @ 2024-09-02 59/week @ 2024-09-09 15/week @ 2024-09-16 52/week @ 2024-09-23 71/week @ 2024-09-30 29/week @ 2024-10-07 180/week @ 2024-10-14 237/week @ 2024-10-21 247/week @ 2024-10-28 138/week @ 2024-11-04 13/week @ 2024-11-11 52/week @ 2024-11-18 56/week @ 2024-11-25 40/week @ 2024-12-02

167 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

76KB
1.5K SLoC

htmlparser

Build Status Crates.io Documentation Rust 1.31+

htmlparser is a low-level, pull-based, zero-allocation HTML parser.


Example

for token in htmlparser::Tokenizer::from("<tagname name='value'/>") {
    println!("{:?}", token);
}

Why a new library?

This library is basically a low-level XML tokenizer that preserves the positions of the tokens and is not intended to be used directly.

This library is a copy of xmlparser with some adjustments to parse html.


Benefits

  • All tokens contain StrSpan structs which represent the position of the substring in the original document.
  • Good error processing. All error types contain the position (line:column) where it occurred.
  • No heap allocations.
  • No dependencies.
  • Tiny. ~1400 LOC and ~30KiB in the release build according to cargo-bloat.
  • Supports no_std builds. To use without the standard library, disable the default features.

Limitations

  • Currently, only ENTITY objects are parsed from the DOCTYPE. All others are ignored.
  • No tree structure validation. So an XML like <root><child></root></child> or a string without root element will be parsed without errors. You should check for this manually. On the other hand <a/><a/> will lead to an error.
  • Duplicated attributes is not an error. So XML like <item a="v1" a="v2"/> will be parsed without errors. You should check for this manually.
  • UTF-8 only.

Safety

  • The library must not panic. Any panic is considered a critical bug and should be reported.
  • The library forbids unsafe code.

License

Licensed under either of

at your option.


Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

Features