This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Strange behaviour rewriting html script tags

No replies
Christoph Driessen
Joined: 2009-04-25,
User offline. Last seen 42 years 45 weeks ago.

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 {

val result =

// 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)
}
}

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland