Node Interface
The Node interface is the primary datatype for the entire Document Object
Model. It represents a single node in the document tree.
IDL Definition
Interface Node {
// NodeType
const unsigned short ELEMENT_NODE = 1;
const unsigned short ATTRIBUTE_NODE = 2;
const unsigned short TEXT_NODE = 3;
const unsigned short CDATA_SECTION_NODE = 4;
const unsigned short ENTITY_REFERENCE_NODE = 5;
const unsigned short ENTITY_NODE = 6;
const unsigned short PROCESSING_INSTRUCTION_NODE = 7;
const unsigned short COMMENT_NODE = 8;
const unsigned short DOCUMENT_NODE = 9;
const unsigned short DOCUMENT_TYPE_NODE = 10
const unsigned short DOCUMENT_FRAGMENT_NODE = 11;
const unsigned short NOTATION_NODE = 12;
readonly attribute DOMString nodeName;
attribute DOMString nodeValue;
// raises (DOMException) on setting
// raises (DOMException) on retreival
readonly attribute unsigned short nodeType;
readonly attribute Node parentNode;
readonly attribute Node childNodes;
readonly attribute Node firstChild;
readonly attribute Node lastChild;
readonly attribute Node previousSibling;
readonly attribute Node nextSibling;
readonly attribute NamedNodeMap attributes;
readonly attribute Document ownerDocument;
Node insertBefore(in Node newChild,
in Node refChild)
raises(DOMException);
Node replaceChild(in None newChild,
in Node oldChild)
raises(DOMException);
Node remove Child(in Node oldChild)
raises(DOMException);
Node appendChild(in Node newChild)
raises(DOMException);
boolean hasChildNodes();
Node cloneNode(in boolean deep);
}
Semantic Requirements
Definition group
- The predefined nodeType integer constant for an Element node is 1 (ELEMENT_NODE).
- The predefined nodeType integer constant for an Attribute node is 2 (ATTRIBUTE_NODE).
- The predefined nodeType integer constant for a Text node is 3 (TEXT_NODE).
- The predefined nodeType integer constant for a CDATASection node is 4 (CDATA_SECTION_NODE).
- The predefined nodeType integer constant for an EntityReference node is 5 (ENTITY_REFERENCE_NODE).
- The predefined nodeType integer constant for an Entity node is 6 (ENTITY_NODE).
- The predefined nodeType integer constant for a ProcessingInstruction node is 7 (PROCESSING_INSTRUCTION_NODE).
- The predefined nodeType integer constant for a Comment node is 8 (COMMENT_NODE).
- The predefined nodeType integer constant for a Document node is 9 (DOCUMENT_NODE).
- The predefined nodeType integer constant for a DocumentType node is 10 (DOCUMENT_TYPE_NODE).
- The predefined nodeType integer constant for a DocumentFragment node is 11 (DOCUMENT_FRAGMENT_NODE).
- The predefined nodeType integer constant for a Notation node is 12 (NOTATION_NODE).
- The values of nodeName, nodeValue and attributes vary according to the node type as follows:
|
nodeName |
NodeValue |
attributes |
Element |
tagName |
null |
namedNodeMap |
Attr |
name of attribute |
value of attribute |
null |
Text |
#text |
context of text node |
null |
CDATASection |
#cdata-section |
content of CDATA Section |
null |
EntityReference |
name of entity referenced |
null |
null |
Entity |
entity name |
null |
null |
ProcessingInstruction |
target |
entire content excluding the target |
null |
Comment |
#comment |
content of the comment |
null |
Document |
#document |
null |
null |
DocumentType |
document type name |
null |
null |
DocumentFragment |
#document-fragment |
null |
null |
Notation |
notation name |
null |
null |
Attributes
- The nodeType attribute contains a code representing the type of the underlying object.
- The nodeName attribute contains the name of this node, depending on its type (see table above).
- The nodeValue attribute contains the value of this node, depending on its type (see table above).
- The attributes attribute contains a NamedNodeMap containing the attributes of this node (if it is an Element).
- The parentNode attribute contains the parent of this node.
- If a node has just been created and not yet added to the tree then its parentNode attribute has a null value.
- If a node has been removed from the tree then its parentNode attribute has a null value.
- The childNodes attribute contains a NodeList of all children of this node.
- If there are no children then the NodeList contained in the childNodes attribute has no nodes.
- Changes in the children of the node are immediately reflected in the NodeList returned by the childNodes attribute.
- The firstChild attribute contains the first child of this node.
- If there is no first child then the firstChild attribute returns null.
- The lastChild attribute returns the last child of this node.
- If there is no last child then the lastChild attribute returns null.
- The previousSibling attribute returns the node immediately preceding this node.
- If there is no node immediately preceding this node then the previousSibling attribute returns null.
- The nextSibling attribute contains the node immediately following this node.
- If there is no node immediately following this node then the nextSibling attribute returns null.
- The ownerDocument attribute contains the Document object associated with this node.
- When this node is a Document then the ownerDocument attribute returns null.
Methods
- The insertBefore(newChild,refChild) method inserts the node newChild before the existing child refChild.
- If refChild is null then the insertBefore(newChild,refChild) method inserts the newChild at the end of the list of children.
- If newChild is a DocumentFragment object, all of its children are inserted in the same order.
- The insertBefore(newChild,refChild) method returns the node being inserted.
- If newChild is already in the tree, the insertBefore(newChild,refChild) method first remove it before the insertion.
- The replaceChild(newChild,oldChild) method replaces the child node oldChild with newChild in the list of children.
- if the newChild is already in the list then the replaceChild(newChild,oldChild) method first remove the already existing newChild node.
- The replaceChild(newChild,oldChild) method returns the node replaced.
- The removeChild(oldChild) method removes the node indicated by oldChild.
- The removeChild(oldChild) method returns the node removed.
- The appendChild(newChild) method adds the node newChild to the end of the list of children of this node.
- if the newChild is already in the list then the appendChild(newCh ild) method first remove the already existing newChild node.
- If newChild (from the appendChild(newChild) method) is a DocumentFragment object then the entire contents of the document fragment is moved into the child list of this node.
- The appendChild(newChild) method returns the node added.
- The hasChildNodes() method returns true if the node has any children.
- The hasChildNodes() method returns false if the node has no children.
- The cloneNode(deep) method returns a duplicate of the node only if deep = false.
- The cloneNode(deep) method returns a duplicate of the node and the subtree under it if deep = true.
- The node returned by the cloneNode(deep) method has no parent.
- The cloneNode(deep) method does not copy any text unless it is deep clone.
- If the cloneNode(deep) method was used to clone an Element then all attributes and their values are copied.
DOMExceptions
- The nodeValue attribute raises a NO_MODIFICATION_ALLOWED_ERR DOMException when the node is readonly.
- The insertBefore(newChild,refChild) method raises a NO_MODIFICATION_ALLOWED_ERR DOMException when the node is readonly.
- The replaceChild(newChild,oldChild) method raises a NO_MODIFICATION_ALLOWED_ERR DOMException when the node is readonly.
- The removeChild(oldChild) method raises a NO_MODIFICATION_ALLOWED_ERR DOMException when the node is readonly.
- The appendChild(newChild) method raises a NO_MODIFICATION_ALLOWED_ERR DOMException when the node is readonly.
- The insertBefore(newChild,refChild) method raises a HIERARCHY_REQUEST_ERR DOMException if this node does not allow children of the type of the newChild node.
- The insertBefore(newChild,refChild) method raises a HIERARCHY_REQUEST_ERR DOMException if the node to insert is one of this node's ancestors.
- The replaceChild(newChild,oldChild) method raises a HIERARCHY_REQUEST_ERR DOMException if this node does not allow children of the type of newChild node.
- The replaceChild(newChild,oldChild) method raises a HIERARCHY_REQUEST_ERR DOMException if the node to put in is one of this node's ancestors.
- The appendChild(newChild) method raises a HIERARCHY_REQUEST_ERR DOMException if this node does not allow children of the type of the newChild node.
- The appendChild(newChild,oldChild) method raises a HIERARCHY_REQUEST_ERR DOMException if the node to append is one of this node's ancestors.
- The insertBefore(newChild,refChild) method raises a NOT_FOUND_ERR DOMException if refChild is not a child of this node.
- The replaceChild(newChild,oldChild) method raises a NOT_FOUND_ERR DOMException if oldChild is not a child of this node.
- The and removeChild(oldChild) method raises a NOT_FOUND_ERR DOMException if oldChild is not a child of this node.
- The insertBefore(newChild,refChild) method raises a WRONG_DOCUMENT_ERR DOMException if newChild was created from a different document that the one that created this node.
- The replaceChild(newChild,oldChild) method raises a WRONG_DOCUMENT_ERR DOMException if newChild was created from a different document that the one that created this node.
- The appendChild(newChild) method raises a WRONG_DOCUMENT_ERR DOMexception if newChild was created from a different document that the one that created this node.
If you have comments or suggestions, email me at mbrady@nist.gov