All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dom.Builder.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package kotlin.dom.build

import org.w3c.dom.*
import kotlin.dom.*

/**
 * Creates a new element which can be configured via a function
 */
fun Document.createElement(name: String, init: Element.() -> Unit): Element {
    val elem = this.createElement(name)
    elem.init()
    return elem
}

/**
 * Creates a new element to an element which has an owner Document which can be configured via a function
 */
fun Element.createElement(name: String, doc: Document? = null, init: Element.() -> Unit): Element {
    val elem = ownerDocument(doc).createElement(name)
    elem.init()
    return elem
}

/**
 * Adds a newly created element which can be configured via a function
 */
fun Document.addElement(name: String, init: Element.() -> Unit): Element {
    val child = createElement(name, init)
    this.appendChild(child)
    return child
}

/**
 * Adds a newly created element to an element which has an owner Document which can be configured via a function
 */
fun Element.addElement(name: String, doc: Document? = null, init: Element.() -> Unit): Element {
    val child = createElement(name, doc, init)
    this.appendChild(child)
    return child
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy