Configuration

Customize dawm's parsing behavior with ParseOptions and FragmentParseOptions.


Parse Options

The ParseOptions interface controls how dawm parses HTML and XML documents.

import { parseHTML, type ParseOptions } from "dawm";

const options: ParseOptions = {
  allowScripts: false,
  contentType: "text/html",
  exactErrors: false,
  quirksMode: "no-quirks",
  dropDoctype: false,
  iframeSrcdoc: false,
  contextElement: null,
};

const doc = parseHTML("<h1>Hello</h1>", options);

Options Reference

OptionTypeDefaultDescription
allowScriptsbooleanfalseWhen false, preserves <noscript> element hierarchy
contentTypestring"text/html"MIME type — use "application/xml" for XML parsing
exactErrorsbooleanfalseEnable detailed error reporting
quirksModestring"no-quirks"Quirks mode: "no-quirks", "quirks", "limited-quirks"
dropDoctypebooleanfalseStrip the doctype declaration from output
iframeSrcdocbooleanfalseSet to true when parsing iframe srcdoc content
contextElementstring | nullnullContext element name for fragment parsing

Content Types

dawm supports two content types:

HTML (text/html)

The default mode. Uses the HTML5 parsing algorithm defined by the WHATWG specification.

const doc = parseHTML("<div><p>Hello</p></div>", {
  contentType: "text/html",
});

XML (application/xml)

Strict XML parsing mode. The document must be well-formed XML.

import { parseDocument } from "dawm";

const doc = parseDocument("<root><item>1</item></root>", "application/xml");

Quirks Modes

The quirksMode option controls how the parser handles legacy HTML behaviors:

Fragment Parsing

When parsing HTML fragments (as opposed to full documents), you can specify a context element:

import { parseFragment } from "dawm/parse";

// Parse list items in the context of a <ul> element
const frag = parseFragment("<li>Item 1</li><li>Item 2</li>", "ul");
console.log(frag.childNodes.length); // 2

The context element determines how the fragment content is interpreted by the HTML5 parser. For example, <tr> elements are only valid inside a <table> context.