Skip to content

Commit 00ca5cd

Browse files
authored
Modified XmlParser to correctly parse CDATA. (#633)
1 parent 2ea306e commit 00ca5cd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: readium/shared/src/main/java/org/readium/r2/shared/util/xml/XmlParser.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public class XmlParser(
6464
val element = buildElement(attributes, children, lang)
6565
stack.peek().first.add(element)
6666
}
67-
XmlPullParser.TEXT, XmlPullParser.ENTITY_REF -> {
67+
XmlPullParser.CDSECT,
68+
XmlPullParser.TEXT,
69+
XmlPullParser.ENTITY_REF,
70+
-> {
6871
text += parser.text
6972
}
7073
}

Diff for: readium/shared/src/test/java/org/readium/r2/shared/util/xml/XmlParserTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@ class XmlParserTest {
209209
fun `An input with no root raises an exception`() {
210210
parseXmlString(" \n \n")
211211
}
212+
213+
@Test
214+
fun `CDATA parsed rightly`() {
215+
val doc = parseXmlString(
216+
"""
217+
<text>
218+
pre text <![CDATA["Some text like <, >, & are safe here"]]> post text
219+
</text>
220+
""".trimIndent()
221+
)
222+
223+
val cdata = doc.children.first() as TextNode
224+
225+
assertEquals("pre text \"Some text like <, >, & are safe here\" post text", cdata.text.trim())
226+
}
212227
}
213228

214229
@RunWith(RobolectricTestRunner::class)

0 commit comments

Comments
 (0)