Skip to content

Commit

Permalink
[alignment] Fix alignment issues so repeated processing works properly
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Jan 3, 2022
1 parent f4d00c2 commit c0479da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/jsoup/nodes/Node.java
Expand Up @@ -696,6 +696,10 @@ public String toString() {
return outerHtml();
}

protected void indent(Appendable accum, int depth, Document.OutputSettings out, int lessSpace) throws IOException {
accum.append('\n').append(StringUtil.padding((depth * out.indentAmount()) - lessSpace, out.maxPaddingWidth()));
}

protected void indent(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
accum.append('\n').append(StringUtil.padding(depth * out.indentAmount(), out.maxPaddingWidth()));
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/jsoup/nodes/TextNode.java
Expand Up @@ -89,8 +89,15 @@ void outerHtmlHead(Appendable accum, int depth, Document.OutputSettings out) thr
if (parentIndent && StringUtil.startsWithNewline(coreValue()) && blank) // we are skippable whitespace
return;

if (prettyPrint && ((siblingIndex == 0 && parent != null && parent.tag().formatAsBlock() && !blank) || (out.outline() && siblingNodes().size()>0 && !blank) ))
indent(accum, depth, out);
if (prettyPrint && ((siblingIndex() == 0 && parent != null && parent.tag().formatAsBlock() && !blank) || (out.outline() && !siblingNodes().isEmpty() && !blank) )) {
String normalized = StringUtil.normaliseWhitespace(coreValue());
boolean startsWithSpace = normalized.startsWith(" ");
if (startsWithSpace) {
indent(accum, depth, out, 1);
} else {
indent(accum, depth, out);
}
}

final boolean normaliseWhite = prettyPrint && !Element.preserveWhitespace(parentNode);
final boolean stripWhite = prettyPrint && parentNode instanceof Document;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/nodes/ElementTest.java
Expand Up @@ -499,7 +499,7 @@ public void testPrettyAndOutlineWithEnDashBody() {
Document document = Jsoup.parse(html);
document.outputSettings().outline(true);

assertEquals("<div>\n <span>1:15</span>\n\n <span>2:15</span>\n &nbsp;p.m.\n</div>", document.body().html());
assertEquals("<div>\n <span>1:15</span>\n\n <span>2:15</span>\n&nbsp;p.m.\n</div>", document.body().html());
}

@Test
Expand Down

0 comments on commit c0479da

Please sign in to comment.