Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c07ba8a

Browse files
committedNov 17, 2013
If pretty-print is disabled, don't trim outer whitespace in Element.html()
Fixes jhy#368
1 parent be64f74 commit c07ba8a

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed
 

‎CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ jsoup changelog
33
*** Release 1.7.4 [PENDING]
44
* Improved the performance of Element.text() by 2.2x
55

6+
* If pretty-print is disabled, don't trim outer whitespace in Element.html()
7+
68
*** Release 1.7.3 [2013-Nov-10]
79
* Introduced FormElement, providing easy access to form controls and their data, and the ability to submit forms
810
with Jsoup.Connect.

‎src/main/java/org/jsoup/nodes/Element.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,8 @@ void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out)
10971097
*/
10981098
public String html() {
10991099
StringBuilder accum = new StringBuilder();
1100-
html(accum);
1101-
return accum.toString().trim();
1100+
html(accum);
1101+
return getOutputSettings().prettyPrint() ? accum.toString().trim() : accum.toString();
11021102
}
11031103

11041104
private void html(StringBuilder accum) {

‎src/main/java/org/jsoup/nodes/Node.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ protected void outerHtml(StringBuilder accum) {
557557
}
558558

559559
// if this node has no document (or parent), retrieve the default output settings
560-
private Document.OutputSettings getOutputSettings() {
560+
Document.OutputSettings getOutputSettings() {
561561
return ownerDocument() != null ? ownerDocument().outputSettings() : (new Document("")).outputSettings();
562562
}
563563

‎src/test/java/org/jsoup/nodes/ElementTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public class ElementTest {
217217
}
218218

219219
@Test public void testInnerHtml() {
220-
Document doc = Jsoup.parse("<div><p>Hello</p></div>");
220+
Document doc = Jsoup.parse("<div>\n <p>Hello</p> </div>");
221221
assertEquals("<p>Hello</p>", doc.getElementsByTag("div").get(0).html());
222222
}
223223

@@ -239,9 +239,12 @@ public class ElementTest {
239239
}
240240

241241
@Test public void testNotPretty() {
242-
Document doc = Jsoup.parse("<div> \n<p>Hello\n there</p></div>");
242+
Document doc = Jsoup.parse("<div> \n<p>Hello\n there\n</p></div>");
243243
doc.outputSettings().prettyPrint(false);
244-
assertEquals("<html><head></head><body><div> \n<p>Hello\n there</p></div></body></html>", doc.html());
244+
assertEquals("<html><head></head><body><div> \n<p>Hello\n there\n</p></div></body></html>", doc.html());
245+
246+
Element div = doc.select("div").first();
247+
assertEquals(" \n<p>Hello\n there\n</p>", div.html());
245248
}
246249

247250
@Test public void testEmptyElementFormatHtml() {

0 commit comments

Comments
 (0)
Please sign in to comment.