Skip to content

Commit

Permalink
Unit tests for scala#73 passing
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgeCaseBerg committed Oct 6, 2016
1 parent cb5a295 commit 3019f09
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3019f09

Please sign in to comment.