langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
changeset 45860 0952e2c6545a
parent 45417 f7479ee8de69
child 46080 65ccd412049b
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Thu Aug 24 16:25:08 2017 +0200
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Wed Jul 05 13:46:05 2017 -0700
@@ -1757,6 +1757,9 @@
         return true;
     }
 
+    // Notify the next DocTree handler to take necessary action
+    private boolean commentRemoved = false;
+
     /**
      * Converts inline tags and text to text strings, expanding the
      * inline tags along the way.  Called wherever text can contain
@@ -1782,27 +1785,32 @@
         CommentHelper ch = utils.getCommentHelper(element);
         // Array of all possible inline tags for this javadoc run
         configuration.tagletManager.checkTags(utils, element, tags, true);
+        commentRemoved = false;
+
         for (ListIterator<? extends DocTree> iterator = tags.listIterator(); iterator.hasNext();) {
+            boolean isFirstNode = !iterator.hasPrevious();
             DocTree tag = iterator.next();
-             // zap block tags
-            if (isFirstSentence && ignoreNonInlineTag(tag))
-                continue;
+            boolean isLastNode  = !iterator.hasNext();
+
+            if (isFirstSentence) {
+                // Ignore block tags
+                if (ignoreNonInlineTag(tag))
+                    continue;
 
-            if (isFirstSentence && iterator.nextIndex() == tags.size() &&
-                    (tag.getKind() == TEXT && isAllWhiteSpace(ch.getText(tag))))
-                continue;
+                // Ignore any trailing whitespace OR whitespace after removed html comment
+                if ((isLastNode || commentRemoved)
+                        && tag.getKind() == TEXT
+                        && isAllWhiteSpace(ch.getText(tag)))
+                    continue;
+
+                // Ignore any leading html comments
+                if ((isFirstNode || commentRemoved) && tag.getKind() == COMMENT) {
+                    commentRemoved = true;
+                    continue;
+                }
+            }
 
             boolean allDone = new SimpleDocTreeVisitor<Boolean, Content>() {
-                // notify the next DocTree handler to take necessary action
-                boolean commentRemoved = false;
-
-                private boolean isLast(DocTree node) {
-                    return node.equals(tags.get(tags.size() - 1));
-                }
-
-                private boolean isFirst(DocTree node) {
-                    return node.equals(tags.get(0));
-                }
 
                 private boolean inAnAtag() {
                     if (utils.isStartElement(tag)) {
@@ -1847,14 +1855,14 @@
                             if (text.startsWith("/..") && !configuration.docrootparent.isEmpty()) {
                                 result.addContent(configuration.docrootparent);
                                 docRootContent = new ContentBuilder();
-                                result.addContent(textCleanup(text.substring(3), isLast(node)));
+                                result.addContent(textCleanup(text.substring(3), isLastNode));
                             } else {
                                 if (!docRootContent.isEmpty()) {
                                     docRootContent = copyDocRootContent(docRootContent);
                                 } else {
                                     text = redirectRelativeLinks(element, (TextTree) dt);
                                 }
-                                result.addContent(textCleanup(text, isLast(node)));
+                                result.addContent(textCleanup(text, isLastNode));
                             }
                         } else {
                             docRootContent = copyDocRootContent(docRootContent);
@@ -1868,10 +1876,6 @@
 
                 @Override
                 public Boolean visitComment(CommentTree node, Content c) {
-                    if (isFirstSentence && isFirst(node)) {
-                        commentRemoved = true;
-                        return this.visit(iterator.next(), c);
-                    }
                     result.addContent(new RawHtml(node.getBody()));
                     return false;
                 }
@@ -1996,8 +2000,7 @@
                 @Override
                 public Boolean visitText(TextTree node, Content c) {
                     String text = node.getBody();
-                    result.addContent(new RawHtml(textCleanup(text, isLast(node), commentRemoved)));
-                    commentRemoved = false;
+                    result.addContent(new RawHtml(textCleanup(text, isLastNode, commentRemoved)));
                     return false;
                 }
 
@@ -2013,6 +2016,7 @@
                 }
 
             }.visit(tag, null);
+            commentRemoved = false;
             if (allDone)
                 break;
         }