diff --git a/src/main/java/org/jsoup/nodes/DataNode.java b/src/main/java/org/jsoup/nodes/DataNode.java index c63fe74073..afcfa60418 100644 --- a/src/main/java/org/jsoup/nodes/DataNode.java +++ b/src/main/java/org/jsoup/nodes/DataNode.java @@ -43,18 +43,35 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr /* For XML output, escape the DataNode in a CData section. The data may contain pseudo-CData content if it was parsed as HTML, so don't double up Cdata. Output in polyglot HTML / XHTML / XML format. */ final String data = getWholeData(); - if (out.syntax() == Document.OutputSettings.Syntax.xml && !data.contains(""); - else if (parentNameIs("style")) - accum.append("/**/"); - else - accum.append(""); + + if (isXmlSyntax(out) && !data.contains(""); + } else if (parentNameIs("style")) { + appendCData(accum, data, "/**/"); + } else { + accum.append(""); + } + } + + private void handleHtmlOutput(Appendable accum, String data) throws IOException { + // In HTML, data is not escaped in the output of data nodes, so < and & in script, style are OK + accum.append(data); + } + + private void appendCData(Appendable accum, String data, String openingTag, String closingTag) throws IOException { + accum.append(openingTag).append(data).append(closingTag); + } @Override void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {} diff --git a/src/main/java/org/jsoup/nodes/DocumentType.java b/src/main/java/org/jsoup/nodes/DocumentType.java index f1c2237eed..82a2de18b2 100644 --- a/src/main/java/org/jsoup/nodes/DocumentType.java +++ b/src/main/java/org/jsoup/nodes/DocumentType.java @@ -83,7 +83,12 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr if (siblingIndex > 0 && out.prettyPrint()) accum.append('\n'); - if (out.syntax() == Syntax.html && !has(PublicId) && !has(SystemId)) { + // simplified 'complex-conditional' statement in if + boolean isHtmlSyntax = out.syntax() == Syntax.html; + boolean noPublicId = !has(PublicId); + boolean noSystemId = !has(SystemId); + + if (isHtmlSyntax && noPublicId && noSystemId) { // looks like a html5 doctype, go lowercase for aesthetics accum.append("