com.gargoylesoftware.htmlunit.html
Class HtmlElement

java.lang.Object
  extended by com.gargoylesoftware.htmlunit.html.DomNode
      extended by com.gargoylesoftware.htmlunit.html.DomNamespaceNode
          extended by com.gargoylesoftware.htmlunit.html.DomElement
              extended by com.gargoylesoftware.htmlunit.html.HtmlElement
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
HtmlBase, HtmlBaseFont, HtmlHead, HtmlHtml, HtmlMeta, HtmlParameter, HtmlScript, HtmlStyle, HtmlTitle, StyledElement

public abstract class HtmlElement
extends DomElement

An abstract wrapper for html elements

Version:
$Revision: 2149 $
Author:
Mike Bowler, Mike J. Bresnahan, David K. Taylor, Christian Sell, David D. Kilzer, Mike Gallaher, Denis N. Antonioli, Marc Guillemot, Ahmed Ashour
See Also:
Serialized Form

Nested Class Summary
protected  class HtmlElement.ChildElementsIterator
          an iterator over the HtmlElement children
static class HtmlElement.MapEntryWrappingIterator
          Deprecated. This class is no longer used since attributes are now represented by HtmlAttr.
 
Nested classes/interfaces inherited from class com.gargoylesoftware.htmlunit.html.DomNode
DomNode.ChildIterator, DomNode.DescendantElementsIterator
 
Field Summary
static String ATTRIBUTE_NOT_DEFINED
          Constant meaning that the specified attribute was not defined.
static String ATTRIBUTE_VALUE_EMPTY
          Constant meaning that the specified attribute was found but its value was empty.
static Short TAB_INDEX_OUT_OF_BOUNDS
          Constant indicating that a tab index value is out of bounds (less than 0 or greater than 32767).
 
Fields inherited from class com.gargoylesoftware.htmlunit.html.DomNode
ATTRIBUTE_NODE, COMMENT_NODE, DOCUMENT_NODE, ELEMENT_NODE, PROPERTY_ELEMENT, READY_STATE_COMPLETE, READY_STATE_INTERACTIVE, READY_STATE_LOADED, READY_STATE_LOADING, READY_STATE_UNINITIALIZED, TEXT_NODE
 
Constructor Summary
protected HtmlElement(String namespaceURI, String qualifiedName, HtmlPage htmlPage, Map attributes)
          Creates an instance.
 
Method Summary
 void addHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
          Adds an HtmlAttributeChangeListener to the listener list.
 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.
protected  void assertNotEmpty(String description, String string)
          Assert that the specified string is not empty.
 void blur()
          Remove focus from this element.
 DomNode cloneDomNode(boolean deep)
          Overrides DomNode.cloneDomNode(boolean) so clone gets its own Map of attributes.
 DomNode cloneNode(boolean deep)
          Deprecated. This method conflicts with the W3C DOM API since the return values are different. Use cloneDomNode(boolean) instead.
 ScriptResult fireEvent(com.gargoylesoftware.htmlunit.javascript.host.Event event)
          Fire the event on the element.
 ScriptResult fireEvent(String eventType)
          Shortcut for fireEvent(Event).
protected  void fireHtmlAttributeAdded(HtmlAttributeChangeEvent event)
          Support for reporting HTML attribute changes.
protected  void fireHtmlAttributeRemoved(HtmlAttributeChangeEvent event)
          Support for reporting html attribute changes.
protected  void fireHtmlAttributeReplaced(HtmlAttributeChangeEvent event)
          Support for reporting html attribute changes.
 void focus()
          Set the focus to this element.
 String getAttribute(String attributeName)
          Return the value of the attribute specified by name or an empty string.
 Iterator getAttributeEntriesIterator()
           
 String getAttributeNS(String namespaceURI, String localName)
          Return the value of the attribute specified by namespace and local name or an empty string.
 String getAttributeValue(String attributeName)
          Return the value of the specified attribute or an empty string.
 Iterator getChildElementsIterator()
           
 HtmlElement getEnclosingElement(String tagName)
          Return the element with the given name that enclosed this element or null if this element is no such element is found.
 HtmlForm getEnclosingForm()
          Return the form that enclosed this element or null if this element is not within a form.
 HtmlForm getEnclosingFormOrDie()
          Return the form that enclosed this element or throw an exception if this element is not within a form.
 org.mozilla.javascript.Function getEventHandler(String eventName)
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Return a Function to be executed when a given event occurs.
 HtmlElement getHtmlElementById(String id)
          Return the html element with the specified id.
 List getHtmlElementsByAttribute(String elementName, String attributeName, String attributeValue)
          Search by the specified criteria and return all the HtmlElement that are found
 List getHtmlElementsByTagName(String tagName)
          Given a list of tag names, return the html elements that correspond to any matching element
 List getHtmlElementsByTagNames(List acceptableTagNames)
          Given a list of tag names, return the html elements that correspond to any matching element
 String getId()
           
 String getNodeName()
          Get the name for the current node.
 short getNodeType()
          Get the type of the current node.
 HtmlElement getOneHtmlElementByAttribute(String elementName, String attributeName, String attributeValue)
          Search by the specified criteria and return the first HtmlElement that is found
 Short getTabIndex()
          Returns this element's tab index, if it has one.
 String getTagName()
          Return the tag name of this element.
 boolean hasAttribute(String attributeName)
          Return whether the attribute specified by name has a value.
 boolean hasAttributeNS(String namespaceURI, String localName)
          Return whether the attribute specified by namespace and local name has a value.
 boolean hasAttributes()
          Return whether this node has any attributes.
 boolean hasHtmlElementWithId(String id)
          Return true if there is a element with the specified id.
 boolean isAttributeDefined(String attributeName)
          Return true if the specified attribute has been defined.
protected  boolean isEmptyXmlTagExpanded()
          Indicates if a node without children should be written in expanded form as xml (i.e.
protected  boolean isSubmittableByEnter()
          Returns true if clicking Enter (ASCII 10, or '\n') should submit the enclosed form (if any).
 void keyDown(int keyCode)
          Deprecated. use type(char) instead
 void keyDown(int keyCode, boolean shiftKey, boolean ctrlKey, boolean altKey)
          Deprecated. use type(char, boolean, boolean, boolean) instead
 Page mouseDown()
          Simulate clicking the mouse in this element.
 Page mouseDown(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
          Simulate clicking the mouse in this element.
 Page mouseMove()
          Simulate moving the mouse inside this element.
 Page mouseMove(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
          Simulate moving the mouse inside this element.
 Page mouseOut()
          Simulate moving the mouse out of this element.
 Page mouseOut(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
          Simulate moving the mouse out of this element.
 Page mouseOver()
          Simulate moving the mouse over this element.
 Page mouseOver(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
          Simulate moving the mouse over this element.
 Page mouseUp()
          Simulate releasing the mouse click in this element.
 Page mouseUp(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
          Simulate releasing the mouse click in this element.
protected  void notImplemented()
          Throw an exception.
protected  void preventDefault()
          This method is called if the current fired event is canceled by preventDefault() in FireFox, or by returning false in Internet Explorer.
protected  void printOpeningTagContentAsXml(PrintWriter printWriter)
          Prints the content between "<" and ">" (or "/>") in the output of the tag name and its attributes in xml format.
protected  void printXml(String indent, PrintWriter printWriter)
          recursively write the XML data for the node tree starting at node
 void removeAttribute(String attributeName)
          Removes an attribute specified by name from this element.
 void removeAttributeNS(String namespaceURI, String localName)
          Removes an attribute specified by namespace and local name from this element.
 void removeChild(String tagName, int i)
          Removes the ith child element with the specified tag name from all relationships, if possible.
 void removeEventHandler(String eventName)
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Removes the specified event handler.
 void removeHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
          Removes an HtmlAttributeChangeListener from the listener list.
 Page rightClick()
          Simulate right clicking the mouse in this element.
 Page rightClick(boolean shiftKey, boolean ctrlKey, boolean altKey)
          Simulate right clicking the mouse in this element.
 void setAttribute(String attributeName, String attributeValue)
          Set the value of the attribute specified by name.
 void setAttributeNS(String namespaceURI, String qualifiedName, String attributeValue)
          Set the value of the attribute specified by namespace and qualified name.
 void setAttributeValue(String attributeName, String attributeValue)
          Set the value of the specified attribute.
 void setAttributeValue(String namespaceURI, String qualifiedName, String attributeValue)
          Set the value of the specified attribute.
 void setEventHandler(String eventName, org.mozilla.javascript.Function eventHandler)
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Register a Function as an event handler.
 void setEventHandler(String eventName, String jsSnippet)
          INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Register a snippet of javascript code as an event handler.
 void setId(String newId)
          Set the identifier this element.
 String toString()
          Return a string representation of this object
 Page type(char c)
          Simulates typing the specified character while this element has focus.
 Page type(char c, boolean shiftKey, boolean ctrlKey, boolean altKey)
          Simulates typing the specified character while this element has focus.
 void type(String text)
          Simulates typing the specified text while this element has focus.
 void type(String text, boolean shiftKey, boolean ctrlKey, boolean altKey)
          Simulates typing the specified text while this element has focus.
 
Methods inherited from class com.gargoylesoftware.htmlunit.html.DomNamespaceNode
getLocalName, getNamespaceURI, getPrefix, getQualifiedName, setPrefix
 
Methods inherited from class com.gargoylesoftware.htmlunit.html.DomNode
addDomChangeListener, addPropertyChangeListener, appendChild, appendDomChild, asText, asXml, fireNodeAdded, fireNodeDeleted, firePropertyChange, getAllHtmlChildElements, getByXPath, getChildIterator, getChildrenAsText, getEndColumnNumber, getEndLineNumber, getFirstByXPath, getFirstChild, getFirstDomChild, getLastChild, getLastDomChild, getLog, getNativePage, getNextDomSibling, getNextSibling, getNodeValue, getPage, getParentDomNode, getParentNode, getPreviousDomSibling, getPreviousSibling, getReadyState, getScriptObject, getStartColumnNumber, getStartLineNumber, insertBefore, isAncestorOf, isRenderedVisible, isTrimmedText, notifyIncorrectness, onAddedToPage, onAllChildrenAddedToPage, printChildrenAsXml, reduceWhitespace, remove, removeAllChildren, removeDomChangeListener, removePropertyChangeListener, replace, setNextSibling, setNodeValue, setParentNode, setPreviousSibling, setReadyState, setScriptObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATTRIBUTE_NOT_DEFINED

public static final String ATTRIBUTE_NOT_DEFINED
Constant meaning that the specified attribute was not defined.


ATTRIBUTE_VALUE_EMPTY

public static final String ATTRIBUTE_VALUE_EMPTY
Constant meaning that the specified attribute was found but its value was empty.


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:
getTabIndex()
Constructor Detail

HtmlElement

protected HtmlElement(String namespaceURI,
                      String qualifiedName,
                      HtmlPage htmlPage,
                      Map attributes)
Creates an instance.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
qualifiedName - The qualified name of the element type to instantiate
htmlPage - 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 Detail

cloneNode

public DomNode cloneNode(boolean deep)
Deprecated. This method conflicts with the W3C DOM API since the return values are different. Use cloneDomNode(boolean) instead.

Overrides DomNode.cloneNode(boolean) so clone gets its own Map of attributes. Make a clone of this node

Overrides:
cloneNode in class DomNode
Parameters:
deep - if true, the clone will be propagated to the whole subtree below this one. Otherwise, the new node will not have any children. The page reference will always be the same as this node's.
Returns:
a new node

cloneDomNode

public DomNode cloneDomNode(boolean deep)
Overrides DomNode.cloneDomNode(boolean) so clone gets its own Map of attributes. Make a clone of this node

Overrides:
cloneDomNode in class DomNode
Parameters:
deep - if true, the clone will be propagated to the whole subtree below this one. Otherwise, the new node will not have any children. The page reference will always be the same as this node's.
Returns:
a new node

getAttribute

public final String getAttribute(String attributeName)
Return the value of the attribute specified by name or an empty string. If the result is an empty string then it will be either ATTRIBUTE_NOT_DEFINED if the attribute wasn't specified or ATTRIBUTE_VALUE_EMPTY if the attribute was specified but it was empty.

Parameters:
attributeName - the name of the attribute
Returns:
The value of the attribute or ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY

getAttributeNS

public final String getAttributeNS(String namespaceURI,
                                   String localName)
Return the value of the attribute specified by namespace and local name or an empty string. If the result is an empty string then it will be either ATTRIBUTE_NOT_DEFINED if the attribute wasn't specified or ATTRIBUTE_VALUE_EMPTY if the attribute was specified but it was empty.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
localName - The name within the namespace.
Returns:
The value of the attribute or ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY

hasAttributes

public boolean hasAttributes()
Return whether this node has any attributes.

Overrides:
hasAttributes in class DomNode
Returns:
true if the node has attributes, false otherwise.

hasAttribute

public final boolean hasAttribute(String attributeName)
Return whether the attribute specified by name has a value.

Parameters:
attributeName - the name of the attribute
Returns:
true if an attribute with the given name is specified on this element or has a default value, false otherwise.

hasAttributeNS

public final boolean hasAttributeNS(String namespaceURI,
                                    String localName)
Return whether the attribute specified by namespace and local name has a value.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
localName - The name within the namespace.
Returns:
true if an attribute with the given name is specified on this element or has a default value, false otherwise.

getAttributeValue

public final String getAttributeValue(String attributeName)
Return the value of the specified attribute or an empty string. If the result is an empty string then it will be either ATTRIBUTE_NOT_DEFINED if the attribute wasn't specified or ATTRIBUTE_VALUE_EMPTY if the attribute was specified but it was empty.

Parameters:
attributeName - the name of the attribute
Returns:
The value of the attribute or ATTRIBUTE_NOT_DEFINED or ATTRIBUTE_VALUE_EMPTY

setAttribute

public final void setAttribute(String attributeName,
                               String attributeValue)
Set the value of the attribute specified by name.

Parameters:
attributeName - the name of the attribute
attributeValue - The value of the attribute

setAttributeNS

public final void setAttributeNS(String namespaceURI,
                                 String qualifiedName,
                                 String attributeValue)
Set the value of the attribute specified by namespace and qualified name.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
qualifiedName - The qualified name (prefix:local) of the attribute.
attributeValue - The value of the attribute

setAttributeValue

public final void setAttributeValue(String attributeName,
                                    String attributeValue)
Set the value of the specified attribute.

Parameters:
attributeName - the name of the attribute
attributeValue - The value of the attribute

setAttributeValue

public void setAttributeValue(String namespaceURI,
                              String qualifiedName,
                              String attributeValue)
Set the value of the specified attribute.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
qualifiedName - The qualified name of the attribute
attributeValue - The value of the attribute

removeAttribute

public final void removeAttribute(String attributeName)
Removes an attribute specified by name from this element.

Parameters:
attributeName - the attribute attributeName

removeAttributeNS

public final void removeAttributeNS(String namespaceURI,
                                    String localName)
Removes an attribute specified by namespace and local name from this element.

Parameters:
namespaceURI - the URI that identifies an XML namespace.
localName - The name within the namespace.

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 HtmlAttributeChangeListener. Note that this methods recursively calls this parent fireHtmlAttributeAdded.

Parameters:
event - The event.

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 HtmlAttributeChangeListener. Note that this methods recursively calls this parent fireHtmlAttributeReplaced.

Parameters:
event - The event.

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 HtmlAttributeChangeListener. Note that this methods recursively calls this parent fireHtmlAttributeRemoved.

Parameters:
event - The event.

isAttributeDefined

public boolean isAttributeDefined(String attributeName)
Return true if the specified attribute has been defined. This is neccessary in order to distinguish between an attribute that is set to an empty string and one that was not defined at all.

Parameters:
attributeName - The attribute to check
Returns:
true if the attribute is defined

getAttributeEntriesIterator

public Iterator getAttributeEntriesIterator()
Returns:
an iterator over the HtmlAttr objects representing the attributes of this element. Each entry holds a string key and a string value. The elements are ordered as found in the html source code.

getTagName

public String getTagName()
Return the tag name of this element. The tag name is the actual html name. For example the tag name for HtmlAnchor is "a" and the tag name for HtmlTable is "table". This tag name will always be in lowercase, no matter what case was used in the original document, when no namespace is defined.

Returns:
the tag name of this element.

getNodeType

public short getNodeType()
Description copied from class: DomNode
Get the type of the current node.

Specified by:
getNodeType in class DomNode
Returns:
the node type

getNodeName

public String getNodeName()
Description copied from class: DomNode
Get the name for the current node.

Specified by:
getNodeName in class DomNode
Returns:
The same value as returned by getTagName(),

getId

public final String getId()
Returns:
the identifier of this element.

setId

public final void setId(String newId)
Set the identifier this element.

Parameters:
newId - The new identifier of this element.

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)
Return the element with the given name that enclosed this element or null if this element is no such element is found.

Parameters:
tagName - the name of the tag searched (case insensitive)
Returns:
See above

getEnclosingForm

public HtmlForm getEnclosingForm()
Return the form that enclosed this element or null if this element is not within a form.

Returns:
See above

getEnclosingFormOrDie

public HtmlForm getEnclosingFormOrDie()
                               throws IllegalStateException
Return the form that enclosed this element or throw an exception if this element is not within a form.

Returns:
See above
Throws:
IllegalStateException - If the element is not within a form.

keyDown

public void keyDown(int keyCode)
Deprecated. use type(char) instead

Simulate pressing a key on this element

Parameters:
keyCode - the key you wish to press

keyDown

public void keyDown(int keyCode,
                    boolean shiftKey,
                    boolean ctrlKey,
                    boolean altKey)
Deprecated. use type(char, boolean, boolean, boolean) instead

Simulate pressing a key on this element

Parameters:
keyCode - the key you wish to press
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed

type

public void type(String text)
          throws IOException
Simulates typing the specified text while this element has focus.

Parameters:
text - the text you with to simulate typing
Throws:
IOException - If an IO error occurs

type

public void type(String text,
                 boolean shiftKey,
                 boolean ctrlKey,
                 boolean altKey)
          throws IOException
Simulates typing the specified text while this element has focus.

Parameters:
text - the text you with to simulate typing
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
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.

Parameters:
c - the character you with to simulate typing
Returns:
The page that occupies this window after typing. It may be the same window or it may be a freshly loaded one.
Throws:
IOException - If an IO error occurs

type

public Page type(char c,
                 boolean shiftKey,
                 boolean ctrlKey,
                 boolean altKey)
          throws IOException
Simulates typing the specified character while this element has focus. Note that for some elements, typing '\n' submits the enclosed form.

Parameters:
c - the character you with to simulate typing
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
Returns:
The page that occupies this window after typing. It may be the same window or it may be a freshly loaded one.
Throws:
IOException - If an IO error occurs

isSubmittableByEnter

protected boolean isSubmittableByEnter()
Returns true if clicking Enter (ASCII 10, or '\n') should submit the enclosed form (if any). Default implementation is false.

Returns:
true if clicking Enter should submit the enclosed form (if any).

printXml

protected void printXml(String indent,
                        PrintWriter printWriter)
recursively write the XML data for the node tree starting at node

Overrides:
printXml in class DomNode
Parameters:
indent - white space to indent child nodes
printWriter - writer where child nodes are written

isEmptyXmlTagExpanded

protected boolean isEmptyXmlTagExpanded()
Indicates if a node without children should be written in expanded form as xml (i.e. with closing tag rather than with "/>")

Returns:
false

printOpeningTagContentAsXml

protected void printOpeningTagContentAsXml(PrintWriter printWriter)
Prints the content between "<" and ">" (or "/>") in the output of the tag name and its attributes in xml format.

Parameters:
printWriter - the writer to print in

toString

public String toString()
Return a string representation of this object

Overrides:
toString in class Object
Returns:
See above

notImplemented

protected final void notImplemented()
Throw an exception. This is a convenience during development only - it will likely be removed in the future.


assertNotEmpty

protected final void assertNotEmpty(String description,
                                    String string)
                             throws IllegalArgumentException
Assert that the specified string is not empty. Throw an exception if it is.

Parameters:
description - The description to pass into the exception if this string is empty
string - The string to check
Throws:
IllegalArgumentException - If the string is empty

getOneHtmlElementByAttribute

public final HtmlElement getOneHtmlElementByAttribute(String elementName,
                                                      String attributeName,
                                                      String attributeValue)
                                               throws ElementNotFoundException
Search by the specified criteria and return the first HtmlElement that is found

Parameters:
elementName - The name of the element
attributeName - The name of the attribute
attributeValue - The value of the attribute
Returns:
The HtmlElement
Throws:
ElementNotFoundException - If a particular xml element could not be found in the dom model

getHtmlElementById

public HtmlElement getHtmlElementById(String id)
                               throws ElementNotFoundException
Return the html element with the specified id. If more than one element has this id (not allowed by the html spec) then return the first one.

Parameters:
id - The id value to search by
Returns:
The html element found
Throws:
ElementNotFoundException - If no element was found that matches the id

hasHtmlElementWithId

public boolean hasHtmlElementWithId(String id)
Return true if there is a element with the specified id. This method is intended for situations where it is enough to know whether a specific element is present in the document.

Implementation note: This method calls getHtmlElementById() internally so writing code like the following would be extremely inefficient.

 if (hasHtmlElementWithId(id)) {
     HtmlElement element = getHtmlElementWithId(id)
     ...
 }
 

Parameters:
id - The id to search by
Returns:
true if an element was found with the specified id.

getHtmlElementsByAttribute

public final List getHtmlElementsByAttribute(String elementName,
                                             String attributeName,
                                             String attributeValue)
Search by the specified criteria and return all the HtmlElement that are found

Parameters:
elementName - The name of the element
attributeName - The name of the attribute
attributeValue - The value of the attribute
Returns:
A list of HtmlElements

getHtmlElementsByTagNames

public final List getHtmlElementsByTagNames(List acceptableTagNames)
Given a list of tag names, return the html elements that correspond to any matching element

Parameters:
acceptableTagNames - The list of tag names to search by.
Returns:
The list of tag names

getHtmlElementsByTagName

public final List getHtmlElementsByTagName(String tagName)
Given a list of tag names, return the html elements that correspond to any matching element

Parameters:
tagName - the tag name to match
Returns:
The list of tag names

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

getChildElementsIterator

public final Iterator getChildElementsIterator()
Returns:
an iterator over the HtmlElement children of this object, i.e. excluding the non-element nodes

getEventHandler

public final org.mozilla.javascript.Function getEventHandler(String eventName)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Return a Function to be executed when a given event occurs.

Overrides:
getEventHandler in class DomNode
Parameters:
eventName - Name of event such as "onclick" or "onblur", etc.
Returns:
A rhino javascript executable Function, or null if no event handler has been defined

setEventHandler

public final void setEventHandler(String eventName,
                                  org.mozilla.javascript.Function eventHandler)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Register a Function as an event handler.

Overrides:
setEventHandler in class DomNode
Parameters:
eventName - Name of event such as "onclick" or "onblur", etc.
eventHandler - A rhino javascript executable Function

setEventHandler

public final void setEventHandler(String eventName,
                                  String jsSnippet)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Register a snippet of javascript code as an event handler. The javascript code will be wrapped inside a unique function declaration which provides one argument named "event"

Overrides:
setEventHandler in class DomNode
Parameters:
eventName - Name of event such as "onclick" or "onblur", etc.
jsSnippet - executable javascript code

removeEventHandler

public final void removeEventHandler(String eventName)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
Removes the specified event handler.

Overrides:
removeEventHandler in class DomNode
Parameters:
eventName - Name of the event such as "onclick" or "onblur", etc.

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(HtmlAttributeChangeListener)

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:
addHtmlAttributeChangeListener(HtmlAttributeChangeListener)

fireEvent

public ScriptResult fireEvent(String eventType)
Shortcut for fireEvent(Event).

Parameters:
eventType - the event type (like "load", "click")
Returns:
the execution result. null if nothing is executed.

fireEvent

public ScriptResult fireEvent(com.gargoylesoftware.htmlunit.javascript.host.Event event)
Fire the event on the element. Nothing is done if JavaScript is disabled

Parameters:
event - the event to fire.
Returns:
the execution result. null if nothing is executed.

preventDefault

protected void preventDefault()
This method is called if the current fired event is canceled by preventDefault() in FireFox, or by returning false in Internet Explorer. The default implementation does nothing.


mouseOver

public Page mouseOver()
Simulate moving the mouse over this element.

Returns:
The page that occupies this window after the mouse moves over this element. It may be the same window or it may be a freshly loaded one.

mouseOver

public Page mouseOver(boolean shiftKey,
                      boolean ctrlKey,
                      boolean altKey,
                      int button)
Simulate moving the mouse over this element.

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
button - the button code, must be MouseEvent.BUTTON_LEFT, MouseEvent.BUTTON_MIDDLE or MouseEvent.BUTTON_RIGHT
Returns:
The page that occupies this window after the mouse moves over this element. It may be the same window or it may be a freshly loaded one.

mouseMove

public Page mouseMove()
Simulate moving the mouse inside this element.

Returns:
The page that occupies this window after the mouse moves inside this element. It may be the same window or it may be a freshly loaded one.

mouseMove

public Page mouseMove(boolean shiftKey,
                      boolean ctrlKey,
                      boolean altKey,
                      int button)
Simulate moving the mouse inside this element.

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
button - the button code, must be MouseEvent.BUTTON_LEFT, MouseEvent.BUTTON_MIDDLE or MouseEvent.BUTTON_RIGHT
Returns:
The page that occupies this window after the mouse moves inside this element. It may be the same window or it may be a freshly loaded one.

mouseOut

public Page mouseOut()
Simulate moving the mouse out of this element.

Returns:
The page that occupies this window after the mouse moves out of this element. It may be the same window or it may be a freshly loaded one.

mouseOut

public Page mouseOut(boolean shiftKey,
                     boolean ctrlKey,
                     boolean altKey,
                     int button)
Simulate moving the mouse out of this element.

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
button - the button code, must be MouseEvent.BUTTON_LEFT, MouseEvent.BUTTON_MIDDLE or MouseEvent.BUTTON_RIGHT
Returns:
The page that occupies this window after the mouse moves out of this element. It may be the same window or it may be a freshly loaded one.

mouseDown

public Page mouseDown()
Simulate clicking the mouse in this element.

Returns:
The page that occupies this window after the mouse is clicked in this element. It may be the same window or it may be a freshly loaded one.

mouseDown

public Page mouseDown(boolean shiftKey,
                      boolean ctrlKey,
                      boolean altKey,
                      int button)
Simulate clicking the mouse in this element.

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
button - the button code, must be MouseEvent.BUTTON_LEFT, MouseEvent.BUTTON_MIDDLE or MouseEvent.BUTTON_RIGHT
Returns:
The page that occupies this window after the mouse is clicked in this element. It may be the same window or it may be a freshly loaded one.

mouseUp

public Page mouseUp()
Simulate releasing the mouse click in this element.

Returns:
The page that occupies this window after the mouse click is released in this element. It may be the same window or it may be a freshly loaded one.

mouseUp

public Page mouseUp(boolean shiftKey,
                    boolean ctrlKey,
                    boolean altKey,
                    int button)
Simulate releasing the mouse click in this element.

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
button - the button code, must be MouseEvent.BUTTON_LEFT, MouseEvent.BUTTON_MIDDLE or MouseEvent.BUTTON_RIGHT
Returns:
The page that occupies this window after the mouse click is released in this element. It may be the same window or it may be a freshly loaded one.

rightClick

public Page rightClick()
Simulate right clicking the mouse in this element.

Returns:
The page that occupies this window after the mouse is right clicked in this element. It may be the same window or it may be a freshly loaded one.

rightClick

public Page rightClick(boolean shiftKey,
                       boolean ctrlKey,
                       boolean altKey)
Simulate right clicking the mouse in this element.

This is equivalent to calling mouseDown(boolean, boolean, boolean, int), then mouseUp(boolean, boolean, boolean, int)

Parameters:
shiftKey - true if SHIFT is pressed
ctrlKey - true if CTRL is pressed
altKey - true if ALT is pressed
Returns:
The page that occupies this window after the mouse is right clicked in this element. It may be the same window or it may be a freshly loaded one.

blur

public void blur()
Remove focus from this element.


focus

public void focus()
Set the focus to this element.



Copyright © 2002-2010 Gargoyle Software Inc.. All Rights Reserved.