Skip to main content

Crate parsec

Crate parsec 

Source
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:

ModuleFeatureDefault
encodingencodingon
urlurlon
xpathxpathon
stylesheetcssoff

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_bytes finds a page’s encoding the way the HTML Standard says a browser does, decodes it and parses it. ByteParser does 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 URL interface 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_with would build from the whole: the same for any way of cutting the input up, with the same spans and errors when asked for.
SerializeOptions
How Document::serialize_with writes a node. The default is the HTML Standard’s serialization, byte for byte what Document::outer_html gives. Most fields are html5lib’s serializer options, under html5lib’s names.
ShadowRoot
A shadow root’s settings, as attachShadow takes them and a declarative <template shadowrootmode> sets them.
SyntaxError
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; None for every ordinary attribute.
EditError
Why an edit was refused.
Kind
What a node is, as Document::kind reports it.
Ns
ParseError
Why a parse returned no document.
Quirks
ShadowRootMode
Whether a shadow root’s host lets scripts reach it, from shadowrootmode or attachShadow’s mode.
SlotAssignment
How a shadow root’s slots get their nodes: by the slot attribute of the host’s children, or only as Document::assign says.

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; not rss.
named_reference
The character(s) HTML’s named reference name stands 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 html as the contents of an element named context in namespace ns, as innerHTML does.
parse_fragment_with
Parses html as parse_fragment does, within a size limit and an optional budget, as parse_with does for a whole document.
parse_with
Parses a whole document, within a size limit and an optional budget.
tag_number
The number Document::known_tag gives elements named name (lowercase), if the parser knows the tag.

Type Aliases§

NodeId
A node’s index in its document; the document itself is node 0.