Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion jvm/src/test/kotlin/streaming.kt
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,16 @@ class TestStreaming {
}

@test fun `we should print empty tags with no close tag`() {
assertEquals("<img src=\"my.jpg\">", StringBuilder().appendHTML(false).img(src = "my.jpg").toString())
assertEquals("<img src=\"my.jpg\">", 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("<img src=\"my.jpg\"/>", StringBuilder().appendHTML(
prettyPrint = false,
xhtmlCompatible = true
).img(src = "my.jpg").toString())
}

@test fun `pretty print should take into account inline tags`() {
Expand Down
10 changes: 7 additions & 3 deletions shared/src/main/kotlin/stream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlinx.html.*
import kotlinx.html.consumers.*
import org.w3c.dom.events.Event

class HTMLStreamBuilder<out O : Appendable>(val out : O, val prettyPrint : Boolean) : TagConsumer<O> {
class HTMLStreamBuilder<out O : Appendable>(val out : O, val prettyPrint : Boolean, val xhtmlCompatible: Boolean) : TagConsumer<O> {
private var level = 0
private var ln = true

Expand Down Expand Up @@ -37,6 +37,10 @@ class HTMLStreamBuilder<out O : Appendable>(val out : O, val prettyPrint : Boole
}
}

if (xhtmlCompatible && tag.emptyTag) {
out.append("/")
}

out.append(">")
ln = false
}
Expand Down Expand Up @@ -118,8 +122,8 @@ class HTMLStreamBuilder<out O : Appendable>(val out : O, val prettyPrint : Boole
}

private val AVERAGE_PAGE_SIZE = 32768
fun createHTML(prettyPrint: Boolean = true): TagConsumer<String> = HTMLStreamBuilder(StringBuilder(AVERAGE_PAGE_SIZE), prettyPrint).onFinalizeMap { sb, _ -> sb.toString() }.delayed()
fun <O : Appendable> O.appendHTML(prettyPrint : Boolean = true) : TagConsumer<O> = HTMLStreamBuilder(this, prettyPrint).delayed()
fun createHTML(prettyPrint: Boolean = true, xhtmlCompatible : Boolean = false): TagConsumer<String> = HTMLStreamBuilder(StringBuilder(AVERAGE_PAGE_SIZE), prettyPrint, xhtmlCompatible).onFinalizeMap { sb, _ -> sb.toString() }.delayed()
fun <O : Appendable> O.appendHTML(prettyPrint : Boolean = true, xhtmlCompatible : Boolean = false) : TagConsumer<O> = HTMLStreamBuilder(this, prettyPrint, xhtmlCompatible).delayed()

private val escapeMap = mapOf(
'<' to "&lt;",
Expand Down