Class HTMLDocument

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.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:
    MSDN documentation, W3C DOM Level 1, Serialized Form
    • Field Detail

      • LOG

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

        private final java.lang.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 Detail

      • HTMLDocument

        public HTMLDocument()
        The constructor.
    • Method Detail

      • 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,
                                 java.lang.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:
        MSDN documentation
      • concatArgsAsString

        private static java.lang.String concatArgsAsString​(java.lang.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,
                                   java.lang.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:
        MSDN documentation
      • 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​(java.lang.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​(java.lang.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 java.lang.String getCookie()
        Returns the cookie property.
        Overrides:
        getCookie in class Document
        Returns:
        the cookie property
      • setCookie

        public void setCookie​(java.lang.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:
        MSDN documentation
      • open

        public java.lang.Object open​(java.lang.Object url,
                                     java.lang.Object name,
                                     java.lang.Object features,
                                     java.lang.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:
        MSDN documentation
      • 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 java.lang.Object appendChild​(java.lang.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​(java.lang.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​(java.lang.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:
        Mozilla doc
      • getElementsByName

        public NodeList getElementsByName​(java.lang.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 java.lang.Object getWithPreemption​(java.lang.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 java.lang.Object getIt​(java.lang.String name)
      • getItComputeElements

        static java.util.List<DomNode> getItComputeElements​(HtmlPage page,
                                                            java.lang.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 java.lang.String getTitle()
        Returns this document's title.
        Overrides:
        getTitle in class Document
        Returns:
        this document's title
      • setTitle

        public void setTitle​(java.lang.String title)
        Sets this document's title.
        Overrides:
        setTitle in class Document
        Parameters:
        title - the new title
      • 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​(java.lang.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 java.lang.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