public abstract class ParentNode extends ChildNode
ParentNode, just like NodeImpl, also implements NodeList, so it can return itself in response to the getChildNodes() query. This eliminiates the need for a separate ChildNodeList object. Note that this is an IMPLEMENTATION DETAIL; applications should _never_ assume that this identity exists. On the other hand, subclasses may need to override this, in case of conflicting names. This is the case for the classes HTMLSelectElementImpl and HTMLFormElementImpl of the HTML DOM.
While we have a direct reference to the first child, the last child is stored as the previous sibling of the first child. First child nodes are marked as being so, and getNextSibling hides this fact.
Note: Not all parent nodes actually need to also be a child. At some point we used to have ParentNode inheriting from NodeImpl and another class called ChildAndParentNode that inherited from ChildNode. But due to the lack of multiple inheritance a lot of code had to be duplicated which led to a maintenance nightmare. At the same time only a few nodes (Document, DocumentFragment, Entity, and Attribute) cannot be a child so the gain in memory wasn't really worth it. The only type for which this would be the case is Attribute, but we deal with there in another special way, so this is not applicable.
This class doesn't directly support mutation events, however, it notifies the document when mutations are performed so that the document class do so.
WARNING: Some of the code here is partially duplicated in AttrImpl, be careful to keep these two classes in sync!
| Modifier and Type | Field and Description |
|---|---|
protected ChildNode |
firstChild
First child.
|
protected org.htmlunit.cyberneko.xerces.dom.NodeListCache |
fNodeListCache
NodeList cache
|
protected CoreDocumentImpl |
ownerDocument
Owner document.
|
nextSibling_, previousSibling_DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_IS_CONTAINED, DOCUMENT_POSITION_PRECEDING, FIRSTCHILD, HASSTRING, ID, NORMALIZED, OWNED, ownerNode_, READONLY, SPECIFIED, SYNCCHILDRENATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE| Modifier | Constructor and Description |
|---|---|
protected |
ParentNode(CoreDocumentImpl ownerDocument)
No public constructor; only subclasses of ParentNode should be instantiated,
and those normally via a Document's factory methods
|
| Modifier and Type | Method and Description |
|---|---|
Node |
cloneNode(boolean deep)
Returns a duplicate of a given node.
|
NodeList |
getChildNodes()
Obtain a NodeList enumerating all children of this node.
|
protected NodeList |
getChildNodesUnoptimized()
Create a NodeList to access children that is use by subclass elements that
have methods named getLength() or item(int).
|
Node |
getFirstChild()
The first child of this Node, or null if none.
|
Node |
getLastChild()
The first child of this Node, or null if none.
|
int |
getLength()
NodeList method: Count the immediate children of this node
By default we do not have any children, ParentNode overrides this.
|
Document |
getOwnerDocument()
Find the Document that this Node belongs to (the document in whose context
the Node was created).
|
String |
getTextContent()
This attribute returns the text content of this node and its descendants.
|
boolean |
hasChildNodes()
Test whether this node has any children.
|
Node |
insertBefore(Node newChild,
Node refChild)
Move one or more node(s) to our list of children.
|
boolean |
isEqualNode(Node arg)
Tests whether two nodes are equal.
|
Node |
item(int index)
NodeList method: Return the Nth immediate child of this node,
or null if the index is out of bounds.
|
Node |
removeChild(Node oldChild)
Remove a child from this Node.
|
Node |
replaceChild(Node newChild,
Node oldChild)
Make newChild occupy the location that oldChild used to have.
|
protected void |
setOwnerDocument(CoreDocumentImpl doc)
NON-DOM set the ownerDocument of this node and its children
|
void |
setTextContent(String textContent)
This attribute returns the text content of this node and its descendants.
|
protected void |
synchronizeChildren()
Override this method in subclass to hook in efficient internal data
structure.
|
getNextSibling, getParentNode, getPreviousSiblingaddEventListener, appendChild, changed, changes, compareDocumentPosition, dispatchEvent, getAttributes, getBaseURI, getFeature, getLocalName, getNamespaceURI, getNodeName, getNodeNumber, getNodeType, getNodeValue, getPrefix, getUserData, hasAttributes, isDefaultNamespace, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, needsSyncChildren, normalize, removeEventListener, setNodeValue, setPrefix, setUserData, toStringprotected CoreDocumentImpl ownerDocument
protected ChildNode firstChild
protected org.htmlunit.cyberneko.xerces.dom.NodeListCache fNodeListCache
protected ParentNode(CoreDocumentImpl ownerDocument)
ownerDocument - the owner documentpublic Node cloneNode(boolean deep)
Note: since we never have any children deep is meaningless here, ParentNode overrides this behavior. Returns a duplicate of a given node. You can consider this a generic "copy constructor" for nodes. The newly returned object should be completely independent of the source object's subtree, so changes in one after the clone has been made will not affect the other.
Note: since we never have any children deep is meaningless here, ParentNode overrides this behavior. Returns a duplicate of a given node. You can consider this a generic "copy constructor" for nodes. The newly returned object should be completely independent of the source object's subtree, so changes in one after the clone has been made will not affect the other.
Example: Cloning a Text node will copy both the node and the text it contains.
Example: Cloning something that has children -- Element or Attr, for example -- will _not_ clone those children unless a "deep clone" has been requested. A shallow clone of an Attr node will yield an empty Attr of the same name.
NOTE: Clones will always be read/write, even if the node being cloned is read-only, to permit applications using only the DOM API to obtain editable copies of locked portions of the tree.
cloneNode in interface NodecloneNode in class ChildNode
Example: Cloning a Text node will copy both the node and the text it
contains.
Example: Cloning something that has children -- Element or Attr, for
example -- will _not_ clone those children unless a "deep clone" has
been requested. A shallow clone of an Attr node will yield an empty Attr
of the same name.
NOTE: Clones will always be read/write, even if the node being cloned is
read-only, to permit applications using only the DOM API to obtain
editable copies of locked portions of the tree.
public Document getOwnerDocument()
getOwnerDocument in interface NodegetOwnerDocument in class NodeImplprotected void setOwnerDocument(CoreDocumentImpl doc)
setOwnerDocument in class NodeImplpublic boolean hasChildNodes()
By default we do not have any children, ParentNode overrides this. Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
hasChildNodes in interface NodehasChildNodes in class NodeImplParentNodepublic NodeList getChildNodes()
NodeLists are "live"; as children are added/removed the NodeList will immediately reflect those changes. Also, the NodeList refers to the actual nodes, so changes to those nodes made via the DOM tree will be reflected in the NodeList and vice versa.
In this implementation, Nodes implement the NodeList interface and provide their own getChildNodes() support. Other DOMs may solve this differently. Obtain a NodeList enumerating all children of this node. If there are none, an (initially) empty NodeList is returned.
NodeLists are "live"; as children are added/removed the NodeList will immediately reflect those changes. Also, the NodeList refers to the actual nodes, so changes to those nodes made via the DOM tree will be reflected in the NodeList and vice versa.
In this implementation, Nodes implement the NodeList interface and provide their own getChildNodes() support. Other DOMs may solve this differently.
getChildNodes in interface NodegetChildNodes in class NodeImplpublic Node getFirstChild()
By default we do not have any children, ParentNode overrides this.
getFirstChild in interface NodegetFirstChild in class NodeImplParentNodepublic Node getLastChild()
By default we do not have any children, ParentNode overrides this.
getLastChild in interface NodegetLastChild in class NodeImplParentNodepublic Node insertBefore(Node newChild, Node refChild) throws DOMException
By default we do not accept any children, ParentNode overrides this. Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
insertBefore in interface NodeinsertBefore in class NodeImplnewChild - The Node to be moved to our subtree. As a convenience
feature, inserting a DocumentNode will instead insert all its
children.refChild - Current child which newChild should be placed immediately
before. If refChild is null, the insertion occurs after all
existing Nodes, like appendChild().DOMException - HIERARCHY_REQUEST_ERR if newChild is of a type that
shouldn't be a child of this node, or if newChild is an
ancestor of this node.DOMException - WRONG_DOCUMENT_ERR if newChild has a different owner
document than we do.DOMException - NOT_FOUND_ERR if refChild is not a child of this node.DOMException - NO_MODIFICATION_ALLOWED_ERR if this node is read-only.ParentNodepublic Node removeChild(Node oldChild) throws DOMException
By default we do not have any children, ParentNode overrides this. Remove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.
removeChild in interface NoderemoveChild in class NodeImplDOMException - NOT_FOUND_ERR if oldChild is not a child of this node.DOMException - NO_MODIFICATION_ALLOWED_ERR if this node is read-only.ParentNodepublic Node replaceChild(Node newChild, Node oldChild) throws DOMException
By default we do not have any children, ParentNode overrides this. Make newChild occupy the location that oldChild used to have. Note that newChild will first be removed from its previous parent, if any. Equivalent to inserting newChild before oldChild, then removing oldChild.
replaceChild in interface NodereplaceChild in class NodeImplDOMException - HIERARCHY_REQUEST_ERR if newChild is of a type that
shouldn't be a child of this node, or if newChild is one
of our ancestors.DOMException - WRONG_DOCUMENT_ERR if newChild has a different owner
document than we do.DOMException - NOT_FOUND_ERR if oldChild is not a child of this node.DOMException - NO_MODIFICATION_ALLOWED_ERR if this node is read-only.ParentNodepublic String getTextContent() throws DOMException
NodeImplText node containing the string this attribute is set to. On
getting, no serialization is performed, the returned string does not contain
any markup. No whitespace normalization is performed, the returned string
does not contain the element content whitespaces . Similarly, on setting, no
parsing is performed either, the input string is taken as pure textual
content. | Node type | Content |
|---|---|
| ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE | concatenation of the
textContent attribute value of every child node, excluding
COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes |
| ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE | nodeValue |
| DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE | null |
getTextContent in interface NodegetTextContent in class NodeImplDOMException - DOMSTRING_SIZE_ERR: Raised when it would return more
characters than fit in a DOMString
variable on the implementation platform.public void setTextContent(String textContent) throws DOMException
NodeImplText node containing the string this attribute is set to. On
getting, no serialization is performed, the returned string does not contain
any markup. No whitespace normalization is performed, the returned string
does not contain the element content whitespaces . Similarly, on setting, no
parsing is performed either, the input string is taken as pure textual
content. | Node type | Content |
|---|---|
| ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE | concatenation of the
textContent attribute value of every child node, excluding
COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes |
| ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE | nodeValue |
| DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE | null |
setTextContent in interface NodesetTextContent in class NodeImplDOMException - DOMSTRING_SIZE_ERR: Raised when it would return more
characters than fit in a DOMString
variable on the implementation platform.public int getLength()
By default we do not have any children, ParentNode overrides this. NodeList method: Count the immediate children of this node
public Node item(int index)
By default we do not have any children, ParentNode overrides this. NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
protected final NodeList getChildNodesUnoptimized()
public boolean isEqualNode(Node arg)
Node.isSameNode. All nodes that are the same will also be equal,
though the reverse may not be true. nodeName, localName, namespaceURI,
prefix, nodeValue , baseURI. This is:
they are both null, or they have the same length and are
character for character identical. The attributes
NamedNodeMaps are equal. This is: they are both
null, or they have the same length and for each node that exists
in one map there is a node that exists in the other map and is equal,
although not necessarily at the same index.The childNodes
NodeLists are equal. This is: they are both null,
or they have the same length and contain equal nodes at the same index. This
is true for Attr nodes as for any other type of node. Note that
normalization can affect equality; to avoid this, nodes should be normalized
before being compared. DocumentType nodes to be equal, the following conditions
must also be satisfied: The following string attributes are equal:
publicId, systemId, internalSubset.The
entities NamedNodeMaps are equal.The
notations NamedNodeMaps are equal. ownerDocument attribute, the specified attribute
for Attr nodes, the isWhitespaceInElementContent
attribute for Text nodes, as well as any user data or event
listeners registered on the nodes.
DOM Level 3 WD- Experimental. Override inherited behavior from NodeImpl to
support deep equal.isEqualNode in interface NodeisEqualNode in class NodeImplarg - The node to compare equality with.true
otherwise false.protected void synchronizeChildren()
Copyright © 2024 HtmlUnit. All rights reserved.