Skip to content

Commit b0a753d

Browse files
authored
Merge pull request #90 from ashawley/pretty-print-empty
Add new config for PrettyPrinter to minimize empty tags
2 parents 9ed63c4 + 8c44f6d commit b0a753d

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

Diff for: jvm/src/test/scala/scala/xml/XMLTest.scala

+20
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,24 @@ class XMLTestJVM {
542542
pp.format(x, sb)
543543
assertEquals(expected, sb.toString)
544544
}
545+
546+
@UnitTest
547+
def issue46: Unit = {
548+
// val x = <node/>
549+
val x = <node></node>
550+
// val x = Elem(null, "node", e, sc)
551+
val pp = new xml.PrettyPrinter(80, 2)
552+
// This assertion passed
553+
assertEquals("<node></node>", x.toString)
554+
// This was the bug, producing <node></node>
555+
assertEquals("<node/>", pp.format(x.copy(minimizeEmpty = true)))
556+
}
557+
558+
@UnitTest
559+
def issue90: Unit = {
560+
val pp = new xml.PrettyPrinter(80, 2, minimizeEmpty = true)
561+
val x = <node><leaf></leaf></node>
562+
assertEquals("<node>\n <leaf/>\n</node>", pp.format(x))
563+
}
564+
545565
}

Diff for: shared/src/main/scala/scala/xml/PrettyPrinter.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import Utility.sbToString
2323
* @param width the width to fit the output into
2424
* @param step indentation
2525
*/
26-
class PrettyPrinter(width: Int, step: Int) {
26+
class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean) {
2727

28+
def this(width: Int, step: Int) = this(width, step, minimizeEmpty = false)
29+
30+
val minimizeMode = if (minimizeEmpty) MinimizeMode.Always else MinimizeMode.Default
2831
class BrokenException() extends java.lang.Exception
2932

3033
class Item
@@ -150,7 +153,7 @@ class PrettyPrinter(width: Int, step: Int) {
150153
case _ =>
151154
val test = {
152155
val sb = new StringBuilder()
153-
Utility.serialize(node, pscope, sb, stripComments = false)
156+
Utility.serialize(node, pscope, sb, stripComments = false, minimizeTags = minimizeMode)
154157
if (doPreserve(node)) sb.toString
155158
else TextBuffer.fromString(sb.toString).toText(0).data
156159
}

Diff for: shared/src/test/scala/scala/xml/UtilityTest.scala

+6
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ class UtilityTest {
5151
</hi>.hashCode // Bug #777
5252
}
5353

54+
@Test
55+
def issue90: Unit = {
56+
val x = <node><leaf></leaf></node>
57+
assertEquals("<node><leaf/></node>", Utility.serialize(x, minimizeTags = MinimizeMode.Always).toString)
58+
}
59+
5460
}

0 commit comments

Comments
 (0)