Aladdin - Scala Bugtracking
[#614] project: compiler priority: low category: feature
submitter assigned to status date submitted
Nikolay Burak fixed 2006-05-26 12:07:17.0
subject [contrib #149] XML attribute values are not escaped
code
object Test
{
        def main(argv : Array[String]) : Unit = {
                val x = "foo & bar"
                val y = (<elem attrib={ x }/>)
                Console.println(y.toString())
        }
}
what happened
At runtime:

Exception in thread "main" scala.xml.MalformedAttributeException: malformed entity reference in attribute value \
[foo & bar]
        at scala.xml.UnprefixedAttribute.(UnprefixedAttribute.scala:22)
        at Test$.main(test.scala:5)
        at Test.main(test.scala)
what expected Output:
<elem attrib="foo &amp; bar"></elem>
Expression results are correctly escaped when they appear within nodes, and the spec. does not distinguish between the node and attribute case, so I presume this is a bug.
[back to overview]
Changes of this bug report
Nikolay  edited on  2006-05-26 12:07:51.0
Burak  edited on  2006-05-29 11:44:59.0
I see the point, but it's not a bug (changing to "feature"). The spec does not talk on escaping. There is no difference in handling node and attribute values for *construction*, and the parser ensures that the XML is well-formed. The parser cannot do this for expression results, so the library does the escaping.
It seems like an arbitrary choice to enable escaping for attributes, but it would break backwards compatibility and it would not be possible to write correct XHTML in Scala and get the expected result [see "ampersand in XHTML"].
Please note that the library *does* some automation for you, for instance if you use quotes " inside an attribute it will print surrounding apostrophes ' when serializing and vice versa.