Add svg support through unsafe

This commit is contained in:
2018-11-07 19:42:53 +01:00
parent 3f3988a7fd
commit 4d29114a15
3 changed files with 15 additions and 5 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
# Created by .ignore support plugin (hsz.mobi) # Created by .ignore support plugin (hsz.mobi)
web/js/generated web/js/generated
gradle.properties gradle.properties
local.properties

View File

@@ -1,5 +1,5 @@
group 'nl.astraeus' group 'nl.astraeus'
version '0.1.0-SNAPSHOT' version '0.1.1-SNAPSHOT'
apply plugin: 'kotlin2js' apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js' apply plugin: 'kotlin-dce-js'

View File

@@ -130,7 +130,7 @@ class KompElement(
return type == ElementType.KOMPONENT return type == ElementType.KOMPONENT
} }
fun create(): Node = when(type) { fun create(svg: Boolean = false): Node = when(type) {
ElementType.KOMPONENT -> { ElementType.KOMPONENT -> {
val komp = komponent val komp = komponent
@@ -154,7 +154,11 @@ class KompElement(
} }
ElementType.TEXT -> document.createTextNode(text) ElementType.TEXT -> document.createTextNode(text)
ElementType.UNSAFE -> { ElementType.UNSAFE -> {
val div = document.createElement("div") val div = if (svg) {
document.createElementNS("http://www.w3.org/2000/svg","svg")
} else {
document.createElement("div")
}
var result: Node? = null var result: Node? = null
div.innerHTML = text div.innerHTML = text
@@ -177,7 +181,12 @@ class KompElement(
result ?: throw IllegalStateException("No element found in unsafe content! [$text]") result ?: throw IllegalStateException("No element found in unsafe content! [$text]")
} }
ElementType.TAG -> { ElementType.TAG -> {
val result = document.createElement(text) var svg = text == "svg"
val result = if (svg) {
document.createElementNS("http://www.w3.org/2000/svg", text)
} else {
document.createElement(text)
}
(attributes?.entries)?.forEach { entry -> (attributes?.entries)?.forEach { entry ->
result.setAttribute(entry.key, entry.value) result.setAttribute(entry.key, entry.value)
@@ -193,7 +202,7 @@ class KompElement(
} }
children?.forEach { child -> children?.forEach { child ->
result.append(child.create()) result.append(child.create(svg))
} }
result result