From 1438e454a62e645094bd56a422453b1b75c23c05 Mon Sep 17 00:00:00 2001 From: Paul de Vrieze Date: Sat, 23 Nov 2024 16:30:22 +0000 Subject: [PATCH] Fix the handling by KtXmlReader of text with entities in it. It will now never be recorded as ignorable whitespace, even when parsed as separate parts. This should fix #241. --- .../nl/adaptivity/xmlutil/core/KtXmlReader.kt | 7 +++++-- testutil/api/testutil.api | 1 + .../xmlutil/test/TestCommonReader.kt | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/core/base/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/KtXmlReader.kt b/core/base/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/KtXmlReader.kt index d7e2312b3..d5e8a5b77 100644 --- a/core/base/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/KtXmlReader.kt +++ b/core/base/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/KtXmlReader.kt @@ -426,7 +426,7 @@ public class KtXmlReader internal constructor( _eventType = COMMENT return } - + val lastEvent = _eventType val eventType = peekType() _eventType = eventType when (eventType) { @@ -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 } diff --git a/testutil/api/testutil.api b/testutil/api/testutil.api index d6644300e..ba7c638ee 100644 --- a/testutil/api/testutil.api +++ b/testutil/api/testutil.api @@ -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 } diff --git a/testutil/src/commonMain/kotlin/nl/adaptivity/xmlutil/test/TestCommonReader.kt b/testutil/src/commonMain/kotlin/nl/adaptivity/xmlutil/test/TestCommonReader.kt index b04d13fd9..0ef687bcd 100644 --- a/testutil/src/commonMain/kotlin/nl/adaptivity/xmlutil/test/TestCommonReader.kt +++ b/testutil/src/commonMain/kotlin/nl/adaptivity/xmlutil/test/TestCommonReader.kt @@ -328,4 +328,24 @@ abstract class TestCommonReader { open fun testProcessingInstructionDom() { testProcessingInstruction(::createReader) { DomWriter() } } + + @Test + open fun testWhiteSpaceWithEntity() { + val data = " & " + 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) + } + + } }