Class DomFragment

All Implemented Interfaces:
Cloneable

@NoTests public final class DomFragment extends DomContainer
Fragment of PushMode DOM tree. Fragment can have children just like DomElement, but it has no attributes nor other element properties. It's a thin extension of DomContainer.

Fragment can be added to any DomContainer, including another DomFragment. When added, the fragment is automatically inlined. Fragment thus cannot appear as a child of DomElement or another DomFragment.

It is recommended that application methods take and return DomContent instead of DomElement, which enables methods to pass around any combination of elements and plain text as DomFragment, including an empty fragment, instead of just a single DomElement.

  • Constructor Details

    • DomFragment

      public DomFragment()
      Creates an empty DOM fragment.
    • DomFragment

      public DomFragment(DomFragment other)
      Creates deep mutable copy of the DOM fragment. Calling this constructor is equivalent to calling clone().
      Parameters:
      other - fragment to copy
      See Also:
  • Method Details

    • clone

      public DomFragment clone()
      Creates mutable deep clone of this DomFragment. All child nodes are cloned recursively. The clone is completely independent of this fragment.
      Specified by:
      clone in class DomContainer
      Returns:
      deep mutable clone
      See Also:
    • assign

      public DomFragment assign(DomFragment other)
      Replaces contents of this fragment with contents of another fragment. Since fragment is merely a list of child nodes, these child nodes are copied from the source fragment into this fragment. Previous content of this fragment is discarded.

      Most of the DOM API consists of builder-like API to construct DOM trees. When DOM tree must be modified, it is recommended to completely rebuild it, perhaps using bits of the old tree in the process, and return the new tree. However, it is sometimes not possible to change the returned pointer and in those cases the fragment must be actually modified. Application should build new fragment as usual and then use this method to replace contents of the original fragment.

      Parameters:
      other - source fragment to be assigned to this fragment
      Returns:
      this
      Throws:
      IllegalStateException - if this fragment is frozen
      See Also:
    • equals

      public boolean equals(Object object)
      Compares content of two fragments. If the supplied object is not DomFragment, this method returns false. Equality is unaffected by whether the fragments are frozen or not. Children of both fragments are compared recursively by calling their DomContent.equals(Object) methods.
      Overrides:
      equals in class DomContainer
      Parameters:
      object - object to compare this fragment with
      Returns:
      true if the two fragments are equal, false otherwise
    • hashCode

      public int hashCode()
      Computes hash code of this fragment. It is computed by combining hash codes of all children. Hash code is unaffected by whether the fragment is frozen or not.
      Overrides:
      hashCode in class DomContainer
      Returns:
      fragment's hash code
    • add

      public DomFragment add(DomContent child)
      Adds new child node to this fragment. Fragments will be inlined, nulls ignored, and text concatenated.
      Overrides:
      add in class DomContainer
      Parameters:
      child - node to add (ignored if null)
      Returns:
      this
      Throws:
      IllegalStateException - if this element or fragment is frozen
      See Also:
    • add

      public <C extends DomContent> DomFragment add(Collection<C> children)
      Adds all nodes in a Collection to this fragment. Fragments will be inlined, nulls ignored, and text concatenated.
      Overrides:
      add in class DomContainer
      Type Parameters:
      C - item type
      Parameters:
      children - collection of child nodes to add (ignored if null)
      Returns:
      this
      Throws:
      IllegalStateException - if this element or fragment is frozen
      See Also:
    • add

      public <C extends DomContent> DomFragment add(Stream<C> children)
      Adds all nodes in a Stream to this fragment. Fragments will be inlined, nulls ignored, and text concatenated.
      Overrides:
      add in class DomContainer
      Type Parameters:
      C - item type
      Parameters:
      children - Stream of child nodes to add (ignored if null)
      Returns:
      this
      Throws:
      IllegalStateException - if this element or fragment is frozen
      See Also:
    • add

      public DomFragment add(String text)
      Adds literal text to this fragment Consecutive text nodes will be concatenated.
      Overrides:
      add in class DomContainer
      Parameters:
      text - text to add (ignored if null or empty)
      Returns:
      this
      Throws:
      IllegalStateException - if this element or fragment is frozen
      See Also:
    • freeze

      public DomFragment freeze()
      Protects this fragment from further modification. For more information, use cases, and thread safety, see DomContent.freeze().
      Overrides:
      freeze in class DomContainer
      Returns:
      this
      See Also:
    • join

      @NoDocs @DraftApi("separate overloads for Collection and Stream") public static DomFragment join(DomContent separator, Iterable<? extends DomContent> items)
    • join

      @NoDocs @DraftApi("separate overloads for Collection and Stream") public static DomFragment join(String separator, Iterable<? extends DomContent> items)