@@ -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