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

[header] Support header comments so line breaks properly #1664

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/org/jsoup/nodes/Comment.java
Expand Up @@ -37,12 +37,23 @@ public Comment setData(String data) {
}

void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
if (out.prettyPrint() && ((siblingIndex() == 0 && parentNode instanceof Element && ((Element) parentNode).tag().formatAsBlock()) || (out.outline() )))
if (out.prettyPrint() && ((siblingIndex() == 0 && parentNode instanceof Element && ((Element) parentNode).tag().formatAsBlock()) || (out.outline() ))) {
indent(accum, depth, out);
accum
}

// Handle Header License Comment Properly under Pretty Print
if (out.prettyPrint() && accum.toString().isEmpty()) {
accum
.append("<!--")
.append(getData())
.append("-->")
.append("\n");
} else {
accum
.append("<!--")
.append(getData())
.append("-->");
}
}

void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) {}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jsoup/nodes/CommentTest.java
Expand Up @@ -21,7 +21,8 @@ public void getData() {

@Test
public void testToString() {
assertEquals("<!-- This is one heck of a comment! -->", comment.toString());
// Tests under toString are pretty printed with first being simulated license header (no content), respect new line
assertEquals("<!-- This is one heck of a comment! -->\n", comment.toString());

Document doc = Jsoup.parse("<div><!-- comment--></div>");
assertEquals("<div>\n <!-- comment-->\n</div>", doc.body().html());
Expand Down