Repository navigation
feat: support Go comments within HTML tag attributes #1251
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
Merged
+395
−0
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb99c0c
feat: add support for Go comments within HTML tag attributes
jackielii 86d6a99
fix: formatter handling of attribute comments
jackielii 0a81c6d
comment according to a-h standard.
jackielii 993de50
merge
jackielii caba4c4
Merge branch 'main' into feat/attribute-comments
jackielii d9aa454
add range and edge cases tests
jackielii a0d8817
fix test: somehow it needs weird format, probably because <link> shou…
jackielii 57be63c
merge: resolve conflict with main branch
a-h cf865db
style: format elementparser_test.go
a-h 85876a6
chore: generate
a-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <form class="flex" action="javascript:void(0)" method="post" id="test-form"> | ||
| <input type="text" name="field1" /> | ||
| </form> | ||
| <div id="main" class="container" data-value="dynamic" hidden>Content</div> | ||
| <button type="button" onclick="handleClick()" disabled="false">Click me</button> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package testattributecomments | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "testing" | ||
|
|
||
| "github.com/a-h/templ/generator/htmldiff" | ||
| ) | ||
|
|
||
| //go:embed expected.html | ||
| var expected string | ||
|
|
||
| func Test(t *testing.T) { | ||
| component := TestAttributeComments() | ||
|
|
||
| _, diff, err := htmldiff.Diff(component, expected) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if diff != "" { | ||
| t.Error(diff) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package testattributecomments | ||
|
|
||
| templ TestAttributeComments() { | ||
| <form | ||
| class="flex" | ||
| // This comment here explains why the following attribute is necessary, | ||
| // namely to prevent submit when hitting the enter key. | ||
| action="javascript:void(0)" | ||
| /* This is a multi-line comment | ||
| that spans multiple lines | ||
| within the attributes */ | ||
| method="post" | ||
| // Another single line comment | ||
| id="test-form" | ||
| > | ||
| <input type="text" name="field1"/> | ||
| </form> | ||
| <div | ||
| // Comment at the start | ||
| id="main" | ||
| /* Multi-line comment */ | ||
| class="container" | ||
| // Comment before expression | ||
| data-value={ "dynamic" } | ||
| // Comment before boolean | ||
| hidden | ||
| // Final comment | ||
| > | ||
| Content | ||
| </div> | ||
| <button | ||
| type="button" | ||
| // This explains the onclick handler | ||
| onclick="handleClick()" | ||
| /* This is a safety measure | ||
| to prevent accidental submits */ | ||
| disabled={ false } | ||
| > | ||
| Click me | ||
| </button> | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
internal/format/testdata/comments_in_attributes_are_preserved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| -- in -- | ||
| package main | ||
|
|
||
| templ test() { | ||
| <form | ||
| class="flex" | ||
| // This comment explains why the action is necessary | ||
| action="javascript:void(0)" | ||
| /* Multi-line comment | ||
| for detailed explanations */ | ||
| method="post" | ||
| // Comment before id | ||
| id="test-form" | ||
| > | ||
| <input type="text" /> | ||
| </form> | ||
| <div | ||
| // Comment at start | ||
| id="main" | ||
| /* Multi-line comment */ | ||
| class="container" | ||
| // Comment before expression | ||
| data-value={ "dynamic" } | ||
| // Comment before boolean | ||
| hidden | ||
| > | ||
| Content | ||
| </div> | ||
| } | ||
| -- out -- | ||
| package main | ||
|
|
||
| templ test() { | ||
| <form | ||
| class="flex" | ||
| // This comment explains why the action is necessary | ||
| action="javascript:void(0)" | ||
| /* Multi-line comment | ||
| for detailed explanations */ | ||
| method="post" | ||
| // Comment before id | ||
| id="test-form" | ||
| > | ||
| <input type="text"/> | ||
| </form> | ||
| <div | ||
| // Comment at start | ||
| id="main" | ||
| /* Multi-line comment */ | ||
| class="container" | ||
| // Comment before expression | ||
| data-value={ "dynamic" } | ||
| // Comment before boolean | ||
| hidden | ||
| > | ||
| Content | ||
| </div> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.