parserino.dom

The document tree.

Every node lives in the arena of its document and is freed with it: removed nodes stay valid. Nodes are plain structs: DomElement, DomCharacterData, ... start with a DomNode: node.as!DomElement (after checking type) gives the struct that contains it.

Types 9

enumNodeType : ubyte

Node types, with the values of DOM nodeType

Element = 1
Text = 3
CDataSection = 4
ProcessingInstruction = 7
Comment = 8
Document = 9
DocumentType = 10
DocumentFragment = 11
enumCompatMode : ubyte

Quirks mode of a document, set by the parser from the doctype

NoQuirks
LimitedQuirks
Quirks
structDomNode

The common part of all the nodes

Fields
Ns ns
uint nameTag of elements (a `Tag` or an id from the document names). Other nodes: `Tag.TextNode`, `Tag.CommentNode`, ...
DomNode * parent
DomNode * firstChild
DomNode * lastChild
DomNode * prev
DomNode * next
DomDocument * document
Methods
bool isHtml(uint tag) @nogc nothrow pure @safe constIs this an element in the html namespace with this tag?
T * as(T)() @nogc nothrow pure @safe @trusted returnThe struct that contains this node: `DomElement`, `DomCharacterData`, `DomDocumentType`, `DomDocumentFragment` or `DomDocument`. `type` must match (checked only by an assert).
const(T) * as(T)() @nogc nothrow pure @safe const @trusted returnditto
DomElement * asElement() @nogc nothrow pure @safe returnThe element / character data, or null if the node is something else
const(DomElement) * asElement() @nogc nothrow pure @safe const returnditto
DomCharacterData * asCharacterData() @nogc nothrow pure @safe returnditto
const(DomCharacterData) * asCharacterData() @nogc nothrow pure @safe const returnditto
bool isCharacterData() @nogc nothrow pure @safe constIs it text, a comment, a processing instruction or a CDATA section?
auto prevElement(this This)() @nogc nothrow pure @safePrevious/next sibling that is an element
auto nextElement(this This)() @nogc nothrow pure @safeditto
void appendChild(DomNode * child) @nogc nothrow pure @safeAppend `child` as the last child (`child` must be detached)
void insertBefore(DomNode * node) @nogc nothrow pure @safeInsert `node` (detached) before this node
void insertAfter(DomNode * node) @nogc nothrow pure @safeInsert `node` (detached) after this node
void remove() @nogc nothrow pure @safeDetach this node from its parent
void moveChildrenTo(DomNode * to) @nogc nothrow pure @safeMove all the children of this node at the end of `to`
auto parentOrHost(this This)() @nogc nothrow pure @safeThe parent, or the `<template>` for the nodes of a template content
auto firstChildOrContent(this This)() @nogc nothrow pure @safeThe first node of the template content for a `<template>`, else the first child
bool contains(const(DomNode) * other) @nogc nothrow pure @safe constIs `other` this node or one of its descendants?
auto nextInTree(this This)(const(DomNode) * root) @nogc nothrow pure @safeNext node in tree order inside `root` (excluded), or null
auto nextSkippingChildren(this This)(const(DomNode) * root) @nogc nothrow pure @safeNext node in tree order after this subtree, inside `root`, or null
const(char)[] localName() @nogc nothrow pure @safe constLocal name ("div", "#text", "!--", ...; the target for processing instructions)
bool isEmpty() @nogc nothrow pure @safe constDoes it have no children that are elements or non-empty text? Comments and processing instructions don't count (as for the `:empty` selector).
bool isBlank() @nogc nothrow pure @safe constLike `isEmpty`, but text made only of whitespace doesn't count either (the `:blank` selector)

An attribute

Fields
uint nameLocal name, lowercase (an `AttrName` or an id from the document names)
Ns ns
const(char)[] qualifiedNameThe name to serialize, when it differs from the local name (`xlink:href`, `viewBox`), else null
const(char)[] value
DomElement * owner
Methods
const(char)[] localName() @nogc nothrow pure @safe constThe local name
const(char)[] fullName() @nogc nothrow pure @safe constThe full name: `xlink:href`, `viewBox`, `class`

An element

Fields
DomNode node
const(char)[] qualifiedNameThe name to serialize, when it differs from the local name (`foreignObject`), else null
DomAttribute * firstAttr
DomAttribute * lastAttr
DomAttribute * idAttr
DomAttribute * classAttr
DomDocumentFragment * templateContentThe content of a `<template>`
ubyte flagsState used by the parser (option selectedness, ...)
Methods
const(char)[] fullName() @nogc nothrow pure @safe constTag name as written in html: `div`, `foreignObject`
CopyConstness!(This, DomAttribute) * attribute(this This)(uint id) @nogc nothrow pure @safeFirst attribute with this (lowercase) name id, any namespace
CopyConstness!(This, DomAttribute) * attribute(this This)(scope const(char)[] name) @nogc nothrow pure @safeFirst attribute with this name (case-insensitive for html elements)
void appendAttribute(DomAttribute * a) @nogc nothrow pure @safeAppend an attribute (no duplicate check)
void removeAttribute(DomAttribute * a) @nogc nothrow pure @safeRemove an attribute of this element
void track(DomAttribute * a) @nogc nothrow pure @safeKeep `idAttr` and `classAttr` up to date

Text, comments, processing instructions, CDATA sections

Fields
DomNode node
const(char)[] targetThe target of a processing instruction
Methods
const(char)[] data() @nogc nothrow pure @safe const return @trustedThe text
void borrowData(return scope const(char)[] s) @nogc nothrow pure @safe @trustedUse `s` as the text, without copying it: `s` must outlive the document and never change (it's never written: a later change of the text reallocates it).
bool setData(scope const(char)[] s) @nogc nothrow pure @safeReplace the text
bool appendData(scope const(char)[] s) @nogc nothrow pure @safe @trustedAppend to the text. It returns false if out of memory.

A doctype: <!DOCTYPE name PUBLIC "publicId" "systemId">

Fields
DomNode node
const(char)[] name
const(char)[] publicId
const(char)[] systemId

A fragment, or the content of a <template>

Fields
DomNode node
DomElement * hostThe `<template>` of a template content, else null

The document: it owns the memory of all its nodes (and the names it meets). It can't be copied: allocate it zeroed, call initialize, and release it at the end.

Fields
DomNode node
Arena arena
CompatMode compatMode
bool scripting
DomDocumentType * doctype
Methods
auto head(this This)() @nogc nothrow pure @safeThe `<head>`: the first `head` child of the `<html>` root element (as in the standard)
auto body(this This)() @nogc nothrow pure @safeThe `<body>`: the first `body` or `frameset` child of the `<html>` root element (as in the standard)
DomElement * documentElement() @nogc nothrow pure @safeThe root element (`<html>`)
const(DomElement) * documentElement() @nogc nothrow pure @safe constditto
void initialize() @nogc nothrow pure @safeInitialize a document allocated by the caller (zeroed memory)
void release() @nogc nothrow pure @safeFree all the memory
uint tagId(scope const(char)[] lowerName) @nogc nothrow pure @safeTag id of a lowercase name, adding it if unknown. 0 if out of memory.
uint findTagName(scope const(char)[] name) @nogc nothrow pure @safe constTag id of a name (any case), 0 if the document never saw it
const(char)[] tagName(uint id) @nogc nothrow pure @safe constThe name of a tag id
uint attrId(scope const(char)[] lowerName) @nogc nothrow pure @safeAttribute id of a lowercase name, adding it if unknown. 0 if out of memory.
uint findAttrName(scope const(char)[] name) @nogc nothrow pure @safe constAttribute id of a name (any case), 0 if the document never saw it
const(char)[] attrName(uint id) @nogc nothrow pure @safe constThe name of an attribute id
DomElement * createElement(uint tag, Ns ns) @nogc nothrow pure @safeA new element (not attached), with an empty content for a `<template>`. It returns null if out of memory, as the other `create...` functions.
DomCharacterData * createText(scope const(char)[] data) @nogc nothrow pure @safeA new text node (not attached), with a copy of `data`
DomCharacterData * createComment(scope const(char)[] data) @nogc nothrow pure @safeA new comment (not attached), with a copy of `data`
DomCharacterData * createProcessingInstruction(scope const(char)[] target, scope const(char)[] data) @nogc nothrow pure @safeA new processing instruction (not attached)
DomDocumentType * createDocumentType(scope const(char)[] name, scope const(char)[] publicId, scope const(char)[] systemId) @nogc nothrow pure @safeA new doctype (not attached)
DomDocumentFragment * createFragment() @nogc nothrow pure @safeA new empty fragment
DomAttribute * createAttribute(scope const(char)[] lowerName, scope const(char)[] value, Ns ns = Ns.None) @nogc nothrow pure @safeA new attribute (not attached). The name must be lowercase.
DomAttribute * createAttribute(uint id, scope const(char)[] value, Ns ns = Ns.None) @nogc nothrow pure @safeditto
DomAttribute * setAttribute(DomElement * e, scope const(char)[] name, scope const(char)[] value) @nogc nothrow pure @safeSet (or add) an attribute. The name is lowercased. It returns null if out of memory.
DomNode * importNode(const(DomNode) * src, bool deep) @nogc nothrow pure @safeA copy of `src` owned by this document. Deep: with all the descendants and the template contents; else only the node (and the attributes). It returns null if out of memory. The visit is iterative, ...
const(char)[] copy(scope const(char)[] s) @nogc nothrow pure @safeA string owned by the document

Functions 2

fnvoid textContent(Sink)(const(DomNode) * node, ref Sink sink)The text of a node and its descendants (DOM `textContent`) written to `sink`
fnconst(char)[] toLower(return scope const(char)[] s, return ref char[64] buf, return ref Buffer!char big) @nogc nothrow pure @safe @trustedASCII lowercase: `s` itself if already lowercase, else a copy in `buf`, or in `big` for long names. It returns null only if out of memory.