Skip to content

Fix bare else attribute emitted by every Button - #65

Open
methodofaction wants to merge 1 commit into
mainfrom
claude/inspiring-chatterjee-f7ca84
Open

Fix bare else attribute emitted by every Button#65
methodofaction wants to merge 1 commit into
mainfrom
claude/inspiring-chatterjee-f7ca84

Conversation

@methodofaction

Copy link
Copy Markdown
Contributor

What

button.templ used an if / else if chain inside the <button> element's attribute list to select the type attribute. The pinned templ version (v0.3.1001, per go.mod) mis-generates this pattern.

Replaced it with two independent, mutually exclusive conditions:

if btn.Type != "" {
    type={ btn.Type }
}
if btn.Type == "" && btn.Copy != "" {
    type="button"
}

Why

The generated code wrote a literal " else" into the markup outside any conditional, and demoted the else-branch to an independent if:

}                                    // end of `if btn.Type != ""`
WriteString(..., " else")            // ← unconditional
if btn.Copy != "" { ... }            // ← no longer mutually exclusive

Two consequences, both broader than a bare Type-only issue:

  1. Every non-anchor Button emitted a bare else attribute — not just ones setting Type.
  2. A Button with both Type and Copy set would emit two type attributes.

Browsers ignore the unknown else attribute and take the first type, so nothing broke at runtime — but the markup fails validation. This is pre-existing at main, not introduced by recent work.

Reviewer notes

Other else if sites are safe. 16 exist across .templ files. The authoritative check was grepping the ~255 committed *_templ.go files for the emitted literal — only button_templ.go matched. The rest are element-content position (checkbox, radio, toast, notification, filter, description_list), which templ compiles correctly; the select.templ hits are JavaScript inside a script block.

Why the comment is on the func doc, not next to the code. templ rejects // comments in attribute position outright. In element-content position it silently strips the comment text but emits the surrounding whitespace — an earlier attempt produced a stray WriteString(..., " ") before <button>. The doc-comment placement has zero output impact.

Regeneration. Do not use a globally-installed templ CLI if it is newer than the go.mod pin — it rewrites all *_templ.go files with templ.ResolveAttributeValue, which the pinned runtime lacks. This was regenerated with:

go run github.com/a-h/templ/cmd/templ@v0.3.1001 generate

The button_templ.go diff is the semantic change plus sequential index renumbering; no ResolveAttributeValue contamination. assets/popui.css is unchanged (no new utility classes).

Heads-up for anyone regenerating: templ generate reported updates=0 on runs where it did rewrite files. The counter is unreliable — verify against git status.

Verification

Rendered Buttons through the real component and asserted the opening tag:

Props type= bare else
Type: "submit" submit no
Type: "submit", Copy: "x" submit (single attr) no
Copy: "x" button no
neither absent no

go generate ./..., go build ./..., go vet ./..., go test ./... all clean.

🤖 Generated with Claude Code

templ v0.3.1001 mis-generates an `if / else if` chain inside an element's
attribute list. In button.templ the chain selecting the `type` attribute
generated code that writes a literal " else" into the markup *outside* any
conditional, and demotes the else-branch to an independent `if`:

    }                          // end of `if btn.Type != ""`
    WriteString(..., " else")  // unconditional
    if btn.Copy != "" { ... }  // no longer mutually exclusive

Two consequences: every non-anchor Button rendered a bare `else` attribute,
and a Button with both Type and Copy set would emit two `type` attributes.
Browsers ignore the unknown attribute so nothing broke at runtime, but the
markup fails validation.

Replace the chain with two independent, mutually exclusive conditions, and
note on the func doc comment why `else if` is avoided here so it doesn't get
folded back. Verified by rendering Buttons across all four Type/Copy
combinations: correct single `type` attribute, Type taking precedence over
Copy, and no bare `else`.

A sweep of the other 16 `else if` uses in .templ files found none in
attribute position — grepping the committed *_templ.go files for the
literal matched only button_templ.go.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@netlify

netlify Bot commented Aug 20, 2026

Copy link
Copy Markdown

Deploy Preview for popui-go ready!

Name Link
🔨 Latest commit 71e72bc
🔍 Latest deploy log https://app.netlify.com/projects/popui-go/deploys/6a87077be1c72e00089bab09
😎 Deploy Preview https://deploy-preview-65--popui-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants