NAME
html/dom - DOM-like classes for parsed HTML documents.
SYNOPSIS
from html/dom import HTMLDocument;
let doc := new HTMLDocument();
let root := doc.createElement("html");
doc.appendChild( doc.createDoctype() );
doc.appendChild(root);
root.appendChild( doc.createElement("body") );
DESCRIPTION
This module provides the pure ZuzuScript mutable DOM core used by html/parser. It intentionally mirrors the practical DOM-like API provided by std/data/xml: construction, traversal, mutation, attributes, simple selector lookup, cloning, equality, tree walking, and serialization.
HTMLTemplateElement owns an HTMLDocumentFragment returned by content(). Parsed template children live in that fragment rather than as direct children of the template element. Elements and attributes are namespace-aware for HTML, SVG, MathML, XLink, XML, and XMLNS data used by the HTML tree builder.
Documents may be created manually through constructors and HTMLDocument factory methods, or by html/parser.
EXPORTS
Classes
HTMLNodeBase node class. It implements shared metadata, traversal, mutation, text content, cloning, equality, tree walking, simple selector lookup, serialization, and unsupported XPath compatibility methods.
Core methods include
nodeName,nodeType,nodeValue,setNodeValue,nodeKind,uniqueKey,unique_id,localName,namespaceURI,firstChild,lastChild,nextSibling,previousSibling,parentNode,ownerDocument,childNodes,children,hasChildNodes,appendChild,prependChild,insertBefore,replaceChild,removeChild,remove,textContent,setTextContent,normalize,cloneNode,isSameNode,isEqualNode,contains,querySelectorAll,querySelector,findnodes,findvalue,visitEach,findFirst,toHTML,toXML, andto_String.HTMLDocumentDocument node class. It adds
documentElement, node factories,getElementsByTagName, andgetElementById. A document may contain one doctype, one element root, and comments. Text directly under a document is rejected.Factory methods are
createElement,createElementNS,createTextNode,createComment,createDoctype, andcreateCDATASection.createElement("template")returns anHTMLTemplateElement.createElementNSshould be used for SVG and MathML elements. The lookup methodsgetElementsByTagNameandgetElementByIdsearch the document's descendant elements.HTMLElementElement node class. It adds
tagName,id,setId,getAttribute,setAttribute,hasAttribute,removeAttribute,attributeNames,attributes, namespaced attribute methods,attributeRecords, and descendantgetElementsByTagName. Namespaced attribute methods aregetAttributeNS,setAttributeNS,hasAttributeNS, andremoveAttributeNS.namespaceURIandlocalNameare authoritative for HTML, SVG, and MathML elements. Attribute values are stored as strings; missing attributes returnnull.HTMLTemplateElement,HTMLDocumentFragmentTemplate elements and their content fragments.
HTMLDocument'screateElement("template")returns anHTMLTemplateElement.HTMLTemplateElement.content()returns the fragment containing parsed or manually inserted template contents.HTMLDocumentFragmentsupports the normal container operations andgetElementsByTagName. Direct children of parsed templates live incontent(), not in the template element's ownchildNodes()array.HTMLText,HTMLComment,HTMLDoctypeText, comment, and doctype node classes. Text and comments expose
dataandsetData. Doctypes exposename,publicId, andsystemId.HTMLDoctypeis normally created withHTMLDocument.createDoctype.DOMNode,DOMDocument,DOMElement,DOMText,DOMComment,DOMDoctypeDOM-compatible aliases implemented as subclasses of the HTML classes.
Constants
HTML_NAMESPACE_URI,SVG_NAMESPACE_URI,MATHML_NAMESPACE_URI,XLINK_NAMESPACE_URI,XML_NAMESPACE_URI,XMLNS_NAMESPACE_URINamespace URI constants used by the parser and DOM namespace APIs.
LIMITATIONS
findnodesandfindvaluethrow clear unsupported errors.There is no XPath or ZPath strategy in this distribution.
querySelectorandquerySelectorAllsupport only simpleselectors: tag names,
#id,.class, and*.Combinators, comma lists, pseudo-classes, namespace selectors, and attribute selectors throw clear unsupported-selector errors.
- Namespace support covers the HTML, SVG, MathML, XLink, XML, and
XMLNS namespace URIs used by the HTML tree builder.
Use
createElementNS,setAttributeNS,getAttributeNS,hasAttributeNS,removeAttributeNS, andattributeRecordsfor namespace-aware code. The older attribute methods remain available and operate on serialized qualified names. createCDATASectionreturns anHTMLTextnode.HTML documents do not have CDATA section nodes in this DOM core, but the method is provided for
std/data/xmlAPI compatibility.toHTML(pretty)andtoXML(pretty)acceptprettybutcurrently serialize compact HTML.
The serializer emits document children in order, doctypes, comments, escaped text and attributes, and HTML void elements without end tags.
COPYRIGHT AND LICENCE
html/dom is copyright Toby Inkster.
It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.