Skip to content

Commit

Permalink
Fix the handling by KtXmlReader of text with entities in it. It will now
Browse files Browse the repository at this point in the history
never be recorded as ignorable whitespace, even when parsed as separate
parts. This should fix #241.
  • Loading branch information
pdvrieze committed Nov 23, 2024
1 parent 51c7c0f commit 1438e45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public class KtXmlReader internal constructor(
_eventType = COMMENT
return
}

val lastEvent = _eventType
val eventType = peekType()
_eventType = eventType
when (eventType) {
Expand All @@ -445,7 +445,10 @@ public class KtXmlReader internal constructor(
if (depth == 1) state = State.POST
}

TEXT -> {
TEXT -> if (lastEvent == ENTITY_REF) { // Entity refs are part of text, so don't
// consider the following text whitespace at all
pushRegularText('<', false)
} else {
pushText('<')
if (isWhitespace) _eventType = IGNORABLE_WHITESPACE
}
Expand Down
1 change: 1 addition & 0 deletions testutil/api/testutil.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ public abstract class nl/adaptivity/xmlutil/test/TestCommonReader {
protected final fun testReadUnknownEntity (Lkotlin/jvm/functions/Function1;)V
public fun testReaderWithBOM ()V
protected final fun testReaderWithBOM (Lkotlin/jvm/functions/Function1;)V
public fun testWhiteSpaceWithEntity ()V
}

Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,24 @@ abstract class TestCommonReader {
open fun testProcessingInstructionDom() {
testProcessingInstruction(::createReader) { DomWriter() }
}

@Test
open fun testWhiteSpaceWithEntity() {
val data = "<x> &amp; </x>"
val r = createReader(data)
r.nextTag()
r.require(EventType.START_ELEMENT, "", "x")
assertEquals(EventType.TEXT, r.next())
r.require(EventType.TEXT, null)
if (r.text.isBlank()) { // either parse as 3 parts or as a single text
assertEquals(" ", r.text)
assertEquals(EventType.ENTITY_REF, r.next())
assertEquals("&", r.text)
assertEquals(EventType.TEXT, r.next())
assertEquals(" ", r.text)
} else {
assertEquals(" & ", r.text)
}

}
}

0 comments on commit 1438e45

Please sign in to comment.