Skip to content

Commit

Permalink
Don't indent elements when in a <pre>
Browse files Browse the repository at this point in the history
Fixes #1891
  • Loading branch information
jhy committed Mar 9, 2023
1 parent 195f484 commit 2f48a61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Release 1.16.1 [PENDING]
* Bugfix: when pretty-printing, the first inline element in a block would not be wrap-indented if it were preceded by
a blank text node.

* Bugfix: when pretty-printing a <pre> containing block tags, those tags were incorrectly indented.
<https://github.com/jhy/jsoup/issues/1891>

Release 1.15.4 [18-Feb-2023]
* Improvement: added the ability to escape CSS selectors (tags, IDs, classes) to match elements that don't follow
regular CSS syntax. For example, to match by classname <p class="one.two">, use document.select("p.one\\.two");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,7 @@ public Range endSourceRange() {
}

boolean shouldIndent(final Document.OutputSettings out) {
return out.prettyPrint() && isFormatAsBlock(out) && !isInlineable(out);
return out.prettyPrint() && isFormatAsBlock(out) && !isInlineable(out) && !preserveWhitespace(parentNode);
}

@Override
Expand Down Expand Up @@ -1701,7 +1701,8 @@ void outerHtmlHead(final Appendable accum, int depth, final Document.OutputSetti
void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
if (!(childNodes.isEmpty() && tag.isSelfClosing())) {
if (out.prettyPrint() && (!childNodes.isEmpty() && (
tag.formatAsBlock() || (out.outline() && (childNodes.size()>1 || (childNodes.size()==1 && (childNodes.get(0) instanceof Element))))
(tag.formatAsBlock() && !preserveWhitespace(parentNode)) ||
(out.outline() && (childNodes.size()>1 || (childNodes.size()==1 && (childNodes.get(0) instanceof Element))))
)))
indent(accum, depth, out);
accum.append("</").append(tagName()).append('>');
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ public void testKeepsPreTextAtDepth() {
assertEquals("<pre><code><span><b>code\n\ncode</b></span></code></pre>", doc.body().html());
}

@Test void doesNotWrapBlocksInPre() {
// https://github.com/jhy/jsoup/issues/1891
String h = "<pre><span><foo><div>TEST\n TEST</div></foo></span></pre>";
Document doc = Jsoup.parse(h);
assertEquals("TEST\n TEST", doc.wholeText());
assertEquals(h, doc.body().html());
}

@Test
public void testBrHasSpace() {
Document doc = Jsoup.parse("<p>Hello<br>there</p>");
Expand Down

0 comments on commit 2f48a61

Please sign in to comment.