Skip to content

Commit

Permalink
Nested inlineable elements should indent
Browse files Browse the repository at this point in the history
Fixes #1926
  • Loading branch information
jhy committed Apr 29, 2023
1 parent d9544a4 commit 8e2b868
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Release 1.16.1 [PENDING]
* Bugfix: when pretty-printing a <pre> containing block tags, those tags were incorrectly indented.
<https://github.com/jhy/jsoup/issues/1891>

* Bugfix: when pretty-printing nested inlineable blocks (such as a <p> in a <td>), the inner element should be
indented.
<https://github.com/jhy/jsoup/issues/1926>

* Bugfix: <br> tags should be wrap-indented when in block tags (and not when in inline tags).
<https://github.com/jhy/jsoup/issues/1911>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ public void onContentsChanged() {
}

private boolean isFormatAsBlock(Document.OutputSettings out) {
return tag.formatAsBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline();
return tag.isBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline();
}

private boolean isInlineable(Document.OutputSettings out) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2701,4 +2701,28 @@ void prettySerializationRoundTrips(Document.OutputSettings settings) {
Document doc = Jsoup.parse(html);
assertEquals(html, doc.body().html());
}

@Test void nestedFormatAsInlinePrintsAsBlock() {
// https://github.com/jhy/jsoup/issues/1926
String h = " <table>\n" +
" <tr>\n" +
" <td>\n" +
" <p style=\"display:inline;\">A</p>\n" +
" <p style=\"display:inline;\">B</p>\n" +
" </td>\n" +
" </tr>\n" +
" </table>";
Document doc = Jsoup.parse(h);
String out = doc.body().html();
assertEquals("<table>\n" +
" <tbody>\n" +
" <tr>\n" +
" <td>\n" +
" <p style=\"display:inline;\">A</p>\n" +
" <p style=\"display:inline;\">B</p></td>\n" +
" </tr>\n" +
" </tbody>\n" +
"</table>", out);
// todo - I would prefer the </td> to wrap down there - but need to reimplement pretty printer to simplify and track indented state
}
}
17 changes: 5 additions & 12 deletions src/test/java/org/jsoup/parser/HtmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -720,15 +720,8 @@ public class HtmlParserTest {
// and the <i> inside the table and does not leak out.
String h = "<p><b>One</p> <table><tr><td><p><i>Three<p>Four</i></td></tr></table> <p>Five</p>";
Document doc = Jsoup.parse(h);
String want = "<p><b>One</b></p><b>\n" +
" <table>\n" +
" <tbody>\n" +
" <tr>\n" +
" <td><p><i>Three</i></p><p><i>Four</i></p></td>\n" +
" </tr>\n" +
" </tbody>\n" +
" </table><p>Five</p></b>";
assertEquals(want, doc.body().html());
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>";
assertEquals(want, TextUtil.stripNewlines(doc.body().html()));
}

@Test public void commentBeforeHtml() {
Expand Down Expand Up @@ -777,7 +770,7 @@ public class HtmlParserTest {

Document two = Jsoup.parse("<title>One<b>Two <p>Test</p>"); // no title, so <b> causes </title> breakout
assertEquals("One", two.title());
assertEquals("<b>Two <p>Test</p></b>", two.body().html());
assertEquals("<b>Two \n <p>Test</p></b>", two.body().html());
}

@Test public void handlesUnclosedScriptAtEof() {
Expand Down Expand Up @@ -1470,7 +1463,7 @@ private boolean didAddElements(String input) {
assertEquals(1, nodes.size());
Node node = nodes.get(0);
assertEquals("h2", node.nodeName());
assertEquals("<p><h2>text</h2></p>", node.parent().outerHtml());
assertEquals("<p>\n <h2>text</h2></p>", node.parent().outerHtml());
}

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

@Test public void nestedAnchorAdoption() {
Expand Down

0 comments on commit 8e2b868

Please sign in to comment.