Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring complex statements #2234

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/main/java/org/jsoup/nodes/DataNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<![CDATA[")) {
if (parentNameIs("script"))
accum.append("//<![CDATA[\n").append(data).append("\n//]]>");
else if (parentNameIs("style"))
accum.append("/*<![CDATA[*/\n").append(data).append("\n/*]]>*/");
else
accum.append("<![CDATA[").append(data).append("]]>");

if (isXmlSyntax(out) && !data.contains("<![CDATA[")) {
handleXmlOutput(accum, data);
} else {
// In HTML, data is not escaped in the output of data nodes, so < and & in script, style is OK
accum.append(getWholeData());
handleHtmlOutput(accum, data);
}
}
private boolean isXmlSyntax(Document.OutputSettings out) {
return out.syntax() == Document.OutputSettings.Syntax.xml;
}

private void handleXmlOutput(Appendable accum, String data) throws IOException {
if (parentNameIs("script")) {
appendCData(accum, data, "//<![CDATA[\n", "\n//]]>");
} else if (parentNameIs("style")) {
appendCData(accum, data, "/*<![CDATA[*/\n", "\n/*]]>*/");
} else {
accum.append("<![CDATA[").append(data).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) {}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jsoup/nodes/DocumentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("<!doctype");
} else {
Expand Down