From 0b3abb718002483616e424b6373bdb3e37b81d06 Mon Sep 17 00:00:00 2001 From: Hirva Dhandhukia Date: Thu, 28 Nov 2024 00:59:22 -0400 Subject: [PATCH 1/2] Refactor: simplified complex statement --- src/main/java/org/jsoup/nodes/DataNode.java | 35 +++++++++++++++------ 1 file changed, 26 insertions(+), 9 deletions(-) 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) {} From 0bd5ff4716986ec8550618e0fd5921cf379321af Mon Sep 17 00:00:00 2001 From: Hirva Dhandhukia Date: Thu, 28 Nov 2024 01:04:01 -0400 Subject: [PATCH 2/2] add: boolean var to simplify complex-if --- src/main/java/org/jsoup/nodes/DocumentType.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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("