Skip to content

Commit 0cf1d8e

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 af657ce commit 0cf1d8e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

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
}

src/test/scala/scala/xml/UtilityTest.scala

+6
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ class UtilityTest {
5454
</hi>.hashCode // Bug #777
5555
}
5656

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

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

+7
Original file line numberDiff line numberDiff line change
@@ -871,5 +871,12 @@ expected closing tag of foo
871871
// This was the bug, producing <node></node>
872872
assertEquals("<node/>", pp.format(x.copy(minimizeEmpty = true)))
873873
}
874+
875+
@UnitTest
876+
def issue90: Unit = {
877+
val pp = new xml.PrettyPrinter(80, 2, minimizeEmpty = true)
878+
val x = <node><leaf></leaf></node>
879+
assertEquals("<node>\n <leaf/>\n</node>", pp.format(x))
880+
}
874881

875882
}

0 commit comments

Comments
 (0)