Skip to content

Commit

Permalink
Fix scala#72 XMLEventReader does not handle ' properly
Browse files Browse the repository at this point in the history
	* src/main/scala/scala/xml/Utility.scala: Uncomment apos in
	Escapes map.
	(escape): Add case for apos.

	* src/test/scala/scala/xml/pull/XMLEventReaderTest.scala (entityRefTest):
	Unit test from Fehmi Can Saglam <fehmican.saglam@gmail.com>
  • Loading branch information
ashawley committed Sep 14, 2016
1 parent 4c09977 commit 79588dd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ object Utility extends AnyRef with parsing.TokenTests {
"lt" -> '<',
"gt" -> '>',
"amp" -> '&',
"quot" -> '"'
// enigmatic comment explaining why this isn't escaped --
// is valid xhtml but not html, and IE doesn't know it, says jweb
// "apos" -> '\''
"quot" -> '"',
"apos" -> '\''
)
val escMap = pairs map { case (s, c) => c -> ("&%s;" format s) }
val unescMap = pairs ++ Map("apos" -> '\'')
val unescMap = pairs
}
import Escapes.{ escMap, unescMap }

Expand All @@ -123,6 +121,7 @@ object Utility extends AnyRef with parsing.TokenTests {
case '>' => s.append("&gt;")
case '&' => s.append("&amp;")
case '"' => s.append("&quot;")
case '\'' => s.append("&apos;")
case '\n' => s.append('\n')
case '\r' => s.append('\r')
case '\t' => s.append('\t')
Expand Down
9 changes: 8 additions & 1 deletion src/test/scala/scala/xml/UtilityTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ class UtilityTest {
def aposEscaping: Unit = {
val z = <bar>''</bar>
val z1 = z.toString
assertEquals("<bar>''</bar>", z1)
assertEquals("<bar>&apos;&apos;</bar>", z1)
}

@Test
def quotEscaping: Unit = {
val z = <bar>""</bar>
val z1 = z.toString
assertEquals("<bar>&quot;&quot;</bar>", z1)
}

@Test
Expand Down
28 changes: 27 additions & 1 deletion src/test/scala/scala/xml/pull/XMLEventReaderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scala.xml
package pull

import org.junit.Test
import org.junit.Assert.{assertFalse, assertTrue}
import org.junit.Assert.{assertEquals,assertFalse, assertTrue}

import scala.io.Source
import scala.xml.parsing.FatalError
Expand Down Expand Up @@ -168,4 +168,30 @@ class XMLEventReaderTest {
while(er.hasNext) er.next()
er.stop()
}

@Test
def entityRefTest: Unit = { // SI-7796
val source = Source.fromString("<text>&quot;&apos;&lt;&gt;&amp;</text>")
val er = new XMLEventReader(source)

assertTrue(er.next match {
case EvElemStart(_, "text", _, _) => true
case _ => false
})

val entities = Seq(
EvEntityRef("quot"),
EvEntityRef("apos"),
EvEntityRef("lt"),
EvEntityRef("gt"),
EvEntityRef("amp"))

assertEquals(entities, er.take(entities.size).toSeq)

assertTrue(er.next match {
case EvElemEnd(_, "text") => true
case _ => false
})
assertTrue(er.isEmpty)
}
}

0 comments on commit 79588dd

Please sign in to comment.