Skip to content

Commit f0a8b4e

Browse files
committed
Fix #72 XMLEventReader does not handle ' properly
* shared/src/main/scala/scala/xml/parsing/MarkupParser.scala: Import unescMap instead of pairs. * shared/src/main/scala/scala/xml/parsing/MarkupParserCommon.scala: Import unescMap instead of pairs. * jvm/src/main/scala/scala/xml/Utility.scala: Uncomment apos in unescape map. * jvm/src/test/scala/scala/xml/pull/XMLEventReaderTest.scala (entityRefTest): Unit test from Fehmi Can Saglam <fehmican.saglam@gmail.com>
1 parent 5f7601a commit f0a8b4e

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-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

0 commit comments

Comments
 (0)