Skip to content

Commit

Permalink
Unit tests for #73 passing
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgeCaseBerg committed Feb 11, 2017
1 parent 990b379 commit 88df70c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions shared/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 88df70c

Please sign in to comment.