Skip to content

Add new config for PrettyPrinter to minimize empty tags #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,24 @@ class XMLTestJVM {
pp.format(x, sb)
assertEquals(expected, sb.toString)
}

@UnitTest
def issue46: Unit = {
// val x = <node/>
val x = <node></node>
// val x = Elem(null, "node", e, sc)
val pp = new xml.PrettyPrinter(80, 2)
// This assertion passed
assertEquals("<node></node>", x.toString)
// This was the bug, producing <node></node>
assertEquals("<node/>", pp.format(x.copy(minimizeEmpty = true)))
}

@UnitTest
def issue90: Unit = {
val pp = new xml.PrettyPrinter(80, 2, minimizeEmpty = true)
val x = <node><leaf></leaf></node>
assertEquals("<node>\n <leaf/>\n</node>", pp.format(x))
}

}
7 changes: 5 additions & 2 deletions shared/src/main/scala/scala/xml/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import Utility.sbToString
* @param width the width to fit the output into
* @param step indentation
*/
class PrettyPrinter(width: Int, step: Int) {
class PrettyPrinter(width: Int, step: Int, minimizeEmpty: Boolean) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please document the expected behaviour when this parameter is set to true? (e.g. in @param)


def this(width: Int, step: Int) = this(width, step, minimizeEmpty = false)

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

class Item
Expand Down Expand Up @@ -150,7 +153,7 @@ class PrettyPrinter(width: Int, step: Int) {
case _ =>
val test = {
val sb = new StringBuilder()
Utility.serialize(node, pscope, sb, stripComments = false)
Utility.serialize(node, pscope, sb, stripComments = false, minimizeTags = minimizeMode)
if (doPreserve(node)) sb.toString
else TextBuffer.fromString(sb.toString).toText(0).data
}
Expand Down
6 changes: 6 additions & 0 deletions shared/src/test/scala/scala/xml/UtilityTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ class UtilityTest {
</hi>.hashCode // Bug #777
}

@Test
def issue90: Unit = {
val x = <node><leaf></leaf></node>
assertEquals("<node><leaf/></node>", Utility.serialize(x, minimizeTags = MinimizeMode.Always).toString)
}

}