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();
}
Aggregations