Skip to content

Commit

Permalink
Pretty-print - don't wrap a blank line after a br
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Jan 24, 2023
1 parent 9d104b7 commit e52224f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/TextNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr
boolean isBlank = isBlank();
boolean couldSkip = (next instanceof Element && ((Element) next).shouldIndent(out)) // next will indent
|| (next instanceof TextNode && (((TextNode) next).isBlank())) // next is blank text, from re-parenting
|| (prev instanceof Element && ((Element) prev).isBlock())
|| (prev instanceof Element && (((Element) prev).isBlock() || prev.isNode("br"))) // br is a bit special - make sure we don't get a dangling blank line, but not a block otherwise wraps in head
;
if (couldSkip && isBlank) return;

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,12 @@ void prettySerializationRoundTrips(Document.OutputSettings settings) {
assertEquals("<p>Hello<br>\n there<br>\n now.</p>", doc.body().html());
}

@Test void prettyprintBrInBlock() {
String html = "<div><br> </div>";
Document doc = Jsoup.parse(html);
assertEquals("<div>\n <br>\n</div>", doc.body().html()); // not div\n br\n \n/div
}

@Test void preformatFlowsToChildTextNodes() {
// https://github.com/jhy/jsoup/issues/1776
String html = "<div><pre>One\n<span>\nTwo</span>\n <span> \nThree</span>\n <span>Four <span>Five</span>\n Six\n</pre>";
Expand Down

0 comments on commit e52224f

Please sign in to comment.