From adb40f3f78fff373a7536fe73ccc3d1a4e3233da Mon Sep 17 00:00:00 2001 From: "Aaron S. Hawley" Date: Fri, 21 Jun 2019 15:25:03 -0400 Subject: [PATCH] Fix SI-5645 Don't escape quotes in PCDATA --- jvm/src/test/scala/scala/xml/XMLTest.scala | 5 ++++- shared/src/main/scala/scala/xml/Text.scala | 2 +- shared/src/main/scala/scala/xml/Utility.scala | 14 +++++++++++++ shared/src/test/scala/scala/xml/XMLTest.scala | 20 +++++++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/jvm/src/test/scala/scala/xml/XMLTest.scala b/jvm/src/test/scala/scala/xml/XMLTest.scala index efdb8165a..0ca1ff1ce 100644 --- a/jvm/src/test/scala/scala/xml/XMLTest.scala +++ b/jvm/src/test/scala/scala/xml/XMLTest.scala @@ -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 diff --git a/shared/src/main/scala/scala/xml/Text.scala b/shared/src/main/scala/scala/xml/Text.scala index 8e2ee498f..caa7fa294 100644 --- a/shared/src/main/scala/scala/xml/Text.scala +++ b/shared/src/main/scala/scala/xml/Text.scala @@ -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) } /** diff --git a/shared/src/main/scala/scala/xml/Utility.scala b/shared/src/main/scala/scala/xml/Utility.scala index 776da5a7a..43776af94 100755 --- a/shared/src/main/scala/scala/xml/Utility.scala +++ b/shared/src/main/scala/scala/xml/Utility.scala @@ -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.. diff --git a/shared/src/test/scala/scala/xml/XMLTest.scala b/shared/src/test/scala/scala/xml/XMLTest.scala index a9644ca6d..1e0a304c7 100644 --- a/shared/src/test/scala/scala/xml/XMLTest.scala +++ b/shared/src/test/scala/scala/xml/XMLTest.scala @@ -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""", .attributes) } + @UnitTest + def t5645: Unit = { + + val bar = "baz" + val script = + + val expected = + """|""".stripMargin + + assertEquals(expected, script.toString) + } + @UnitTest def t5843: Unit = { val foo = scala.xml.Attribute(null, "foo", "1", scala.xml.Null)