Skip to content

Commit 62de21c

Browse files
authored
Merge pull request #279 from ashawley/si-5645
Fix SI-5645 Don't escape quotes in PCDATA
2 parents e266cfa + adb40f3 commit 62de21c

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

jvm/src/test/scala/scala/xml/XMLTest.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,10 @@ class XMLTestJVM {
315315
val inputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
316316
val streamReader = new java.io.InputStreamReader(inputStream, XML.encoding)
317317

318-
assertEquals(xml.toString, XML.load(streamReader).toString)
318+
def unescapeQuotes(str: String) =
319+
""".r.replaceFirstIn(str, "\"")
320+
val xmlFixed = unescapeQuotes(xml.toString)
321+
assertEquals(xmlFixed, XML.load(streamReader).toString)
319322
}
320323

321324
@UnitTest

shared/src/main/scala/scala/xml/Text.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Text(data: String) extends Atom[String](data) {
2323
* specification.
2424
*/
2525
override def buildString(sb: StringBuilder): StringBuilder =
26-
Utility.escape(data, sb)
26+
Utility.escapeText(data, sb)
2727
}
2828

2929
/**

shared/src/main/scala/scala/xml/Utility.scala

+14
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ object Utility extends AnyRef with parsing.TokenTests {
127127
}
128128
}
129129

130+
/**
131+
* Appends escaped string to `s`, but not ".
132+
*/
133+
final def escapeText(text: String, s: StringBuilder): StringBuilder = {
134+
val escTextMap = escMap - '"' // Remove quotes from escMap
135+
text.iterator.foldLeft(s) { (s, c) =>
136+
escTextMap.get(c) match {
137+
case Some(str) => s ++= str
138+
case _ if c >= ' ' || "\n\r\t".contains(c) => s += c
139+
case _ => s // noop
140+
}
141+
}
142+
}
143+
130144
/**
131145
* Appends unescaped string to `s`, `amp` becomes `&`,
132146
* `lt` becomes `<` etc..

shared/src/test/scala/scala/xml/XMLTest.scala

+18-2
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ class XMLTest {
307307
@UnitTest
308308
def escape =
309309
assertEquals("""
310-
"Come, come again, whoever you are, come!
310+
"Come, come again, whoever you are, come!
311311
Heathen, fire worshipper or idolatrous, come!
312312
Come even if you broke your penitence a hundred times,
313-
Ours is the portal of hope, come as you are."
313+
Ours is the portal of hope, come as you are."
314314
Mevlana Celaleddin Rumi""", <![CDATA[
315315
"Come, come again, whoever you are, come!
316316
Heathen, fire worshipper or idolatrous, come!
@@ -473,6 +473,22 @@ Ours is the portal of hope, come as you are."
473473
assertHonorsIterableContract(<a a="" y={ null: String }/>.attributes)
474474
}
475475

476+
@UnitTest
477+
def t5645: Unit = {
478+
479+
val bar = "baz"
480+
val script = <script type="text/javascript">
481+
foo("{bar}");
482+
</script>
483+
484+
val expected =
485+
"""|<script type="text/javascript">
486+
| foo("baz");
487+
| </script>""".stripMargin
488+
489+
assertEquals(expected, script.toString)
490+
}
491+
476492
@UnitTest
477493
def t5843: Unit = {
478494
val foo = scala.xml.Attribute(null, "foo", "1", scala.xml.Null)

0 commit comments

Comments
 (0)