- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Strange behaviour rewriting html script tags
Fri, 2010-12-31, 18:43
I got this strange behaviour trying to rewrite the "script" tags in an html document. I simplified the example to the following.
If a "script" tag is matched it should simply be replaced with "". But depending on the input this simple rewrite works or not. If you pass in a script tag with type "text/javascript" it does _not_ work, if you slightly change the type attribute to e.g. "text/javascrip" (last t is missing) then it works. So it seems to depend on the presence of attribute type="text/javascript". Please also note the amount of "REPLACE" printed in each test case.
Can anyone explain?
-Christoph
import xml._
import xml.transform._
object ScriptTransform {
object Xform extends RewriteRule {
override def transform(n: Node) = n match {
case e@Elem(_, "script", _, _) =>
println("REPLACE")
case _ => n
}
}
def xform(in: NodeSeq) = new RuleTransformer(Xform).transform(in)
}
import org.specs.Specification
class XmlTransformSpecs extends Specification {
// This one FAILS!
"A ScriptTransform sets the src attribute to 'url'" in {
ScriptTransform.xform(
) must equalIgnoreSpace (result)
}
// SUCCEEDS
"A ScriptTransform sets the src attribute to 'url'" in {
ScriptTransform.xform(
) must equalIgnoreSpace (result)
}
// SUCCEEDS
"A ScriptTransform sets the src attribute to 'url'" in {
ScriptTransform.xform(
) must equalIgnoreSpace (result)
}
}