Skip to content

Commit fa0a3c8

Browse files
committed
8326332: Unclosed inline tags cause misalignment in summary tables
Reviewed-by: rschmelter Backport-of: cc85abc2120b5d1b1c5eca5c9b89a73386956bb7
1 parent 8a5f67d commit fa0a3c8

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import static com.sun.source.doctree.DocTree.Kind.LINK;
119119
import static com.sun.source.doctree.DocTree.Kind.LINK_PLAIN;
120120
import static com.sun.source.doctree.DocTree.Kind.SEE;
121+
import static com.sun.source.doctree.DocTree.Kind.START_ELEMENT;
121122
import static com.sun.source.doctree.DocTree.Kind.TEXT;
122123
import static jdk.javadoc.internal.doclets.toolkit.util.CommentHelper.SPACER;
123124

@@ -1273,21 +1274,37 @@ private void addCommentTags(Element element, List<? extends DocTree> tags, boole
12731274
}
12741275
}
12751276

1276-
boolean ignoreNonInlineTag(DocTree dtree) {
1277+
// helper methods because jdk21 functionality is not allowed
1278+
private static Name getLastHelper(List<Name> l) {
1279+
return l.get(l.size() - 1);
1280+
}
1281+
1282+
private static Name removeLastHelper(List<Name> l) {
1283+
return l.remove(l.size() - 1);
1284+
}
1285+
1286+
boolean ignoreNonInlineTag(DocTree dtree, List<Name> openTags) {
12771287
Name name = null;
1278-
if (dtree.getKind() == Kind.START_ELEMENT) {
1279-
StartElementTree setree = (StartElementTree)dtree;
1280-
name = setree.getName();
1281-
} else if (dtree.getKind() == Kind.END_ELEMENT) {
1282-
EndElementTree eetree = (EndElementTree)dtree;
1283-
name = eetree.getName();
1288+
Kind kind = dtree.getKind();
1289+
if (kind == Kind.START_ELEMENT) {
1290+
name = ((StartElementTree)dtree).getName();
1291+
} else if (kind == Kind.END_ELEMENT) {
1292+
name = ((EndElementTree)dtree).getName();
12841293
}
12851294

12861295
if (name != null) {
12871296
HtmlTag htmlTag = HtmlTag.get(name);
1288-
if (htmlTag != null &&
1289-
htmlTag.blockType != jdk.javadoc.internal.doclint.HtmlTag.BlockType.INLINE) {
1290-
return true;
1297+
if (htmlTag != null) {
1298+
if (htmlTag.blockType != HtmlTag.BlockType.INLINE) {
1299+
return true;
1300+
}
1301+
// Keep track of open inline tags that need to be closed, see 8326332
1302+
if (kind == START_ELEMENT && htmlTag.endKind == HtmlTag.EndKind.REQUIRED) {
1303+
openTags.add(name);
1304+
} else if (kind == Kind.END_ELEMENT && !openTags.isEmpty()
1305+
&& getLastHelper(openTags).equals(name)) {
1306+
removeLastHelper(openTags);
1307+
}
12911308
}
12921309
}
12931310
return false;
@@ -1377,6 +1394,7 @@ public ContentBuilder add(CharSequence text) {
13771394
// Array of all possible inline tags for this javadoc run
13781395
configuration.tagletManager.checkTags(element, trees, true);
13791396
commentRemoved = false;
1397+
List<Name> openTags = new ArrayList<>();
13801398

13811399
for (ListIterator<? extends DocTree> iterator = trees.listIterator(); iterator.hasNext();) {
13821400
boolean isFirstNode = !iterator.hasPrevious();
@@ -1385,14 +1403,16 @@ public ContentBuilder add(CharSequence text) {
13851403

13861404
if (context.isFirstSentence) {
13871405
// Ignore block tags
1388-
if (ignoreNonInlineTag(tag))
1406+
if (ignoreNonInlineTag(tag, openTags)) {
13891407
continue;
1408+
}
13901409

13911410
// Ignore any trailing whitespace OR whitespace after removed html comment
13921411
if ((isLastNode || commentRemoved)
13931412
&& tag.getKind() == TEXT
1394-
&& isAllWhiteSpace(ch.getText(tag)))
1413+
&& isAllWhiteSpace(ch.getText(tag))) {
13951414
continue;
1415+
}
13961416

13971417
// Ignore any leading html comments
13981418
if ((isFirstNode || commentRemoved) && tag.getKind() == COMMENT) {
@@ -1638,6 +1658,10 @@ protected Boolean defaultAction(DocTree node, Content c) {
16381658
if (allDone)
16391659
break;
16401660
}
1661+
// Close any open inline tags
1662+
while (!openTags.isEmpty()) {
1663+
result.add(RawHtml.endElement(removeLastHelper(openTags)));
1664+
}
16411665
return result;
16421666
}
16431667

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/RawHtml.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,14 @@ public boolean write(Writer out, boolean atNewline) throws IOException {
126126
out.write(rawHtmlContent);
127127
return rawHtmlContent.endsWith(DocletConstants.NL);
128128
}
129+
130+
/**
131+
* Creates HTML for the end of an element.
132+
*
133+
* @param name the name of the element
134+
* @return the HTML
135+
*/
136+
public static RawHtml endElement(CharSequence name) {
137+
return new RawHtml("</" + name + ">");
138+
}
129139
}

test/langtools/jdk/javadoc/doclet/testBreakIterator/TestBreakIterator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 4165985
26+
* @bug 4165985 8326332
2727
* @summary Determine the end of the first sentence using BreakIterator.
2828
* If the first sentence of "method" is parsed correctly, the test passes.
2929
* Correct Answer: "This is a class (i.e. it is indeed a class)."
@@ -76,5 +76,10 @@ public void test() {
7676
"""
7777
<div class="block">A constant indicating that the keyLocation is indeterminate
7878
or not relevant.</div>""");
79+
80+
checkOutput("pkg/BreakIteratorTest.html", true,
81+
"""
82+
<div class="block">Inline tags <i><a href="../index-all.html">extending
83+
beyond the first sentence.</a></i></div>""");
7984
}
8085
}

test/langtools/jdk/javadoc/doclet/testBreakIterator/pkg/BreakIteratorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public void foobar(){}
5656
*/
5757
public void fe(){}
5858

59+
/**
60+
* Inline tags <i><a href="{@docRoot}/index-all.html">extending
61+
* beyond the first sentence. Tags are closed here.</a></i>
62+
*/
63+
public void meh(){}
5964
}

0 commit comments

Comments
 (0)