htmlunit-2.7

                                                   }
                                                                                                                        }
                                                                                                                      }
                                                                                                                    }
                                                                                                                  }
                                                                                                                }
                                                                                                              }
                                                                                                            }
                                                                                                          }
                                                                                                        }
                                                                                                      }
                                                                                                    }
                                                                                                  }
                                                                                                }
                                                                                              }
                                                                                            }
                                                                                          }
                                                                                        }
                                                                                      }
                                                                                    }
                                                                                  }
                                                                                }
                                                                              }
                                                                            }
                                                                          }
                                                                        }
                                                                      }
                                                                    }
                                                                  }
                                                                }
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    HtmlElement element;
    return element;
  }
  
  static Map<String, DomAttr> setAttributes(SgmlPage page, Attributes attributes)
  {
    Map<String, DomAttr> attributeMap = null;
    if (attributes != null)
    {
      attributeMap = HtmlElement.createAttributeMap(attributes.getLength());
      for (int i = 0; i < attributes.getLength(); i++)
      {
        String qName = attributes.getQName(i);
        if (!attributeMap.containsKey(qName))
        {
          String namespaceURI = attributes.getURI(i);
          if ((namespaceURI != null) && (namespaceURI.length() == 0)) {
            namespaceURI = null;
          }
          HtmlElement.addAttributeToMap(page, attributeMap, namespaceURI, qName, attributes.getValue(i));
        }
      }
    }
    return attributeMap;
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.DefaultElementFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import java.util.Iterator;
import java.util.NoSuchElementException;

abstract class NodeIterator
  implements Iterator<DomNode>
{
  private DomNode node_;
  
  public NodeIterator(DomNode contextNode)
  {
    node_ = getFirstNode(contextNode);
  }
  
  public boolean hasNext()
  {
    return node_ != null;
  }
  
  public DomNode next()
  {
    if (node_ == null) {
      throw new NoSuchElementException();
    }
    DomNode ret = node_;
    node_ = getNextNode(node_);
    return ret;
  }
  
  public void remove()
  {
    throw new UnsupportedOperationException();
  }
  
  protected abstract DomNode getFirstNode(DomNode paramDomNode);
  
  protected abstract DomNode getNextNode(DomNode paramDomNode);
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.NodeIterator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import java.util.ListIterator;
import java.util.NoSuchElementException;

class SiblingDomNodeList$SiblingListIterator
  implements ListIterator<DomNode>
{
  private DomNode prev_;
  private DomNode next_;
  private int nextIndex_;
  
  public SiblingDomNodeList$SiblingListIterator(SiblingDomNodeList arg1, int index)
  {
    next_ = SiblingDomNodeList.access$000(???).getFirstChild();
    nextIndex_ = 0;
    for (int i = 0; i < index; i++) {
      next();
    }
  }
  
  public boolean hasNext()
  {
    return next_ != null;
  }
  
  public DomNode next()
  {
    if (!hasNext()) {
      throw new NoSuchElementException();
    }
    prev_ = next_;
    next_ = next_.getNextSibling();
    nextIndex_ += 1;
    return prev_;
  }
  
  public boolean hasPrevious()
  {
    return prev_ != null;
  }
  
  public DomNode previous()
  {
    if (!hasPrevious()) {
      throw new NoSuchElementException();
    }
    next_ = prev_;
    prev_ = prev_.getPreviousSibling();
    nextIndex_ -= 1;
    return next_;
  }
  
  public int nextIndex()
  {
    return nextIndex_;
  }
  
  public int previousIndex()
  {
    return nextIndex_ - 1;
  }
  
  public void add(DomNode e)
  {
    throw new UnsupportedOperationException();
  }
  
  public void remove()
  {
    throw new UnsupportedOperationException();
  }
  
  public void set(DomNode e)
  {
    throw new UnsupportedOperationException();
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.SiblingDomNodeList.SiblingListIterator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import java.util.AbstractSequentialList;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.w3c.dom.Node;

class SiblingDomNodeList
  extends AbstractSequentialList<DomNode>
  implements DomNodeList<DomNode>
{
  private DomNode parent_;
  
  public SiblingDomNodeList(DomNode parent)
  {
    parent_ = parent;
  }
  
  public int getLength()
  {
    int length = 0;
    for (DomNode node = parent_.getFirstChild(); node != null; node = node.getNextSibling()) {
      length++;
    }
    return length;
  }
  
  public int size()
  {
    return getLength();
  }
  
  public Node item(int index)
  {
    return get(index);
  }
  
  public DomNode get(int index)
  {
    int i = 0;
    for (DomNode node = parent_.getFirstChild(); node != null; node = node.getNextSibling())
    {
      if (i == index) {
        return node;
      }
      i++;
    }
    return null;
  }
  
  public ListIterator<DomNode> listIterator(int index)
  {
    return new SiblingListIterator(index);
  }
  
  public String toString()
  {
    return "SiblingDomNodeList[" + parent_ + "]";
  }
  
  private class SiblingListIterator
    implements ListIterator<DomNode>
  {
    private DomNode prev_;
    private DomNode next_;
    private int nextIndex_;
    
    public SiblingListIterator(int index)
    {
      next_ = parent_.getFirstChild();
      nextIndex_ = 0;
      for (int i = 0; i < index; i++) {
        next();
      }
    }
    
    public boolean hasNext()
    {
      return next_ != null;
    }
    
    public DomNode next()
    {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      prev_ = next_;
      next_ = next_.getNextSibling();
      nextIndex_ += 1;
      return prev_;
    }
    
    public boolean hasPrevious()
    {
      return prev_ != null;
    }
    
    public DomNode previous()
    {
      if (!hasPrevious()) {
        throw new NoSuchElementException();
      }
      next_ = prev_;
      prev_ = prev_.getPreviousSibling();
      nextIndex_ -= 1;
      return next_;
    }
    
    public int nextIndex()
    {
      return nextIndex_;
    }
    
    public int previousIndex()
    {
      return nextIndex_ - 1;
    }
    
    public void add(DomNode e)
    {
      throw new UnsupportedOperationException();
    }
    
    public void remove()
    {
      throw new UnsupportedOperationException();
    }
    
    public void set(DomNode e)
    {
      throw new UnsupportedOperationException();
    }
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.SiblingDomNodeList
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.WebAssert;
import java.net.URL;
import org.apache.xerces.util.DefaultErrorHandler;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLParseException;

class HTMLErrorHandler
  extends DefaultErrorHandler
{
  private final HTMLParserListener listener_;
  private final URL url_;
  
  HTMLErrorHandler(HTMLParserListener listener, URL url)
  {
    WebAssert.notNull("listener", listener);
    WebAssert.notNull("url", url);
    listener_ = listener;
    url_ = url;
  }
  
  public void error(String domain, String key, XMLParseException exception)
    throws XNIException
  {
    listener_.error(exception.getMessage(), url_, exception.getLineNumber(), exception.getColumnNumber(), key);
  }
  
  public void warning(String domain, String key, XMLParseException exception)
    throws XNIException
  {
    listener_.warning(exception.getMessage(), url_, exception.getLineNumber(), exception.getColumnNumber(), key);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HTMLErrorHandler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.xerces.util.XMLStringBuffer;
import org.apache.xerces.xni.XMLDocumentHandler;
import org.cyberneko.html.HTMLScanner.ContentScanner;

class HTMLScannerForIE$ContentScannerForIE
  extends HTMLScanner.ContentScanner
{
  private final BrowserVersion browserVersion_;
  
  HTMLScannerForIE$ContentScannerForIE(HTMLScannerForIE paramHTMLScannerForIE, BrowserVersion browserVersion)
  {
    super(paramHTMLScannerForIE);
    browserVersion_ = browserVersion;
  }
  
  protected void scanComment()
    throws IOException
  {
    String s = nextContent(30);
    if ((s.startsWith("[if ")) && (s.contains("]>")))
    {
      String condition = StringUtils.substringBefore(s.substring(4), "]>");
      try
      {
        if (IEConditionalCommentExpressionEvaluator.evaluate(condition, browserVersion_))
        {
          for (int i = 0; i < condition.length() + 6; i++) {
            HTMLScannerForIE.access$000(this$0);
          }
          return;
        }
      }
      catch (Exception e)
      {
        XMLStringBuffer buffer = new XMLStringBuffer("<!--");
        scanMarkupContent(buffer, '-');
        buffer.append("-->");
        HTMLScannerForIE.access$200(this$0).characters(buffer, HTMLScannerForIE.access$100(this$0));
        return;
      }
    }
    super.scanComment();
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HTMLScannerForIE.ContentScannerForIE
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.xerces.util.XMLStringBuffer;
import org.apache.xerces.xni.XMLDocumentHandler;
import org.cyberneko.html.HTMLScanner;
import org.cyberneko.html.HTMLScanner.ContentScanner;

class HTMLScannerForIE
  extends HTMLScanner
{
  HTMLScannerForIE(BrowserVersion browserVersion)
  {
    fContentScanner = new ContentScannerForIE(browserVersion);
  }
  
  class ContentScannerForIE
    extends HTMLScanner.ContentScanner
  {
    private final BrowserVersion browserVersion_;
    
    ContentScannerForIE(BrowserVersion browserVersion)
    {
      super();
      browserVersion_ = browserVersion;
    }
    
    protected void scanComment()
      throws IOException
    {
      String s = nextContent(30);
      if ((s.startsWith("[if ")) && (s.contains("]>")))
      {
        String condition = StringUtils.substringBefore(s.substring(4), "]>");
        try
        {
          if (IEConditionalCommentExpressionEvaluator.evaluate(condition, browserVersion_))
          {
            for (int i = 0; i < condition.length() + 6; i++) {
              read();
            }
            return;
          }
        }
        catch (Exception e)
        {
          XMLStringBuffer buffer = new XMLStringBuffer("<!--");
          scanMarkupContent(buffer, '-');
          buffer.append("-->");
          fDocumentHandler.characters(buffer, locationAugs());
          return;
        }
      }
      super.scanComment();
    }
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HTMLScannerForIE
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.SgmlPage;
import java.util.Map;

public class HtmlOrderedList
  extends HtmlElement
{
  private static final long serialVersionUID = -6804521464322433501L;
  public static final String TAG_NAME = "ol";
  
  HtmlOrderedList(String namespaceURI, String qualifiedName, SgmlPage page, Map<String, DomAttr> attributes)
  {
    super(namespaceURI, qualifiedName, page, attributes);
  }
  
  public final String getTypeAttribute()
  {
    return getAttribute("type");
  }
  
  public final String getCompactAttribute()
  {
    return getAttribute("compact");
  }
  
  public final String getStartAttribute()
  {
    return getAttribute("start");
  }
  
  protected boolean isBlock()
  {
    return true;
  }
  
  protected boolean isEmptyXmlTagExpanded()
  {
    return true;
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HtmlOrderedList
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import java.util.Iterator;

class HtmlElement$1
  implements Iterable<HtmlElement>
{
  HtmlElement$1(HtmlElement paramHtmlElement) {}
  
  public Iterator<HtmlElement> iterator()
  {
    return new HtmlElement.ChildElementsIterator(this$0);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HtmlElement.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class HtmlElement$ChildElementsIterator
  implements Iterator<HtmlElement>
{
  private HtmlElement nextElement_;
  
  protected HtmlElement$ChildElementsIterator(HtmlElement paramHtmlElement)
  {
    if (paramHtmlElement.getFirstChild() != null) {
      if ((paramHtmlElement.getFirstChild() instanceof HtmlElement)) {
        nextElement_ = ((HtmlElement)paramHtmlElement.getFirstChild());
      } else {
        setNextElement(paramHtmlElement.getFirstChild());
      }
    }
  }
  
  public boolean hasNext()
  {
    return nextElement_ != null;
  }
  
  public HtmlElement next()
  {
    return nextElement();
  }
  
  public void remove()
  {
    if (nextElement_ == null) {
      throw new IllegalStateException();
    }
    DomNode sibling = nextElement_.getPreviousSibling();
    if (sibling != null) {
      sibling.remove();
    }
  }
  
  public HtmlElement nextElement()
  {
    if (nextElement_ != null)
    {
      HtmlElement result = nextElement_;
      setNextElement(nextElement_);
      return result;
    }
    throw new NoSuchElementException();
  }
  
  private void setNextElement(DomNode node)
  {
    DomNode next = node.getNextSibling();
    while ((next != null) && (!(next instanceof HtmlElement))) {
      next = next.getNextSibling();
    }
    nextElement_ = ((HtmlElement)next);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HtmlElement.ChildElementsIterator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.javascript.host.Event;
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;

class HtmlElement$2
  implements ContextAction
{
  HtmlElement$2(HtmlElement paramHtmlElement, HTMLElement paramHTMLElement, Event paramEvent) {}
  
  public Object run(Context cx)
  {
    return val$jsElt.fireEvent(val$event);
  }
}

/* Location:
 * Qualified Name:     com.gargoylesoftware.htmlunit.html.HtmlElement.2
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.gargoylesoftware.htmlunit.html;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.SgmlPage;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import com.gargoylesoftware.htmlunit.javascript.host.Event;
import com.gargoylesoftware.htmlunit.javascript.host.EventHandler;
import com.gargoylesoftware.htmlunit.javascript.host.KeyboardEvent;
import com.gargoylesoftware.htmlunit.javascript.host.MouseEvent;
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import net.sourceforge.htmlunit.corejs.javascript.BaseFunction;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
import net.sourceforge.htmlunit.corejs.javascript.Function;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Comment;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Node;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

public abstract class HtmlElement
  extends DomElement
{
  private static final long serialVersionUID = -2841932584831342634L;
  private static final Log LOG = LogFactory.getLog(HtmlElement.class);
  public static final Short TAB_INDEX_OUT_OF_BOUNDS = new Short((short)Short.MIN_VALUE);
  private List<HtmlAttributeChangeListener> attributeListeners_;
  private HtmlForm owningForm_;
  
  protected HtmlElement(String namespaceURI, String qualifiedName, SgmlPage page, Map<String, DomAttr> attributes)
  {
    super(namespaceURI, qualifiedName, page, attributes);
    if ((page != null) && (page.getWebClient().getBrowserVersion().isFirefox()))
    {
      String value = getAttribute("class");
      if (value != ATTRIBUTE_NOT_DEFINED) {
        getAttributeNode("class").setValue(value.trim());
      }
    }
  }
  
  public void setAttributeNS(String namespaceURI, String qualifiedName, String attributeValue)
  {
    String oldAttributeValue = getAttribute(qualifiedName);
    String value = attributeValue;
    
    boolean mappedElement = HtmlPage.isMappedElement(getOwnerDocument(), qualifiedName);
    if (mappedElement) {
      ((HtmlPage)getPage()).removeMappedElement(this);
    }
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue);
    if (value.length() == 0) {
      value = ATTRIBUTE_VALUE_EMPTY;
    }
    if (!(getOwnerDocument() instanceof HtmlPage)) {
      return;
    }
    HtmlPage htmlPage = (HtmlPage)getPage();
    if (mappedElement) {
      htmlPage.addMappedElement(this);
    }
    HtmlAttributeChangeEvent htmlEvent;
    HtmlAttributeChangeEvent htmlEvent;
    if (oldAttributeValue == ATTRIBUTE_NOT_DEFINED) {
      htmlEvent = new HtmlAttributeChangeEvent(this, qualifiedName, attributeValue);
    } else {
      htmlEvent = new HtmlAttributeChangeEvent(this, qualifiedName, oldAttributeValue);
    }
    if (oldAttributeValue == ATTRIBUTE_NOT_DEFINED)
    {
      fireHtmlAttributeAdded(htmlEvent);
      ((HtmlPage)getPage()).fireHtmlAttributeAdded(htmlEvent);
    }
    else
    {
      fireHtmlAttributeReplaced(htmlEvent);
      ((HtmlPage)getPage()).fireHtmlAttributeReplaced(htmlEvent);
    }
    if (getPage().getWebClient().getBrowserVersion().isIE()) {
      fireEvent(Event.createPropertyChangeEvent(this, qualifiedName));
    }
  }
  
  public final List<HtmlElement> getHtmlElementsByTagNames(List<String> tagNames)
  {
    List<HtmlElement> list = new ArrayList();
    for (String tagName : tagNames) {
      list.addAll(getHtmlElementsByTagName(tagName));
    }
    return list;
  }
  
  public final <E extends HtmlElement> List<E> getHtmlElementsByTagName(String tagName)
  {
    List<E> list = new ArrayList();
    String lowerCaseTagName = tagName.toLowerCase();
    Iterable<HtmlElement> iterable = getHtmlElementDescendants();
    for (HtmlElement element : iterable) {
      if (lowerCaseTagName.equals(element.getTagName())) {
        list.add(element);
      }
    }
    return list;
  }
  
  public final void removeAttribute(String attributeName)
  {
    String value = getAttribute(attributeName);
    if ((getPage() instanceof HtmlPage)) {
      ((HtmlPage)getPage()).removeMappedElement(this);
    }
    super.removeAttribute(attributeName);
    if ((getPage() instanceof HtmlPage))
    {
      ((HtmlPage)getPage()).addMappedElement(this);
      
      HtmlAttributeChangeEvent event = new HtmlAttributeChangeEvent(this, attributeName, value);
      fireHtmlAttributeRemoved(event);
      ((HtmlPage)getPage()).fireHtmlAttributeRemoved(event);
    }
  }
  
  protected void fireHtmlAttributeAdded(HtmlAttributeChangeEvent event)
  {
    if (attributeListeners_ != null) {
      synchronized (this)
      {
        for (HtmlAttributeChangeListener listener : attributeListeners_) {
          listener.attributeAdded(event);
        }
      }
    }
    DomNode parentNode = getParentNode();
    if ((parentNode instanceof HtmlElement)) {
      ((HtmlElement)parentNode).fireHtmlAttributeAdded(event);
    }
  }
  
  protected void fireHtmlAttributeReplaced(HtmlAttributeChangeEvent event)
  {
    if (attributeListeners_ != null) {
      synchronized (this)
      {
        for (HtmlAttributeChangeListener listener : attributeListeners_) {
          listener.attributeReplaced(event);
        }
      }
    }
    DomNode parentNode = getParentNode();
    if ((parentNode instanceof HtmlElement)) {
      ((HtmlElement)parentNode).fireHtmlAttributeReplaced(event);
    }
  }
  
  protected void fireHtmlAttributeRemoved(HtmlAttributeChangeEvent event)
  {
    if (attributeListeners_ != null) {
      synchronized (this)
      {
        for (HtmlAttributeChangeListener listener : attributeListeners_) {
          listener.attributeRemoved(event);
        }
      }
    }
    DomNode parentNode = getParentNode();
    if ((parentNode instanceof HtmlElement)) {
      ((HtmlElement)parentNode).fireHtmlAttributeRemoved(event);
    }
  }
  
  public String getNodeName()
  {
    String name = getLocalName();
    if (getPrefix() != null) {
      name = getPrefix() + ':' + name;
    }
    name = name.toLowerCase();
    return name;
  }
  
  public final String getId()
  {
    return getAttribute("id");
  }
  
  public final void setId(String newId)
  {
    setAttribute("id", newId);
  }
  
  public Short getTabIndex()
  {
    String index = getAttribute("tabindex");
    if ((index == null) || (index.length() == 0)) {
      return null;
    }
    try
    {
      long l = Long.parseLong(index);
      if ((l >= 0L) && (l <= 32767L)) {
        return new Short(new Long(l).shortValue());
      }
      return TAB_INDEX_OUT_OF_BOUNDS;
    }
    catch (NumberFormatException e) {}
    return null;
  }
  
  public HtmlElement getEnclosingElement(String tagName)
  {
    String tagNameLC = tagName.toLowerCase();
    
    DomNode currentNode = getParentNode();
    while (currentNode != null)
    {
      if (((currentNode instanceof HtmlElement)) && (currentNode.getNodeName().equals(tagNameLC))) {
        return (HtmlElement)currentNode;
      }
      currentNode = currentNode.getParentNode();
    }
    return null;
  }
  
  public HtmlForm getEnclosingForm()
  {
    if (owningForm_ != null) {
      return owningForm_;
    }
    return (HtmlForm)getEnclosingElement("form");
  }
  
  public HtmlForm getEnclosingFormOrDie()
    throws IllegalStateException
  {
    HtmlForm form = getEnclosingForm();
    if (form == null) {
      throw new IllegalStateException("Element is not contained within a form: " + this);
    }
    return form;
  }
  
  public void type(String text)
    throws IOException
  {
    for (char ch : text.toCharArray()) {
      type(ch);
    }
  }
  
  public void type(String text, boolean shiftKey, boolean ctrlKey, boolean altKey)
    throws IOException
  {
    for (char ch : text.toCharArray()) {
      type(ch, shiftKey, ctrlKey, altKey);
    }
  }
  
  public Page type(char c)
    throws IOException
  {
    return type(c, false, false, false);
  }
  
  public Page type(char c, boolean shiftKey, boolean ctrlKey, boolean altKey)
    throws IOException
  {
    if (((this instanceof DisabledElement)) && (((DisabledElement)this).isDisabled())) {
      return getPage();
    }
    HtmlPage page = (HtmlPage)getPage();
    if (page.getFocusedElement() != this) {
      focus();
    }
    int keyCode = KeyboardEvent.charToKeyCode(c);
    Event keyDown = new KeyboardEvent(this, "keydown", keyCode, shiftKey, ctrlKey, altKey);
    ScriptResult keyDownResult = fireEvent(keyDown);
    
    Event keyPress = new KeyboardEvent(this, "keypress", keyCode, shiftKey, ctrlKey, altKey);
    ScriptResult keyPressResult = fireEvent(keyPress);
    if ((!keyDown.isAborted(keyDownResult)) && (!keyPress.isAborted(keyPressResult))) {
      doType(c, shiftKey, ctrlKey, altKey);
    }
    boolean ie = page.getWebClient().getBrowserVersion().isIE();
    if ((!ie) && (((this instanceof HtmlTextInput)) || ((this instanceof HtmlTextArea)) || ((this instanceof HtmlPasswordInput))))
    {
      Event input = new KeyboardEvent(this, "input", keyCode, shiftKey, ctrlKey, altKey);
      fireEvent(input);
    }
    Event keyUp = new KeyboardEvent(this, "keyup", keyCode, shiftKey, ctrlKey, altKey);
    fireEvent(keyUp);
    
    HtmlForm form = getEnclosingForm();
    if ((form != null) && (c == '\n') && (isSubmittableByEnter()))
    {
      if (!ie)
      {
        HtmlSubmitInput submit = (HtmlSubmitInput)form.getFirstByXPath(".//input[@type='submit']");
        if (submit != null) {
          return submit.click();
        }
      }
      form.submit((SubmittableElement)this);
      page.getWebClient().getJavaScriptEngine().processPostponedActions();
    }
    return page.getWebClient().getCurrentWindow().getEnclosedPage();
  }
  
  protected void doType(char c, boolean shiftKey, boolean ctrlKey, boolean altKey) {}
  
  protected boolean isSubmittableByEnter()
  {
    return false;
  }
  
  public String toString()
  {
    StringBuilder buffer = new StringBuilder();
    
    buffer.append(ClassUtils.getShortClassName(getClass()));
    buffer.append("[<");
    
    StringWriter writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    printOpeningTagContentAsXml(printWriter);
    buffer.append(writer.toString());
    
    buffer.append(">]");
    
    return buffer.toString();
  }
  
  public final <E extends HtmlElement> E getOneHtmlElementByAttribute(String elementName, String attributeName, String attributeValue)
    throws ElementNotFoundException
  {
    WebAssert.notNull("elementName", elementName);
    WebAssert.notNull("attributeName", attributeName);
    WebAssert.notNull("attributeValue", attributeValue);
    
    List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue);
    
    int listSize = list.size();
    if (listSize == 0) {
      throw new ElementNotFoundException(elementName, attributeName, attributeValue);
    }
    return (HtmlElement)list.get(0);
  }
  
  public <E extends HtmlElement> E getElementById(String id)
    throws ElementNotFoundException
  {
    return ((HtmlPage)getPage()).getHtmlElementById(id);
  }
  
  public boolean hasHtmlElementWithId(String id)
  {
    try
    {
      getElementById(id);
      return true;
    }
    catch (ElementNotFoundException e) {}
    return false;
  }
  
  public final <E extends HtmlElement> List<E> getElementsByAttribute(String elementName, String attributeName, String attributeValue)
  {
    List<E> list = new ArrayList();
    String lowerCaseTagName = elementName.toLowerCase();
    for (HtmlElement next : getHtmlElementDescendants()) {
      if (next.getTagName().equals(lowerCaseTagName))
      {
        String attValue = next.getAttribute(attributeName);
        if ((attValue != null) && (attValue.equals(attributeValue))) {
          list.add(next);
        }
      }
    }
    return list;
  }
  
  public final HtmlElement appendChildIfNoneExists(String tagName)
  {
    List<HtmlElement> children = getHtmlElementsByTagName(tagName);
    HtmlElement child;
    if (children.isEmpty())
    {
      HtmlElement child = ((HtmlPage)getPage()).createElement(tagName);
      appendChild(child);
    }
    else
    {
      child = (HtmlElement)children.get(0);
    }
    return child;
  }
  
  public final void removeChild(String tagName, int i)
  {
    List<HtmlElement> children = getHtmlElementsByTagName(tagName);
    if ((i >= 0) && (i < children.size())) {
      ((HtmlElement)children.get(i)).remove();
    }
  }
  
  public final Iterable<HtmlElement> getChildElements()
  {
    new Iterable()
    {
      public Iterator<HtmlElement> iterator()
      {
        return new HtmlElement.ChildElementsIterator(HtmlElement.this);
      }
    };
  }
  
  protected class ChildElementsIterator
    implements Iterator<HtmlElement>
  {
    private HtmlElement nextElement_;
    
    protected ChildElementsIterator()
    {
      if (getFirstChild() != null) {
        if ((getFirstChild() instanceof HtmlElement)) {
          nextElement_ = ((HtmlElement)getFirstChild());
        } else {
          setNextElement(getFirstChild());
        }
      }
    }
    
    public boolean hasNext()
    {
      return nextElement_ != null;
    }
    
    public HtmlElement next()
    {
      return nextElement();
    }
    
    public void remove()
    {
      if (nextElement_ == null) {
        throw new IllegalStateException();
      }
      DomNode sibling = nextElement_.getPreviousSibling();
      if (sibling != null) {
        sibling.remove();
      }
    }
    
    public HtmlElement nextElement()
    {
      if (nextElement_ != null)
      {
        HtmlElement result = nextElement_;
        setNextElement(nextElement_);
        return result;
      }
      throw new NoSuchElementException();
    }
    
    private void setNextElement(DomNode node)
    {
      DomNode next = node.getNextSibling();
      while ((next != null) && (!(next instanceof HtmlElement))) {
        next = next.getNextSibling();
      }
      nextElement_ = ((HtmlElement)next);
    }
  }
  
  static Map<String, DomAttr> createAttributeMap(int attributeCount)
  {
    return new LinkedHashMap(attributeCount);
  }
  
  static DomAttr addAttributeToMap(SgmlPage page, Map<String, DomAttr> attributeMap, String namespaceURI, String qualifiedName, String value)
  {
    DomAttr newAttr = new DomAttr(page, namespaceURI, qualifiedName, value, true);
    attributeMap.put(qualifiedName, newAttr);
    return newAttr;
  }
  
  public final Function getEventHandler(String eventName)
  {
    HTMLElement jsObj = (HTMLElement)getScriptObject();
    return jsObj.getEventHandler(eventName);
  }
  
  public final void setEventHandler(String eventName, Function eventHandler)
  {
    HTMLElement jsObj = (HTMLElement)getScriptObject();
    jsObj.setEventHandler(eventName, eventHandler);
  }
  
  public final void setEventHandler(String eventName, String jsSnippet)
  {
    BaseFunction function = new EventHandler(this, eventName, jsSnippet);
    setEventHandler(eventName, function);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Created event handler " + function.getFunctionName() + " for " + eventName + " on " + this);
    }
  }
  
  public final void removeEventHandler(String eventName)
  {
    setEventHandler(eventName, (Function)null);
  }
  
  public void addHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
  {
    WebAssert.notNull("listener", listener);
    synchronized (this)
    {
      if (attributeListeners_ == null) {
        attributeListeners_ = new ArrayList();
      }
      attributeListeners_.add(listener);
    }
  }
  
  public void removeHtmlAttributeChangeListener(HtmlAttributeChangeListener listener)
  {
    WebAssert.notNull("listener", listener);
    synchronized (this)
    {
      if (attributeListeners_ != null) {
        attributeListeners_.remove(listener);
      }
    }
  }
  
  public ScriptResult fireEvent(String eventType)
  {
    return fireEvent(new Event(this, eventType));
  }
  
  public ScriptResult fireEvent(final Event event)
  {
    WebClient client = getPage().getWebClient();
    if (!client.isJavaScriptEnabled()) {
      return null;
    }
    if (LOG.isDebugEnabled()) {
      LOG.debug("Firing " + event);
    }
    final HTMLElement jsElt = (HTMLElement)getScriptObject();
    ContextAction action = new ContextAction()
    {
      public Object run(Context cx)
      {
        return jsElt.fireEvent(event);
      }
    };
    ContextFactory cf = client.getJavaScriptEngine().getContextFactory();
    ScriptResult result = (ScriptResult)cf.call(action);
    if (event.isAborted(result)) {
      preventDefault();
    }
    return result;
  }
  
  protected void preventDefault() {}
  
  public Page mouseOver()
  {
    return mouseOver(false, false, false, 0);
  }
  
  public Page mouseOver(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    return doMouseEvent("mouseover", shiftKey, ctrlKey, altKey, button);
  }
  
  public Page mouseMove()
  {
    return mouseMove(false, false, false, 0);
  }
  
  public Page mouseMove(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    return doMouseEvent("mousemove", shiftKey, ctrlKey, altKey, button);
  }
  
  public Page mouseOut()
  {
    return mouseOut(false, false, false, 0);
  }
  
  public Page mouseOut(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    return doMouseEvent("mouseout", shiftKey, ctrlKey, altKey, button);
  }
  
  public Page mouseDown()
  {
    return mouseDown(false, false, false, 0);
  }
  
  public Page mouseDown(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    return doMouseEvent("mousedown", shiftKey, ctrlKey, altKey, button);
  }
  
  public Page mouseUp()
  {
    return mouseUp(false, false, false, 0);
  }
  
  public Page mouseUp(boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    return doMouseEvent("mouseup", shiftKey, ctrlKey, altKey, button);
  }
  
  public Page rightClick()
  {
    return rightClick(false, false, false);
  }
  
  public Page rightClick(boolean shiftKey, boolean ctrlKey, boolean altKey)
  {
    Page mouseDownPage = mouseDown(shiftKey, ctrlKey, altKey, 2);
    if (mouseDownPage != getPage())
    {
      if (LOG.isDebugEnabled()) {
        LOG.debug("rightClick() is incomplete, as mouseDown() loaded a different page.");
      }
      return mouseDownPage;
    }
    Page mouseUpPage = mouseUp(shiftKey, ctrlKey, altKey, 2);
    if (mouseUpPage != getPage())
    {
      if (LOG.isDebugEnabled()) {
        LOG.debug("rightClick() is incomplete, as mouseUp() loaded a different page.");
      }
      return mouseUpPage;
    }
    return doMouseEvent("contextmenu", shiftKey, ctrlKey, altKey, 2);
  }
  
  private Page doMouseEvent(String eventType, boolean shiftKey, boolean ctrlKey, boolean altKey, int button)
  {
    if (((this instanceof DisabledElement)) && (((DisabledElement)this).isDisabled())) {
      return getPage();
    }
    HtmlPage page = (HtmlPage)getPage();
    Event event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
    ScriptResult scriptResult = fireEvent(event);
    Page currentPage;
    Page currentPage;
    if (scriptResult == null) {
      currentPage = page;
    } else {
      currentPage = scriptResult.getNewPage();
    }
    return currentPage;
  }
  
  public void blur()
  {
    ((HtmlPage)getPage()).setFocusedElement(null);
  }
  
  public void focus()
  {
    HtmlPage page = (HtmlPage)getPage();
    page.setFocusedElement(this);
    WebClient webClient = page.getWebClient();
    if (webClient.getBrowserVersion().isIE())
    {
      HTMLElement jsElt = (HTMLElement)getScriptObject();
      jsElt.jsxFunction_setActive();
    }
  }
  
  protected void checkChildHierarchy(Node childNode)
    throws DOMException
  {
    if ((!(childNode instanceof Element)) && (!(childNode instanceof Text)) && (!(childNode instanceof Comment)) && (!(childNode instanceof ProcessingInstruction)) && (!(childNode instanceof CDATASection)) && (!(childNode instanceof EntityReference))) {
      throw new DOMException((short)3, "The Element may not have a child of this type: " + childNode.getNodeType());
    }
    super.checkChildHierarchy(childNode);
  }
  
  void setOwningForm(HtmlForm form)
  {
    owningForm_ = form;
  }
  
  void removeFocus() {}
  
  protected boolean isAttributeCaseSensitive()
  {
    return false;
  }
  
  public <P extends Page> P click()
    throws IOException
  {
    return click(false, false, false);
  }
  
  public <P extends Page> P click(boolean shiftKey, boolean ctrlKey, boolean altKey)
    throws IOException
  {
    if (((this instanceof DisabledElement)) && (((DisabledElement)this).isDisabled())) {
      return getPage();
    }
    mouseDown(shiftKey, ctrlKey, altKey, 0);
    
    HtmlElement elementToFocus = (t
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd