@@ -45,17 +45,28 @@ object Utility extends AnyRef with parsing.TokenTests {
45
45
*/
46
46
def trim (x : Node ): Node = x match {
47
47
case Elem (pre, lab, md, scp, child@_* ) =>
48
- val children = child flatMap trimProper
48
+ val children = combineAdjacentTextNodes( child:_* ) flatMap trimProper
49
49
Elem (pre, lab, md, scp, children.isEmpty, children : _* )
50
50
}
51
51
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
+
52
63
/**
53
64
* trim a child of an element. `Attribute` values and `Atom` nodes that
54
65
* are not `Text` nodes are unaffected.
55
66
*/
56
67
def trimProper (x : Node ): Seq [Node ] = x match {
57
68
case Elem (pre, lab, md, scp, child@_* ) =>
58
- val children = child flatMap trimProper
69
+ val children = combineAdjacentTextNodes( child:_* ) flatMap trimProper
59
70
Elem (pre, lab, md, scp, children.isEmpty, children : _* )
60
71
case Text (s) =>
61
72
new TextBuffer ().append(s).toText
0 commit comments