Skip to content

Commit

Permalink
Fix HTML escaping after literals in non-code Javadoc (#1409)
Browse files Browse the repository at this point in the history
When formatting Javadocs, we use a state machine to track what we should
be escaping and when. In the case of non-code blocks, we generally
HTML-escape all characters except `@`, which must be encoded using a
Javadoc `{@literal @}` block. If we see a sequence of `@` characters, we
enter a state where we try to print as many `@`s as possible before
closing the `@literal` block. Unfortunately, there is a bug whereby when
closing the literal block (because we have seen a non-`@` character), we
do not HTML-escape the first character after it. This commit fixes this
issue.

Fixes #1408
  • Loading branch information
lunaris authored Aug 13, 2024
1 parent 1479147 commit 548849f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Improvements

### Bug Fixes

### Bug Fixes
- Fix HTML escaping after `{@literal ...}` blocks in Javadocs
2 changes: 1 addition & 1 deletion pkg/codegen/java/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (w *plainJavadocWriter) WriteRune(r rune) {
w.b.WriteRune('@')
} else {
w.b.WriteRune('}')
w.b.WriteRune(r)
w.b.WriteString(html.EscapeString(string(r)))
w.state = plainJavadocText
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/codegen/java/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ func TestFormatForeignComments(t *testing.T) {
- @Foo @ bar
- Foo @ bar
- Foo bar @@
- Foo @<this> works?
`,
expected: autogold.Expect(` * - {@literal @}Foo {@literal @} bar
* - Foo {@literal @} bar
* - Foo bar {@literal @@}
* - Foo {@literal @}&lt;this&gt; works?
* `),
},
{
Expand Down

0 comments on commit 548849f

Please sign in to comment.