Class ParentNode

All Implemented Interfaces:
Cloneable, EventTarget, Node, NodeList
Direct Known Subclasses:
CoreDocumentImpl, DocumentFragmentImpl, DocumentTypeImpl, ElementImpl, EntityImpl, EntityReferenceImpl

public abstract class ParentNode extends ChildNode
ParentNode inherits from ChildNode and adds the capability of having child nodes. Not every node in the DOM can have children, so only nodes that can should inherit from this class and pay the price for it.

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!

Author:
Arnaud Le Hors, IBM, Joe Kesselman, IBM, Andy Clark, IBM
  • Field Details

    • ownerDocument

      protected CoreDocumentImpl ownerDocument
      Owner document.
    • firstChild

      protected ChildNode firstChild
      First child.
    • fNodeListCache

      protected org.htmlunit.cyberneko.xerces.dom.NodeListCache fNodeListCache
      NodeList cache.
  • Constructor Details

    • ParentNode

      protected ParentNode(CoreDocumentImpl ownerDocument)
      No public constructor; only subclasses of ParentNode should be instantiated, and those normally via a Document's factory methods.
      Parameters:
      ownerDocument - the owner document
  • Method Details

    • cloneNode

      public Node cloneNode(boolean deep)
      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.

      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.

      Specified by:
      cloneNode in interface Node
      Overrides:
      cloneNode in class ChildNode
      Parameters:
      deep - whether to recursively clone all children in the subtree
      Returns:
      the cloned node
      See Also:
    • getOwnerDocument

      public Document getOwnerDocument()
      Finds the Document that this Node belongs to (the document in whose context the Node was created). The Node may or may not currently be part of that Document's actual contents. Find the Document that this Node belongs to (the document in whose context the Node was created). The Node may or may not currently be part of that Document's actual contents.
      Specified by:
      getOwnerDocument in interface Node
      Overrides:
      getOwnerDocument in class NodeImpl
    • setOwnerDocument

      protected void setOwnerDocument(CoreDocumentImpl doc)
      NON-DOM set the ownerDocument of this node and its children
      Overrides:
      setOwnerDocument in class NodeImpl
    • hasChildNodes

      public boolean hasChildNodes()
      Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
      Specified by:
      hasChildNodes in interface Node
      Overrides:
      hasChildNodes in class NodeImpl
    • getChildNodes

      public NodeList getChildNodes()
      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.

      Specified by:
      getChildNodes in interface Node
      Overrides:
      getChildNodes in class NodeImpl
    • getFirstChild

      public Node getFirstChild()
      Specified by:
      getFirstChild in interface Node
      Overrides:
      getFirstChild in class NodeImpl
    • getLastChild

      public Node getLastChild()
      Specified by:
      getLastChild in interface Node
      Overrides:
      getLastChild in class NodeImpl
    • insertBefore

      public Node insertBefore(Node newChild, Node refChild) throws DOMException
      Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
      Specified by:
      insertBefore in interface Node
      Overrides:
      insertBefore in class NodeImpl
      Parameters:
      newChild - 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().
      Returns:
      newChild, in its new state (relocated, or emptied in the case of DocumentNode.)
      Throws:
      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.
    • removeChild

      public Node removeChild(Node oldChild) throws DOMException
      Remove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.
      Specified by:
      removeChild in interface Node
      Overrides:
      removeChild in class NodeImpl
      Returns:
      oldChild, in its new state (removed).
      Throws:
      DOMException - NOT_FOUND_ERR if oldChild is not a child of this node.
      DOMException - NO_MODIFICATION_ALLOWED_ERR if this node is read-only.
    • replaceChild

      public Node replaceChild(Node newChild, Node oldChild) throws DOMException
      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.
      Specified by:
      replaceChild in interface Node
      Overrides:
      replaceChild in class NodeImpl
      Returns:
      oldChild, in its new state (removed).
      Throws:
      DOMException - 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.
    • getTextContent

      public String getTextContent() throws DOMException
      Description copied from class: NodeImpl
      Specified by:
      getTextContent in interface Node
      Overrides:
      getTextContent in class NodeImpl
      Throws:
      DOMException
    • setTextContent

      public void setTextContent(String textContent) throws DOMException
      Description copied from class: NodeImpl
      Specified by:
      setTextContent in interface Node
      Overrides:
      setTextContent in class NodeImpl
      Throws:
      DOMException
    • getLength

      public int getLength()
      NodeList method: Count the immediate children of this node.
      Specified by:
      getLength in interface NodeList
      Overrides:
      getLength in class NodeImpl
      Returns:
      int
    • item

      public Node item(int index)
      NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
      Specified by:
      item in interface NodeList
      Overrides:
      item in class NodeImpl
      Parameters:
      index - int
      Returns:
      org.w3c.dom.Node
    • getChildNodesUnoptimized

      protected final NodeList getChildNodesUnoptimized()
      Create a NodeList to access children that is use by subclass elements that have methods named getLength() or item(int). ChildAndParentNode optimizes getChildNodes() by implementing NodeList itself. However if a subclass Element implements methods with the same name as the NodeList methods, they will override the actually methods in this class.
      Returns:
      a node list
    • isEqualNode

      public boolean isEqualNode(Node arg)
      DOM Level 3 WD- Experimental. Override inherited behavior from NodeImpl to support deep equal.
      Specified by:
      isEqualNode in interface Node
      Overrides:
      isEqualNode in class NodeImpl
    • synchronizeChildren

      protected void synchronizeChildren()
      Override this method in subclass to hook in efficient internal data structure.