Class HTMLDocument

All Implemented Interfaces:
Serializable, Cloneable, org.htmlunit.corejs.javascript.ConstProperties, org.htmlunit.corejs.javascript.debug.DebuggableObject, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.SymbolScriptable

public class HTMLDocument extends Document
A JavaScript object for HTMLDocument.
See Also:
  • Field Details

    • LOG

      private static final org.apache.commons.logging.Log LOG
    • writeBuilder_

      private final StringBuilder writeBuilder_
      The buffer that will be used for calls to document.write().
    • writeInCurrentDocument_

      private boolean writeInCurrentDocument_
    • closePostponedAction_

      private boolean closePostponedAction_
    • executionExternalPostponed_

      private boolean executionExternalPostponed_
  • Constructor Details

    • HTMLDocument

      public HTMLDocument()
      The constructor.
  • Method Details

    • jsConstructor

      public void jsConstructor()
      JavaScript constructor.
      Overrides:
      jsConstructor in class Document
    • getDomNodeOrDie

      public DomNode 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
    • getPage

      public HtmlPage getPage()
      Returns the HTML page that this document is modeling.
      Overrides:
      getPage in class Document
      Returns:
      the HTML page that this document is modeling
    • write

      public static void write(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      JavaScript function "write" may accept a variable number of arguments.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      See Also:
    • concatArgsAsString

      private static String concatArgsAsString(Object[] args)
      Converts the arguments to strings and concatenate them.
      Parameters:
      args - the JavaScript arguments
      Returns:
      the string concatenation
    • writeln

      public static void writeln(org.htmlunit.corejs.javascript.Context context, org.htmlunit.corejs.javascript.Scriptable scope, org.htmlunit.corejs.javascript.Scriptable thisObj, Object[] args, org.htmlunit.corejs.javascript.Function function)
      JavaScript function "writeln" may accept a variable number of arguments.
      Parameters:
      context - the JavaScript context
      scope - the scope
      thisObj - the scriptable
      args - the arguments passed into the method
      function - the function
      See Also:
    • getDocument

      private static HTMLDocument getDocument(org.htmlunit.corejs.javascript.Scriptable thisObj)
      Returns the current document instance, using thisObj as a hint.
      Parameters:
      thisObj - a hint as to the current document (may be the prototype when function is used without "this")
      Returns:
      the current document instance
    • setExecutingDynamicExternalPosponed

      public void setExecutingDynamicExternalPosponed(boolean executing)
      This a hack!!! A cleaner way is welcome. Handle a case where document.write is simply ignored. See HTMLDocumentWrite2Test.write_fromScriptAddedWithAppendChild_external.
      Parameters:
      executing - indicates if executing or not
    • write

      protected void write(String content)
      JavaScript function "write".

      See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for a good description of the semantics of open(), write(), writeln() and close().

      Parameters:
      content - the content to write
    • scheduleImplicitClose

      private void scheduleImplicitClose()
    • canAlreadyBeParsed

      static boolean canAlreadyBeParsed(String content)
      Indicates if the content is a well formed HTML snippet that can already be parsed to be added to the DOM.
      Parameters:
      content - the HTML snippet
      Returns:
      false if it not well formed
    • getLastHtmlElement

      HtmlElement getLastHtmlElement(HtmlElement node)
      Gets the node that is the last one when exploring following nodes, depth-first.
      Parameters:
      node - the node to search
      Returns:
      the searched node
    • getCookie

      public String getCookie()
      Returns the cookie property.
      Overrides:
      getCookie in class Document
      Returns:
      the cookie property
    • setCookie

      public void setCookie(String newCookie)
      Adds a cookie, as long as cookies are enabled.
      Parameters:
      newCookie - in the format "name=value[;expires=date][;domain=domainname][;path=path][;secure]
      See Also:
    • open

      public Object open(Object url, Object name, Object features, Object replace)
      JavaScript function "open".

      See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for a good description of the semantics of open(), write(), writeln() and close().

      Parameters:
      url - when a new document is opened, url is a String that specifies a MIME type for the document. When a new window is opened, url is a String that specifies the URL to render in the new window
      name - the name
      features - the features
      replace - whether to replace in the history list or no
      Returns:
      a reference to the new document object.
      See Also:
    • close

      public void close() throws IOException
      JavaScript function close.

      See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-dynamic.html for a good description of the semantics of open(), write(), writeln() and close().

      Overrides:
      close in class Document
      Throws:
      IOException - if an IO problem occurs
    • getDocumentElement

      public Element getDocumentElement()
      Gets the JavaScript property documentElement for the document.
      Overrides:
      getDocumentElement in class Document
      Returns:
      the root node for the document
    • implicitCloseIfNecessary

      private void implicitCloseIfNecessary()
      Closes the document implicitly, i.e. flushes the document.write buffer (IE only).
    • appendChild

      public Object appendChild(Object childObject)
      Adds a DOM node to the node.
      Overrides:
      appendChild in class Node
      Parameters:
      childObject - the node to add to this node
      Returns:
      the newly added child node
    • getElementById

      public HtmlUnitScriptable getElementById(String id)
      Returns the element with the specified ID, or null if that element could not be found.
      Overrides:
      getElementById in class Document
      Parameters:
      id - the ID to search for
      Returns:
      the element, or null if it could not be found
    • getElementsByClassName

      public HTMLCollection getElementsByClassName(String className)
      Returns all the descendant elements with the specified class name.
      Overrides:
      getElementsByClassName in class Document
      Parameters:
      className - the name to search for
      Returns:
      all the descendant elements with the specified class name
      See Also:
    • getElementsByName

      public NodeList getElementsByName(String elementName)
      Returns all HTML elements that have a name attribute with the specified value.

      Refer to The DOM spec for details.

      Overrides:
      getElementsByName in class Document
      Parameters:
      elementName - - value of the name attribute to look for
      Returns:
      all HTML elements that have a name attribute with the specified value
    • getWithPreemption

      protected Object getWithPreemption(String name)
      Calls to document.XYZ should first look at elements named XYZ before using standard functions.

      Called by HtmlUnitScriptable.get(String, Scriptable) to allow retrieval of the property before the prototype chain is searched.

      IMPORTANT: This method is invoked *very* often by Rhino. If you override this method, the implementation needs to be as fast as possible!

      Overrides:
      getWithPreemption in class HtmlUnitScriptable
      Parameters:
      name - the property name
      Returns:
      Scriptable.NOT_FOUND if not found
    • getIt

      private Object getIt(String name)
    • getItComputeElements

      static List<DomNode> getItComputeElements(HtmlPage page, String name, boolean alsoFrames)
    • getHead

      public HTMLElement getHead()
      Returns this document's head element.
      Overrides:
      getHead in class Document
      Returns:
      this document's head element
    • getTitle

      public String getTitle()
      Returns this document's title.
      Overrides:
      getTitle in class Document
      Returns:
      this document's title
    • setTitle

      public void setTitle(String title)
      Sets this document's title.
      Overrides:
      setTitle in class Document
      Parameters:
      title - the new title
    • getActiveElement

      public HTMLElement getActiveElement()
      Returns the value of the activeElement property.
      Overrides:
      getActiveElement in class Document
      Returns:
      the value of the activeElement property
      See Also:
    • hasFocus

      public boolean hasFocus()
      Returns false if the active element in the document has no focus; true if the active element in the document has focus.
      Overrides:
      hasFocus in class Document
      Returns:
      whether the active element in the document has focus or not
    • dispatchEvent

      public boolean dispatchEvent(Event event)
      Dispatches an event into the event system (standards-conformant browsers only). See the Gecko DOM reference for more information.
      Overrides:
      dispatchEvent in class EventTarget
      Parameters:
      event - the event to be dispatched
      Returns:
      false if at least one of the event handlers which handled the event called preventDefault; true otherwise
    • getSelection

      public Selection getSelection()
      Returns the current selection.
      Overrides:
      getSelection in class Document
      Returns:
      the current selection
    • createAttribute

      public Attr createAttribute(String attributeName)
      Creates a new HTML attribute with the specified name.
      Overrides:
      createAttribute in class Document
      Parameters:
      attributeName - the name of the attribute to create
      Returns:
      an attribute with the specified name
    • getBaseURI

      public String getBaseURI()
      Returns the Base URI as a string.
      Overrides:
      getBaseURI in class Node
      Returns:
      the Base URI as a string
    • elementFromPoint

      public HtmlUnitScriptable elementFromPoint(int x, int y)
      Returns the element for the specified x coordinate and the specified y coordinate. The current implementation always returns the <body> element.
      Overrides:
      elementFromPoint in class Document
      Parameters:
      x - the x offset, in pixels
      y - the y offset, in pixels
      Returns:
      the element for the specified x coordinate and the specified y coordinate