Skip to content

Commit b578bbf

Browse files
committed
Fix SI-5645 Don't escape quotes in PCDATA
1 parent 5bb1af0 commit b578bbf

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
@@ -321,7 +321,10 @@ class XMLTestJVM {
321321
val inputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
322322
val streamReader = new java.io.InputStreamReader(inputStream, XML.encoding)
323323

324-
assertEquals(xml.toString, XML.load(streamReader).toString)
324+
def unescapeQuotes(str: String) =
325+
""".r.replaceFirstIn(str, "\"")
326+
val xmlFixed = unescapeQuotes(xml.toString)
327+
assertEquals(xmlFixed, XML.load(streamReader).toString)
325328
}
326329

327330
@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!
@@ -492,6 +492,22 @@ Ours is the portal of hope, come as you are."
492492
assertTrue(g)
493493
}
494494

495+
@UnitTest
496+
def t5645: Unit = {
497+
498+
val bar = "baz"
499+
val script = <script type="text/javascript">
500+
foo("{bar}");
501+
</script>
502+
503+
val expected =
504+
"""|<script type="text/javascript">
505+
| foo("baz");
506+
| </script>""".stripMargin
507+
508+
assertEquals(expected, script.toString)
509+
}
510+
495511
@UnitTest
496512
def t5843: Unit = {
497513
val foo = scala.xml.Attribute(null, "foo", "1", scala.xml.Null)

0 commit comments

Comments
 (0)