diff --git a/api/kweb-core.api b/api/kweb-core.api index 353e08a294..5813fbbf03 100644 --- a/api/kweb-core.api +++ b/api/kweb-core.api @@ -195,9 +195,9 @@ public class kweb/ElementCreator { public final fun attr (Lkotlin/jvm/functions/Function1;)V public final fun cleanup ()V public final fun closeOnCleanup (Ljava/lang/AutoCloseable;)V - public final fun element (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)Lkweb/Element; + public final fun element (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lkweb/Element; public final fun element (Lkotlin/jvm/functions/Function1;)V - public static synthetic fun element$default (Lkweb/ElementCreator;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;ILjava/lang/Object;)Lkweb/Element; + public static synthetic fun element$default (Lkweb/ElementCreator;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkweb/Element; public final fun getBrowser ()Lkweb/WebBrowser; public final fun getElement ()Lkweb/Element; public final fun getElementsCreatedCount ()I diff --git a/src/main/kotlin/kweb/ElementCreator.kt b/src/main/kotlin/kweb/ElementCreator.kt index a7a17929b3..ebc7797e68 100755 --- a/src/main/kotlin/kweb/ElementCreator.kt +++ b/src/main/kotlin/kweb/ElementCreator.kt @@ -57,7 +57,7 @@ open class ElementCreator( * @param namespace If non-null elements will be created with [Document.createElementNS()](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS) * with the specified namespace. If null then Kweb will use [Document.createElement](https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement). */ - fun element(tag: String, attributes: Map = attr, namespace: String? = null): Element { + fun element(tag: String, attributes: Map = attr, namespace: String? = null, new: (ElementCreator<*>.() -> Unit)? = null): Element { val mutAttributes = HashMap(attributes) @@ -158,6 +158,11 @@ open class ElementCreator( logger.debug { "Deleting element ${newElement.id}" } newElement.deleteIfExists() } + + if (new != null) { + newElement.new { new() } + } + return newElement } @@ -206,10 +211,14 @@ open class ElementCreator( } } + // text() Deprecated because these may create confusion about whether element properties + // are set on the Element or the ElementCreator + @Deprecated("Use element.text() instead", ReplaceWith("element.text(text)")) fun text(text: String) { this.element.text(text) } + @Deprecated("Use element.text() instead", ReplaceWith("element.text(text)")) fun text(text: KVal) { this.element.text(text) } diff --git a/src/test/kotlin/kweb/docs/dom.kt b/src/test/kotlin/kweb/docs/dom.kt index c2f4f9f74c..25c44d1526 100644 --- a/src/test/kotlin/kweb/docs/dom.kt +++ b/src/test/kotlin/kweb/docs/dom.kt @@ -89,9 +89,10 @@ inputKVar.addListener { old, new -> // ANCHOR_END: read_value // ANCHOR: blink -val blink = element("blink").text("I am annoying!") +element("blink") { + span().text("Blinking Text") +} // ANCHOR_END: blink - blink.id // Remove unused warning } } }