htmlDocument

Parses an HTML string into an HTML document object that supports CSS selectors. Pass mode "fragment" to parse the markup as a fragment, "document" to force full-document parsing, or omit it to auto-detect (strings that look like a full document are parsed as documents, everything else as a fragment). The returned object exposes select(selector), selectWithin(element, selector), attr(element, name), setAttr(element, name, value), hasAttr, removeAttr, text, innerHTML, outerHTML, tagName, allElements and documentHTML()/toString() to serialise back to HTML.

htmlDocument(html [, mode]) → returns any

This function requires RustCFML.  Not supported on Lucee, Adobe ColdFusion, etc.

Argument Reference

html string
Required

The HTML markup to parse.

mode string

Optional parse mode: fragment or document. Omit to auto-detect.
Values:
  • fragment
  • document

Examples
Sample code invoking the htmlDocument function

Parse a fragment and pull out the class of every .item element.

doc = htmlDocument("<ul><li class=\"item\">one</li><li class=\"item\">two</li></ul>");
items = doc.select(".item");
for (i in items) {
	writeOutput(doc.attr(items[i], "class") & " ");
}

Expected Result: item item

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub