Skip to content

Commit

Permalink
8326332: Unclosed inline tags cause misalignment in summary tables
Browse files Browse the repository at this point in the history
Reviewed-by: rschmelter
Backport-of: cc85abc2120b5d1b1c5eca5c9b89a73386956bb7
  • Loading branch information
MBaesken committed Jul 29, 2024
1 parent 2b9228a commit 5ed58c2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import static com.sun.source.doctree.DocTree.Kind.LINK;
import static com.sun.source.doctree.DocTree.Kind.LINK_PLAIN;
import static com.sun.source.doctree.DocTree.Kind.SEE;
import static com.sun.source.doctree.DocTree.Kind.START_ELEMENT;
import static com.sun.source.doctree.DocTree.Kind.TEXT;
import static jdk.javadoc.internal.doclets.toolkit.util.CommentHelper.SPACER;

Expand Down Expand Up @@ -1273,21 +1274,37 @@ private void addCommentTags(Element element, List<? extends DocTree> tags, boole
}
}

boolean ignoreNonInlineTag(DocTree dtree) {
// helper methods because jdk21 functionality is not allowed
private static Name getLastHelper(List<Name> l) {
return l.get(l.size() - 1);
}

private static Name removeLastHelper(List<Name> l) {
return l.remove(l.size() - 1);
}

boolean ignoreNonInlineTag(DocTree dtree, List<Name> openTags) {
Name name = null;
if (dtree.getKind() == Kind.START_ELEMENT) {
StartElementTree setree = (StartElementTree)dtree;
name = setree.getName();
} else if (dtree.getKind() == Kind.END_ELEMENT) {
EndElementTree eetree = (EndElementTree)dtree;
name = eetree.getName();
Kind kind = dtree.getKind();
if (kind == Kind.START_ELEMENT) {
name = ((StartElementTree)dtree).getName();
} else if (kind == Kind.END_ELEMENT) {
name = ((EndElementTree)dtree).getName();
}

if (name != null) {
HtmlTag htmlTag = HtmlTag.get(name);
if (htmlTag != null &&
htmlTag.blockType != jdk.javadoc.internal.doclint.HtmlTag.BlockType.INLINE) {
return true;
if (htmlTag != null) {
if (htmlTag.blockType != HtmlTag.BlockType.INLINE) {
return true;
}
// Keep track of open inline tags that need to be closed, see 8326332
if (kind == START_ELEMENT && htmlTag.endKind == HtmlTag.EndKind.REQUIRED) {
openTags.add(name);
} else if (kind == Kind.END_ELEMENT && !openTags.isEmpty()
&& getLastHelper(openTags).equals(name)) {
removeLastHelper(openTags);
}
}
}
return false;
Expand Down Expand Up @@ -1377,6 +1394,7 @@ public ContentBuilder add(CharSequence text) {
// Array of all possible inline tags for this javadoc run
configuration.tagletManager.checkTags(element, trees, true);
commentRemoved = false;
List<Name> openTags = new ArrayList<>();

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

if (context.isFirstSentence) {
// Ignore block tags
if (ignoreNonInlineTag(tag))
if (ignoreNonInlineTag(tag, openTags)) {
continue;
}

// Ignore any trailing whitespace OR whitespace after removed html comment
if ((isLastNode || commentRemoved)
&& tag.getKind() == TEXT
&& isAllWhiteSpace(ch.getText(tag)))
&& isAllWhiteSpace(ch.getText(tag))) {
continue;
}

// Ignore any leading html comments
if ((isFirstNode || commentRemoved) && tag.getKind() == COMMENT) {
Expand Down Expand Up @@ -1638,6 +1658,10 @@ protected Boolean defaultAction(DocTree node, Content c) {
if (allDone)
break;
}
// Close any open inline tags
while (!openTags.isEmpty()) {
result.add(RawHtml.endElement(removeLastHelper(openTags)));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ public boolean write(Writer out, boolean atNewline) throws IOException {
out.write(rawHtmlContent);
return rawHtmlContent.endsWith(DocletConstants.NL);
}

/**
* Creates HTML for the end of an element.
*
* @param name the name of the element
* @return the HTML
*/
public static RawHtml endElement(CharSequence name) {
return new RawHtml("</" + name + ">");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 4165985
* @bug 4165985 8326332
* @summary Determine the end of the first sentence using BreakIterator.
* If the first sentence of "method" is parsed correctly, the test passes.
* Correct Answer: "This is a class (i.e. it is indeed a class)."
Expand Down Expand Up @@ -76,5 +76,10 @@ public void test() {
"""
<div class="block">A constant indicating that the keyLocation is indeterminate
or not relevant.</div>""");

checkOutput("pkg/BreakIteratorTest.html", true,
"""
<div class="block">Inline tags <i><a href="../index-all.html">extending
beyond the first sentence.</a></i></div>""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public void foobar(){}
*/
public void fe(){}

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

1 comment on commit 5ed58c2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.