Skip to content

Commit 8e2b868

Browse files
committed
Nested inlineable elements should indent
Fixes #1926
1 parent d9544a4 commit 8e2b868

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Release 1.16.1 [PENDING]
3838
* Bugfix: when pretty-printing a <pre> containing block tags, those tags were incorrectly indented.
3939
<https://github.com/jhy/jsoup/issues/1891>
4040

41+
* Bugfix: when pretty-printing nested inlineable blocks (such as a <p> in a <td>), the inner element should be
42+
indented.
43+
<https://github.com/jhy/jsoup/issues/1926>
44+
4145
* Bugfix: <br> tags should be wrap-indented when in block tags (and not when in inline tags).
4246
<https://github.com/jhy/jsoup/issues/1911>
4347

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ public void onContentsChanged() {
18441844
}
18451845

18461846
private boolean isFormatAsBlock(Document.OutputSettings out) {
1847-
return tag.formatAsBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline();
1847+
return tag.isBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline();
18481848
}
18491849

18501850
private boolean isInlineable(Document.OutputSettings out) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,4 +2701,28 @@ void prettySerializationRoundTrips(Document.OutputSettings settings) {
27012701
Document doc = Jsoup.parse(html);
27022702
assertEquals(html, doc.body().html());
27032703
}
2704+
2705+
@Test void nestedFormatAsInlinePrintsAsBlock() {
2706+
// https://github.com/jhy/jsoup/issues/1926
2707+
String h = " <table>\n" +
2708+
" <tr>\n" +
2709+
" <td>\n" +
2710+
" <p style=\"display:inline;\">A</p>\n" +
2711+
" <p style=\"display:inline;\">B</p>\n" +
2712+
" </td>\n" +
2713+
" </tr>\n" +
2714+
" </table>";
2715+
Document doc = Jsoup.parse(h);
2716+
String out = doc.body().html();
2717+
assertEquals("<table>\n" +
2718+
" <tbody>\n" +
2719+
" <tr>\n" +
2720+
" <td>\n" +
2721+
" <p style=\"display:inline;\">A</p>\n" +
2722+
" <p style=\"display:inline;\">B</p></td>\n" +
2723+
" </tr>\n" +
2724+
" </tbody>\n" +
2725+
"</table>", out);
2726+
// todo - I would prefer the </td> to wrap down there - but need to reimplement pretty printer to simplify and track indented state
2727+
}
27042728
}

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -720,15 +720,8 @@ public class HtmlParserTest {
720720
// and the <i> inside the table and does not leak out.
721721
String h = "<p><b>One</p> <table><tr><td><p><i>Three<p>Four</i></td></tr></table> <p>Five</p>";
722722
Document doc = Jsoup.parse(h);
723-
String want = "<p><b>One</b></p><b>\n" +
724-
" <table>\n" +
725-
" <tbody>\n" +
726-
" <tr>\n" +
727-
" <td><p><i>Three</i></p><p><i>Four</i></p></td>\n" +
728-
" </tr>\n" +
729-
" </tbody>\n" +
730-
" </table><p>Five</p></b>";
731-
assertEquals(want, doc.body().html());
723+
String want = "<p><b>One</b></p><b><table><tbody><tr><td><p><i>Three</i></p><p><i>Four</i></p></td></tr></tbody></table><p>Five</p></b>";
724+
assertEquals(want, TextUtil.stripNewlines(doc.body().html()));
732725
}
733726

734727
@Test public void commentBeforeHtml() {
@@ -777,7 +770,7 @@ public class HtmlParserTest {
777770

778771
Document two = Jsoup.parse("<title>One<b>Two <p>Test</p>"); // no title, so <b> causes </title> breakout
779772
assertEquals("One", two.title());
780-
assertEquals("<b>Two <p>Test</p></b>", two.body().html());
773+
assertEquals("<b>Two \n <p>Test</p></b>", two.body().html());
781774
}
782775

783776
@Test public void handlesUnclosedScriptAtEof() {
@@ -1470,7 +1463,7 @@ private boolean didAddElements(String input) {
14701463
assertEquals(1, nodes.size());
14711464
Node node = nodes.get(0);
14721465
assertEquals("h2", node.nodeName());
1473-
assertEquals("<p><h2>text</h2></p>", node.parent().outerHtml());
1466+
assertEquals("<p>\n <h2>text</h2></p>", node.parent().outerHtml());
14741467
}
14751468

14761469
@Test public void nestedPFragments() {
@@ -1479,7 +1472,7 @@ private boolean didAddElements(String input) {
14791472
List<Node> nodes = new Document("").parser().parseFragmentInput(bareFragment, new Element("p"), "");
14801473
assertEquals(2, nodes.size());
14811474
Node node = nodes.get(0);
1482-
assertEquals("<p><p></p><a></a></p>", node.parent().outerHtml()); // mis-nested because fragment forced into the element, OK
1475+
assertEquals("<p>\n <p></p><a></a></p>", node.parent().outerHtml()); // mis-nested because fragment forced into the element, OK
14831476
}
14841477

14851478
@Test public void nestedAnchorAdoption() {

0 commit comments

Comments
 (0)