From 43545f88a52c9259c2a498a0914cbc2307d37c8f Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Mon, 6 Nov 2023 09:47:57 +0000 Subject: [PATCH] feat: support HTMX wildcard attribute names, fixes #271 --- .version | 2 +- generator/test-element-attributes/expected.html | 10 ++++++++++ generator/test-element-attributes/template.templ | 9 +++++++++ generator/test-element-attributes/template_templ.go | 11 ++++++++++- parser/v2/elementparser.go | 2 +- parser/v2/elementparser_test.go | 9 +++++++++ parser/v2/types.go | 2 +- 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.version b/.version index fa8dd6dbc..01ab163a9 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.458 \ No newline at end of file +0.2.459 \ No newline at end of file diff --git a/generator/test-element-attributes/expected.html b/generator/test-element-attributes/expected.html index 3520103a6..5a6f04a48 100644 --- a/generator/test-element-attributes/expected.html +++ b/generator/test-element-attributes/expected.html @@ -18,3 +18,13 @@ end"> +

HTMX Wildcard attribute

+ +
+ +
diff --git a/generator/test-element-attributes/template.templ b/generator/test-element-attributes/template.templ index 19c7719c8..05706e81d 100644 --- a/generator/test-element-attributes/template.templ +++ b/generator/test-element-attributes/template.templ @@ -34,4 +34,13 @@ templ render(p person) { do something end" > +

HTMX Wildcard attribute

+
+ +
} diff --git a/generator/test-element-attributes/template_templ.go b/generator/test-element-attributes/template_templ.go index 4cf9c3deb..de626d7b0 100644 --- a/generator/test-element-attributes/template_templ.go +++ b/generator/test-element-attributes/template_templ.go @@ -165,7 +165,16 @@ func render(p person) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var9 := `HTMX Wildcard attribute` + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var9) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/parser/v2/elementparser.go b/parser/v2/elementparser.go index 4e700b3ed..290c601e9 100644 --- a/parser/v2/elementparser.go +++ b/parser/v2/elementparser.go @@ -81,7 +81,7 @@ var elementCloseTagParser = parse.Func(func(in *parse.Input) (ct elementCloseTag // Attribute name. var ( attributeNameFirst = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:_@" - attributeNameSubsequent = attributeNameFirst + "-.0123456789" + attributeNameSubsequent = attributeNameFirst + "-.0123456789*" attributeNameParser = parse.Func(func(in *parse.Input) (name string, ok bool, err error) { start := in.Index() var prefix, suffix string diff --git a/parser/v2/elementparser_test.go b/parser/v2/elementparser_test.go index 8ef890aee..54126b920 100644 --- a/parser/v2/elementparser_test.go +++ b/parser/v2/elementparser_test.go @@ -309,6 +309,15 @@ if test { Value: `<">`, }, }, + { + name: "HTMX wildcard attribute names are supported", + input: ` hx-target-*="#errors"`, + parser: StripType(constantAttributeParser), + expected: ConstantAttribute{ + Name: "hx-target-*", + Value: `#errors`, + }, + }, } for _, tt := range tests { tt := tt diff --git a/parser/v2/types.go b/parser/v2/types.go index 38eedefde..bd0ecfd7a 100644 --- a/parser/v2/types.go +++ b/parser/v2/types.go @@ -694,7 +694,7 @@ type ExpressionAttribute struct { func (ea ExpressionAttribute) String() string { sb := new(strings.Builder) - ea.Write(sb, 0) + _ = ea.Write(sb, 0) return sb.String() }