Skip to content

Commit 6578a8e

Browse files
committed
Unit tests for #73 passing
1 parent add4c09 commit 6578a8e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

shared/src/main/scala/scala/xml/Utility.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,28 @@ object Utility extends AnyRef with parsing.TokenTests {
4545
*/
4646
def trim(x: Node): Node = x match {
4747
case Elem(pre, lab, md, scp, child@_*) =>
48-
val children = child flatMap trimProper
48+
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper
4949
Elem(pre, lab, md, scp, children.isEmpty, children: _*)
5050
}
5151

52+
private def combineAdjacentTextNodes(children: Node*): Seq[Node] = {
53+
children.foldLeft(Seq.empty[Node]) { (acc, n) =>
54+
(acc.lastOption, n) match {
55+
case (Some(Text(l)), Text(r)) => {
56+
acc.dropRight(1) :+ Text(l + r)
57+
}
58+
case _ => acc :+ n
59+
}
60+
}
61+
}
62+
5263
/**
5364
* trim a child of an element. `Attribute` values and `Atom` nodes that
5465
* are not `Text` nodes are unaffected.
5566
*/
5667
def trimProper(x: Node): Seq[Node] = x match {
5768
case Elem(pre, lab, md, scp, child@_*) =>
58-
val children = child flatMap trimProper
69+
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper
5970
Elem(pre, lab, md, scp, children.isEmpty, children: _*)
6071
case Text(s) =>
6172
new TextBuffer().append(s).toText

0 commit comments

Comments
 (0)