Skip to content

Commit

Permalink
Fix XMLEventReader does not handle ' properly
Browse files Browse the repository at this point in the history
Fixes scala#72
  • Loading branch information
fehmicansaglam committed Feb 6, 2017
1 parent 5f7601a commit 3237688
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
36 changes: 36 additions & 0 deletions jvm/src/test/scala/scala/xml/pull/XMLEventReaderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,40 @@ class XMLEventReaderTest {
while(er.hasNext) er.next()
er.stop()
}

@Test
def entityRefTest: Unit = {
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
})
assertTrue(er.next match {
case EvEntityRef("quot") => true
case e => false
})
assertTrue(er.next match {
case EvEntityRef("apos") => true
case _ => false
})
assertTrue(er.next match {
case EvEntityRef("lt") => true
case _ => false
})
assertTrue(er.next match {
case EvEntityRef("gt") => true
case _ => false
})
assertTrue(er.next match {
case EvEntityRef("amp") => true
case _ => false
})
assertTrue(er.next match {
case EvElemEnd(_, "text") => true
case _ => false
})
assert(er.isEmpty)
}
}
2 changes: 1 addition & 1 deletion shared/src/main/scala/scala/xml/Utility.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object Utility extends AnyRef with parsing.TokenTests {
* For reasons unclear escape and unescape are a long ways from
* being logical inverses.
*/
val pairs = Map(
private val pairs = Map(
"lt" -> '<',
"gt" -> '>',
"amp" -> '&',
Expand Down
13 changes: 7 additions & 6 deletions shared/src/main/scala/scala/xml/parsing/MarkupParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package parsing

import scala.io.Source
import scala.xml.dtd._
import Utility.Escapes.{ pairs => unescape }
import Utility.Escapes.unescMap

/**
* An XML parser.
Expand Down Expand Up @@ -470,11 +470,12 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
case _ => // EntityRef
val n = xName
xToken(';')

if (unescape contains n) {
handle.entityRef(tmppos, n)
ts &+ unescape(n)
} else push(n)
unescMap.get(n) match {
case None => push(n)
case Some(theChar) =>
handle.entityRef(tmppos, n)
ts &+ theChar
}
}
case _ => // text content
appendText(tmppos, ts, xText)
Expand Down

0 comments on commit 3237688

Please sign in to comment.