Class Element

All Implemented Interfaces:
CharSequence, Comparable<Segment>

public final class Element extends Segment
Represents an element in a specific source document, which encompasses a start tag, an optional end tag and all content in between.

Take the following HTML segment as an example:

<p>This is a sample paragraph.</p>

The whole segment is represented by an Element object. This is comprised of the StartTag "<p>", the EndTag "</p>", as well as the text in between. An element may also contain other elements between its start and end tags.

The term normal element refers to an element having a start tag with a type of StartTagType.NORMAL. This comprises all HTML elements and non-HTML elements.

Element instances are obtained using one of the following methods:

See also the HTMLElements class, and the XML 1.0 specification for elements.

Element Structure

The three possible structures of an element are listed below:

Single Tag Element:
Example:
<img src="mypicture.jpg">

The element consists only of a single start tag and has no element content (although the start tag itself may have tag content).
getEndTag()==null
isEmpty()==true
getEnd()==getStartTag().getEnd()

This occurs in the following situations:

Explicitly Terminated Element:
Example:
<p>This is a sample paragraph.</p>

The element consists of a start tag, content, and an end tag.
getEndTag()!=null.
isEmpty()==false (provided the end tag doesn't immediately follow the start tag)
getEnd()==getEndTag().getEnd().

This occurs in the following situations, assuming the start tag's matching end tag is present in the source document:

Implicitly Terminated Element:
Example:
<p>This text is included in the paragraph element even though no end tag is present.
<p>This is the next paragraph.

The element consists of a start tag and content, but no end tag.
getEndTag()==null.
isEmpty()==false
getEnd()!=getStartTag().getEnd().

This only occurs in an HTML element for which the end tag is optional.

The element ends at the start of a tag which implies the termination of the element, called the implicitly terminating tag. If the implicitly terminating tag is situated immediately after the element's start tag, the element is classed as a single tag element.

See the element parsing rules for HTML elements with optional end tags for details on which tags can implicitly terminate a given element.

See also the documentation of the HTMLElements.getEndTagOptionalElementNames() method.

Element Parsing Rules

The following rules describe the algorithm used in the StartTag.getElement() method to construct an element. The detection of the start tag's matching end tag or other terminating tags always takes into account the possible nesting of elements.

See Also:
  • Method Details

    • getParentElement

      public Element getParentElement()
      Returns the parent of this element in the document element hierarchy.

      The Source.fullSequentialParse() method must be called (either explicitly or implicitly) immediately after construction of the Source object if this method is to be used. An IllegalStateException is thrown if a full sequential parse has not been performed or if it was performed after this element was found.

      This method returns null for a top-level element, as well as any element formed from a server tag, regardless of whether it is nested inside a normal element.

      See the Source.getChildElements() method for more details.

      Returns:
      the parent of this element in the document element hierarchy, or null if this element is a top-level element.
      Throws:
      IllegalStateException - if a full sequential parse has not been performed or if it was performed after this element was found.
      See Also:
    • getChildElements

      public final List<Element> getChildElements()
      Returns a list of the immediate children of this element in the document element hierarchy.

      The objects in the list are all of type Element.

      See the Source.getChildElements() method for more details.

      Overrides:
      getChildElements in class Segment
      Returns:
      a list of the immediate children of this element in the document element hierarchy, guaranteed not null.
      See Also:
    • getDepth

      public int getDepth()
      Returns the nesting depth of this element in the document element hierarchy.

      The Source.fullSequentialParse() method must be called (either explicitly or implicitly) after construction of the Source object if this method is to be used. An IllegalStateException is thrown if a full sequential parse has not been performed or if it was performed after this element was found.

      A top-level element has a nesting depth of 0.

      An element formed from a server tag always have a nesting depth of 0, regardless of whether it is nested inside a normal element.

      See the Source.getChildElements() method for more details.

      Returns:
      the nesting depth of this element in the document element hierarchy.
      Throws:
      IllegalStateException - if a full sequential parse has not been performed or if it was performed after this element was found.
      See Also:
    • getContent

      public Segment getContent()
      Returns the segment representing the content of the element.

      This segment spans between the end of the start tag and the start of the end tag. If the end tag is not present, the content reaches to the end of the element.

      A zero-length segment is returned if the element is empty,

      Returns:
      the segment representing the content of the element, guaranteed not null.
    • getStartTag

      public StartTag getStartTag()
      Returns the start tag of the element.
      Returns:
      the start tag of the element.
    • getEndTag

      public EndTag getEndTag()
      Returns the end tag of the element.

      If the element has no end tag this method returns null.

      Returns:
      the end tag of the element, or null if the element has no end tag.
    • getName

      public String getName()
      Returns the name of the start tag of this element, always in lower case.

      This is equivalent to getStartTag().getName().

      See the Tag.getName() method for more information.

      Returns:
      the name of the start tag of this element, always in lower case.
    • isEmpty

      public boolean isEmpty()
      Indicates whether this element has zero-length content.

      This is equivalent to getContent().length()==0.

      Note that this is a broader definition than that of both the HTML definition of an empty element, which is only those elements whose end tag is forbidden, and the XML definition of an empty element, which is "either a start-tag immediately followed by an end-tag, or an empty-element tag". The other possibility covered by this property is the case of an HTML element with an optional end tag that is immediately followed by another tag that implicitly terminates the element.

      Returns:
      true if this element has zero-length content, otherwise false.
      See Also:
    • isEmptyElementTag

      public boolean isEmptyElementTag()
      Indicates whether this element is an empty-element tag.

      This is equivalent to getStartTag().isEmptyElementTag().

      Returns:
      true if this element is an empty-element tag, otherwise false.
    • getAttributes

      public Attributes getAttributes()
      Returns the attributes specified in this element's start tag.

      This is equivalent to getStartTag().getAttributes().

      Returns:
      the attributes specified in this element's start tag.
      See Also:
    • getAttributeValue

      public String getAttributeValue(String attributeName)
      Returns the decoded value of the attribute with the specified name (case insensitive).

      Returns null if the start tag of this element does not have attributes, no attribute with the specified name exists or the attribute has no value.

      This is equivalent to getStartTag().getAttributeValue(attributeName).

      Parameters:
      attributeName - the name of the attribute to get.
      Returns:
      the decoded value of the attribute with the specified name, or null if the attribute does not exist or has no value.
    • getFormControl

      public FormControl getFormControl()
      Returns the FormControl defined by this element.
      Returns:
      the FormControl defined by this element, or null if it is not a control.
    • getDebugInfo

      public String getDebugInfo()
      Description copied from class: Segment
      Returns a string representation of this object useful for debugging purposes.
      Overrides:
      getDebugInfo in class Segment
      Returns:
      a string representation of this object useful for debugging purposes.