Search in sources :

Example 1 with CDATASection

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

SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)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 DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)1 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1