Open
Description
import scala.xml._
import scala.xml.transform.{RewriteRule, RuleTransformer}
object TransformIssue {
def addNewElement(): RewriteRule = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case <element></element> => <element><new></new></element>
}
}
def addThingElement(): RewriteRule = new RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case <element>{ children@_*}</element> => <element>{ children }</element>
case <new></new> => <new><thing></thing></new>
}
}
def change(node: Node): Node =
new RuleTransformer(
addNewElement(),
addThingElement()
).transform(node).head
def changeWorkaround(node: Node): Node = {
val out1 = new RuleTransformer(
addNewElement()
).transform(node).head
new RuleTransformer(
addThingElement()
).transform(out1).head
}
}
import org.scalatest.{FlatSpec, FunSpec}
import org.scalatest._
class TransformIssueSpec extends FlatSpec with Matchers {
it should "apply transform to created elements" in {
val output = TransformIssue.change(<element></element>)
output should be(<element><new><thing></thing></new></element>)
} // fails
it should "work the same as the workaround imo" in {
TransformIssue.change(<element></element>) should equal(TransformIssue.changeWorkaround(<element></element>))
} // fails
}
When we apply a transform with two rewrite rules: the first one adding a new element, the second one adding children to the new element; then the second rewrite rule does not match on the elements added in the first rule.
When we apply the same RewriteRules in two separate RuleTransformers it does add the children to the elements added in the first step. We would expect the change
and changeWorkaround
functions to produce the same output.