public final class Element extends Segment
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:
StartTag.getElement()
EndTag.getElement()
Segment.getAllElements()
Segment.getAllElements(String name)
Segment.getAllElements(StartTagType)
See also the HTMLElements
class, and the
XML 1.0 specification for elements.
The three possible structures of an element are listed below:
<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:
<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:
<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.
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.
StartTagType.NORMAL
:
isEmptyElementTag()
method for more information.
StartTagType.NORMAL
:
HTMLElements
Modifier and Type | Method and Description |
---|---|
Attributes |
getAttributes()
Returns the attributes specified in this element's start tag.
|
java.lang.String |
getAttributeValue(java.lang.String attributeName)
Returns the decoded value of the attribute with the specified name (case insensitive).
|
java.util.List<Element> |
getChildElements()
Returns a list of the immediate children of this element in the document element hierarchy.
|
Segment |
getContent()
Returns the segment representing the content of the element.
|
java.lang.String |
getDebugInfo()
Returns a string representation of this object useful for debugging purposes.
|
int |
getDepth()
Returns the nesting depth of this element in the document element hierarchy.
|
EndTag |
getEndTag()
Returns the end tag of the element.
|
FormControl |
getFormControl()
Returns the
FormControl defined by this element. |
java.lang.String |
getName()
|
Element |
getParentElement()
Returns the parent of this element in the document element hierarchy.
|
StartTag |
getStartTag()
Returns the start tag of the element.
|
boolean |
isEmpty()
Indicates whether this element has zero-length content.
|
boolean |
isEmptyElementTag()
Indicates whether this element is an empty-element tag.
|
charAt, compareTo, encloses, encloses, equals, getAllCharacterReferences, getAllElements, getAllElements, getAllElements, getAllElements, getAllElements, getAllElementsByClass, getAllStartTags, getAllStartTags, getAllStartTags, getAllStartTags, getAllStartTags, getAllStartTagsByClass, getAllTags, getAllTags, getBegin, getEnd, getFirstElement, getFirstElement, getFirstElement, getFirstElement, getFirstElementByClass, getFirstStartTag, getFirstStartTag, getFirstStartTag, getFirstStartTag, getFirstStartTag, getFirstStartTagByClass, getFormControls, getFormFields, getMaxDepthIndicator, getNodeIterator, getRenderer, getRowColumnVector, getSource, getStyleURISegments, getTextExtractor, getURIAttributes, hashCode, ignoreWhenParsing, isWhiteSpace, isWhiteSpace, length, parseAttributes, subSequence, toString
public Element getParentElement()
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.
null
if this element is a top-level element.java.lang.IllegalStateException
- if a full sequential parse has not been performed or if it was performed after this element was found.getChildElements()
public final java.util.List<Element> getChildElements()
The objects in the list are all of type Element
.
See the Source.getChildElements()
method for more details.
getChildElements
in class Segment
null
.getParentElement()
public int getDepth()
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.
java.lang.IllegalStateException
- if a full sequential parse has not been performed or if it was performed after this element was found.getParentElement()
public Segment getContent()
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,
null
.public StartTag getStartTag()
public EndTag getEndTag()
If the element has no end tag this method returns null
.
null
if the element has no end tag.public java.lang.String getName()
This is equivalent to getStartTag()
.
getName()
.
See the Tag.getName()
method for more information.
public boolean isEmpty()
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.
true
if this element has zero-length content, otherwise false
.isEmptyElementTag()
public boolean isEmptyElementTag()
This is equivalent to getStartTag()
.
isEmptyElementTag()
.
true
if this element is an empty-element tag, otherwise false
.public Attributes getAttributes()
This is equivalent to getStartTag()
.
getAttributes()
.
StartTag.getAttributes()
public java.lang.String getAttributeValue(java.lang.String attributeName)
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)
.
attributeName
- the name of the attribute to get.null
if the attribute does not exist or has no value.public FormControl getFormControl()
FormControl
defined by this element.FormControl
defined by this element, or null
if it is not a control.public java.lang.String getDebugInfo()
Segment
getDebugInfo
in class Segment