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