Search in sources :

Example 1 with ProxyDomNode

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode in project htmlunit by HtmlUnit.

the class Element method setOuterHTML.

/**
 * Replaces this element (including all child elements) with the supplied value.
 * @param value the new value for replacing this element
 */
@JsxSetter({ CHROME, EDGE, FF, FF_ESR })
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);
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 2 with ProxyDomNode

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode in project htmlunit by HtmlUnit.

the class Element method insertAdjacentHTML.

/**
 * 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, EDGE, FF, FF_ESR })
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);
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 ProxyDomNode (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)1 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)1