# Parserino: Fast HTML5 Parser and DOM Manipulation for D Parserino is a high-performance HTML5 parser and DOM manipulation library for the D programming language, written in pure D (no C dependencies; born as a D port of lexbor, from which the tree construction is derived). Names and behaviour follow the DOM, HTML and Selectors 4 standards; the parser passes the tree construction tests of the web-platform-tests. ## Rules - **Nodes and elements**: `Node` is any node; `Element` is a node that is an element. An `Element` converts implicitly to `Node`; `node.asElement` gives an `Element` (invalid if the node isn't an element). - **Navigation filter**: navigation methods take the kinds of nodes to see as a template parameter `Show` (the bits of DOM `NodeFilter.whatToShow`, combine them with `|`). The default is `Show.Element`, and then the result is an `Element` or a range of `Element`s; with other filters the results are `Node`s. - **Errors**: searches and navigation give an invalid node (`== null`, `isValid == false`) or an empty range when nothing is found; nodes that can't have children (texts, comments) just have none. Calling a method on an invalid node or document, or an impossible mutation (a child of a text, a node inside itself, a node of another document, a sibling of a node without parent), throws `ParserinoException`. - **Views**: getters that copy a stored string have a `...View` variant returning a slice of the document memory (valid until the value changes or the document is destroyed): `localNameView`, `idView`, `getAttributeView`, `classesView`, `attributesView`, `dataView`. Computed values (`textContent` of an element, `innerHTML`, `title`) have none. - **Memory**: `Document` is reference counted; every `Node` keeps its document alive; removed nodes stay valid and can be inserted again. ## Core Types ### `Document` The root of the DOM tree. - `this(string html, Parsing parsing = Parsing.Eager, size_t chunkSize = DefaultChunkSize)`: Parses the document. With `Parsing.Lazy` it parses incrementally, only as far as queries and accessors need. Results are identical to a full parse. Mutations complete the parsing first. - `this(string html, ParseOptions options)`: `ParseOptions` has `parsing`, `chunkSize` and `scripting` (with `scripting: true` the content of `` is text, as in a browser; by default it's parsed as html). - `ctDocument!(html, ParseOptions options = ParseOptions.init)`: A document parsed at compile time. Each call returns a new mutable document, rebuilt from the stored tree without parsing (e.g. `ctDocument!(import("page.html"))`). - `Snapshot snapshot`, `this(Snapshot)`: A compact, immutable copy of the tree; `Document(snap)` makes a new independent document from it much faster than parsing. Use it for a page filled many times (a template, for each request): parse once, then `Document(snap)` each time. Snapshots can be shared between threads; the documents borrow their strings (the snapshot is kept alive as long as needed). - `Document dup`: A deep copy. - `void finishParsing()`, `bool isParsing`, `size_t bytesParsed`: Lazy parsing. - `bool isValid`: True if the document is initialized. - `Element documentElement`, `Element head`, `Element body` (the first `` or `` child of ``, as in the DOM), `Node node` (the document itself as a `Node`). - `string title` (get/set; the getter strips and collapses whitespace), `string rawTitle`. - `Element createElement(string tagName)`, `Node createText(string text)`, `Node createComment(string text)`. - `Node fragment(string html, string context = "body")`: Parses html in the context of an element (as the standard fragment parsing: `"tr"` makes `` valid, `"textarea"` makes everything text, `"svg"`/`"math"` for foreign content) into a `DocumentFragment` node. Inserting the fragment moves its children. - `string toString()`, `toString(sink)` (a delegate or an output range). - `string toPrettyString()`: The html indented, to read it: each block element on its own line, two spaces per level; elements with only text and inline elements stay on one line. Whitespace between nodes changes (not in ``, ``, `
`, ``, `