Search in sources :

Example 1 with DocumentFragment

use of com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment 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)

Example 2 with DocumentFragment

use of com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment in project htmlunit by HtmlUnit.

the class HTMLTemplateElement method getContent.

/**
 * @return the value of the {@code content} property
 */
@JsxGetter
public DocumentFragment getContent() {
    final DocumentFragment result = new DocumentFragment();
    result.setPrototype(getPrototype(result.getClass()));
    result.setParentScope(getParentScope());
    result.setDomNode(((HtmlTemplate) getDomNodeOrDie()).getContent());
    return result;
}
Also used : DocumentFragment(com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 3 with DocumentFragment

use of com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment in project htmlunit by HtmlUnit.

the class XMLSerializer method serializeToString.

/**
 * The subtree rooted by the specified element is serialized to a string.
 * @param root the root of the subtree to be serialized (this may be any node, even a document)
 * @return the serialized string
 */
@JsxFunction
public String serializeToString(Node root) {
    if (root == null) {
        return "";
    }
    if (root instanceof DocumentFragment) {
        if (root.getOwnerDocument() instanceof HTMLDocument && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_HTML_DOCUMENT_FRAGMENT_ALWAYS_EMPTY)) {
            return "";
        }
        Node node = root.getFirstChild();
        if (node == null) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();
        while (node != null) {
            builder.append(serializeToString(node));
            node = node.getNextSibling();
        }
        return builder.toString();
    }
    if (root instanceof Document) {
        root = ((Document) root).getDocumentElement();
    }
    if (root instanceof Element) {
        final StringBuilder builder = new StringBuilder();
        final DomNode node = root.getDomNodeOrDie();
        final SgmlPage page = node.getPage();
        final boolean isHtmlPage = page != null && page.isHtmlPage();
        String forcedNamespace = null;
        if (isHtmlPage) {
            forcedNamespace = "http://www.w3.org/1999/xhtml";
        }
        toXml(1, node, builder, forcedNamespace);
        return builder.toString();
    }
    if (root instanceof CDATASection && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_ROOT_CDATA_AS_ESCAPED_TEXT)) {
        final DomCDataSection domCData = (DomCDataSection) root.getDomNodeOrDie();
        final String data = domCData.getData();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(data)) {
            return StringUtils.escapeXmlChars(data);
        }
    }
    return root.getDomNodeOrDie().asXml();
}
Also used : CDATASection(com.gargoylesoftware.htmlunit.javascript.host.dom.CDATASection) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Node(com.gargoylesoftware.htmlunit.javascript.host.dom.Node) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document) DocumentFragment(com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)1 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)1 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)1 CDATASection (com.gargoylesoftware.htmlunit.javascript.host.dom.CDATASection)1 Document (com.gargoylesoftware.htmlunit.javascript.host.dom.Document)1 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1