Class HtmlElement

All Implemented Interfaces:
Serializable, Cloneable, Element, Node
Direct Known Subclasses:
BaseFrameElement, HtmlAbbreviated, HtmlAcronym, HtmlAddress, HtmlAnchor, HtmlArea, HtmlArticle, HtmlAside, HtmlBackgroundSound, HtmlBase, HtmlBaseFont, HtmlBidirectionalIsolation, HtmlBidirectionalOverride, HtmlBig, HtmlBlockQuote, HtmlBody, HtmlBold, HtmlBreak, HtmlButton, HtmlCanvas, HtmlCaption, HtmlCenter, HtmlCitation, HtmlCode, HtmlData, HtmlDataList, HtmlDefinition, HtmlDefinitionDescription, HtmlDefinitionList, HtmlDefinitionTerm, HtmlDeletedText, HtmlDetails, HtmlDialog, HtmlDirectory, HtmlDivision, HtmlEmbed, HtmlEmphasis, HtmlExample, HtmlFieldSet, HtmlFigure, HtmlFigureCaption, HtmlFont, HtmlFooter, HtmlForm, HtmlFrameSet, HtmlHead, HtmlHeader, HtmlHeading1, HtmlHeading2, HtmlHeading3, HtmlHeading4, HtmlHeading5, HtmlHeading6, HtmlHorizontalRule, HtmlHtml, HtmlImage, HtmlInlineQuotation, HtmlInput, HtmlInsertedText, HtmlItalic, HtmlKeyboard, HtmlLabel, HtmlLayer, HtmlLegend, HtmlLink, HtmlListing, HtmlListItem, HtmlMain, HtmlMap, HtmlMark, HtmlMarquee, HtmlMedia, HtmlMenu, HtmlMeta, HtmlNav, HtmlNoBreak, HtmlNoEmbed, HtmlNoFrames, HtmlNoLayer, HtmlNoScript, HtmlObject, HtmlOption, HtmlOptionGroup, HtmlOrderedList, HtmlOutput, HtmlParagraph, HtmlParameter, HtmlPicture, HtmlPlainText, HtmlPreformattedText, HtmlProgress, HtmlRb, HtmlRp, HtmlRt, HtmlRtc, HtmlRuby, HtmlS, HtmlSample, HtmlScript, HtmlSection, HtmlSelect, HtmlSlot, HtmlSmall, HtmlSource, HtmlSpan, HtmlStrike, HtmlStrong, HtmlStyle, HtmlSubscript, HtmlSummary, HtmlSuperscript, HtmlSvg, HtmlTable, HtmlTableCell, HtmlTableColumn, HtmlTableColumnGroup, HtmlTableRow, HtmlTeletype, HtmlTemplate, HtmlTextArea, HtmlTime, HtmlTitle, HtmlTrack, HtmlUnderlined, HtmlUnknownElement, HtmlUnorderedList, HtmlVariable, HtmlWordBreak, TableRowGroup

public abstract class HtmlElement extends DomElement
An abstract wrapper for HTML elements.
See Also:
  • Field Details

    • TAB_INDEX_OUT_OF_BOUNDS

      public static final Short TAB_INDEX_OUT_OF_BOUNDS
      Constant indicating that a tab index value is out of bounds (less than 0 or greater than 32767).
      See Also:
    • ATTRIBUTE_REQUIRED

      protected static final String ATTRIBUTE_REQUIRED
      Constant 'required'.
      See Also:
    • ATTRIBUTE_CHECKED

      protected static final String ATTRIBUTE_CHECKED
      Constant 'checked'.
      See Also:
    • ATTRIBUTE_HIDDEN

      protected static final String ATTRIBUTE_HIDDEN
      Constant 'hidden'.
      See Also:
  • Constructor Details

    • HtmlElement

      protected HtmlElement(String qualifiedName, SgmlPage page, Map<String,DomAttr> attributes)
      Creates an instance.
      Parameters:
      qualifiedName - the qualified name of the element type to instantiate
      page - the page that contains this element
      attributes - a map ready initialized with the attributes for this element, or null. The map will be stored as is, not copied.
    • HtmlElement

      protected HtmlElement(String namespaceURI, String qualifiedName, SgmlPage page, Map<String,DomAttr> attributes)
      Creates an instance of a DOM element that can have a namespace.
      Parameters:
      namespaceURI - the URI that identifies an XML namespace
      qualifiedName - the qualified name of the element type to instantiate
      page - the page that contains this element
      attributes - a map ready initialized with the attributes for this element, or null. The map will be stored as is, not copied.
  • Method Details

    • setAttributeNS

      protected void setAttributeNS(String namespaceURI, String qualifiedName, String attributeValue, boolean notifyAttributeChangeListeners, boolean notifyMutationObservers)
      Sets the value of the attribute specified by namespace and qualified name.
      Overrides:
      setAttributeNS in class DomElement
      Parameters:
      namespaceURI - the URI that identifies an XML namespace
      qualifiedName - the qualified name (prefix:local) of the attribute
      attributeValue - the value of the attribute
      notifyAttributeChangeListeners - to notify the associated HtmlAttributeChangeListeners
      notifyMutationObservers - to notify MutationObservers or not
    • notifyAttributeChangeListeners

      protected static void notifyAttributeChangeListeners(HtmlAttributeChangeEvent event, HtmlElement element, String oldAttributeValue, boolean notifyMutationObservers)
      Recursively notifies all HtmlAttributeChangeListeners.
      Parameters:
      event - the event
      element - the element
      oldAttributeValue - the old attribute value
      notifyMutationObservers - whether to notify MutationObservers or not
    • setAttributeNode

      public Attr setAttributeNode(Attr attribute)
      Sets the specified attribute. This method may be overridden by subclasses which are interested in specific attribute value changes, but such methods must invoke super.setAttributeNode(), and should consider the value of the cloning parameter when deciding whether or not to execute custom logic.
      Specified by:
      setAttributeNode in interface Element
      Overrides:
      setAttributeNode in class DomElement
      Parameters:
      attribute - the attribute to set
      Returns:
    • removeAttribute

      public void removeAttribute(String attributeName)
      Removes an attribute specified by name from this element.
      Specified by:
      removeAttribute in interface Element
      Overrides:
      removeAttribute in class DomElement
      Parameters:
      attributeName - the attribute attributeName
    • fireHtmlAttributeAdded

      protected void fireHtmlAttributeAdded(HtmlAttributeChangeEvent event)
      Support for reporting HTML attribute changes. This method can be called when an attribute has been added and it will send the appropriate HtmlAttributeChangeEvent to any registered HtmlAttributeChangeListeners.

      Note that this method recursively calls this element's parent's fireHtmlAttributeAdded(HtmlAttributeChangeEvent) method.

      Parameters:
      event - the event
      See Also:
    • fireHtmlAttributeReplaced

      protected void fireHtmlAttributeReplaced(HtmlAttributeChangeEvent event)
      Support for reporting HTML attribute changes. This method can be called when an attribute has been replaced and it will send the appropriate HtmlAttributeChangeEvent to any registered HtmlAttributeChangeListeners.

      Note that this method recursively calls this element's parent's fireHtmlAttributeReplaced(HtmlAttributeChangeEvent) method.

      Parameters:
      event - the event
      See Also:
    • fireHtmlAttributeRemoved

      protected void fireHtmlAttributeRemoved(HtmlAttributeChangeEvent event)
      Support for reporting HTML attribute changes. This method can be called when an attribute has been removed and it will send the appropriate HtmlAttributeChangeEvent to any registered HtmlAttributeChangeListeners.

      Note that this method recursively calls this element's parent's fireHtmlAttributeRemoved(HtmlAttributeChangeEvent) method.

      Parameters:
      event - the event
      See Also:
    • getNodeName

      public String getNodeName()
      Description copied from class: DomElement
      Specified by:
      getNodeName in interface Node
      Overrides:
      getNodeName in class DomElement
      Returns:
      the same value as returned by DomElement.getTagName()
    • getTabIndex

      public Short getTabIndex()
      Returns this element's tab index, if it has one. If the tab index is outside of the valid range (less than 0 or greater than 32767), this method returns TAB_INDEX_OUT_OF_BOUNDS. If this element does not have a tab index, or its tab index is otherwise invalid, this method returns null.
      Returns:
      this element's tab index
    • getEnclosingElement

      public HtmlElement getEnclosingElement(String tagName)
      Returns the first element with the specified tag name that is an ancestor to this element, or null if no such element is found.
      Parameters:
      tagName - the name of the tag searched (case insensitive)
      Returns:
      the first element with the specified tag name that is an ancestor to this element
    • getEnclosingForm

      public HtmlForm getEnclosingForm()
      Returns the form which contains this element, or null if this element is not inside of a form.
      Returns:
      the form which contains this element
    • getEnclosingFormOrDie

      public HtmlForm getEnclosingFormOrDie()
      Returns the form which contains this element. If this element is not inside a form, this method throws an IllegalStateException.
      Returns:
      the form which contains this element
    • type

      public void type(String text) throws IOException
      Simulates typing the specified text while this element has focus. Note that for some elements, typing '\n' submits the enclosed form.
      Parameters:
      text - the text you with to simulate typing
      Throws:
      IOException - If an IO error occurs
    • type

      public Page type(char c) throws IOException
      Simulates typing the specified character while this element has focus, returning the page contained by this element's window after typing. Note that it may or may not be the same as the original page, depending on the JavaScript event handlers, etc. Note also that for some elements, typing '\n' submits the enclosed form.
      Parameters:
      c - the character you wish to simulate typing
      Returns:
      the page that occupies this window after typing
      Throws:
      IOException - if an IO error occurs
    • type

      public Page type(int keyCode)
      Simulates typing the specified key code while this element has focus, returning the page contained by this element's window after typing. Note that it may or may not be the same as the original page, depending on the JavaScript event handlers, etc. Note also that for some elements, typing XXXXXXXXXXX submits the enclosed form.

      An example of predefined values is KeyboardEvent.DOM_VK_PAGE_DOWN.

      Parameters:
      keyCode - the key code to simulate typing
      Returns:
      the page that occupies this window after typing
    • type

      public Page type(Keyboard keyboard) throws IOException
      Simulates typing the specified Keyboard while this element has focus, returning the page contained by this element's window after typing. Note that it may or may not be the same as the original page, depending on the JavaScript event handlers, etc. Note also that for some elements, typing XXXXXXXXXXX submits the enclosed form.
      Parameters:
      keyboard - the keyboard
      Returns:
      the page that occupies this window after typing
      Throws:
      IOException - if an IO error occurs
    • doType

      protected void doType(char c, boolean lastType)
      Performs the effective type action, called after the keyPress event and before the keyUp event.
      Parameters:
      c - the character you with to simulate typing
      lastType - is this the last character to type
    • doType

      protected void doType(int keyCode, boolean lastType)
      Performs the effective type action, called after the keyPress event and before the keyUp event.

      An example of predefined values is KeyboardEvent.DOM_VK_PAGE_DOWN.

      Parameters:
      keyCode - the key code wish to simulate typing
      lastType - is this the last to type
    • typeDone

      protected void typeDone(String newValue, boolean notifyAttributeChangeListeners)
      Called from DoTypeProcessor.
      Parameters:
      newValue - the new value
      notifyAttributeChangeListeners - to notify the associated HtmlAttributeChangeListeners
    • acceptChar

      protected boolean acceptChar(char c)
      Indicates if the provided character can by "typed" in the element.
      Parameters:
      c - the character
      Returns:
      true if it is accepted
    • isSubmittableByEnter

      protected boolean isSubmittableByEnter()
      Returns true if clicking Enter (ASCII 10, or '\n') should submit the enclosed form (if any). The default implementation returns false.
      Returns:
      true if clicking Enter should submit the enclosed form (if any)
    • getOneHtmlElementByAttribute

      public final <E extends HtmlElement> E getOneHtmlElementByAttribute(String elementName, String attributeName, String attributeValue) throws ElementNotFoundException
      Searches for an element based on the specified criteria, returning the first element which matches said criteria. Only elements which are descendants of this element are included in the search.
      Type Parameters:
      E - the sub-element type
      Parameters:
      elementName - the name of the element to search for
      attributeName - the name of the attribute to search for
      attributeValue - the value of the attribute to search for
      Returns:
      the first element which matches the specified search criteria
      Throws:
      ElementNotFoundException - if no element matches the specified search criteria
    • getElementsByAttribute

      public final <E extends HtmlElement> List<E> getElementsByAttribute(String elementName, String attributeName, String attributeValue)
      Returns all elements which are descendants of this element and match the specified search criteria.
      Type Parameters:
      E - the sub-element type
      Parameters:
      elementName - the name of the element to search for
      attributeName - the name of the attribute to search for
      attributeValue - the value of the attribute to search for
      Returns:
      all elements which are descendants of this element and match the specified search criteria
    • appendChildIfNoneExists

      public final HtmlElement appendChildIfNoneExists(String tagName)
      Appends a child element to this HTML element with the specified tag name if this HTML element does not already have a child with that tag name. Returns the appended child element, or the first existent child element with the specified tag name if none was appended.
      Parameters:
      tagName - the tag name of the child to append
      Returns:
      the added child, or the first existing child if none was added
    • removeChild

      public final void removeChild(String tagName, int i)
      Removes the ith child element with the specified tag name from all relationships, if possible.
      Parameters:
      tagName - the tag name of the child to remove
      i - the index of the child to remove
    • hasEventHandlers

      public final boolean hasEventHandlers(String eventName)
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Returns true if this element has any JavaScript functions that need to be executed when the specified event occurs.
      Parameters:
      eventName - the name of the event, such as "onclick" or "onblur", etc
      Returns:
      true if an event handler has been defined otherwise false
    • addHtmlAttributeChangeListener

      public void addHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
      Adds an HtmlAttributeChangeListener to the listener list. The listener is registered for all attributes of this HtmlElement, as well as descendant elements.
      Parameters:
      listener - the attribute change listener to be added
      See Also:
    • removeHtmlAttributeChangeListener

      public void removeHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
      Removes an HtmlAttributeChangeListener from the listener list. This method should be used to remove HtmlAttributeChangeListener that were registered for all attributes of this HtmlElement, as well as descendant elements.
      Parameters:
      listener - the attribute change listener to be removed
      See Also:
    • checkChildHierarchy

      protected void checkChildHierarchy(Node childNode) throws DOMException
      Check for insertion errors for a new child node. This is overridden by derived classes to enforce which types of children are allowed.
      Overrides:
      checkChildHierarchy in class DomNode
      Parameters:
      childNode - the new child node that is being inserted below this node
      Throws:
      DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors or this node itself, or if this node is of type Document and the DOM application attempts to insert a second DocumentType or Element node. WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
    • setOwningForm

      public void setOwningForm(HtmlForm form)
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Allows the parser to connect to a form that is not a parent of this due to malformed HTML code
      Parameters:
      form - the owning form
    • isAttributeCaseSensitive

      protected boolean isAttributeCaseSensitive()
      Indicates if the attribute names are case sensitive.
      Overrides:
      isAttributeCaseSensitive in class DomElement
      Returns:
      false
    • getLangAttribute

      public final String getLangAttribute()
      Returns the value of the attribute lang. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute lang or an empty string if that attribute isn't defined
    • getXmlLangAttribute

      public final String getXmlLangAttribute()
      Returns the value of the attribute xml:lang. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute xml:lang or an empty string if that attribute isn't defined
    • getTextDirectionAttribute

      public final String getTextDirectionAttribute()
      Returns the value of the attribute dir. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute dir or an empty string if that attribute isn't defined
    • getOnClickAttribute

      public final String getOnClickAttribute()
      Returns the value of the attribute onclick. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onclick or an empty string if that attribute isn't defined
    • getOnDblClickAttribute

      public final String getOnDblClickAttribute()
      Returns the value of the attribute ondblclick. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute ondblclick or an empty string if that attribute isn't defined
    • getOnMouseDownAttribute

      public final String getOnMouseDownAttribute()
      Returns the value of the attribute onmousedown. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onmousedown or an empty string if that attribute isn't defined
    • getOnMouseUpAttribute

      public final String getOnMouseUpAttribute()
      Returns the value of the attribute onmouseup. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onmouseup or an empty string if that attribute isn't defined
    • getOnMouseOverAttribute

      public final String getOnMouseOverAttribute()
      Returns the value of the attribute onmouseover. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onmouseover or an empty string if that attribute isn't defined
    • getOnMouseMoveAttribute

      public final String getOnMouseMoveAttribute()
      Returns the value of the attribute onmousemove. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onmousemove or an empty string if that attribute isn't defined
    • getOnMouseOutAttribute

      public final String getOnMouseOutAttribute()
      Returns the value of the attribute onmouseout. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onmouseout or an empty string if that attribute isn't defined
    • getOnKeyPressAttribute

      public final String getOnKeyPressAttribute()
      Returns the value of the attribute onkeypress. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onkeypress or an empty string if that attribute isn't defined
    • getOnKeyDownAttribute

      public final String getOnKeyDownAttribute()
      Returns the value of the attribute onkeydown. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onkeydown or an empty string if that attribute isn't defined
    • getOnKeyUpAttribute

      public final String getOnKeyUpAttribute()
      Returns the value of the attribute onkeyup. Refer to the HTML 4.01 documentation for details on the use of this attribute.
      Returns:
      the value of the attribute onkeyup or an empty string if that attribute isn't defined
    • getCanonicalXPath

      public String getCanonicalXPath()

      Returns the canonical XPath expression which identifies this node, for instance "/html/body/table[3]/tbody/tr[5]/td[2]/span/a[3]".

      WARNING: This sort of automated XPath expression is often quite bad at identifying a node, as it is highly sensitive to changes in the DOM tree.

      Overrides:
      getCanonicalXPath in class DomNode
      Returns:
      the canonical XPath expression which identifies this node
      See Also:
    • isHidden

      public boolean isHidden()
      Returns:
      true if the hidden attribute is set.
    • setHidden

      public void setHidden(String hidden)
      Sets the hidden property.
      Parameters:
      hidden - the hidden property
    • setHidden

      public void setHidden(boolean hidden)
      Sets the hidden property.
      Parameters:
      hidden - the hidden property
    • isDisplayed

      public boolean isDisplayed()

      Returns true if this node is displayed and can be visible to the user (ignoring screen size, scrolling limitations, color, font-size, or overlapping nodes).

      NOTE: If CSS is disabled, this method does not take this element's style into consideration!

      Overwritten to support the hidden attribute (html5).
      Overrides:
      isDisplayed in class DomNode
      Returns:
      true if the node is visible to the user, false otherwise
      See Also:
    • getDefaultStyleDisplay

      public HtmlElement.DisplayStyle getDefaultStyleDisplay()
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Returns the default display style.
      Returns:
      the default display style
    • getSrcAttributeNormalized

      protected final String getSrcAttributeNormalized()
      Helper for src retrieval and normalization.
      Returns:
      the value of the attribute src with all line breaks removed or an empty string if that attribute isn't defined.
    • detach

      protected void detach()
      INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Detach this node from all relationships with other nodes. This is the first step of a move.
      Overrides:
      detach in class DomNode
    • handles

      public boolean handles(org.htmlunit.javascript.host.event.Event event)
      Indicates if the provided event can be applied to this node. Overwrite this.
      Overrides:
      handles in class DomNode
      Parameters:
      event - the event
      Returns:
      false if the event can't be applied
    • isShiftPressed

      protected boolean isShiftPressed()
      Returns whether the SHIFT is currently pressed.
      Returns:
      whether the SHIFT is currently pressed
    • isCtrlPressed

      public boolean isCtrlPressed()
      Returns whether the CTRL is currently pressed.
      Returns:
      whether the CTRL is currently pressed
    • isAltPressed

      public boolean isAltPressed()
      Returns whether the ALT is currently pressed.
      Returns:
      whether the ALT is currently pressed
    • isValid

      public boolean isValid()
      Returns whether this element satisfies all form validation constraints set.
      Returns:
      whether this element satisfies all form validation constraints set
    • isRequiredSupported

      protected boolean isRequiredSupported()
      Returns whether this element supports the required constraint.
      Returns:
      whether this element supports the required constraint
    • isRequired

      public boolean isRequired()
      Returns:
      the true if the required attribute is set
    • isOptional

      public boolean isOptional()
      Returns:
      the true if the required attribute is supported and set
    • setRequired

      public void setRequired(boolean required)
      Sets the required attribute.
      Parameters:
      required - the new attribute value
    • cloneNode

      public DomNode cloneNode(boolean deep)
      Specified by:
      cloneNode in interface Node
      Overrides:
      cloneNode in class DomElement