Skip to content

feat: support Go comments within HTML tag attributes#1251

Merged
a-h merged 10 commits into
a-h:mainfrom
jackielii:feat/attribute-comments
May 10, 2026
Merged

feat: support Go comments within HTML tag attributes#1251
a-h merged 10 commits into
a-h:mainfrom
jackielii:feat/attribute-comments

Conversation

@jackielii
Copy link
Copy Markdown
Contributor

Summary

Implements support for single-line (//) and multi-line (/* */) Go comments within HTML element attributes, addressing issue #866.

Problem

Previously, templ did not allow Go comments within HTML tag attributes, requiring developers to place comments above the element which wasn't always convenient for documenting specific attributes.

Solution

This PR adds support for Go-style comments directly within HTML tag attributes. The comments are parsed and stored in the AST but excluded from the generated output.

Example

<form
  class="flex"
  // This comment explains why the following attribute is necessary
  action="javascript:void(0)"
  /* Multi-line comment
     for more detailed explanations */
  method="post"
>

Changes

  • Added AttributeComment type to represent comments within attributes
  • Updated parser to recognize and handle comments between attributes
  • Updated generator to skip comments during code generation
  • Added comprehensive test coverage in generator/test-attribute-comments/

Testing

  • Added test case following the existing test pattern in the generator folder
  • Test verifies that comments are properly excluded from generated output
  • All existing tests continue to pass

Fixes #866

Implements support for single-line (//) and multi-line (/* */) Go comments
within HTML element attributes, addressing issue a-h#866.

Changes:
- Add AttributeComment type to represent comments within attributes
- Update parser to recognize and handle comments between attributes
- Update generator to skip comments during code generation
- Add comprehensive test coverage

This allows developers to add explanatory comments directly within HTML tags
for better code documentation without affecting the generated output.

Fixes a-h#866
@jackielii
Copy link
Copy Markdown
Contributor Author

@a-h @joerdav anything else you need for this PR?

Comment thread parser/v2/types.go
@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Oct 13, 2025

Besides the 1 comment I think this looks good, @a-h any thoughts?

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Oct 13, 2025

Maybe could do with some tests with attribute values that contain // just to ensure no odd edge cases!

@jackielii
Copy link
Copy Markdown
Contributor Author

Maybe could do with some tests with attribute values that contain // just to ensure no odd edge cases!

added edge case tests in https://github.com/jackielii/templ/blob/feat/attribute-comments/generator/test-attribute-comments/template.templ#L41-L68

@jackielii
Copy link
Copy Markdown
Contributor Author

@a-h anything else you need?

@b0ch3nski
Copy link
Copy Markdown

This is a really cool feature! Do we need anything else here?

@a-h
Copy link
Copy Markdown
Owner

a-h commented Feb 15, 2026

@joerdav - we discussed this one last Friday, right?

IIRC, we said we'd merge it.

I was a bit reluctant because it's hard to walk something back once you've added it, and it changes the syntax highlighting and some other stuff.

@jackielii
Copy link
Copy Markdown
Contributor Author

I've been using this patch over a year now. I'm exclusive Neovim user, the treesitter already supports this. I think it's a natural extension to existing syntax: support commenting inside the HTML tag is less unexpected than not supporting it.

@a-h
Copy link
Copy Markdown
Owner

a-h commented May 10, 2026

Agreed. I'm merging this today, just fixing the conflicts.

a-h added 3 commits May 10, 2026 17:29
Keep both the attribute comment tests from this branch and the unquoted
attribute tests from main. Update comment test expectations to include
ValueRange and Range fields added in main.
@a-h a-h merged commit b757ca2 into a-h:main May 10, 2026
4 checks passed
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.

proposal: Go comments within HTML tags

4 participants