Added unsafe option
This commit is contained in:
@@ -11,7 +11,9 @@ import kotlinx.html.js.onClickFunction
|
||||
import kotlinx.html.span
|
||||
import org.w3c.dom.Node
|
||||
import org.w3c.dom.events.Event
|
||||
import org.w3c.dom.get
|
||||
import kotlin.browser.document
|
||||
import kotlin.dom.isElement
|
||||
|
||||
/**
|
||||
* User: rnentjes
|
||||
@@ -19,40 +21,51 @@ import kotlin.browser.document
|
||||
* Time: 21:58
|
||||
*/
|
||||
|
||||
enum class ElementType {
|
||||
KOMPONENT,
|
||||
TAG,
|
||||
TEXT,
|
||||
UNSAFE,
|
||||
}
|
||||
|
||||
class KompElement(
|
||||
val type: ElementType,
|
||||
val komponent: Komponent?,
|
||||
var text: String,
|
||||
var unsafe: Boolean = false,
|
||||
val attributes: MutableMap<String, String>? = null,
|
||||
val children: MutableList<KompElement>? = null,
|
||||
val events: MutableMap<String, (Event) -> Unit>? = null
|
||||
) {
|
||||
|
||||
constructor(text: String, isText: Boolean = true) : this(
|
||||
null,
|
||||
constructor(text: String, type: ElementType) : this(
|
||||
type,
|
||||
if (type == ElementType.KOMPONENT) {
|
||||
throw IllegalStateException("Type KOMPONENT not allowed in String constructor")
|
||||
} else {
|
||||
null
|
||||
},
|
||||
text,
|
||||
false,
|
||||
if (isText) {
|
||||
null
|
||||
} else {
|
||||
if (type == ElementType.TAG) {
|
||||
HashMap()
|
||||
},
|
||||
if (isText) {
|
||||
null
|
||||
} else {
|
||||
null
|
||||
},
|
||||
if (type == ElementType.TAG) {
|
||||
ArrayList()
|
||||
},
|
||||
if (isText) {
|
||||
null
|
||||
} else {
|
||||
null
|
||||
},
|
||||
if (type == ElementType.TAG) {
|
||||
HashMap()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
)
|
||||
|
||||
constructor(komponent: Komponent) : this(
|
||||
ElementType.KOMPONENT,
|
||||
komponent,
|
||||
"",
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
@@ -61,22 +74,47 @@ class KompElement(
|
||||
/* shallow equals check */
|
||||
fun equals(other: KompElement): Boolean {
|
||||
if (komponent != null) {
|
||||
return komponent == other.komponent
|
||||
val result = komponent == other.komponent
|
||||
if (!result && Komponent.logEquals) {
|
||||
console.log("!= komponent", this, other)
|
||||
}
|
||||
return result
|
||||
} else if (other.isText() || isText()) {
|
||||
if (other.text != text && Komponent.logEquals) {
|
||||
console.log("!= text", this, other)
|
||||
}
|
||||
return other.text == text
|
||||
} else {
|
||||
if (other.attributes?.size != attributes?.size || other.events?.size != events?.size) {
|
||||
if (Komponent.logEquals) {
|
||||
console.log("!= attr size or event size", this, other)
|
||||
}
|
||||
return false
|
||||
} else {
|
||||
(attributes?.entries)?.forEach { entry ->
|
||||
if (other.attributes?.get(entry.key) != entry.value) {
|
||||
if (!other.attributes?.get(entry.key).equals(entry.value)) {
|
||||
if (Komponent.logEquals) {
|
||||
console.log("!= attr", this, other)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
(events?.entries)?.forEach { entry ->
|
||||
if (other.events?.get(entry.key) != entry.value) {
|
||||
return false
|
||||
val thisFunction: dynamic = other.events?.get(entry.key)
|
||||
val otherFunction: dynamic = entry.value
|
||||
|
||||
if (thisFunction != null && thisFunction.callableName != undefined) {
|
||||
val result = thisFunction.callableName == otherFunction.callableName
|
||||
if (!result && Komponent.logEquals) {
|
||||
console.log("!= event", thisFunction, otherFunction)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
if (Komponent.logEquals) {
|
||||
console.log("!= event, events have no callableName", thisFunction, otherFunction)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,28 +122,61 @@ class KompElement(
|
||||
return true
|
||||
}
|
||||
|
||||
fun isText() = attributes == null && komponent == null
|
||||
fun isText(): Boolean {
|
||||
return type == ElementType.TEXT
|
||||
}
|
||||
|
||||
fun isKomponent() = komponent != null
|
||||
fun isKomponent(): Boolean {
|
||||
return type == ElementType.KOMPONENT
|
||||
}
|
||||
|
||||
fun create(): Node = when {
|
||||
komponent != null -> {
|
||||
komponent.element?.also {
|
||||
Komponent.remove(it)
|
||||
fun create(): Node = when(type) {
|
||||
ElementType.KOMPONENT -> {
|
||||
val komp = komponent
|
||||
|
||||
if (komp == null) {
|
||||
throw IllegalStateException("komponent == null in type Komponent!")
|
||||
} else {
|
||||
komp.element?.also {
|
||||
Komponent.remove(it)
|
||||
}
|
||||
|
||||
val kompElement = komp.create()
|
||||
val element = kompElement.create()
|
||||
|
||||
komp.kompElement = kompElement
|
||||
komp.element = element
|
||||
|
||||
Komponent.define(element, komp)
|
||||
|
||||
element
|
||||
}
|
||||
}
|
||||
ElementType.TEXT -> document.createTextNode(text)
|
||||
ElementType.UNSAFE -> {
|
||||
val div = document.createElement("div")
|
||||
var result: Node? = null
|
||||
|
||||
div.innerHTML = text
|
||||
|
||||
console.log("div element with unsafe innerHTML", div)
|
||||
|
||||
for (index in 0 until div.childNodes.length) {
|
||||
val node = div.childNodes[index]!!
|
||||
|
||||
console.log("$index -> ", node)
|
||||
|
||||
if (node.isElement) {
|
||||
if (result != null) {
|
||||
throw IllegalStateException("Only one element allowed in unsafe block!")
|
||||
}
|
||||
result = node
|
||||
}
|
||||
}
|
||||
|
||||
val kompElement = komponent.kompElement ?: komponent.create()
|
||||
val element = kompElement.create()
|
||||
|
||||
komponent.kompElement = kompElement
|
||||
komponent.element = element
|
||||
|
||||
Komponent.define(element, komponent)
|
||||
|
||||
element
|
||||
result ?: throw IllegalStateException("No element found in unsafe content! [$text]")
|
||||
}
|
||||
isText() -> document.createTextNode(text)
|
||||
else -> {
|
||||
ElementType.TAG -> {
|
||||
val result = document.createElement(text)
|
||||
|
||||
(attributes?.entries)?.forEach { entry ->
|
||||
@@ -184,6 +255,14 @@ class KompElement(
|
||||
}
|
||||
}
|
||||
|
||||
class UnsafeWrapper: Unsafe {
|
||||
var text = ""
|
||||
|
||||
override fun String.unaryPlus() {
|
||||
text += this@unaryPlus
|
||||
}
|
||||
}
|
||||
|
||||
class KompConsumer : TagConsumer<KompElement> {
|
||||
val stack = ArrayList<KompElement>()
|
||||
var currentTag: KompElement? = null
|
||||
@@ -202,7 +281,7 @@ class KompConsumer : TagConsumer<KompElement> {
|
||||
override fun onTagContent(content: CharSequence) {
|
||||
//console.log("KC.onTagContent", content)
|
||||
|
||||
currentTag?.children?.add(KompElement(content.toString(), true))
|
||||
currentTag?.children?.add(KompElement(content.toString(), ElementType.TEXT))
|
||||
}
|
||||
|
||||
override fun onTagContentEntity(entity: Entities) {
|
||||
@@ -211,12 +290,16 @@ class KompConsumer : TagConsumer<KompElement> {
|
||||
|
||||
override fun onTagContentUnsafe(block: Unsafe.() -> Unit) {
|
||||
//console.log("KC.onTagContentUnsafe", block)
|
||||
val txt = UnsafeWrapper()
|
||||
|
||||
throw IllegalStateException("unsafe blocks are not supported atm.")
|
||||
block.invoke(txt)
|
||||
|
||||
console.log("KC.onTagContentUnsafe", txt)
|
||||
currentTag?.children?.add(KompElement(txt.text, ElementType.UNSAFE))
|
||||
}
|
||||
|
||||
override fun onTagEnd(tag: Tag) {
|
||||
//console.log("KC.onTagEnd", tag)
|
||||
console.log("KC.onTagEnd", tag)
|
||||
|
||||
check(currentTag != null)
|
||||
check(currentTag?.children != null)
|
||||
@@ -238,13 +321,13 @@ class KompConsumer : TagConsumer<KompElement> {
|
||||
}
|
||||
|
||||
override fun onTagStart(tag: Tag) {
|
||||
//console.log("KC.onTagStart", tag)
|
||||
console.log("KC.onTagStart", tag)
|
||||
|
||||
currentTag?.apply {
|
||||
stack.add(this)
|
||||
}
|
||||
|
||||
currentTag = KompElement(tag.tagName, false)
|
||||
currentTag = KompElement(tag.tagName, ElementType.TAG)
|
||||
|
||||
for (attr in tag.attributes.entries) {
|
||||
currentTag?.attributes?.set(attr.key, attr.value)
|
||||
|
||||
Reference in New Issue
Block a user