Expand description
Parsec: an HTML parser that follows the WHATWG HTML Standard, with CSS selectors and a tree you can change and write back out, and the other standards a page needs beside it.
The parser is at the top of this crate, and each standard is a module behind a cargo feature:
| Module | Feature | Default |
|---|---|---|
encoding | encoding | on |
url | url | on |
xpath | xpath | on |
stylesheet | css | off |
css is the selector engine, which is always here; stylesheet is
CSS Syntax and the cascade, which a scraper rarely needs.
use parsec::css::SelectorList;
let doc = parsec::parse("<ul><li class=hit>one<li>two</ul>", false);
let hits = SelectorList::parse("li.hit").unwrap().select(&doc, doc.root());
assert_eq!(doc.text(hits[0]), "one");Modules§
- css
- CSS selectors, as Selectors Level 4 defines them and browsers match them in a static page: parsed once, matched right to left.
- encoding
- Bytes into Parsec.
parse_bytesfinds a page’s encoding the way the HTML Standard says a browser does, decodes it and parses it.ByteParserdoes the same with the bytes as they arrive. - stylesheet
- CSS Syntax Level 3: the tokenizer, the parser and its entry points, the
An+B and
<urange>microsyntaxes, and serialization back to CSS, as the W3C’s current editor’s draft defines them, nesting included. - tokenizer
- The tokenizer, as section 13.2.5 of the HTML Standard describes it.
- url
- The WHATWG URL Standard: URLs with every getter and setter the
URLinterface has,URLSearchParams, and the host, IDNA and Punycode algorithms under them. - xpath
- XPath 1.0 over a parsec-html
Document, written from the W3C recommendation: every axis, node test and function of the core library, with the number, string and boolean rules of sections 3 and 4.
Structs§
- Document
- A parsed HTML document, or a fragment.
- Options
- How to parse.
- Parser
- Parses a document as it arrives, a piece at a time, into the tree
parse_withwould build from the whole: the same for any way of cutting the input up, with the same spans and errors when asked for. - Serialize
Options - How
Document::serialize_withwrites a node. The default is the HTML Standard’s serialization, byte for byte whatDocument::outer_htmlgives. Most fields are html5lib’s serializer options, under html5lib’s names. - Shadow
Root - A shadow root’s settings, as
attachShadowtakes them and a declarative<template shadowrootmode>sets them. - Syntax
Error - A parse error the HTML Standard defines, from
Document::errors. The parser recovered from it, as the spec says to; it’s for a linter or an editor to report.
Enums§
- AttrNs
- A namespaced attribute’s namespace, from the spec’s “adjust foreign
attributes” table;
Nonefor every ordinary attribute. - Edit
Error - Why an edit was refused.
- Kind
- What a node is, as
Document::kindreports it. - Ns
- Parse
Error - Why a parse returned no document.
- Quirks
- Shadow
Root Mode - Whether a shadow root’s host lets scripts reach it, from
shadowrootmodeorattachShadow’smode. - Slot
Assignment - How a shadow root’s slots get their nodes: by the
slotattribute of the host’s children, or only asDocument::assignsays.
Constants§
- MAX_
INPUT - The most input a document can hold. Spans keep offsets in 30 bits, and the parse buffer, which only holds copies of what the input says, stays well under that.
Functions§
- is_
html_ element - Whether
name(in any case) names an element of HTML, current or obsolete, or the root of SVG or MathML:div,marquee,svg; notrss. - named_
reference - The character(s) HTML’s named reference
namestands for, without the&and with or without the;as the table has it:nbsp;gives U+00A0. - parse
- Parses a whole document. A leading byte order mark (U+FEFF) is dropped, as decoding the page would have.
- parse_
fragment - Parses
htmlas the contents of an element namedcontextin namespacens, asinnerHTMLdoes. - parse_
fragment_ with - Parses
htmlasparse_fragmentdoes, within a size limit and an optional budget, asparse_withdoes for a whole document. - parse_
with - Parses a whole document, within a size limit and an optional budget.
- tag_
number - The number
Document::known_taggives elements namedname(lowercase), if the parser knows the tag.
Type Aliases§
- NodeId
- A node’s index in its document; the document itself is node 0.