Search in sources :

Example 1 with DomDocumentFragment

use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.

the class Range method createContextualFragment.

/**
 * Parses an HTML snippet.
 * @param valueAsString text that contains text and tags to be converted to a document fragment
 * @return a document fragment
 * @see <a href="https://developer.mozilla.org/en-US/docs/DOM/range.createContextualFragment">Mozilla
 * documentation</a>
 */
@JsxFunction
public Object createContextualFragment(final String valueAsString) {
    final SgmlPage page = startContainer_.getDomNodeOrDie().getPage();
    final DomDocumentFragment fragment = new DomDocumentFragment(page);
    try {
        page.getWebClient().getPageCreator().getHtmlParser().parseFragment(fragment, startContainer_.getDomNodeOrDie(), valueAsString, false);
    } catch (final Exception e) {
        LogFactory.getLog(Range.class).error("Unexpected exception occurred in createContextualFragment", e);
        throw Context.reportRuntimeError("Unexpected exception occurred in createContextualFragment: " + e.getMessage());
    }
    return fragment.getScriptableObject();
}
Also used : DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with DomDocumentFragment

use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.

the class XMLDOMNode method appendChild.

/**
 * Appends a new child as the last child of the node.
 * @param newChild the new child node to be appended at the end of the list of children belonging to this node
 * @return the new child node successfully appended to the list
 */
@JsxFunction
public Object appendChild(final Object newChild) {
    if (newChild == null || "null".equals(newChild)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    Object appendedChild = null;
    if (newChild instanceof XMLDOMNode) {
        final XMLDOMNode childNode = (XMLDOMNode) newChild;
        // Get XML node for the DOM node passed in
        final DomNode childDomNode = childNode.getDomNodeOrDie();
        // Get the parent XML node that the child should be added to.
        final DomNode parentNode = getDomNodeOrDie();
        // Append the child to the parent node
        parentNode.appendChild(childDomNode);
        appendedChild = newChild;
        // create a DocumentFragment to be the parentNode's parentNode.
        if (!(parentNode instanceof SgmlPage) && !(this instanceof XMLDOMDocumentFragment) && parentNode.getParentNode() == null) {
            final DomDocumentFragment fragment = parentNode.getPage().createDocumentFragment();
            fragment.appendChild(parentNode);
        }
    }
    return appendedChild;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with DomDocumentFragment

use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.

the class XMLDOMNode method insertBeforeImpl.

/**
 * Inserts a child node to the left of the specified node, or at the end of the list.
 * @param args the arguments
 * <ul>
 *   <li>args[0]=<b>newChild</b> the new node to be inserted
 *   <li>args[1]=<b>refChild</b> the reference node; the <code>newChild</code> parameter is inserted to the left
 *   of the <code>refChild</code> parameter; if {@code null}, the <code>newChild</code> parameter is inserted
 *   at the end of the child list
 * </ul>
 * @return on success, returns the child node that was inserted
 */
protected Object insertBeforeImpl(final Object[] args) {
    final Object newChildObject = args[0];
    final Object refChildObject;
    if (args.length > 1) {
        refChildObject = args[1];
    } else {
        refChildObject = Undefined.instance;
    }
    Object appendedChild = null;
    if (newChildObject instanceof XMLDOMNode) {
        final XMLDOMNode newChild = (XMLDOMNode) newChildObject;
        final DomNode newChildNode = newChild.getDomNodeOrDie();
        if (newChildNode instanceof DomDocumentFragment) {
            final DomDocumentFragment fragment = (DomDocumentFragment) newChildNode;
            for (final DomNode child : fragment.getChildren()) {
                insertBeforeImpl(new Object[] { child.getScriptableObject(), refChildObject });
            }
            return newChildObject;
        }
        final DomNode refChildNode;
        // IE accepts non standard calls with only one arg
        if (Undefined.isUndefined(refChildObject)) {
            if (args.length > 1) {
                throw Context.reportRuntimeError("Invalid argument.");
            }
            refChildNode = null;
        } else if (refChildObject == null) {
            refChildNode = null;
        } else {
            refChildNode = ((XMLDOMNode) refChildObject).getDomNodeOrDie();
        }
        final DomNode domNode = getDomNodeOrDie();
        // Append the child to the parent node
        if (refChildNode == null) {
            domNode.appendChild(newChildNode);
        } else {
            refChildNode.insertBefore(newChildNode);
        }
        appendedChild = newChildObject;
        // if parentNode is null in IE, create a DocumentFragment to be the parentNode
        if (domNode.getParentNode() == null) {
            final DomDocumentFragment fragment = domNode.getPage().createDocumentFragment();
            fragment.appendChild(domNode);
        }
    }
    return appendedChild;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment)

Example 4 with DomDocumentFragment

use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.

the class XSLProcessor method transform.

/**
 * Starts the transformation process or resumes a previously failed transformation.
 */
@JsxFunction
public void transform() {
    final XMLDOMNode input = input_;
    final SgmlPage page = input.getDomNodeOrDie().getPage();
    if (output_ == null || !(output_ instanceof XMLDOMNode)) {
        final DomDocumentFragment fragment = page.createDocumentFragment();
        final XMLDOMDocumentFragment node = new XMLDOMDocumentFragment();
        node.setParentScope(getParentScope());
        node.setPrototype(getPrototype(node.getClass()));
        node.setDomNode(fragment);
        output_ = fragment.getScriptableObject();
    }
    transform(input_, ((XMLDOMNode) output_).getDomNodeOrDie());
    final XMLSerializer serializer = new XMLSerializer(false);
    final StringBuilder output = new StringBuilder();
    for (final DomNode child : ((XMLDOMNode) output_).getDomNodeOrDie().getChildren()) {
        if (child instanceof DomText) {
            // See XMLDocumentTest.testLoadXML_XMLSpaceAttribute()
            if (StringUtils.isNotBlank(((DomText) child).getData())) {
                output.append(((DomText) child).getData());
            }
        } else {
            // remove trailing "\r\n"
            final String serializedString = serializer.serializeToString(child.getScriptableObject());
            output.append(serializedString, 0, serializedString.length() - 2);
        }
    }
    output_ = output.toString();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with DomDocumentFragment

use of com.gargoylesoftware.htmlunit.html.DomDocumentFragment in project htmlunit by HtmlUnit.

the class XSLTProcessor method transformToFragment.

/**
 * Transforms the node source applying the stylesheet given by the importStylesheet() function.
 * The owner document of the output node owns the returned document fragment.
 * @param source the node to be transformed
 * @param output This document is used to generate the output
 * @return the result of the transformation
 */
@JsxFunction
public DocumentFragment transformToFragment(final Node source, final Object output) {
    final SgmlPage page = (SgmlPage) ((Document) output).getDomNodeOrDie();
    final DomDocumentFragment fragment = page.createDocumentFragment();
    final DocumentFragment rv = new DocumentFragment();
    rv.setPrototype(getPrototype(rv.getClass()));
    rv.setParentScope(getParentScope());
    rv.setDomNode(fragment);
    transform(source, fragment);
    return rv;
}
Also used : DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) DocumentFragment(com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)9 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)5 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)5 DomText (com.gargoylesoftware.htmlunit.html.DomText)1 DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)1