Skip to content

Commit

Permalink
Fix HTML escaping after literals in non-code Javadoc
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 committed Aug 13, 2024
1 parent 1479147 commit d8f2f35
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
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 d8f2f35

Please sign in to comment.