@@ -46,17 +46,24 @@ 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.foldRight(Seq .empty[Node ]) {
55+ case (Text (left), Text (right) +: accMinusLast) => Text (left + right) +: accMinusLast
56+ case (n, acc) => n +: acc
57+ }
58+ }
59+
5360 /**
5461 * trim a child of an element. `Attribute` values and `Atom` nodes that
5562 * are not `Text` nodes are unaffected.
5663 */
5764 def trimProper (x : Node ): Seq [Node ] = x match {
5865 case Elem (pre, lab, md, scp, child@_* ) =>
59- val children = child flatMap trimProper
66+ val children = combineAdjacentTextNodes( child:_* ) flatMap trimProper
6067 Elem (pre, lab, md, scp, children.isEmpty, children : _* )
6168 case Text (s) =>
6269 new TextBuffer ().append(s).toText
0 commit comments