commonMain.io.nacular.doodle.dom.impl.HtmlFactoryImpl.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of browser Show documentation
Show all versions of browser Show documentation
A pure Kotlin, UI framework for the Web and Desktop
package io.nacular.doodle.dom.impl
import io.nacular.doodle.dom.Document
import io.nacular.doodle.dom.HTMLButtonElement
import io.nacular.doodle.dom.HTMLElement
import io.nacular.doodle.dom.HTMLImageElement
import io.nacular.doodle.dom.HTMLInputElement
import io.nacular.doodle.dom.HtmlFactory
internal class HtmlFactoryImpl(override val root: HTMLElement, private val document: Document): HtmlFactory {
override fun create() = create("DIV") as T
@Suppress("UNCHECKED_CAST")
override fun create(tag: String) = document.createElement(tag) as T
override fun createText(text: String) = document.createTextNode(text)
override fun createImage(source: String) = create("IMG").apply { src = source; draggable = false }
override fun createOrUse(tag: String, possible: HTMLElement?): HTMLElement = when {
possible == null || possible.parentNode != null && !possible.nodeName.equals(tag, ignoreCase = true) -> create(tag)
else -> possible
}
override fun createInput (): HTMLInputElement = create("INPUT" )
override fun createButton(): HTMLButtonElement = create("BUTTON")
}