scala-xml icon indicating copy to clipboard operation
scala-xml copied to clipboard

Characters unnecessarily escaped by toString method of XML elements

Open som-snytt opened this issue 9 years ago • 6 comments

https://issues.scala-lang.org/browse/SI-5645

som-snytt avatar Jun 06 '15 19:06 som-snytt

The buildString method of Text class seems broken(see https://github.com/scala/scala-xml/blob/e3d85ebdefe013a3bfd3baa2278b23ea526706c1/src/main/scala/scala/xml/Text.scala#L26). The possible workaround is to define VerbatimText class and override buildString method without escape functionality.

Maintainers, is PR needed?

raymank26 avatar Jun 21 '15 13:06 raymank26

Currently this is totally expected since XML has two different "values" one is CDATA and one is PCDATA. PCDATA means escaped values and all XML values are automatically escaped if you don't wrap your data with a CDATA field.

schmitch avatar Sep 03 '15 14:09 schmitch

My comment on the scala ticket was:

toString encodes, but text does not. Pick one. Or, I wonder if ScalaTags has special smarts for scripters, embedding in comments etc.

This suggestion may be out of scope, but the API is confusing. toString is not normally used for specialized string representations, arguably.

som-snytt avatar Feb 04 '17 18:02 som-snytt

It seems nothing in the XML specification says that double quotes " are an illegal character within the text of an element. So generally encoding them as "&quot" seems somewhat arbitrary.

lutzh avatar Dec 20 '17 13:12 lutzh

I found this to be an effective work around:

      val verbatimAtom = new xml.Atom(verbatimText) {                                                                                              
        override def buildString(sb: StringBuilder): StringBuilder =                                                                                
          sb.append(importMapStr)                                                                                                                   
      }                                                                                                                                             

coreyoconnor avatar May 19 '19 03:05 coreyoconnor

fix was reverted because it caused https://github.com/scala/scala-xml/issues/413 — someone want to see if they can solve them both at once?

SethTisue avatar Mar 05 '21 03:03 SethTisue