Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
improve DSL for custom elements
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Oct 30, 2022
1 parent b993938 commit 361d715
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions api/kweb-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/main/kotlin/kweb/ElementCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ open class ElementCreator<out PARENT_TYPE : Element>(
* @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<String, JsonPrimitive> = attr, namespace: String? = null): Element {
fun element(tag: String, attributes: Map<String, JsonPrimitive> = attr, namespace: String? = null, new: (ElementCreator<*>.() -> Unit)? = null): Element {

val mutAttributes = HashMap(attributes)

Expand Down Expand Up @@ -158,6 +158,11 @@ open class ElementCreator<out PARENT_TYPE : Element>(
logger.debug { "Deleting element ${newElement.id}" }
newElement.deleteIfExists()
}

if (new != null) {
newElement.new { new() }
}

return newElement
}

Expand Down Expand Up @@ -206,10 +211,14 @@ open class ElementCreator<out PARENT_TYPE : Element>(
}
}

// 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<String>) {
this.element.text(text)
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/kweb/docs/dom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 361d715

Please sign in to comment.