Module org.htmlunit

Class Element

java.lang.Object
org.htmlunit.corejs.javascript.SlotMapOwner<org.htmlunit.corejs.javascript.Scriptable>
org.htmlunit.corejs.javascript.ScriptableObject
All Implemented Interfaces:
Serializable, Cloneable, org.htmlunit.corejs.javascript.ConstProperties<org.htmlunit.corejs.javascript.Scriptable>, org.htmlunit.corejs.javascript.debug.DebuggableObject, org.htmlunit.corejs.javascript.PropHolder<org.htmlunit.corejs.javascript.Scriptable>, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.SymbolScriptable
Direct Known Subclasses:
HTMLElement, SVGElement

public class Element extends Node
JavaScript host object for Element.
Author:
Ahmed Ashour, Marc Guillemot, Sudhan Moghe, Ronald Brill, Frank Danek, Anton Demydenko
See Also:
  • Constructor Details

    • Element

      public Element()
  • Method Details

    • jsConstructor

      public void jsConstructor()
      Creates an instance of this object.
      Overrides:
      jsConstructor in class Node
    • setDomNode

      public void setDomNode(DomNode domNode)
      Sets the DOM node that corresponds to this JavaScript object.
      Overrides:
      setDomNode in class HtmlUnitScriptable
      Parameters:
      domNode - the DOM node
    • createEventHandler

      protected void createEventHandler(String eventName, String attrValue)
      Creates the event handler function from the attribute value.
      Parameters:
      eventName - the event name (e.g. onclick)
      attrValue - the attribute value
    • getTagName

      public String getTagName()
      Returns the tag name of this element.
      Returns:
      the tag name
    • getAttributes

      public NamedNodeMap getAttributes()
      Returns the attributes of this XML element.
      Overrides:
      getAttributes in class Node
      Returns:
      the attributes of this XML element
      See Also:
    • createAttributesObject

      protected NamedNodeMap createAttributesObject()
      Creates the JS object for the attributes property. This object will be cached.
      Returns:
      the JS object
    • getAttribute

      public String getAttribute(String attributeName)
      Returns the value of the specified attribute, or null if the attribute is not defined.
      Parameters:
      attributeName - the name of the attribute to retrieve
      Returns:
      the value of the specified attribute, or null if not defined
    • setAttribute

      public void setAttribute(String name, String value)
      Sets the specified attribute to the given value.
      Parameters:
      name - the name of the attribute to set
      value - the value to set the attribute to
    • getElementsByTagName

      public HTMLCollection getElementsByTagName(String tagName)
      Returns all descendant elements with the specified tag name.
      Parameters:
      tagName - the tag name to search for
      Returns:
      all descendant elements with the specified tag name
    • getAttributeNode

      public HtmlUnitScriptable getAttributeNode(String name)
      Retrieves an attribute node by name.
      Parameters:
      name - the name of the attribute to retrieve
      Returns:
      the Attr node with the specified name, or null if there is no such attribute
    • getElementsByTagNameNS

      public HTMLCollection getElementsByTagNameNS(Object namespaceURI, String localName)
      Returns a live HTMLCollection of elements with the given tag name belonging to the given namespace.
      Parameters:
      namespaceURI - the namespace URI of elements to look for
      localName - the local name of elements to look for, or "*" to match all elements
      Returns:
      a live HTMLCollection of found elements in document order
    • hasAttribute

      public boolean hasAttribute(String name)
      Returns true when an attribute with the given name is specified on this element or has a default value.
      Parameters:
      name - the name of the attribute to look for
      Returns:
      true if the attribute exists or has a default value
    • hasAttributes

      public boolean hasAttributes()
      Returns whether this node has any attributes.
      Overrides:
      hasAttributes in class Node
      Returns:
      true if this node has one or more attributes
    • getDomNodeOrDie

      public DomElement getDomNodeOrDie()
      Returns the DOM node that corresponds to this JavaScript object or throw an exception if one cannot be found.
      Overrides:
      getDomNodeOrDie in class HtmlUnitScriptable
      Returns:
      the DOM node
    • removeAttribute

      public void removeAttribute(String name)
      Removes the attribute with the specified name.
      Parameters:
      name - the name of the attribute to remove
    • getBoundingClientRect

      public DOMRect getBoundingClientRect()
      Returns the bounding rectangle of this element relative to the viewport.
      Returns:
      a DOMRect object describing the element's size and position
      See Also:
    • getChildElementCount

      public int getChildElementCount()
      Returns the current number of child elements.
      Overrides:
      getChildElementCount in class Node
      Returns:
      the child element count
    • getFirstElementChild

      public Element getFirstElementChild()
      Returns the first element child.
      Overrides:
      getFirstElementChild in class Node
      Returns:
      the first element child
    • getLastElementChild

      public Element getLastElementChild()
      Returns the last element child.
      Overrides:
      getLastElementChild in class Node
      Returns:
      the last element child
    • getNextElementSibling

      public Element getNextElementSibling()
      Returns the next sibling that is an element.
      Returns:
      the next element sibling, or null if none
    • getPreviousElementSibling

      public Element getPreviousElementSibling()
      Returns the previous sibling that is an element.
      Returns:
      the previous element sibling, or null if none
    • getParentElement

      public Element getParentElement()
      Returns the first ancestor that is an Element. Skips non-Element nodes.
      Overrides:
      getParentElement in class Node
      Returns:
      the parent element
      See Also:
    • getChildren

      public HTMLCollection getChildren()
      Returns the child elements of this node.
      Overrides:
      getChildren in class Node
      Returns:
      a live collection of this node's child elements
      See Also:
    • getClassList

      public DOMTokenList getClassList()
      Returns the token list of the class attribute.
      Returns:
      the token list of the class attribute
    • getAttributeNS

      public String getAttributeNS(String namespaceURI, String localName)
      Returns the value of the specified attribute in the given namespace, or null if the attribute is not defined.
      Parameters:
      namespaceURI - the namespace URI
      localName - the local name of the attribute to retrieve
      Returns:
      the attribute value, or null if not found
    • hasAttributeNS

      public boolean hasAttributeNS(String namespaceURI, String localName)
      Returns true if the element has an attribute with the given namespace URI and local name.
      Parameters:
      namespaceURI - the namespace URI
      localName - the local name of the attribute to look for
      Returns:
      true if the attribute exists
    • setAttributeNS

      public void setAttributeNS(String namespaceURI, String qualifiedName, String value)
      Sets the attribute with the given namespace URI and qualified name to the given value.
      Parameters:
      namespaceURI - the namespace URI
      qualifiedName - the qualified name of the attribute
      value - the new attribute value
    • removeAttributeNS

      public void removeAttributeNS(String namespaceURI, String localName)
      Removes the attribute with the given namespace URI and local name.
      Parameters:
      namespaceURI - the namespace URI of the attribute to remove
      localName - the local name of the attribute to remove
    • setAttributeNode

      public Attr setAttributeNode(Attr newAtt)
      Sets the attribute node for the specified attribute, replacing the existing node if present.
      Parameters:
      newAtt - the attribute node to set
      Returns:
      the replaced attribute node, if any
    • querySelectorAll

      public NodeList querySelectorAll(String selectors)
      Returns a static NodeList of all descendant elements matching the given CSS selector(s).
      Parameters:
      selectors - the CSS selector(s)
      Returns:
      the static node list of matching elements
    • querySelector

      public Node querySelector(String selectors)
      Returns the first descendant element that matches the specified CSS selector, or null if no matches are found.
      Parameters:
      selectors - the CSS selector(s)
      Returns:
      the first matching element, or null
    • getClassName_js

      public String getClassName_js()
      Returns the value of the class attribute.
      Returns:
      the class name
    • setClassName_js

      public void setClassName_js(String className)
      Sets the class attribute for this element.
      Parameters:
      className - the new class name
    • getClientHeight

      public int getClientHeight()
      Returns the clientHeight property.
      Returns:
      the clientHeight property
    • getClientWidth

      public int getClientWidth()
      Returns the clientWidth property.
      Returns:
      the clientWidth property
    • getClientLeft

      public int getClientLeft()
      Returns the clientLeft property.
      Returns:
      the clientLeft property
    • getClientTop

      public int getClientTop()
      Returns the clientTop property.
      Returns:
      the clientTop property
    • getAttributeNodeNS

      public HtmlUnitScriptable getAttributeNodeNS(String namespaceURI, String localName)
      Returns the attribute node with the given namespace URI and local name.
      Parameters:
      namespaceURI - the namespace URI
      localName - the local name of the attribute to retrieve
      Returns:
      the specified attribute node, or null if not found
    • getElementsByClassName

      public HTMLCollection getElementsByClassName(String className)
      Returns all descendant elements that have the specified class name(s).
      Parameters:
      className - the class name(s) to search for (space-separated)
      Returns:
      all descendant elements with the specified class name(s)
    • getClientRects

      public DOMRectList getClientRects()
      Returns a collection of DOMRect objects that describe the layout of the element's content on screen. Each rectangle represents one line of the element's content.
      Returns:
      a DOMRectList describing the element's line boxes
    • getAttributeNames

      public org.htmlunit.corejs.javascript.Scriptable getAttributeNames()
      Returns the attribute names of this element as an array of strings. Returns an empty array if the element has no attributes.
      Returns:
      the attribute names as an array
    • isDisplayNone

      protected final boolean isDisplayNone()
      Returns whether the display style of this element or any ancestor is none.
      Returns:
      true if the element or any ancestor has display: none
    • insertAdjacentElement

      public Node insertAdjacentElement(String where, Object insertedElement)
      Inserts the given element at the specified position relative to this element.
      Parameters:
      where - specifies where to insert the element; one of beforebegin, afterbegin, beforeend, or afterend (case-insensitive)
      insertedElement - the element to insert
      Returns:
      the inserted element
      See Also:
    • insertAdjacentText

      public void insertAdjacentText(String where, String text)
      Inserts the given text at the specified position relative to this element.
      Parameters:
      where - specifies where to insert the text; one of beforebegin, afterbegin, beforeend, or afterend (case-insensitive)
      text - the text to insert
      See Also:
    • insertAdjacentHTML

      public void insertAdjacentHTML(String position, String text)
      Parses the given text as HTML or XML and inserts the resulting nodes at the specified position.
      Parameters:
      position - specifies where to insert the nodes; one of beforebegin, afterbegin, beforeend, or afterend (case-insensitive)
      text - the HTML or XML text to parse and insert
      See Also:
    • moveBefore

      public static void moveBefore(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Moves a given node inside this element as a direct child, before a given reference node.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
    • getHTML

      public String getHTML()
      Returns the contents of this node as HTML, ignoring shadow DOM parameters as shadow DOM is not supported.
      Returns:
      the contents of this node as HTML
    • getInnerHTML

      public String getInnerHTML()
      Returns the innerHTML of this element.
      Returns:
      the contents of this node as HTML
    • setInnerHTML

      public void setInnerHTML(Object value)
      Replaces all child elements of this element with the supplied HTML value.
      Parameters:
      value - the new HTML content for this element
    • getInnerHTML

      protected String getInnerHTML(DomNode domNode)
      Helper for getInnerHTML, reusable by HTMLTemplateElement.
      Parameters:
      domNode - the node to serializep
      Returns:
      the contents of this node as HTML
    • getOuterHTML

      public String getOuterHTML()
      Returns the outerHTML of this element, including the element's own tags.
      Returns:
      the contents of this node as HTML, including the opening and closing tags
      See Also:
    • setOuterHTML

      public void setOuterHTML(Object value)
      Replaces this element (including all child elements) with the supplied HTML value.
      Parameters:
      value - the new HTML to replace this element
    • printChildren

      protected final void printChildren(StringBuilder builder, DomNode node, boolean html, org.htmlunit.javascript.host.Element.TextEscape escape)
    • printNode

      protected void printNode(StringBuilder builder, DomNode node, boolean html, org.htmlunit.javascript.host.Element.TextEscape escape)
      Serializes the children of the given node to the provided builder.
      Parameters:
      builder - the builder to write to
      node - the node whose children are to be serialized
      html - whether to use HTML serialization
      escape - whether to use HTML escaping
    • isEndTagForbidden

      protected boolean isEndTagForbidden()
      Returns whether the end tag is forbidden for this element.
      Returns:
      whether the end tag is forbidden
      See Also:
    • getId

      public String getId()
      Returns the element ID.
      Returns:
      the ID of this element
    • setId

      public void setId(String newId)
      Sets the ID of this element.
      Parameters:
      newId - the new ID value for this element
    • removeAttributeNode

      public void removeAttributeNode(Attr attribute)
      Removes the specified attribute node from this element.
      Parameters:
      attribute - the attribute node to remove
    • getScrollTop

      public int getScrollTop()
      Returns the scrollTop value for this element.
      Returns:
      the scrollTop value
      See Also:
    • setScrollTop

      public void setScrollTop(int scroll)
      Sets the scrollTop value for this element.
      Parameters:
      scroll - the new scrollTop value
    • getScrollLeft

      public int getScrollLeft()
      Returns the scrollLeft value for this element.
      Returns:
      the scrollLeft value
      See Also:
    • setScrollLeft

      public void setScrollLeft(int scroll)
      Sets the scrollLeft value for this element.
      Parameters:
      scroll - the new scrollLeft value
    • getScrollHeight

      public int getScrollHeight()
      Returns the scrollHeight for this element. Currently returns the same value as getClientHeight().
      Returns:
      the scroll height
      See Also:
    • getScrollWidth

      public int getScrollWidth()
      Returns the scrollWidth for this element. Currently returns the same value as getClientWidth().
      Returns:
      the scroll width
      See Also:
    • getStyle

      protected CSSStyleDeclaration getStyle()
      Returns the style object for this element.
      Returns:
      the style object for this element
    • setStyle

      protected void setStyle(String style)
      Sets the CSS text for this element's inline style.
      Parameters:
      style - the new CSS style text
    • scroll

      public void scroll(org.htmlunit.corejs.javascript.Scriptable x, org.htmlunit.corejs.javascript.Scriptable y)
      Scrolls to a particular set of coordinates inside this element.
      Parameters:
      x - the horizontal pixel value to scroll to
      y - the vertical pixel value to scroll to
    • scrollBy

      public void scrollBy(org.htmlunit.corejs.javascript.Scriptable x, org.htmlunit.corejs.javascript.Scriptable y)
      Scrolls the element by the given amount.
      Parameters:
      x - the horizontal pixel value to scroll by
      y - the vertical pixel value to scroll by
    • scrollTo

      public void scrollTo(org.htmlunit.corejs.javascript.Scriptable x, org.htmlunit.corejs.javascript.Scriptable y)
      Scrolls to a particular set of coordinates inside this element.
      Parameters:
      x - the horizontal pixel value to scroll to
      y - the vertical pixel value to scroll to
    • scrollIntoView

      public void scrollIntoView()
      Scrolls the element into the visible area of the browser window. This implementation triggers the scroll event but does not actually scroll (headless environment).
    • scrollIntoViewIfNeeded

      public void scrollIntoViewIfNeeded()
      Scrolls the element into the visible area if needed. This is a no-op implementation.
    • getPrefix

      public String getPrefix()
      Returns the namespace prefix.
      Overrides:
      getPrefix in class Node
      Returns:
      the namespace prefix
    • getLocalName

      public String getLocalName()
      Returns the local name of this attribute.
      Overrides:
      getLocalName in class Node
      Returns:
      the local name of this attribute
    • getNamespaceURI

      public String getNamespaceURI()
      Returns the URI that identifies an XML namespace.
      Overrides:
      getNamespaceURI in class Node
      Returns:
      the URI that identifies an XML namespace
    • getOnbeforecopy

      public org.htmlunit.corejs.javascript.Function getOnbeforecopy()
      Returns the onbeforecopy event handler for this element.
      Returns:
      the onbeforecopy event handler for this element
    • setOnbeforecopy

      public void setOnbeforecopy(Object onbeforecopy)
      Sets the onbeforecopy event handler for this element.
      Parameters:
      onbeforecopy - the onbeforecopy event handler for this element
    • getOnbeforecut

      public org.htmlunit.corejs.javascript.Function getOnbeforecut()
      Returns the onbeforecut event handler for this element.
      Returns:
      the onbeforecut event handler for this element
    • setOnbeforecut

      public void setOnbeforecut(Object onbeforecut)
      Sets the onbeforecut event handler for this element.
      Parameters:
      onbeforecut - the onbeforecut event handler for this element
    • getOnbeforepaste

      public org.htmlunit.corejs.javascript.Function getOnbeforepaste()
      Returns the onbeforepaste event handler for this element.
      Returns:
      the onbeforepaste event handler for this element
    • setOnbeforepaste

      public void setOnbeforepaste(Object onbeforepaste)
      Sets the onbeforepaste event handler for this element.
      Parameters:
      onbeforepaste - the onbeforepaste event handler for this element
    • getOnsearch

      public org.htmlunit.corejs.javascript.Function getOnsearch()
      Returns the onsearch event handler for this element.
      Returns:
      the onsearch event handler for this element
    • setOnsearch

      public void setOnsearch(Object onsearch)
      Sets the onsearch event handler for this element.
      Parameters:
      onsearch - the onsearch event handler for this element
    • getOnwebkitfullscreenchange

      public org.htmlunit.corejs.javascript.Function getOnwebkitfullscreenchange()
      Returns the onwebkitfullscreenchange event handler for this element.
      Returns:
      the onwebkitfullscreenchange event handler for this element
    • setOnwebkitfullscreenchange

      public void setOnwebkitfullscreenchange(Object onwebkitfullscreenchange)
      Sets the onwebkitfullscreenchange event handler for this element.
      Parameters:
      onwebkitfullscreenchange - the onwebkitfullscreenchange event handler for this element
    • getOnwebkitfullscreenerror

      public org.htmlunit.corejs.javascript.Function getOnwebkitfullscreenerror()
      Returns the onwebkitfullscreenerror event handler for this element.
      Returns:
      the onwebkitfullscreenerror event handler for this element
    • setOnwebkitfullscreenerror

      public void setOnwebkitfullscreenerror(Object onwebkitfullscreenerror)
      Sets the onwebkitfullscreenerror event handler for this element.
      Parameters:
      onwebkitfullscreenerror - the onwebkitfullscreenerror event handler for this element
    • getOnwheel

      public org.htmlunit.corejs.javascript.Function getOnwheel()
      Returns the onwheel event handler for this element.
      Returns:
      the onwheel event handler for this element
    • setOnwheel

      public void setOnwheel(Object onwheel)
      Sets the onwheel event handler for this element.
      Parameters:
      onwheel - the onwheel event handler for this element
    • remove

      public void remove()
      Removes the DOM node from its parent.
      Overrides:
      remove in class Node
      See Also:
    • setCapture

      public void setCapture(boolean retargetToElement)
      Sets mouse capture to the object that belongs to the current document. This is a mock implementation.
      Parameters:
      retargetToElement - if true, all events are targeted directly to this element; if false, events can also fire at descendants of this element
    • releaseCapture

      public void releaseCapture()
      Releases mouse capture from the object in the current document. This is a mock implementation.
    • before

      public static void before(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Inserts a set of Node or DOMString objects in the children list of this node's parent, just before this node.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
    • after

      public static void after(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Inserts a set of Node or DOMString objects in the children list of this node's parent, just after this node.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
    • replaceWith

      public static void replaceWith(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Replaces this node with a set of Node or DOMString objects.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
    • matches

      public static boolean matches(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Returns true if the element would be selected by the specified CSS selector string.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      Returns:
      true if the element matches the selector
    • mozMatchesSelector

      public static boolean mozMatchesSelector(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      Returns:
      true if the element matches the selector
    • webkitMatchesSelector

      public static boolean webkitMatchesSelector(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      Returns:
      true if the element matches the selector
    • closest

      public static Element closest(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Traverses this element and its ancestors until it finds a node that matches the specified CSS selector.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
      Returns:
      the closest matching ancestor element, or null if none found
    • toggleAttribute

      public boolean toggleAttribute(String name, Object force)
      Toggles a Boolean attribute on this element. If force is true, adds the attribute. If force is false, removes the attribute. If force is not specified, the attribute is toggled.
      Parameters:
      name - the name of the attribute to toggle; automatically converted to lower-case for HTML elements
      force - if true, adds the attribute; if false, removes it
      Returns:
      true if the attribute is present after the call, false otherwise
      See Also:
    • append

      public static void append(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Inserts a set of Node objects or string objects after the last child of this element. String objects are inserted as equivalent Text nodes.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
    • prepend

      public static void prepend(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Inserts a set of Node objects or string objects before the first child of this element. String objects are inserted as equivalent Text nodes.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function
    • replaceChildren

      public static void replaceChildren(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.VarScope scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      Replaces the existing children of this element with a specified new set of children. These can be string or Node objects.
      Parameters:
      context - the context
      scope - the scope
      thisObj - this object
      args - the arguments
      function - the function