From 1e3b2883c88f8824f71194072348bc59f23561fd Mon Sep 17 00:00:00 2001 From: Ethan Joachim Eldridge Date: Thu, 6 Oct 2016 00:10:47 -0400 Subject: [PATCH] Unit tests for #73 passing --- shared/src/main/scala/scala/xml/Utility.scala | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/shared/src/main/scala/scala/xml/Utility.scala b/shared/src/main/scala/scala/xml/Utility.scala index b40adf2d8..2f528e1eb 100755 --- a/shared/src/main/scala/scala/xml/Utility.scala +++ b/shared/src/main/scala/scala/xml/Utility.scala @@ -46,17 +46,28 @@ object Utility extends AnyRef with parsing.TokenTests { */ def trim(x: Node): Node = x match { case Elem(pre, lab, md, scp, child@_*) => - val children = child flatMap trimProper + val children = combineAdjacentTextNodes(child:_*) flatMap trimProper Elem(pre, lab, md, scp, children.isEmpty, children: _*) } + private def combineAdjacentTextNodes(children: Node*): Seq[Node] = { + children.foldLeft(Seq.empty[Node]) { (acc, n) => + (acc.lastOption, n) match { + case (Some(Text(l)), Text(r)) => { + acc.dropRight(1) :+ Text(l + r) + } + case _ => acc :+ n + } + } + } + /** * trim a child of an element. `Attribute` values and `Atom` nodes that * are not `Text` nodes are unaffected. */ def trimProper(x: Node): Seq[Node] = x match { case Elem(pre, lab, md, scp, child@_*) => - val children = child flatMap trimProper + val children = combineAdjacentTextNodes(child:_*) flatMap trimProper Elem(pre, lab, md, scp, children.isEmpty, children: _*) case Text(s) => new TextBuffer().append(s).toText