8198522: Javadoc search broken after output files organization for modules
authorbpatel
Mon, 26 Feb 2018 17:18:15 -0800
changeset 48991 978c3036fbfc
parent 48990 9c006936068b
child 48992 b8cd2fcc98e2
child 56195 6bcf49a043cc
8198522: Javadoc search broken after output files organization for modules Reviewed-by: jjg
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/Head.java
src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js
test/langtools/jdk/javadoc/doclet/testSearch/TestSearch.java
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Mon Feb 26 16:05:25 2018 -0800
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java	Mon Feb 26 17:18:15 2018 -0800
@@ -426,6 +426,7 @@
                 .setCharset(configuration.charset)
                 .addKeywords(metakeywords)
                 .setStylesheets(configuration.getMainStylesheet(), configuration.getAdditionalStylesheets())
+                .setUseModuleDirectories(configuration.useModuleDirectories)
                 .setIndex(configuration.createindex, mainBodyScript);
 
         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/Head.java	Mon Feb 26 16:05:25 2018 -0800
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/Head.java	Mon Feb 26 17:18:15 2018 -0800
@@ -60,6 +60,7 @@
     private boolean showTimestamp;
     private boolean showGeneratedBy;    // temporary: for compatibility
     private boolean showMetaCreated;    // temporary: for compatibility
+    private boolean useModuleDirectories;
     private DocFile mainStylesheetFile;
     private List<DocFile> additionalStylesheetFiles = Collections.emptyList();
     private boolean index;
@@ -175,6 +176,17 @@
     }
 
     /**
+     * Sets whether the module directories should be used. This is used to set the JavaScript variable.
+     *
+     * @param useModuleDirectories true if the module directories should be used
+     * @return  this object
+     */
+    public Head setUseModuleDirectories(boolean useModuleDirectories) {
+        this.useModuleDirectories = useModuleDirectories;
+        return this;
+    }
+
+    /**
      * Sets whether or not to include the supporting scripts and stylesheets for the
      * "search" feature.
      * If the feature is enabled, a {@code Script} must be provided into which some
@@ -305,7 +317,9 @@
                 String ptrPath = pathToRoot.isEmpty() ? "." : pathToRoot.getPath();
                 mainBodyScript.append("var pathtoroot = ")
                         .appendStringLiteral(ptrPath + "/")
-                        .append(";loadScripts(document, \'script\');");
+                        .append(";\n")
+                        .append("var useModuleDirectories = " + useModuleDirectories + ";\n")
+                        .append("loadScripts(document, \'script\');");
             }
             addJQueryFile(tree, DocPaths.JSZIP_MIN);
             addJQueryFile(tree, DocPaths.JSZIPUTILS_MIN);
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Mon Feb 26 16:05:25 2018 -0800
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Mon Feb 26 17:18:15 2018 -0800
@@ -76,6 +76,25 @@
     }
     return label;
 }
+function getURLPrefix(ui) {
+    var urlPrefix="";
+    if (useModuleDirectories) {
+        var slash = "/";
+        if (ui.item.category === catModules) {
+            return ui.item.l + slash;
+        } else if (ui.item.category === catPackages) {
+            return ui.item.m + slash;
+        } else if (ui.item.category === catTypes || ui.item.category === catMembers) {
+            $.each(packageSearchIndex, function(index, item) {
+                if (ui.item.p == item.l) {
+                    urlPrefix = item.m + slash;
+                }
+            });
+            return urlPrefix;
+        }
+    }
+    return urlPrefix;
+}
 var watermark = 'Search';
 $(function() {
     $("#search").val('');
@@ -314,22 +333,26 @@
         },
         select: function(event, ui) {
             if (ui.item.l !== noResult.l) {
-                var url = "";
+                var url = getURLPrefix(ui);
                 if (ui.item.category === catModules) {
-                    url = ui.item.l + "-summary.html";
+                    if (useModuleDirectories) {
+                        url += "module-summary.html";
+                    } else {
+                        url = ui.item.l + "-summary.html";
+                    }
                 } else if (ui.item.category === catPackages) {
-                    url = ui.item.l.replace(/\./g, '/') + "/package-summary.html";
+                    url += ui.item.l.replace(/\./g, '/') + "/package-summary.html";
                 } else if (ui.item.category === catTypes) {
                     if (ui.item.p === "<Unnamed>") {
-                        url = ui.item.l + ".html";
+                        url += ui.item.l + ".html";
                     } else {
-                        url = ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
+                        url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.l + ".html";
                     }
                 } else if (ui.item.category === catMembers) {
                     if (ui.item.p === "<Unnamed>") {
-                        url = ui.item.c + ".html" + "#";
+                        url += ui.item.c + ".html" + "#";
                     } else {
-                        url = ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
+                        url += ui.item.p.replace(/\./g, '/') + "/" + ui.item.c + ".html" + "#";
                     }
                     if (ui.item.url) {
                         url += ui.item.url;
--- a/test/langtools/jdk/javadoc/doclet/testSearch/TestSearch.java	Mon Feb 26 16:05:25 2018 -0800
+++ b/test/langtools/jdk/javadoc/doclet/testSearch/TestSearch.java	Mon Feb 26 17:18:15 2018 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,7 @@
 /*
  * @test
  * @bug 8141492 8071982 8141636 8147890 8166175 8168965 8176794 8175218 8147881
- *      8181622 8182263 8074407 8187521
+ *      8181622 8182263 8074407 8187521 8198522
  * @summary Test the search feature of javadoc.
  * @author bpatel
  * @library ../lib
@@ -43,7 +43,7 @@
     void test1() {
         javadoc("-d", "out-1", "-sourcepath", "-use", testSrc("UnnamedPkgClass.java"));
         checkExit(Exit.OK);
-        checkSearchOutput("UnnamedPkgClass.html", true);
+        checkSearchOutput("UnnamedPkgClass.html", true, true);
         checkJqueryAndImageFiles(true);
         checkSearchJS();
         checkFiles(false,
@@ -249,6 +249,16 @@
                 "type-search-index.js");
     }
 
+    @Test
+    void testNoModuleDirectories() {
+        javadoc("-d", "out-noMdlDir", "--no-module-directories", "-Xdoclint:none",
+                "-sourcepath", testSrc,
+                "-use", "pkg", "pkg1", "pkg2", "pkg3");
+        checkExit(Exit.OK);
+        checkSearchOutput(true, false);
+        checkSearchJS();
+    }
+
     void checkDocLintErrors() {
         checkOutput(Output.OUT, true,
                 "A sample method. Testing search tag for {@index \"unclosed quote}.",
@@ -258,10 +268,14 @@
     }
 
     void checkSearchOutput(boolean expectedOutput) {
-        checkSearchOutput("overview-summary.html", expectedOutput);
+        checkSearchOutput("overview-summary.html", expectedOutput, true);
     }
 
-    void checkSearchOutput(String fileName, boolean expectedOutput) {
+    void checkSearchOutput(boolean expectedOutput, boolean moduleDirectoriesVar) {
+        checkSearchOutput("overview-summary.html", expectedOutput, moduleDirectoriesVar);
+    }
+
+    void checkSearchOutput(String fileName, boolean expectedOutput, boolean moduleDirectoriesVar) {
         // Test for search related markup
         checkOutput(fileName, expectedOutput,
                 "<link rel=\"stylesheet\" type=\"text/css\" href=\"jquery/jquery-ui.css\" title=\"Style\">\n",
@@ -272,7 +286,9 @@
                 "<![endif]-->\n",
                 "<script type=\"text/javascript\" src=\"jquery/jquery-1.10.2.js\"></script>\n",
                 "<script type=\"text/javascript\" src=\"jquery/jquery-ui.js\"></script>",
-                "var pathtoroot = \"./\";loadScripts(document, 'script');",
+                "var pathtoroot = \"./\";\n"
+                + "var useModuleDirectories = " + moduleDirectoriesVar + ";\n"
+                + "loadScripts(document, 'script');",
                 "<ul class=\"navListSearch\">\n",
                 "<li><label for=\"search\">SEARCH:</label>\n"
                 + "<input type=\"text\" id=\"search\" value=\"search\" disabled=\"disabled\">\n"
@@ -503,7 +519,26 @@
                 + "        if ($(this).val() == watermark) {\n"
                 + "            $(this).val('').removeClass('watermark');\n"
                 + "        }\n"
-                + "    });");
+                + "    });",
+                "function getURLPrefix(ui) {\n"
+                + "    var urlPrefix=\"\";\n"
+                + "    if (useModuleDirectories) {\n"
+                + "        var slash = \"/\";\n"
+                + "        if (ui.item.category === catModules) {\n"
+                + "            return ui.item.l + slash;\n"
+                + "        } else if (ui.item.category === catPackages) {\n"
+                + "            return ui.item.m + slash;\n"
+                + "        } else if (ui.item.category === catTypes || ui.item.category === catMembers) {\n"
+                + "            $.each(packageSearchIndex, function(index, item) {\n"
+                + "                if (ui.item.p == item.l) {\n"
+                + "                    urlPrefix = item.m + slash;\n"
+                + "                }\n"
+                + "            });\n"
+                + "            return urlPrefix;\n"
+                + "        }\n"
+                + "    }\n"
+                + "    return urlPrefix;\n"
+                + "}");
     }
 
     void checkSingleIndexSearchTagDuplication() {