Skip to content

Commit

Permalink
Fix SI-5645 Don't escape quotes in PCDATA
Browse files Browse the repository at this point in the history
  • Loading branch information
ashawley committed Jun 27, 2019
1 parent e266cfa commit adb40f3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,10 @@ class XMLTestJVM {
val inputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
val streamReader = new java.io.InputStreamReader(inputStream, XML.encoding)

assertEquals(xml.toString, XML.load(streamReader).toString)
def unescapeQuotes(str: String) =
""".r.replaceFirstIn(str, "\"")
val xmlFixed = unescapeQuotes(xml.toString)
assertEquals(xmlFixed, XML.load(streamReader).toString)
}

@UnitTest
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/scala/scala/xml/Text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Text(data: String) extends Atom[String](data) {
* specification.
*/
override def buildString(sb: StringBuilder): StringBuilder =
Utility.escape(data, sb)
Utility.escapeText(data, sb)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions shared/src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ object Utility extends AnyRef with parsing.TokenTests {
}
}

/**
* Appends escaped string to `s`, but not ".
*/
final def escapeText(text: String, s: StringBuilder): StringBuilder = {
val escTextMap = escMap - '"' // Remove quotes from escMap
text.iterator.foldLeft(s) { (s, c) =>
escTextMap.get(c) match {
case Some(str) => s ++= str
case _ if c >= ' ' || "\n\r\t".contains(c) => s += c
case _ => s // noop
}
}
}

/**
* Appends unescaped string to `s`, `amp` becomes `&`,
* `lt` becomes `<` etc..
Expand Down
20 changes: 18 additions & 2 deletions shared/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ class XMLTest {
@UnitTest
def escape =
assertEquals("""
"Come, come again, whoever you are, come!
"Come, come again, whoever you are, come!
Heathen, fire worshipper or idolatrous, come!
Come even if you broke your penitence a hundred times,
Ours is the portal of hope, come as you are."
Ours is the portal of hope, come as you are."
Mevlana Celaleddin Rumi""", <![CDATA[
"Come, come again, whoever you are, come!
Heathen, fire worshipper or idolatrous, come!
Expand Down Expand Up @@ -473,6 +473,22 @@ Ours is the portal of hope, come as you are."
assertHonorsIterableContract(<a a="" y={ null: String }/>.attributes)
}

@UnitTest
def t5645: Unit = {

val bar = "baz"
val script = <script type="text/javascript">
foo("{bar}");
</script>

val expected =
"""|<script type="text/javascript">
| foo("baz");
| </script>""".stripMargin

assertEquals(expected, script.toString)
}

@UnitTest
def t5843: Unit = {
val foo = scala.xml.Attribute(null, "foo", "1", scala.xml.Null)
Expand Down

0 comments on commit adb40f3

Please sign in to comment.