Skip to content

Keep the blank line before a Javadoc <div> instead of inferring a <p> - #270

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/javadoc-do-not-insert-a-p-tag-before-a-div-that
Nov 24, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/javadoc-do-not-insert-a-p-tag-before-a-div-that

Conversation

@abashev

@abashev abashev commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #268. Based on 21.X.X; there are no other open pull requests.

Where a blank line separates two words of a Javadoc comment, JavadocLexer.inferParagraphTags puts a <p> in front of the second. When that word is a <div> start tag, the <p> comes out empty: a <p> cannot hold a <div>, so the <div> closes it, and doclint warns empty <p> tag. google-java-format#1534 (open, its author has not signed the CLA) keeps the blank line there instead; this does the same in our lexer.

/**
 * Intro.
 *
 * <div class="notice">Block content.</div>
 */

was formatted with <p><div class="notice"> and now stays as it is.

What changes

  • inferParagraphTags turns the whitespace into a new BLANK_LINE token, instead of adding a <p> after it, when the next literal starts a <div>: <div>, <DIV>, <div/>, or <div alone, which is how a tag with attributes starts, since a literal ends at whitespace. <div-custom>, <span> and plain text still get a <p>, and a <p> written in the comment stays.

  • Lists and block tags. Inside a list item or a block tag such as @param, the writer turns the blank lines that other tokens ask for into newlines. A newline before the <div> would be plain whitespace the next time the output is formatted, and the <div> would join the line before it. So BLANK_LINE asks the writer for a FORCED_BLANK_LINE, which is written there too:

     * <ul>
     *   <li>Item.
     *
     *       <div>In an item.</div>
     * </ul>
    

    The upstream change writes a newline there, and so changes its own output on a second pass; its test has no list or block tag.

  • Unlike what Javadoc: do not insert a <p> tag before a <div> that follows a blank line #268 says, the upstream change applies in block tags; what it leaves out are inline tags and snippets. Here the lexer leaves out <pre>, <table>, <code> and snippets as before, whose line breaks are FORCED_NEWLINE.

Output

Javadoc is formatted only with JavaFormatterOptions.builder().formatJavadoc(true); neither the command line nor the plugins turn it on.

Among the 128k files of JDK 21's src.zip and the sources jars in the Gradle cache, 17 have a Javadoc line that starts with a <div>. Formatted through the API with formatJavadoc(true), by the old and the new allJar, 7 of them differ, each only by a <p> in front of a <div> that is no longer there, for example in javax/swing/JScrollPane.java:

- * <p><div style="float:right;text-align:center">
+ * <div style="float:right;text-align:center">

Formatting the output again changes nothing, with either jar.

Tests

noParagraphTagInferredBeforeDiv covers the cases above, in a list item and after @param too, and formats its expected output again. Before the change it fails with <p> in front of each <div>.

./gradlew check passes on JDK 21, 1,439 tests in open-java-format.

Where a blank line separates two words of a Javadoc comment, the
lexer infers a <p> in front of the second. When that word is a <div>
start tag, the <p> comes out empty: a <p> cannot hold a <div>, so the
<div> closes it, and doclint warns "empty <p> tag". The blank line now
stays without the <p>, as google-java-format#1534 proposes. <DIV> and
<div class=...> count, <div-custom> does not, and a <p> written in the
comment stays.

Inside a list item or a block tag the writer turns the blank lines
that other tokens ask for into newlines. A newline before the <div>
would be plain whitespace the next time the output is formatted, and
the <div> would join the line before it, so this blank line is
written there too. The upstream change writes a newline, and so
changes its own output on a second pass.

Among the 128k files of JDK 21's src.zip and the Gradle cache's
sources jars, 17 have a Javadoc line that starts with a <div>. In 7
of them an inferred <p> goes, and nothing else changes; formatting
the output again changes nothing.

Fixes #268.
@abashev
abashev merged commit f4b4f9d into 21.X.X Nov 24, 2026
16 checks passed
@abashev
abashev deleted the abashev/javadoc-do-not-insert-a-p-tag-before-a-div-that branch November 24, 2026 11:33
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.

Javadoc: do not insert a <p> tag before a <div> that follows a blank line

1 participant