Java Code Examples for com.gargoylesoftware.htmlunit.html.DomNode#getPage()

The following examples show how to use com.gargoylesoftware.htmlunit.html.DomNode#getPage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Element.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the given text into the element at the specified location.
 * @param where specifies where to insert the text, using one of the following values (case-insensitive):
 *      beforebegin, afterbegin, beforeend, afterend
 * @param text the text to insert
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536453.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF, FF68, FF60})
public void insertAdjacentText(final String where, final String text) {
    final Object[] values = getInsertAdjacentLocation(where);
    final DomNode node = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    final DomText domText = new DomText(node.getPage(), text);
    // add the new nodes
    if (append) {
        node.appendChild(domText);
    }
    else {
        node.insertBefore(domText);
    }
}
 
Example 2
Source File: EventListenersContainer.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private ScriptResult executeEventHandler(final Event event, final Object[] propHandlerArgs) {
    final DomNode node = jsNode_.getDomNodeOrNull();
    // some event don't apply on all kind of nodes, for instance "blur"
    if (node != null && !node.handles(event)) {
        return null;
    }
    final Function handler = getEventHandler(event.getType());
    if (handler != null) {
        event.setCurrentTarget(jsNode_);
        final HtmlPage page = (HtmlPage) (node != null
                ? node.getPage()
                : jsNode_.getWindow().getWebWindow().getEnclosedPage());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing " + event.getType() + " handler for " + node);
        }
        return page.executeJavaScriptFunction(handler, jsNode_,
                propHandlerArgs, page);
    }
    return null;
}
 
Example 3
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Inserts the given text into the element at the specified location.
 * @param where specifies where to insert the text, using one of the following values (case-insensitive):
 *      beforebegin, afterbegin, beforeend, afterend
 * @param text the text to insert
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536453.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF52})
public void insertAdjacentText(final String where, final String text) {
    final Object[] values = getInsertAdjacentLocation(where);
    final DomNode node = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    final DomText domText = new DomText(node.getPage(), text);
    // add the new nodes
    if (append) {
        node.appendChild(domText);
    }
    else {
        node.insertBefore(domText);
    }
}
 
Example 4
Source File: HTMLTitleElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} attribute.
 * @param text the {@code text} attribute
 */
@JsxSetter
public void setText(final String text) {
    final DomNode htmlElement = getDomNodeOrDie();
    DomNode firstChild = htmlElement.getFirstChild();
    if (firstChild == null) {
        firstChild = new DomText(htmlElement.getPage(), text);
        htmlElement.appendChild(firstChild);
    }
    else {
        firstChild.setNodeValue(text);
    }
}
 
Example 5
Source File: SimpleScriptable.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the parent scope of a newly created scriptable.
 * @param domNode the DOM node for the script object
 * @param scriptable the script object to initialize
 */
protected void initParentScope(final DomNode domNode, final SimpleScriptable scriptable) {
    final SgmlPage page = domNode.getPage();
    final WebWindow enclosingWindow = page.getEnclosingWindow();
    if (enclosingWindow != null && enclosingWindow.getEnclosedPage() == page) {
        scriptable.setParentScope(enclosingWindow.getScriptableObject());
    }
    else {
        scriptable.setParentScope(ScriptableObject.getTopLevelScope(page.getScriptableObject()));
    }
}
 
Example 6
Source File: Text.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the node.
 * @return the value of the node
 */
@JsxGetter(IE)
public Object getText() {
    final DomNode node = getDomNodeOrDie();
    if (node.getPage() instanceof XmlPage) {
        return ((DomText) node).getWholeText();
    }
    return Undefined.instance;
}
 
Example 7
Source File: XSLTProcessor.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void transform(final Node source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtil.appendChild(parentPage, parent, children.item(i), true);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 8
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the parent scope of a newly created scriptable.
 * @param domNode the DOM node for the script object
 * @param scriptable the script object to initialize
 */
protected void initParentScope(final DomNode domNode, final SimpleScriptable scriptable) {
    final SgmlPage page = domNode.getPage();
    final WebWindow enclosingWindow = page.getEnclosingWindow();
    if (enclosingWindow != null && enclosingWindow.getEnclosedPage() == page) {
        scriptable.setParentScope((Scriptable) enclosingWindow.getScriptableObject());
    }
    else {
        scriptable.setParentScope(ScriptableObject.getTopLevelScope((Scriptable) page.getScriptableObject()));
    }
}
 
Example 9
Source File: XSLProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void transform(final XMLDOMNode source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtils.appendChild(parentPage, parent, children.item(i), false);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 10
Source File: XSLProcessor.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void transform(final XMLDOMNode source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtil.appendChild(parentPage, parent, children.item(i), false);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 11
Source File: HTMLTitleElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} attribute.
 * @param text the {@code text} attribute
 */
@JsxSetter
public void setText(final String text) {
    final DomNode htmlElement = getDomNodeOrDie();
    DomNode firstChild = htmlElement.getFirstChild();
    if (firstChild == null) {
        firstChild = new DomText(htmlElement.getPage(), text);
        htmlElement.appendChild(firstChild);
    }
    else {
        firstChild.setNodeValue(text);
    }
}
 
Example 12
Source File: Text.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the node.
 * @return the value of the node
 */
@JsxGetter(IE)
public Object getText() {
    final DomNode node = getDomNodeOrDie();
    if (node.getPage() instanceof XmlPage) {
        return ((DomText) node).getWholeText();
    }
    return Undefined.instance;
}
 
Example 13
Source File: XSLTProcessor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void transform(final Node source, final DomNode parent) {
    final Object result = transform(source);
    if (result instanceof org.w3c.dom.Node) {
        final SgmlPage parentPage = parent.getPage();
        final NodeList children = ((org.w3c.dom.Node) result).getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            XmlUtils.appendChild(parentPage, parent, children.item(i), true);
        }
    }
    else {
        final DomText text = new DomText(parent.getPage(), (String) result);
        parent.appendChild(text);
    }
}
 
Example 14
Source File: HtmlUnitNekoHtmlParser.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Parses the HTML content from the given string into an object tree representation.
 *
 * @param parent where the new parsed nodes will be added to
 * @param context the context to build the fragment context stack
 * @param source the (X)HTML to be parsed
 * @throws SAXException if a SAX error occurs
 * @throws IOException if an IO error occurs
 */
@Override
public void parseFragment(final DomNode parent, final DomNode context, final String source)
    throws SAXException, IOException {
    final Page page = parent.getPage();
    if (!(page instanceof HtmlPage)) {
        return;
    }
    final HtmlPage htmlPage = (HtmlPage) page;
    final URL url = htmlPage.getUrl();

    final HtmlUnitNekoDOMBuilder domBuilder = new HtmlUnitNekoDOMBuilder(this, parent, url, source);
    domBuilder.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
    // build fragment context stack
    DomNode node = context;
    final List<QName> ancestors = new ArrayList<>();
    while (node != null && node.getNodeType() != Node.DOCUMENT_NODE) {
        ancestors.add(0, new QName(null, node.getNodeName(), null, null));
        node = node.getParentNode();
    }
    if (ancestors.isEmpty() || !"html".equals(ancestors.get(0).localpart)) {
        ancestors.add(0, new QName(null, "html", null, null));
    }
    if (ancestors.size() == 1 || !"body".equals(ancestors.get(1).localpart)) {
        ancestors.add(1, new QName(null, "body", null, null));
    }

    domBuilder.setFeature(HTMLScanner.ALLOW_SELFCLOSING_TAGS, true);
    domBuilder.setProperty(HTMLTagBalancer.FRAGMENT_CONTEXT_STACK, ancestors.toArray(new QName[ancestors.size()]));

    final XMLInputSource in = new XMLInputSource(null, url.toString(), null, new StringReader(source), null);

    htmlPage.registerParsingStart();
    htmlPage.registerSnippetParsingStart();
    try {
        domBuilder.parse(in);
    }
    finally {
        htmlPage.registerParsingEnd();
        htmlPage.registerSnippetParsingEnd();
    }
}
 
Example 15
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces this element (including all child elements) with the supplied value.
 * @param value the new value for replacing this element
 */
@JsxSetter({CHROME, FF})
public void setOuterHTML(final Object value) {
    final DomNode domNode = getDomNodeOrDie();
    final DomNode parent = domNode.getParentNode();
    if (null == parent) {
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_REMOVES_CHILDREN_FOR_DETACHED)) {
            domNode.removeAllChildren();
        }
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
            throw Context.reportRuntimeError("outerHTML is readonly for detached nodes");
        }
        return;
    }

    if (value == null && !getBrowserVersion().hasFeature(JS_OUTER_HTML_NULL_AS_STRING)) {
        domNode.remove();
        return;
    }
    final String valueStr = Context.toString(value);
    if (valueStr.isEmpty()) {
        domNode.remove();
        return;
    }

    final DomNode nextSibling = domNode.getNextSibling();
    domNode.remove();

    final DomNode target;
    final boolean append;
    if (nextSibling != null) {
        target = nextSibling;
        append = false;
    }
    else {
        target = parent;
        append = true;
    }

    final DomNode proxyDomNode = new ProxyDomNode(target.getPage(), target, append);
    parseHtmlSnippet(proxyDomNode, valueStr);
}
 
Example 16
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * To be called when the property detection fails in normal scenarios.
 *
 * @param name the name
 * @return the found object, or {@link Scriptable#NOT_FOUND}
 */
public Object getWithFallback(final String name) {
    Object result = NOT_FOUND;

    final DomNode domNode = getDomNodeOrNull();
    if (domNode != null) {

        // May be attempting to retrieve a frame by name.
        final HtmlPage page = (HtmlPage) domNode.getPage();
        result = getFrameWindowByName(page, name);

        if (result == NOT_FOUND) {
            result = getElementsByName(page, name);

            if (result == NOT_FOUND) {
                // May be attempting to retrieve element by ID (try map-backed operation again instead of XPath).
                try {
                    final HtmlElement htmlElement = page.getHtmlElementById(name);
                    if (getBrowserVersion().hasFeature(JS_WINDOW_FRAME_BY_ID_RETURNS_WINDOW)
                            && htmlElement instanceof HtmlFrame) {
                        final HtmlFrame frame = (HtmlFrame) htmlElement;
                        result = getScriptableFor(frame.getEnclosedWindow());
                    }
                    else {
                        result = getScriptableFor(htmlElement);
                    }
                }
                catch (final ElementNotFoundException e) {
                    result = NOT_FOUND;
                }
            }
        }

        if (result instanceof Window) {
            final WebWindow webWindow = ((Window) result).getWebWindow();
            result = getProxy(webWindow);
        }
    }

    return result;
}
 
Example 17
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * To be called when the property detection fails in normal scenarios.
 *
 * @param name the name
 * @return the found object, or {@link Scriptable#NOT_FOUND}
 */
public Object getWithFallback(final String name) {
    Object result = NOT_FOUND;

    final DomNode domNode = getDomNodeOrNull();
    if (domNode != null) {

        // May be attempting to retrieve a frame by name.
        final HtmlPage page = (HtmlPage) domNode.getPage();
        result = getFrameWindowByName(page, name);

        if (result == NOT_FOUND) {
            result = getElementsByName(page, name);

            if (result == NOT_FOUND) {
                // May be attempting to retrieve element by ID (try map-backed operation again instead of XPath).
                try {
                    final HtmlElement htmlElement = page.getHtmlElementById(name);
                    if (getBrowserVersion().hasFeature(JS_WINDOW_FRAME_BY_ID_RETURNS_WINDOW)
                            && htmlElement instanceof HtmlFrame) {
                        final HtmlFrame frame = (HtmlFrame) htmlElement;
                        result = getScriptableFor(frame.getEnclosedWindow());
                    }
                    else {
                        result = getScriptableFor(htmlElement);
                    }
                }
                catch (final ElementNotFoundException e) {
                    result = NOT_FOUND;
                }
            }
        }

        if (result instanceof Window) {
            final WebWindow webWindow = ((Window) result).getWebWindow();
            result = getProxy(webWindow);
        }
    }

    return result;
}
 
Example 18
Source File: Element.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Replaces this element (including all child elements) with the supplied value.
 * @param value the new value for replacing this element
 */
@JsxSetter({CHROME, FF, FF68, FF60})
public void setOuterHTML(final Object value) {
    final DomNode domNode = getDomNodeOrDie();
    final DomNode parent = domNode.getParentNode();
    if (null == parent) {
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_REMOVES_CHILDREN_FOR_DETACHED)) {
            domNode.removeAllChildren();
        }
        if (getBrowserVersion().hasFeature(JS_OUTER_HTML_THROWS_FOR_DETACHED)) {
            throw Context.reportRuntimeError("outerHTML is readonly for detached nodes");
        }
        return;
    }

    if (value == null && !getBrowserVersion().hasFeature(JS_OUTER_HTML_NULL_AS_STRING)) {
        domNode.remove();
        return;
    }
    final String valueStr = Context.toString(value);
    if (valueStr.isEmpty()) {
        domNode.remove();
        return;
    }

    final DomNode nextSibling = domNode.getNextSibling();
    domNode.remove();

    final DomNode target;
    final boolean append;
    if (nextSibling != null) {
        target = nextSibling;
        append = false;
    }
    else {
        target = parent;
        append = true;
    }

    final DomNode proxyDomNode = new ProxyDomNode(target.getPage(), target, append);
    parseHtmlSnippet(proxyDomNode, valueStr);
}
 
Example 19
Source File: Element.java    From HtmlUnit-Android with Apache License 2.0 3 votes vote down vote up
/**
 * Parses the given text as HTML or XML and inserts the resulting nodes into the tree in the position given by the
 * position argument.
 * @param position specifies where to insert the nodes, using one of the following values (case-insensitive):
 *        <code>beforebegin</code>, <code>afterbegin</code>, <code>beforeend</code>, <code>afterend</code>
 * @param text the text to parse
 *
 * @see <a href="http://www.w3.org/TR/DOM-Parsing/#methods-2">W3C Spec</a>
 * @see <a href="http://domparsing.spec.whatwg.org/#dom-element-insertadjacenthtml">WhatWG Spec</a>
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML"
 *      >Mozilla Developer Network</a>
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536452.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF})
public void insertAdjacentHTML(final String position, final String text) {
    final Object[] values = getInsertAdjacentLocation(position);
    final DomNode domNode = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    // add the new nodes
    final DomNode proxyDomNode = new ProxyDomNode(domNode.getPage(), domNode, append);
    parseHtmlSnippet(proxyDomNode, text);
}
 
Example 20
Source File: Element.java    From htmlunit with Apache License 2.0 3 votes vote down vote up
/**
 * Parses the given text as HTML or XML and inserts the resulting nodes into the tree in the position given by the
 * position argument.
 * @param position specifies where to insert the nodes, using one of the following values (case-insensitive):
 *        <code>beforebegin</code>, <code>afterbegin</code>, <code>beforeend</code>, <code>afterend</code>
 * @param text the text to parse
 *
 * @see <a href="http://www.w3.org/TR/DOM-Parsing/#methods-2">W3C Spec</a>
 * @see <a href="http://domparsing.spec.whatwg.org/#dom-element-insertadjacenthtml">WhatWG Spec</a>
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element.insertAdjacentHTML"
 *      >Mozilla Developer Network</a>
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536452.aspx">MSDN</a>
 */
@JsxFunction({CHROME, FF, FF68, FF60})
public void insertAdjacentHTML(final String position, final String text) {
    final Object[] values = getInsertAdjacentLocation(position);
    final DomNode domNode = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();

    // add the new nodes
    final DomNode proxyDomNode = new ProxyDomNode(domNode.getPage(), domNode, append);
    parseHtmlSnippet(proxyDomNode, text);
}