Skip to content

Commit 5876568

Browse files
committed
Add new config for PrettyPrinter to minimize empty tags #90
* src/main/scala/scala/xml/PrettyPrinter.scala (PrettyPrinter): New parameter minimizeEmpty. (PrettyPrinter.traverse): Pass config for minimizeTags to Utility.serialize(). * src/test/scala/scala/xml/UtilityTest.scala (issue90): New test. * src/test/scala/scala/xml/XMLTest.scala (issue90): New test.
1 parent c182281 commit 5876568

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

jvm/src/test/scala/scala/xml/XMLTest.scala

+7
Original file line numberDiff line numberDiff line change
@@ -555,4 +555,11 @@ class XMLTestJVM {
555555
assertEquals("<node/>", pp.format(x.copy(minimizeEmpty = true)))
556556
}
557557

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+
558565
}

shared/src/main/scala/scala/xml/PrettyPrinter.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ 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 = false) {
2727

28+
val minimizeMode = if (minimizeEmpty) MinimizeMode.Always else MinimizeMode.Default
2829
class BrokenException() extends java.lang.Exception
2930

3031
class Item
@@ -150,7 +151,7 @@ class PrettyPrinter(width: Int, step: Int) {
150151
case _ =>
151152
val test = {
152153
val sb = new StringBuilder()
153-
Utility.serialize(node, pscope, sb, stripComments = false)
154+
Utility.serialize(node, pscope, sb, stripComments = false, minimizeTags = minimizeMode)
154155
if (doPreserve(node)) sb.toString
155156
else TextBuffer.fromString(sb.toString).toText(0).data
156157
}

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)