diff --git a/jvm/src/test/kotlin/streaming.kt b/jvm/src/test/kotlin/streaming.kt index be7c495d..96ab79ca 100644 --- a/jvm/src/test/kotlin/streaming.kt +++ b/jvm/src/test/kotlin/streaming.kt @@ -284,7 +284,16 @@ class TestStreaming { } @test fun `we should print empty tags with no close tag`() { - assertEquals("", StringBuilder().appendHTML(false).img(src = "my.jpg").toString()) + assertEquals("", StringBuilder().appendHTML( + prettyPrint = false + ).img(src = "my.jpg").toString()) + } + + @test fun `we should print empty tags with close tag if xhtmlCompatible flag is set to true`() { + assertEquals("", StringBuilder().appendHTML( + prettyPrint = false, + xhtmlCompatible = true + ).img(src = "my.jpg").toString()) } @test fun `pretty print should take into account inline tags`() { diff --git a/shared/src/main/kotlin/stream.kt b/shared/src/main/kotlin/stream.kt index 7cde231e..199ae6cd 100644 --- a/shared/src/main/kotlin/stream.kt +++ b/shared/src/main/kotlin/stream.kt @@ -4,7 +4,7 @@ import kotlinx.html.* import kotlinx.html.consumers.* import org.w3c.dom.events.Event -class HTMLStreamBuilder(val out : O, val prettyPrint : Boolean) : TagConsumer { +class HTMLStreamBuilder(val out : O, val prettyPrint : Boolean, val xhtmlCompatible: Boolean) : TagConsumer { private var level = 0 private var ln = true @@ -37,6 +37,10 @@ class HTMLStreamBuilder(val out : O, val prettyPrint : Boole } } + if (xhtmlCompatible && tag.emptyTag) { + out.append("/") + } + out.append(">") ln = false } @@ -118,8 +122,8 @@ class HTMLStreamBuilder(val out : O, val prettyPrint : Boole } private val AVERAGE_PAGE_SIZE = 32768 -fun createHTML(prettyPrint: Boolean = true): TagConsumer = HTMLStreamBuilder(StringBuilder(AVERAGE_PAGE_SIZE), prettyPrint).onFinalizeMap { sb, _ -> sb.toString() }.delayed() -fun O.appendHTML(prettyPrint : Boolean = true) : TagConsumer = HTMLStreamBuilder(this, prettyPrint).delayed() +fun createHTML(prettyPrint: Boolean = true, xhtmlCompatible : Boolean = false): TagConsumer = HTMLStreamBuilder(StringBuilder(AVERAGE_PAGE_SIZE), prettyPrint, xhtmlCompatible).onFinalizeMap { sb, _ -> sb.toString() }.delayed() +fun O.appendHTML(prettyPrint : Boolean = true, xhtmlCompatible : Boolean = false) : TagConsumer = HTMLStreamBuilder(this, prettyPrint, xhtmlCompatible).delayed() private val escapeMap = mapOf( '<' to "<",