Skip to content

Commit ec7fcd3

Browse files
committed
Don't trim whitespace if previous element was inline
Fixes #2325
1 parent 8e24baa commit ec7fcd3

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Bug Fixes
1717
* The contents of a `script` in a `svg` foreign context should be parsed as script data, not text. [#2320](https://github.com/jhy/jsoup/issues/2320)
1818
* `Tag#isFormSubmittable()` was updating the Tag's options. [#2323](https://github.com/jhy/jsoup/issues/2323)
19+
* The HTML pretty-printer would incorrectly trim whitespace when text followed an inline element in a block element. [#2325](https://github.com/jhy/jsoup/issues/2325)
1920

2021
## 1.20.1 (2025-04-29)
2122

src/main/java/org/jsoup/nodes/Printer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,13 @@ int textTrim(TextNode node, int options) {
119119
Node prev = node.previousSibling();
120120
Node next = node.nextSibling();
121121

122-
if (prev == null || !(prev instanceof TextNode) && shouldIndent(prev))
123-
options |= Entities.TrimLeading;
122+
// if previous is not an inline element
123+
if (!(prev instanceof Element && !isBlockEl(prev))) {
124+
// if there is no previous sib; or not a text node and should be indented
125+
if (prev == null || !(prev instanceof TextNode) && shouldIndent(prev))
126+
options |= Entities.TrimLeading;
127+
}
128+
124129
if (next == null || !(next instanceof TextNode) && shouldIndent(next))
125130
options |= Entities.TrimTrailing; // don't trim if there is a TextNode sequence
126131

src/test/java/org/jsoup/nodes/PrinterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public class PrinterTest {
7171
assertEquals("<div>\n <div></div>\n Hello <!-- -_- -->\n there\n</div>", body.html());
7272
}
7373

74+
@Test void spaceAfterSpanInBlock() {
75+
Document doc = Jsoup.parse("<div> <span>Span</span> \n Text <span>Follow</span></div> <p> <span>Span</span> Text <span>Follow</span> </p>");
76+
Element body = doc.body();
77+
assertEquals("Span Text Follow Span Text Follow", body.text());
78+
assertEquals("<div>\n <span>Span</span> Text <span>Follow</span>\n</div>\n<p><span>Span</span> Text <span>Follow</span></p>", body.html());
79+
}
80+
7481
}

src/test/java/org/jsoup/parser/XmlTreeBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ private static void assertXmlNamespace(Element el) {
495495
doc.outputSettings().prettyPrint(true);
496496
assertEquals("<package>\n" +
497497
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" +
498-
" <dc:identifier id=\"pub-id\">id</dc:identifier><dc:title>title</dc:title> <dc:language>ja</dc:language> <dc:description>desc</dc:description>\n" +
498+
" <dc:identifier id=\"pub-id\">id</dc:identifier> <dc:title>title</dc:title> <dc:language>ja</dc:language> <dc:description>desc</dc:description>\n" +
499499
" </metadata>\n" +
500500
"</package>", doc.html());
501501

0 commit comments

Comments
 (0)