From 8e2b86839b276191cf215a3500c405b1489cb215 Mon Sep 17 00:00:00 2001
From: Jonathan Hedley
Date: Sat, 29 Apr 2023 12:25:07 +1000
Subject: [PATCH] Nested inlineable elements should indent
Fixes #1926
---
CHANGES | 4 ++++
src/main/java/org/jsoup/nodes/Element.java | 2 +-
.../java/org/jsoup/nodes/ElementTest.java | 24 +++++++++++++++++++
.../java/org/jsoup/parser/HtmlParserTest.java | 17 ++++---------
4 files changed, 34 insertions(+), 13 deletions(-)
diff --git a/CHANGES b/CHANGES
index bca360ab1f..0da569130d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -38,6 +38,10 @@ Release 1.16.1 [PENDING]
* Bugfix: when pretty-printing a containing block tags, those tags were incorrectly indented.
+ * Bugfix: when pretty-printing nested inlineable blocks (such as a in a
), the inner element should be
+ indented.
+
+
* Bugfix: tags should be wrap-indented when in block tags (and not when in inline tags).
diff --git a/src/main/java/org/jsoup/nodes/Element.java b/src/main/java/org/jsoup/nodes/Element.java
index 8b2763787a..05ee2e7bd3 100644
--- a/src/main/java/org/jsoup/nodes/Element.java
+++ b/src/main/java/org/jsoup/nodes/Element.java
@@ -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) {
diff --git a/src/test/java/org/jsoup/nodes/ElementTest.java b/src/test/java/org/jsoup/nodes/ElementTest.java
index 0bb433cefc..5ff3041563 100644
--- a/src/test/java/org/jsoup/nodes/ElementTest.java
+++ b/src/test/java/org/jsoup/nodes/ElementTest.java
@@ -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 = " \n" +
+ " \n" +
+ " \n" +
+ " A \n" +
+ " B \n" +
+ " | \n" +
+ " \n" +
+ " ";
+ Document doc = Jsoup.parse(h);
+ String out = doc.body().html();
+ assertEquals("\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " A \n" +
+ " B | \n" +
+ " \n" +
+ " \n" +
+ " ", out);
+ // todo - I would prefer the | to wrap down there - but need to reimplement pretty printer to simplify and track indented state
+ }
}
diff --git a/src/test/java/org/jsoup/parser/HtmlParserTest.java b/src/test/java/org/jsoup/parser/HtmlParserTest.java
index 383f677ce0..e7f5c23a41 100644
--- a/src/test/java/org/jsoup/parser/HtmlParserTest.java
+++ b/src/test/java/org/jsoup/parser/HtmlParserTest.java
@@ -720,15 +720,8 @@ public class HtmlParserTest {
// and the inside the table and does not leak out.
String h = "One
Five
";
Document doc = Jsoup.parse(h);
- String want = "One
\n" +
- " \n" +
- " \n" +
- " \n" +
- " Three Four | \n" +
- "
\n" +
- " \n" +
- "
Five
";
- assertEquals(want, doc.body().html());
+ String want = "One
Five
";
+ assertEquals(want, TextUtil.stripNewlines(doc.body().html()));
}
@Test public void commentBeforeHtml() {
@@ -777,7 +770,7 @@ public class HtmlParserTest {
Document two = Jsoup.parse("OneTwo Test
"); // no title, so causes breakout
assertEquals("One", two.title());
- assertEquals("Two Test
", two.body().html());
+ assertEquals("Two \n Test
", two.body().html());
}
@Test public void handlesUnclosedScriptAtEof() {
@@ -1470,7 +1463,7 @@ private boolean didAddElements(String input) {
assertEquals(1, nodes.size());
Node node = nodes.get(0);
assertEquals("h2", node.nodeName());
- assertEquals("text
", node.parent().outerHtml());
+ assertEquals("\n
text
", node.parent().outerHtml());
}
@Test public void nestedPFragments() {
@@ -1479,7 +1472,7 @@ private boolean didAddElements(String input) {
List nodes = new Document("").parser().parseFragmentInput(bareFragment, new Element("p"), "");
assertEquals(2, nodes.size());
Node node = nodes.get(0);
- assertEquals("", node.parent().outerHtml()); // mis-nested because fragment forced into the element, OK
+ assertEquals("\n
", node.parent().outerHtml()); // mis-nested because fragment forced into the element, OK
}
@Test public void nestedAnchorAdoption() {