@@ -46,17 +46,28 @@ object Utility extends AnyRef with parsing.TokenTests {
4646 */
4747 def trim (x : Node ): Node = x match {
4848 case Elem (pre, lab, md, scp, child@_* ) =>
49- val children = child flatMap trimProper
49+ val children = combineAdjacentTextNodes( child:_* ) flatMap trimProper
5050 Elem (pre, lab, md, scp, children.isEmpty, children : _* )
5151 }
5252
53+ private def combineAdjacentTextNodes (children : Node * ): Seq [Node ] = {
54+ children.foldLeft(Seq .empty[Node ]) { (acc, n) =>
55+ (acc.lastOption, n) match {
56+ case (Some (Text (l)), Text (r)) => {
57+ acc.dropRight(1 ) :+ Text (l + r)
58+ }
59+ case _ => acc :+ n
60+ }
61+ }
62+ }
63+
5364 /**
5465 * trim a child of an element. `Attribute` values and `Atom` nodes that
5566 * are not `Text` nodes are unaffected.
5667 */
5768 def trimProper (x : Node ): Seq [Node ] = x match {
5869 case Elem (pre, lab, md, scp, child@_* ) =>
59- val children = child flatMap trimProper
70+ val children = combineAdjacentTextNodes( child:_* ) flatMap trimProper
6071 Elem (pre, lab, md, scp, children.isEmpty, children : _* )
6172 case Text (s) =>
6273 new TextBuffer ().append(s).toText
0 commit comments