From dafa1222e9cfc5b08cf6f6e8f61e0e312a638242 Mon Sep 17 00:00:00 2001 From: Shagen Ogandzhanian Date: Wed, 23 Jun 2021 17:22:23 +0200 Subject: [PATCH] AttrBuilder is nullable by default --- .../androidx/compose/web/elements/Base.kt | 8 +- .../androidx/compose/web/elements/Elements.kt | 164 ++++++++++-------- 2 files changed, 96 insertions(+), 76 deletions(-) diff --git a/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Base.kt b/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Base.kt index f292ba580a..fac3bc1d94 100644 --- a/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Base.kt +++ b/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Base.kt @@ -194,7 +194,7 @@ interface ElementBuilder { @Composable fun TagElement( elementBuilder: ElementBuilder, - applyAttrs: AttrsBuilder.() -> Unit, + applyAttrs: (AttrsBuilder.() -> Unit)?, content: (@Composable ElementScope.() -> Unit)? ) { val scope = remember { ElementScopeImpl() } @@ -207,7 +207,11 @@ fun TagElement( } }, attrsSkippableUpdate = { - val attrsApplied = AttrsBuilder().also { it.applyAttrs() } + val attrsApplied = AttrsBuilder().also { + if (applyAttrs != null) { + it.applyAttrs() + } + } refEffect.effect = attrsApplied.refEffect val attrsCollected = attrsApplied.collect() val events = attrsApplied.collectListeners() diff --git a/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Elements.kt b/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Elements.kt index c3f5635842..96f40112f0 100644 --- a/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Elements.kt +++ b/web/core/src/jsMain/kotlin/androidx/compose/web/elements/Elements.kt @@ -66,7 +66,7 @@ typealias ContentBuilder = @Composable ElementScope.() -> Unit @Composable fun Address( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -78,7 +78,7 @@ fun Address( @Composable fun Article( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -90,7 +90,7 @@ fun Article( @Composable fun Aside( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -102,7 +102,7 @@ fun Aside( @Composable fun Header( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -114,7 +114,7 @@ fun Header( @Composable fun Area( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -126,7 +126,7 @@ fun Area( @Composable fun Audio( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -138,7 +138,7 @@ fun Audio( @Composable fun HTMLMap( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -150,7 +150,7 @@ fun HTMLMap( @Composable fun Track( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -162,7 +162,7 @@ fun Track( @Composable fun Video( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -174,7 +174,7 @@ fun Video( @Composable fun Datalist( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -186,7 +186,7 @@ fun Datalist( @Composable fun Fieldset( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -198,7 +198,7 @@ fun Fieldset( @Composable fun Legend( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -210,7 +210,7 @@ fun Legend( @Composable fun Meter( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -222,7 +222,7 @@ fun Meter( @Composable fun Output( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -234,7 +234,7 @@ fun Output( @Composable fun Progress( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -246,7 +246,7 @@ fun Progress( @Composable fun Embed( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -258,7 +258,7 @@ fun Embed( @Composable fun Iframe( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -270,7 +270,7 @@ fun Iframe( @Composable fun Object( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -282,7 +282,7 @@ fun Object( @Composable fun Param( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -294,7 +294,7 @@ fun Param( @Composable fun Picture( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -306,7 +306,7 @@ fun Picture( @Composable fun Source( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -328,7 +328,7 @@ fun Text(value: String) { @Composable fun Div( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -341,7 +341,7 @@ fun Div( @Composable fun A( href: String? = null, - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -350,7 +350,9 @@ fun A( if (href != null) { this.href(href) } - attrs() + if (attrs != null) { + attrs() + } }, content = content ) @@ -360,14 +362,16 @@ fun A( fun Input( type: InputType = InputType.Text, value: String = "", - attrs: AttrBuilderContext = {} + attrs: AttrBuilderContext? = null ) { TagElement( elementBuilder = ElementBuilder.Input, applyAttrs = { type(type) value(value) - attrs() + if (attrs != null) { + attrs() + } }, content = null ) @@ -375,107 +379,107 @@ fun Input( @Composable fun Button( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Button, applyAttrs = attrs, content = content) @Composable fun H1( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H1, applyAttrs = attrs, content = content) @Composable fun H2( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H2, applyAttrs = attrs, content = content) @Composable fun H3( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H3, applyAttrs = attrs, content = content) @Composable fun H4( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H4, applyAttrs = attrs, content = content) @Composable fun H5( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H5, applyAttrs = attrs, content = content) @Composable fun H6( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.H6, applyAttrs = attrs, content = content) @Composable fun P( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.P, applyAttrs = attrs, content = content) @Composable fun Em( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Em, applyAttrs = attrs, content = content) @Composable fun I( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.I, applyAttrs = attrs, content = content) @Composable fun B( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.B, applyAttrs = attrs, content = content) @Composable fun Small( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Small, applyAttrs = attrs, content = content) @Composable fun Span( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Span, applyAttrs = attrs, content = content) @Composable -fun Br(attrs: AttrBuilderContext = {}) = +fun Br(attrs: AttrBuilderContext? = null) = TagElement(elementBuilder = ElementBuilder.Br, applyAttrs = attrs, content = null) @Composable fun Ul( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Ul, applyAttrs = attrs, content = content) @Composable fun Ol( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Ol, applyAttrs = attrs, content = content) @Composable fun DOMScope.Li( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Li, applyAttrs = attrs, content = content) @Composable fun DOMScope.Li( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement(elementBuilder = ElementBuilder.Li, applyAttrs = attrs, content = content) @@ -483,12 +487,14 @@ fun DOMScope.Li( fun Img( src: String, alt: String = "", - attrs: AttrBuilderContext = {} + attrs: AttrBuilderContext? = null ) = TagElement( elementBuilder = ElementBuilder.Img, applyAttrs = { src(src).alt(alt) - attrs() + if (attrs != null) { + attrs() + } }, content = null ) @@ -496,20 +502,22 @@ fun Img( @Composable fun Form( action: String? = null, - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.Form, applyAttrs = { if (!action.isNullOrEmpty()) action(action) - attrs() + if (attrs != null) { + attrs() + } }, content = content ) @Composable fun Select( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.Select, @@ -520,13 +528,15 @@ fun Select( @Composable fun Option( value: String, - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.Option, applyAttrs = { value(value) - attrs() + if (attrs != null) { + attrs() + } }, content = content ) @@ -534,20 +544,22 @@ fun Option( @Composable fun OptGroup( label: String, - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.OptGroup, applyAttrs = { label(label) - attrs() + if (attrs != null) { + attrs() + } }, content = content ) @Composable fun Section( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.Section, @@ -557,13 +569,15 @@ fun Section( @Composable fun TextArea( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, value: String ) = TagElement( elementBuilder = ElementBuilder.TextArea, applyAttrs = { value(value) - attrs() + if (attrs != null) { + attrs() + } } ) { Text(value) @@ -571,7 +585,7 @@ fun TextArea( @Composable fun Nav( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) = TagElement( elementBuilder = ElementBuilder.Nav, @@ -581,7 +595,7 @@ fun Nav( @Composable fun Pre( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -593,7 +607,7 @@ fun Pre( @Composable fun Code( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -605,7 +619,7 @@ fun Code( @Composable fun Main( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -617,7 +631,7 @@ fun Main( @Composable fun Footer( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -629,7 +643,7 @@ fun Footer( @Composable fun Hr( - attrs: AttrBuilderContext = {} + attrs: AttrBuilderContext? = null ) { TagElement( elementBuilder = ElementBuilder.Hr, @@ -641,7 +655,7 @@ fun Hr( @Composable fun Label( forId: String? = null, - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -650,7 +664,9 @@ fun Label( if (forId != null) { forId(forId) } - attrs() + if (attrs != null) { + attrs() + } }, content = content ) @@ -658,7 +674,7 @@ fun Label( @Composable fun Table( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -670,7 +686,7 @@ fun Table( @Composable fun Caption( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -682,7 +698,7 @@ fun Caption( @Composable fun Col( - attrs: AttrBuilderContext = {} + attrs: AttrBuilderContext? = null ) { TagElement( elementBuilder = ElementBuilder.Col, @@ -693,7 +709,7 @@ fun Col( @Composable fun Colgroup( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -705,7 +721,7 @@ fun Colgroup( @Composable fun Tr( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -717,7 +733,7 @@ fun Tr( @Composable fun Thead( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -729,7 +745,7 @@ fun Thead( @Composable fun Th( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -741,7 +757,7 @@ fun Th( @Composable fun Td( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -753,7 +769,7 @@ fun Td( @Composable fun Tbody( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement( @@ -765,7 +781,7 @@ fun Tbody( @Composable fun Tfoot( - attrs: AttrBuilderContext = {}, + attrs: AttrBuilderContext? = null, content: ContentBuilder? = null ) { TagElement(