Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow to assign null value in attr #780

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fun CodeSampleSwitcher(count: Int, current: Int, onSelect: (Int) -> Unit) {
Input(type = InputType.Radio, value = "snippet$ix", attrs = {
name("code-snippet")
id("snippet$ix")
if (current == ix) checked(true)
if (current == ix) checked()
onRadioInput { onSelect(ix) }
})
Label(forId = "snippet$ix") { Text("${ix + 1}") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ fun CodeSampleSwitcher(count: Int, current: Int, onSelect: (Int) -> Unit) {
Input(type = InputType.Radio, value = "snippet$ix", attrs = {
name("code-snippet")
id("snippet$ix")
if (current == ix) checked(true)
if (current == ix) checked()
onRadioInput { onSelect(ix) }
})
Label(forId = "snippet$ix") { Text("${ix + 1}") }
}
}
}
}
14 changes: 6 additions & 8 deletions web/core/src/jsMain/kotlin/androidx/compose/web/DomApplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ open class DomNodeWrapper(open val node: Node) {


class DomElementWrapper(override val node: HTMLElement): DomNodeWrapper(node) {
private var currentAttrs = emptyMap<String, String?>()

fun updateAttrs(attrs: Map<String, String?>) {
currentAttrs.forEach {
node.removeAttribute(it.key)
fun updateAttrs(attrs: Map<String, String>) {
while (node.attributes.length > 0) {
node.removeAttributeNode(node.attributes[0]!!)
}
currentAttrs = attrs
currentAttrs.forEach {
if (it.value != null) node.setAttribute(it.key, it.value ?: "")

attrs.forEach {
node.setAttribute(it.key, it.value)
}
}

Expand Down
106 changes: 53 additions & 53 deletions web/core/src/jsMain/kotlin/androidx/compose/web/attributes/Attrs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.w3c.dom.HTMLTableCellElement
import org.w3c.dom.HTMLTableColElement
import org.w3c.dom.HTMLTextAreaElement

fun AttrsBuilder<HTMLAnchorElement>.href(value: String?) =
fun AttrsBuilder<HTMLAnchorElement>.href(value: String) =
attr("href", value)

fun AttrsBuilder<HTMLAnchorElement>.target(value: ATarget = ATarget.Self) =
Expand All @@ -36,11 +36,11 @@ fun AttrsBuilder<HTMLAnchorElement>.download(value: String = "") =

/* Button attributes */

fun AttrsBuilder<HTMLButtonElement>.autoFocus(value: Boolean = true) =
attr("autofocus", if (value) "" else null)
fun AttrsBuilder<HTMLButtonElement>.autoFocus() =
attr("autofocus", "")

fun AttrsBuilder<HTMLButtonElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLButtonElement>.disabled() =
attr("disabled", "")

fun AttrsBuilder<HTMLButtonElement>.form(formId: String) =
attr("form", formId)
Expand All @@ -54,8 +54,8 @@ fun AttrsBuilder<HTMLButtonElement>.formEncType(value: ButtonFormEncType) =
fun AttrsBuilder<HTMLButtonElement>.formMethod(value: ButtonFormMethod) =
attr("formmethod", value.methodStr)

fun AttrsBuilder<HTMLButtonElement>.formNoValidate(value: Boolean = true) =
attr("formnovalidate", if (value) "" else null)
fun AttrsBuilder<HTMLButtonElement>.formNoValidate() =
attr("formnovalidate", "")

fun AttrsBuilder<HTMLButtonElement>.formTarget(value: ButtonFormTarget) =
attr("formtarget", value.targetStr)
Expand All @@ -77,17 +77,17 @@ fun AttrsBuilder<HTMLFormElement>.action(value: String) =
fun AttrsBuilder<HTMLFormElement>.acceptCharset(value: String) =
attr("accept-charset", value)

fun AttrsBuilder<HTMLFormElement>.autoComplete(value: Boolean) =
attr("autocomplete", if (value) "" else null)
fun AttrsBuilder<HTMLFormElement>.autoComplete() =
attr("autocomplete", "")

fun AttrsBuilder<HTMLFormElement>.encType(value: FormEncType) =
attr("enctype", value.typeStr)

fun AttrsBuilder<HTMLFormElement>.method(value: FormMethod) =
attr("method", value.methodStr)

fun AttrsBuilder<HTMLFormElement>.noValidate(value: Boolean = true) =
attr("novalidate", if (value) "" else null)
fun AttrsBuilder<HTMLFormElement>.noValidate() =
attr("novalidate", "")

fun AttrsBuilder<HTMLFormElement>.target(value: FormTarget) =
attr("target", value.targetStr)
Expand All @@ -103,23 +103,23 @@ fun AttrsBuilder<HTMLInputElement>.accept(value: String) =
fun AttrsBuilder<HTMLInputElement>.alt(value: String) =
attr("alt", value) // type: image only

fun AttrsBuilder<HTMLInputElement>.autoComplete(value: Boolean = true) =
attr("autocomplete", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.autoComplete() =
attr("autocomplete", "")

fun AttrsBuilder<HTMLInputElement>.autoFocus(value: Boolean = true) =
attr("autofocus", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.autoFocus() =
attr("autofocus", "")

fun AttrsBuilder<HTMLInputElement>.capture(value: String) =
attr("capture", value) // type: file only

fun AttrsBuilder<HTMLInputElement>.checked(value: Boolean = true) =
attr("checked", if (value) "" else null) // radio, checkbox
fun AttrsBuilder<HTMLInputElement>.checked() =
attr("checked", "") // radio, checkbox

fun AttrsBuilder<HTMLInputElement>.dirName(value: String) =
attr("dirname", value) // text, search

fun AttrsBuilder<HTMLInputElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.disabled() =
attr("disabled", "")

fun AttrsBuilder<HTMLInputElement>.form(id: String) =
attr("form", id)
Expand All @@ -133,8 +133,8 @@ fun AttrsBuilder<HTMLInputElement>.formEncType(value: InputFormEncType) =
fun AttrsBuilder<HTMLInputElement>.formMethod(value: InputFormMethod) =
attr("formmethod", value.methodStr)

fun AttrsBuilder<HTMLInputElement>.formNoValidate(value: Boolean = true) =
attr("formnovalidate", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.formNoValidate() =
attr("formnovalidate", "")

fun AttrsBuilder<HTMLInputElement>.formTarget(value: InputFormTarget) =
attr("formtarget", value.targetStr)
Expand All @@ -160,8 +160,8 @@ fun AttrsBuilder<HTMLInputElement>.min(value: String) =
fun AttrsBuilder<HTMLInputElement>.minLength(value: Int) =
attr("minlength", value.toString())

fun AttrsBuilder<HTMLInputElement>.multiple(value: Boolean = true) =
attr("multiple", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.multiple() =
attr("multiple", "")

fun AttrsBuilder<HTMLInputElement>.name(value: String) =
attr("name", value)
Expand All @@ -172,8 +172,8 @@ fun AttrsBuilder<HTMLInputElement>.pattern(value: String) =
fun AttrsBuilder<HTMLInputElement>.placeholder(value: String) =
attr("placeholder", value)

fun AttrsBuilder<HTMLInputElement>.readOnly(value: Boolean = true) =
attr("readonly", if (value) "" else null)
fun AttrsBuilder<HTMLInputElement>.readOnly() =
attr("readonly", "")

fun AttrsBuilder<HTMLInputElement>.required(value: Boolean = true) =
attr("required", value.toString())
Expand All @@ -182,7 +182,7 @@ fun AttrsBuilder<HTMLInputElement>.size(value: Int) =
attr("size", value.toString())

fun AttrsBuilder<HTMLInputElement>.src(value: String) =
attr("src", value.toString()) // image only
attr("src", value) // image only

fun AttrsBuilder<HTMLInputElement>.step(value: Int) =
attr("step", value.toString()) // numeric types only
Expand All @@ -200,11 +200,11 @@ fun AttrsBuilder<HTMLInputElement>.value(value: String): AttrsBuilder<HTMLInputE
fun AttrsBuilder<HTMLOptionElement>.value(value: String) =
attr("value", value)

fun AttrsBuilder<HTMLOptionElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLOptionElement>.disabled() =
attr("disabled", "")

fun AttrsBuilder<HTMLOptionElement>.selected(value: Boolean = true) =
attr("selected", if (value) "" else null)
fun AttrsBuilder<HTMLOptionElement>.selected() =
attr("selected", "")

fun AttrsBuilder<HTMLOptionElement>.label(value: String) =
attr("label", value)
Expand All @@ -214,23 +214,23 @@ fun AttrsBuilder<HTMLOptionElement>.label(value: String) =
fun AttrsBuilder<HTMLSelectElement>.autocomplete(value: String) =
attr("autocomplete", value)

fun AttrsBuilder<HTMLSelectElement>.autofocus(value: Boolean = true) =
attr("autofocus", if (value) "" else null)
fun AttrsBuilder<HTMLSelectElement>.autofocus() =
attr("autofocus", "")

fun AttrsBuilder<HTMLSelectElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLSelectElement>.disabled() =
attr("disabled", "")

fun AttrsBuilder<HTMLSelectElement>.form(formId: String) =
attr("form", formId)

fun AttrsBuilder<HTMLSelectElement>.multiple(value: Boolean = true) =
attr("multiple", if (value) "" else null)
fun AttrsBuilder<HTMLSelectElement>.multiple() =
attr("multiple", "")

fun AttrsBuilder<HTMLSelectElement>.name(value: String) =
attr("name", value)

fun AttrsBuilder<HTMLSelectElement>.required(value: Boolean = true) =
attr("required", if (value) "" else null)
fun AttrsBuilder<HTMLSelectElement>.required() =
attr("required", "")

fun AttrsBuilder<HTMLSelectElement>.size(numberOfRows: Int) =
attr("size", numberOfRows.toString())
Expand All @@ -240,22 +240,22 @@ fun AttrsBuilder<HTMLSelectElement>.size(numberOfRows: Int) =
fun AttrsBuilder<HTMLOptGroupElement>.label(value: String) =
attr("label", value)

fun AttrsBuilder<HTMLOptGroupElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLOptGroupElement>.disabled() =
attr("disabled", "")

/* TextArea attributes */

fun AttrsBuilder<HTMLTextAreaElement>.autoComplete(value: Boolean = true) =
attr("autocomplete", if (value) "on" else "off")

fun AttrsBuilder<HTMLTextAreaElement>.autoFocus(value: Boolean = true) =
attr("autofocus", if (value) "" else null)
fun AttrsBuilder<HTMLTextAreaElement>.autoFocus() =
attr("autofocus", "")

fun AttrsBuilder<HTMLTextAreaElement>.cols(value: Int) =
attr("cols", value.toString())

fun AttrsBuilder<HTMLTextAreaElement>.disabled(value: Boolean = true) =
attr("disabled", if (value) "" else null)
fun AttrsBuilder<HTMLTextAreaElement>.disabled() =
attr("disabled", "")

fun AttrsBuilder<HTMLTextAreaElement>.form(formId: String) =
attr("form", formId)
Expand All @@ -272,11 +272,11 @@ fun AttrsBuilder<HTMLTextAreaElement>.name(value: String) =
fun AttrsBuilder<HTMLTextAreaElement>.placeholder(value: String) =
attr("placeholder", value)

fun AttrsBuilder<HTMLTextAreaElement>.readOnly(value: Boolean = true) =
attr("readonly", if (value) "" else null)
fun AttrsBuilder<HTMLTextAreaElement>.readOnly() =
attr("readonly", "")

fun AttrsBuilder<HTMLTextAreaElement>.required(value: Boolean = true) =
attr("required", if (value) "" else null)
fun AttrsBuilder<HTMLTextAreaElement>.required() =
attr("required", "")

fun AttrsBuilder<HTMLTextAreaElement>.rows(value: Int) =
attr("rows", value.toString())
Expand All @@ -291,26 +291,26 @@ fun AttrsBuilder<HTMLTextAreaElement>.value(value: String): AttrsBuilder<HTMLTex

/* Img attributes */

fun AttrsBuilder<HTMLImageElement>.src(value: String?): AttrsBuilder<HTMLImageElement> =
fun AttrsBuilder<HTMLImageElement>.src(value: String): AttrsBuilder<HTMLImageElement> =
attr("src", value)

fun AttrsBuilder<HTMLImageElement>.alt(value: String?): AttrsBuilder<HTMLImageElement> =
fun AttrsBuilder<HTMLImageElement>.alt(value: String): AttrsBuilder<HTMLImageElement> =
attr("alt", value)

private val setInputValue: (HTMLInputElement, String) -> Unit = { e, v ->
e.value = v
}

/* Img attributes */
fun AttrsBuilder<HTMLLabelElement>.forId(value: String?): AttrsBuilder<HTMLLabelElement> =
fun AttrsBuilder<HTMLLabelElement>.forId(value: String): AttrsBuilder<HTMLLabelElement> =
attr("for", value)

/* Table attributes */
fun AttrsBuilder<HTMLTableColElement>.span(value: Int): AttrsBuilder<HTMLTableColElement> =
attr("span", value.toString())

fun AttrsBuilder<HTMLTableCellElement>.scope(value: Scope?): AttrsBuilder<HTMLTableCellElement> =
attr("scope", value?.str)
fun AttrsBuilder<HTMLTableCellElement>.scope(value: Scope): AttrsBuilder<HTMLTableCellElement> =
attr("scope", value.str)

fun AttrsBuilder<HTMLTableCellElement>.colspan(value: Int): AttrsBuilder<HTMLTableCellElement> =
attr("colspan", value.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ class AttrsBuilder<TElement : Element> : EventsListenerBuilder() {
this.refEffect = effect
}

fun attr(attr: String, value: String?): AttrsBuilder<TElement> {
if (value == null) {
attributesMap.remove(attr)
} else {
attributesMap[attr] = value
}

fun attr(attr: String, value: String): AttrsBuilder<TElement> {
attributesMap[attr] = value
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ fun A(
TagElement(
elementBuilder = ElementBuilder.A,
applyAttrs = {
href(href)
if (href != null) {
this.href(href)
}
attrs()
},
content = content
Expand Down Expand Up @@ -645,7 +647,9 @@ fun Label(
TagElement(
elementBuilder = ElementBuilder.Label,
applyAttrs = {
forId(forId)
if (forId != null) {
forId(forId)
}
attrs()
},
content = content
Expand Down
4 changes: 3 additions & 1 deletion web/core/src/jsTest/kotlin/elements/AttributesTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class AttributesTests {
composition {
Button(
{
disabled(disabled)
if (disabled) {
disabled()
}
}
) {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class InputsTests {
type = InputType.Checkbox,
attrs = {
id("checkbox")
checked(checked)
if (checked) {
checked()
}
onCheckboxInput {
checked = !checked
}
Expand Down