**This package was copied from the old "htmld" project with minor stylistic changes. It is distributed under the MIT license.**

Original project: [https://github.com/eBookingServices/htmld](https://github.com/eBookingServices/htmld)

---

# htmld


Lightweight and forgiving HTML parser and DOM.

The parser was inspired by [htmlparse2](https://github.com/fb55/htmlparser2) by [fb55](https://github.com/fb55).

HTML Entity parsing and decoding are both optional. The current parser interface is based on callbacks.
It can also output compact HTML, and can do basic validation.


Creating the DOM from source:
```d
auto doc = createDocument(`<html><body>&nbsp;</body></html>`);
writeln(doc.root.outerHTML);
```


Creating/mutating DOM manually:
```d
auto doc = createDocument();
doc.root.html = `<body>&nbsp;</body>`;

auto container = doc.createElement("div", doc.root.firstChild);
container.attr("class", "container");
container.html = "<p>moo!</p>";

auto app = appender!string;
doc.root.outerHTML(app);
```


Compacting HTML:
```d
auto doc = createDocument!(DOMCreateOptions.none)("<html>\n<body attr="a" attr="a b c">    abc    </body>\n</html>");
writeln(doc.root.compactHTML);
```


QuerySelector interface:
```d
if (auto p = doc.querySelector("p:nth-of-type(2)"))
    p.text = "mooo";

foreach(p; doc.querySelectorAll("p")) {
    p.text = "mooo";
}
```


Example parser usage:
```d
auto builder = DOMBuilder();
parseHTML(`<html><body>&nbsp;</body></html>`, builder);
```


Example handler:
```d
struct DOMBuilder {
    void onText(const(char)[] data) {}
    void onSelfClosing() {}
    void onOpenStart(const(char)[] data) {}
    void onOpenEnd(const(char)[] data) {}
    void onClose(const(char)[] data) {}
    void onAttrName(const(char)[] data) {}
    void onAttrEnd() {}
    void onAttrValue(const(char)[] data) {}
    void onComment(const(char)[] data) {}
    void onDeclaration(const(char)[] data) {}
    void onProcessingInstruction(const(char)[] data) {}
    void onCDATA(const(char)[] data) {}

    // the following are required if ParseEntities is set
    void onNamedEntity(const(char)[] data) {}
    void onNumericEntity(const(char)[] data) {}
    void onHexEntity(const(char)[] data) {}

    // required only if DecodeEntities is set
    void onEntity(const(char)[] data, const(char)[] decoded) {}

    void onDocumentEnd() {}
}
```
