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 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
ashawley committed May 24, 2017
commit 063ab3b47e81a4547395aec6ac6f80c3936a2cf9
7 changes: 7 additions & 0 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,11 @@ class XMLTestJVM {
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))
}

}
5 changes: 3 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,9 @@ 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 = false) {

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

class Item
Expand Down Expand Up @@ -150,7 +151,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)
}

}