Skip to content

Rework Utility.trim #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions shared/src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ object Utility extends AnyRef with parsing.TokenTests {
*/
def trim(x: Node): Node = x match {
case Elem(pre, lab, md, scp, child@_*) =>
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper
val children = combineAdjacentTextNodes(child) flatMap trimProper
Elem(pre, lab, md, scp, children.isEmpty, children: _*)
}

private def combineAdjacentTextNodes(children: Node*): Seq[Node] = {
private def combineAdjacentTextNodes(children: Seq[Node]): Seq[Node] = {
children.foldRight(Seq.empty[Node]) {
case (Text(left), Text(right) +: accMinusLast) => Text(left + right) +: accMinusLast
case (n, acc) => n +: acc
case (Text(left), Text(right) +: nodes) => Text(left + right) +: nodes
case (n, nodes) => n +: nodes
}
}

Expand All @@ -63,7 +63,7 @@ object Utility extends AnyRef with parsing.TokenTests {
*/
def trimProper(x: Node): Seq[Node] = x match {
case Elem(pre, lab, md, scp, child@_*) =>
val children = combineAdjacentTextNodes(child:_*) flatMap trimProper
val children = combineAdjacentTextNodes(child) flatMap trimProper
Elem(pre, lab, md, scp, children.isEmpty, children: _*)
case Text(s) =>
new TextBuffer().append(s).toText
Expand Down