Skip to content

Commit 4ac3474

Browse files
committed
Fix #72 XMLEventReader does not handle ' properly
* jvm/src/main/scala/scala/xml/Utility.scala: Uncomment apos in Escapes map. (escape): Add case for apos. * jvm/src/test/scala/scala/xml/pull/XMLEventReaderTest.scala (entityRefTest): Unit test from Fehmi Can Saglam <[email protected]>
1 parent 5f7601a commit 4ac3474

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

jvm/src/test/scala/scala/xml/pull/XMLEventReaderTest.scala

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scala.xml
22
package pull
33

44
import org.junit.Test
5-
import org.junit.Assert.{assertFalse, assertTrue}
5+
import org.junit.Assert.{assertEquals,assertFalse, assertTrue}
66

77
import scala.io.Source
88
import scala.xml.parsing.FatalError
@@ -168,4 +168,28 @@ class XMLEventReaderTest {
168168
while(er.hasNext) er.next()
169169
er.stop()
170170
}
171+
172+
@Test
173+
def entityRefTest: Unit = { // SI-7796
174+
val source = Source.fromString("<text>&quot;&apos;&lt;&gt;&amp;</text>")
175+
val er = new XMLEventReader(source)
176+
177+
assertTrue(er.next match {
178+
case EvElemStart(_, "text", _, _) => true
179+
case _ => false
180+
})
181+
182+
assertEquals(EvEntityRef("quot"), er.next)
183+
assertEquals(EvEntityRef("apos"), er.next)
184+
assertEquals(EvEntityRef("lt"), er.next)
185+
assertEquals(EvEntityRef("gt"), er.next)
186+
assertEquals(EvEntityRef("amp"), er.next)
187+
188+
assertTrue(er.next match {
189+
case EvElemEnd(_, "text") => true
190+
case _ => false
191+
})
192+
193+
assertTrue(er.isEmpty)
194+
}
171195
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,14 @@ object Utility extends AnyRef with parsing.TokenTests {
9393
* For reasons unclear escape and unescape are a long ways from
9494
* being logical inverses.
9595
*/
96-
val pairs = Map(
96+
val unescMap = Map(
9797
"lt" -> '<',
9898
"gt" -> '>',
9999
"amp" -> '&',
100-
"quot" -> '"'
101-
// enigmatic comment explaining why this isn't escaped --
102-
// is valid xhtml but not html, and IE doesn't know it, says jweb
103-
// "apos" -> '\''
100+
"quot" -> '"',
101+
"apos" -> '\''
104102
)
105-
val escMap = pairs map { case (s, c) => c -> ("&%s;" format s) }
106-
val unescMap = pairs ++ Map("apos" -> '\'')
103+
val escMap = (unescMap - "apos") map { case (s, c) => c -> ("&%s;" format s) }
107104
}
108105
import Escapes.{ escMap, unescMap }
109106

shared/src/main/scala/scala/xml/parsing/MarkupParser.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package parsing
1212

1313
import scala.io.Source
1414
import scala.xml.dtd._
15-
import Utility.Escapes.{ pairs => unescape }
15+
import Utility.Escapes.{ unescMap => unescape }
1616

1717
/**
1818
* An XML parser.

shared/src/main/scala/scala/xml/parsing/MarkupParserCommon.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package parsing
1212

1313
import scala.io.Source
1414
import scala.annotation.switch
15-
import Utility.Escapes.{ pairs => unescape }
15+
import Utility.Escapes.{ unescMap => unescape }
1616

1717
import Utility.SU
1818

shared/src/test/scala/scala/xml/UtilityTest.scala

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class UtilityTest {
3838
assertEquals("<bar>''</bar>", z1)
3939
}
4040

41+
@Test
42+
def quotEscaping: Unit = {
43+
val z = <bar>""</bar>
44+
val z1 = z.toString
45+
assertEquals("<bar>&quot;&quot;</bar>", z1)
46+
}
47+
4148
@Test
4249
def sort: Unit = {
4350
val q = xml.Utility.sort(<a g='3' j='2' oo='2' a='2'/>)

0 commit comments

Comments
 (0)