The DOM
class, when instantiated, parses the HTML that is passed as the first argument, with the newly constructed instance acting as the document's root node. The DOM
class extends Node
, providing document instances with the full suite of Node
methods and properties with a couple overrides. The properties and methods described below are only those that are unique to the DOM
class or have been overridden from the implementation of the same name in the Node
class.
new DOM( htmlContent[, options] )
Parameters
-
htmlContent
StringThe HTML to parse into an editable DOM object.
-
An object with some or all of the following properties:
-
allowCustomRootElement
Boolean (default:false
)Whether to allow the document type
Node
to specify the tag name of the rootNode
. In HTML documents, the rootNode
is always an<html>
element, but in XML documents, the<!DOCTYPE>
node'sname
is used to specify which element is the root.For example,
<!DOCTYPE doc>
indicates the root node to be the<doc>
element. However, for that to work, the<doc>
element has to be the top mostNode
in the document's hierarchy. -
allowSelfClosingSyntax
Boolean (default:false
)Whether to allow tags to be self-closing (ie. have a forward-slash "/" at the very end of the open tag instead of having a closing tag, like
<div />
). Useful for parsing XML. -
allowCDATA
Boolean (default:false
)Whether to parse XML CDATA tags (like
<![CDATA[ data ]]>
). When this isfalse
, any CDATA tags that are encountered will be parsed as comments. -
allowProcessingInstructions
Boolean (default:false
)Whether to parse processing instructions (like
<?instruction data?>
). When this isfalse
, any processing instructions that are encountered will be parsed as comments. -
decodeEntities
Boolean (default:false
)Whether to decode HTML entities (eg. turning
&
into&
) while parsing HTML. -
encodeEntities
Boolean or RegExp (default:false
)If specified as a
Boolean
, whether to encode HTML entities (eg. turning&
into&
) when serializing nodes as text.If specified as a
RegExp
, the regular expression will be used to determine which characters (or sequences of characters) should be encoded as entities. The entities have to exist in this document'sentityEncoder
, or the match made by theRegExp
will be ignored. -
entities
Entities (default:undefined
)If specified, this will be the set of entities used during entity encoding and decoding. Leaving this
undefined
, or setting it to a falsy value, results in the set of default entities being used instead. The default set of entities can be changed by assigning a set of entities toEntityEncoder.defaultEntities
. -
collapseWhitespace
Boolean (default:false
)Whether to collapse multiple consecutive whitespace characters in text nodes into a single space character, mimicking how browsers render consecutive whitespace characters. This option is ignored if
trimWhitespace
istrue
. -
trimWhitespace
Boolean (default:false
)Whether to remove whitespace characters from both ends of text nodes, which is useful for minifying HTML documents. When this option is
true
, thecollapseWhitespace
option is ignored. -
lowerAttributeCase
Boolean (default:false
)Whether to convert attribute names on tags to lower case. When this is
false
, attribute selectors will be case sensitive (ie. the selector[CaSe]
will match the element<div CaSe></div>
, but the selector[case]
will not). This isfalse
by default for performance reasons, but should betrue
if you want standards-compliant behaviour.
-
-
entityEncoder
EntityEncoder (read-only)The
EntityEncoder
instance used by thisDOM
instance for entity encoding and decoding. Changing theentities
of thisEntityEncoder
will only affect this document. -
Gets or sets the HTML markup of the entire document, including any
<!DOCTYPE>
node that may be present. When setting this with markup that either doesn't specify a<!DOCTYPE>
or doesn't have an<html>
element as the root node, the document'snodeType
property will beDOCUMENT_FRAGMENT_NODE
instead ofDOCUMENT_NODE
. When specifying a<!DOCTYPE>
, the node that is named in the document type tag will be expected as the document's root instead of<html>
. -
This simply overrides
Node.outerHTML
, making it do nothing onDOM
instances.
-
doctype
Node, Object, or Null — [standard] [MDN]Gets the document type node associated with the document if one is present, or
null
otherwise. Unlike the standard, you may set this property using a document type node (such as fromDOM.createDocumentType()
), or anObject
withname
(required),publicId
(optional), andsystemId
(optional) properties as strings. You may also set this tonull
to remove an existing document type from a document.When set to an
Object
, if the object either doesn't have aname
property, or the property's value isn't a string, the document type'sname
,publicId
, andsystemId
properties will be set to empty strings. -
documentElement
Node (read-only) — [standard] [MDN]Gets the document's root
Node
, ornull
if there is no rootNode
(such as when theDOM
instance represents a document fragment).
-
Gets the document's
<body>
element, ornull
if one doesn't exist. When setting, if the node's tag name is either"BODY"
or"FRAMESET"
, this will add theNode
to the document's root node if the document doesn't already have a body element, or the existing body element will be replaced by theNode
. -
head
Node (read-only) — [standard] [MDN]Gets the document's
<head>
element, ornull
if one doesn't exist. -
title
String — [standard] [MDN]Gets or sets the value of the document's
<title>
element if the document has a<head>
element, creating the<title>
element (when setting) if it doesn't exist.
document.createCDATASection( data )
Creates a new Node
of type CDATA_SECTION_NODE
.
Parameters
-
data
StringA
String
that is used as the contents of the CDATA section (eg." data "
in<![CDATA[ data ]]>
).
Return Value
The new Node
.
Exceptions
An Error
is thrown if data
contains the end sequence ]]>
.
document.createComment( data )
Creates a new Node
of type COMMENT_NODE
.
Parameters
-
data
StringA
String
that is used as the contents of the comment.⚠️ Caution: No check is performed for the sequence-->
, which can result in the early termination of the comment when the document is output as text, making the remaining contents of the comment appear as text in the document.
Return Value
The new Node
.
document.createDocumentType( name[, publicId[, systemId]] )
Creates a new Node
of type DOCUMENT_TYPE_NODE
.
Parameters
-
name
StringA
String
that is used as the document type's name (eg."html"
in<!DOCTYPE html>
). -
publicId
String (optional)A
String
that is used as the document type's public identifier. If this isn't a non-empty string, an empty string will be used instead. -
systemId
String (optional)A
String
that is used as the document type's system identifier. If this isn't a non-empty string, an empty string will be used instead.
Return Value
The new Node
.
document.createElement( tagName )
Creates a new Node
of type ELEMENT_NODE
.
Parameters
-
tagName
StringA
String
that specifies thetagName
of the element (eg.<name></name>
)
Return Value
The new Node
, or undefined
if the specified tagName
parameter isn't a String
or is an empty string.
document.createProcessingInstruction( target, data )
Creates a new Node
of type PROCESSING_INSTRUCTION_NODE
.
Parameters
-
target
StringA
String
representing the target of the processing instruction (eg."target"
in<?target?>
). -
data
StringA
String
containing the data of the processing instruction (eg."data"
in<?target data?>
). This string can be anything, except the sequence?>
which indicates the end of the processing instruction node.
Return Value
The new Node
.
Exceptions
An Error
is thrown if either of the following is true
:
target
is invalid — it must be aString
that is a valid XML name.data
contains the end sequence?>
.
document.createTextNode( text )
Creates a new Node
of type TEXT_NODE
.
Parameters
-
text
StringA
String
that is used as the contents of the new text node.
Return Value
The new Node
.
document.getElementsByName( name )
Gets all elements (ELEMENT_NODE
type nodes) which have a name
attribute that matches the specified name
parameter string.
Parameters
-
name
StringThe
name
attribute of the elements to locate. The comparison is case sensitive.
Return Value
An Array
of all elements in the document whose name
attribute matches the specified name. The array can be empty if no elements matched.
The below convenience methods read and parse the file "lib/entities.json" (which includes all standard HTML 5 entities) and are only available when using FauxDOM in Node.js.
ℹ️ Note: As the standard set of entities is extremely comprehensive, using the standard set can lead to potentially undesirable output, such as encoding new-line characters (which would normally be ignored by browsers) as


. This sort of unintended encoding of entities can lead to rather broken looking documents.To avoid this, either create your document using the
encodeEntities
option as aRegExp
that matches only the entities you care about, or use a custom set of entities that only defines the entities you care about.
document.importStandardEntities()
Sets the entities
of this document's entityEncoder
to the standard set of HTML 5 entities.
DOM.importStandardEntities()
The static version of the above method. Sets EntityEncoder.defaultEntities
to the standard set of HTML 5 entities.