Skip to content

Commit

Permalink
Not add line break when pretty printing inline tags
Browse files Browse the repository at this point in the history
This is a fix for issue jhy#1305.
  • Loading branch information
kovacstamasx authored and jhy committed Feb 2, 2020
1 parent b891b6f commit 3a9bf46
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
16 changes: 14 additions & 2 deletions src/main/java/org/jsoup/nodes/Element.java
Expand Up @@ -1395,7 +1395,7 @@ public Element val(String value) {
}

void outerHtmlHead(final Appendable accum, int depth, final Document.OutputSettings out) throws IOException {
if (out.prettyPrint() && (tag.formatAsBlock() || (parent() != null && parent().tag().formatAsBlock()) || out.outline())) {
if (out.prettyPrint() && isFormatAsBlock(out) && !isInlineable(out)) {
if (accum instanceof StringBuilder) {
if (((StringBuilder) accum).length() > 0)
indent(accum, depth, out);
Expand All @@ -1417,7 +1417,7 @@ void outerHtmlHead(final Appendable accum, int depth, final Document.OutputSetti
accum.append('>');
}

void outerHtmlTail(Appendable accum, int depth, Document.OutputSettings out) throws IOException {
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 TextNode))))
Expand Down Expand Up @@ -1522,4 +1522,16 @@ public void onContentsChanged() {
owner.nodelistChanged();
}
}

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

private boolean isInlineable(Document.OutputSettings out) {
return tag().isInline()
&& !tag().isEmpty()
&& parent().isBlock()
&& previousElementSibling() != null
&& !out.outline();
}
}
10 changes: 7 additions & 3 deletions src/main/java/org/jsoup/parser/Tag.java
Expand Up @@ -16,14 +16,15 @@ public class Tag {

private String tagName;
private String normalName; // always the lower case version of this tag, regardless of case preservation mode
private boolean isBlock = true; // block or inline
private boolean isBlock = true; // block
private boolean formatAsBlock = true; // should be formatted as a block
private boolean canContainInline = true; // only pcdata if not
private boolean empty = false; // can hold nothing; e.g. img
private boolean selfClosing = false; // can self close (<foo />). used for unknown tags that self close, without forcing them as empty.
private boolean preserveWhitespace = false; // for pre, textarea, script etc
private boolean formList = false; // a control that appears in forms: input, textarea, output etc
private boolean formSubmit = false; // a control that can be submitted in a form: input etc
private boolean inlineTag = false; // an inline tag

private Tag(String tagName) {
this.tagName = tagName;
Expand Down Expand Up @@ -122,7 +123,7 @@ public boolean canContainBlock() {
* @return if this tag is an inline tag.
*/
public boolean isInline() {
return !isBlock;
return inlineTag;
}

/**
Expand Down Expand Up @@ -216,7 +217,8 @@ public boolean equals(Object o) {
if (preserveWhitespace != tag.preserveWhitespace) return false;
if (selfClosing != tag.selfClosing) return false;
if (formList != tag.formList) return false;
return formSubmit == tag.formSubmit;
if (formSubmit != tag.formSubmit) return false;
return inlineTag == tag.inlineTag;
}

@Override
Expand All @@ -230,6 +232,7 @@ public int hashCode() {
result = 31 * result + (preserveWhitespace ? 1 : 0);
result = 31 * result + (formList ? 1 : 0);
result = 31 * result + (formSubmit ? 1 : 0);
result = 31 * result + (inlineTag ? 1 : 0);
return result;
}

Expand Down Expand Up @@ -286,6 +289,7 @@ public String toString() {
Tag tag = new Tag(tagName);
tag.isBlock = false;
tag.formatAsBlock = false;
tag.inlineTag = true;
register(tag);
}

Expand Down
26 changes: 24 additions & 2 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Expand Up @@ -347,6 +347,29 @@ public class ElementTest {
Element div = doc.select("div").first();
assertEquals(" \n<p>Hello\n there\n</p>", div.html());
}

@Test public void testNotPrettyWithEnDashBody() {
String html = "<div><span>1:15</span>&ndash;<span>2:15</span>&nbsp;p.m.</div>";
Document document = Jsoup.parse(html);
document.outputSettings().prettyPrint(false);

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

@Test public void testPrettyWithEnDashBody() {
String html = "<div><span>1:15</span>&ndash;<span>2:15</span>&nbsp;p.m.</div>";
Document document = Jsoup.parse(html);

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

@Test public void testPrettyAndOutlineWithEnDashBody() {
String html = "<div><span>1:15</span>&ndash;<span>2:15</span>&nbsp;p.m.</div>";
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());
}

@Test public void testEmptyElementFormatHtml() {
// don't put newlines into empty blocks
Expand Down Expand Up @@ -1136,8 +1159,7 @@ public void testIs() {
assertEquals("Another", els3.get(2).text());

assertEquals("<p><a>One</a></p>\n" +
"<p>P3</p>\n" +
"<span>Another</span>\n" +
"<p>P3</p><span>Another</span>\n" +
"<p><a>Two</a></p>\n" +
"<p>P4</p>Three", div.html());
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/jsoup/parser/HtmlParserTest.java
Expand Up @@ -694,8 +694,7 @@ 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>\n" +
"<b> \n" +
String want = "<p><b>One</b></p><b> \n" +
" <table>\n" +
" <tbody>\n" +
" <tr>\n" +
Expand Down Expand Up @@ -1033,7 +1032,7 @@ public void testInvalidTableContents() throws IOException {
@Test public void testNormalisesIsIndex() {
Document doc = Jsoup.parse("<body><isindex action='/submit'></body>");
String html = doc.outerHtml();
assertEquals("<form action=\"/submit\"> <hr> <label>This is a searchable index. Enter search keywords: <input name=\"isindex\"></label> <hr> </form>",
assertEquals("<form action=\"/submit\"> <hr><label>This is a searchable index. Enter search keywords: <input name=\"isindex\"></label> <hr> </form>",
StringUtil.normaliseWhitespace(doc.body().html()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/parser/TagTest.java
Expand Up @@ -64,7 +64,7 @@ public class TagTest {
Tag foo2 = Tag.valueOf("FOO");

assertEquals(foo, foo2);
assertTrue(foo.isInline());
assertFalse(foo.isInline());
assertTrue(foo.formatAsBlock());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/safety/CleanerTest.java
Expand Up @@ -310,6 +310,6 @@ public void bailsIfRemovingProtocolThatsNotSet() {
String dirty = "<a>One</a> <a href>Two</a>";
Whitelist relaxedWithAnchor = Whitelist.relaxed().addProtocols("a", "href", "#");
String clean = Jsoup.clean(dirty, relaxedWithAnchor);
assertEquals("<a>One</a> \n<a>Two</a>", clean);
assertEquals("<a>One</a> <a>Two</a>", clean);
}
}

0 comments on commit 3a9bf46

Please sign in to comment.