Skip to content

Commit

Permalink
Don't skip blanks in preserveWhiteSpace descenders
Browse files Browse the repository at this point in the history
Fixes #1776.

Regressed in f4d00c2
  • Loading branch information
jhy committed May 18, 2022
1 parent ccbd65f commit f6d9aa0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -6,6 +6,10 @@ jsoup changelog
internal buffer size is read.
<https://github.com/jhy/jsoup/issues/1774>

* Bugfix: when serializing HTML, newlines in elements descending from a pre tag were incorrectly skipped. That caused
what should have been preformatted output to instead be a run of text.
<https://github.com/jhy/jsoup/issues/1776>

*** Release 1.15.1 [2022-May-15]
* Change: removed previously deprecated methods and classes (including org.jsoup.safety.Whitelist; use
org.jsoup.safety.Safelist instead).
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jsoup/nodes/TextNode.java
Expand Up @@ -85,14 +85,14 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr
final Element parent = parentNode instanceof Element ? ((Element) parentNode) : null;
final boolean parentIndent = parent != null && parent.shouldIndent(out);
final boolean blank = isBlank();
final boolean normaliseWhite = prettyPrint && !Element.preserveWhitespace(parentNode);

if (parentIndent && StringUtil.startsWithNewline(coreValue()) && blank) // we are skippable whitespace
if (normaliseWhite && parentIndent && StringUtil.startsWithNewline(coreValue()) && blank) // we are skippable whitespace
return;

if (prettyPrint && ((siblingIndex == 0 && parent != null && parent.tag().formatAsBlock() && !blank) || (out.outline() && siblingNodes().size()>0 && !blank) ))
indent(accum, depth, out);

final boolean normaliseWhite = prettyPrint && !Element.preserveWhitespace(parentNode);
final boolean stripWhite = prettyPrint && parentNode instanceof Document;
Entities.escape(accum, coreValue(), out, false, normaliseWhite, stripWhite);
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Expand Up @@ -2216,4 +2216,42 @@ void prettySerializationRoundTrips(Document.OutputSettings settings) {
assertEquals("Hello World", p.text());
assertEquals("Hello\nWorld", p.wholeText());
}

@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>";
Document doc = Jsoup.parse(html);
doc.outputSettings().indentAmount(2).prettyPrint(true);

Element div = doc.selectFirst("div");
assertNotNull(div);
String actual = div.outerHtml();
String expect = "<div>\n" +
" <pre>One\n" +
"<span>\n" +
"Two</span>\n" +
" <span> \n" +
"Three</span>\n" +
" <span>Four <span>Five</span>\n" +
" Six\n" +
"</span></pre>\n" +
"</div>";
assertEquals(expect, actual);

String expectText = "One\n" +
"\n" +
"Two\n" +
" \n" +
"Three\n" +
" Four Five\n" +
" Six\n";
assertEquals(expectText, div.wholeText());

String expectOwn = "One\n" +
"\n" +
" \n" +
" ";
assertEquals(expectOwn, div.child(0).wholeOwnText());

}
}

0 comments on commit f6d9aa0

Please sign in to comment.