changeset 3705:673c53147055

8141636: Javadoc search should support camelCase search Reviewed-by: jjg
author bpatel
date Thu, 13 Oct 2016 10:48:09 -0700
parents 2fd02a123cf0
children 9671307f8157
files src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js test/jdk/javadoc/doclet/testSearch/TestSearch.java
diffstat 2 files changed, 52 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Wed Oct 12 17:27:54 2016 -0700
+++ b/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/resources/search.js	Thu Oct 13 10:48:09 2016 -0700
@@ -30,6 +30,9 @@
 var catTypes = "Types";
 var catMembers = "Members";
 var catSearchTags = "SearchTags";
+var highlight = "<span class=\"resultHighlight\">$&</span>";
+var camelCaseRegexp = "";
+var secondaryMatcher = "";
 function getName(name) {
     var anchor = "";
     var ch = '';
@@ -65,27 +68,35 @@
     }
     return anchor;
 }
+function getHighlightedText(item) {
+    var ccMatcher = new RegExp(camelCaseRegexp);
+    var label = item.replace(ccMatcher, highlight);
+    if (label === item) {
+        label = item.replace(secondaryMatcher, highlight);
+    }
+    return label;
+}
 var watermark = 'Search';
 $(function() {
     $("#search").prop("disabled", false);
     $("#reset").prop("disabled", false);
     $("#search").val(watermark).addClass('watermark');
-    $("#search").blur(function(){
+    $("#search").blur(function() {
         if ($(this).val().length == 0) {
             $(this).val(watermark).addClass('watermark');
         }
     });
-    $("#search").keydown(function(){
-       if ($(this).val() == watermark) {
+    $("#search").keydown(function() {
+        if ($(this).val() == watermark) {
             $(this).val('').removeClass('watermark');
         }
     });
-    $("#reset").click(function(){
-       $("#search").val('');
-       $("#search").focus();
+    $("#reset").click(function() {
+        $("#search").val('');
+        $("#search").focus();
     });
     $("#search").focus();
-    $("#search")[0].setSelectionRange(0,0);
+    $("#search")[0].setSelectionRange(0, 0);
 });
 $.widget("custom.catcomplete", $.ui.autocomplete, {
     _create: function() {
@@ -112,22 +123,19 @@
         });
     },
     _renderItem: function(ul, item) {
-        var result = this.element.val();
-        var regexp = new RegExp($.ui.autocomplete.escapeRegex(result), "i");
-        highlight = "<span class=\"resultHighlight\">$&</span>";
         var label = "";
         if (item.category === catModules) {
-            label = item.l.replace(regexp, highlight);
+            label = getHighlightedText(item.l);
         } else if (item.category === catPackages) {
             label = (item.m)
-                    ? (item.m + "/" + item.l).replace(regexp, highlight)
-                    : item.l.replace(regexp, highlight);
+                    ? getHighlightedText(item.m + "/" + item.l)
+                    : getHighlightedText(item.l);
         } else if (item.category === catTypes) {
-            label += (item.p + "." + item.l).replace(regexp, highlight);
+            label = getHighlightedText(item.p + "." + item.l);
         } else if (item.category === catMembers) {
-            label += item.p + "." + (item.c + "." + item.l).replace(regexp, highlight);
+            label = getHighlightedText(item.p + "." + (item.c + "." + item.l));
         } else if (item.category === catSearchTags) {
-            label = item.l.replace(regexp, highlight);
+            label = getHighlightedText(item.l);
         } else {
             label = item.l;
         }
@@ -163,7 +171,9 @@
             var tgresult = new Array();
             var displayCount = 0;
             var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i");
-            var secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
+            camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join("([a-z0-9_$]*?)");
+            var camelCaseMatcher = new RegExp("^" + camelCaseRegexp);
+            secondaryMatcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
             if (moduleSearchIndex) {
                 var mdleCount = 0;
                 $.each(moduleSearchIndex, function(index, item) {
@@ -171,6 +181,8 @@
                     if (exactMatcher.test(item.l)) {
                         result.unshift(item);
                         mdleCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        result.unshift(item);
                     } else if (secondaryMatcher.test(item.l)) {
                         result.push(item);
                     }
@@ -188,6 +200,8 @@
                     if (exactMatcher.test(item.l)) {
                         presult.unshift(item);
                         pCount++;
+                    } else if (camelCaseMatcher.test(pkg)) {
+                        presult.unshift(item);
                     } else if (secondaryMatcher.test(pkg)) {
                         presult.push(item);
                     }
@@ -202,6 +216,8 @@
                     if (exactMatcher.test(item.l)) {
                         tresult.unshift(item);
                         tCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        tresult.unshift(item);
                     } else if (secondaryMatcher.test(item.p + "." + item.l)) {
                         tresult.push(item);
                     }
@@ -216,6 +232,8 @@
                     if (exactMatcher.test(item.l)) {
                         mresult.unshift(item);
                         mCount++;
+                    } else if (camelCaseMatcher.test(item.l)) {
+                        mresult.unshift(item);
                     } else if (secondaryMatcher.test(item.c + "." + item.l)) {
                         mresult.push(item);
                     }
@@ -294,4 +312,4 @@
             }
         }
     });
-});
+});
\ No newline at end of file
--- a/test/jdk/javadoc/doclet/testSearch/TestSearch.java	Wed Oct 12 17:27:54 2016 -0700
+++ b/test/jdk/javadoc/doclet/testSearch/TestSearch.java	Thu Oct 13 10:48:09 2016 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8141492 8071982
+ * @bug 8141492 8071982 8141636
  * @summary Test the search feature of javadoc.
  * @author bpatel
  * @library ../lib
@@ -45,6 +45,7 @@
         checkExit(Exit.OK);
         checkSearchOutput("UnnamedPkgClass.html", true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(false,
                 "package-search-index.zip",
                 "tag-search-index.zip");
@@ -62,6 +63,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -78,6 +80,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -110,6 +113,7 @@
         checkSearchOutput(true);
         checkSingleIndex(true);
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -142,6 +146,7 @@
         checkSearchOutput(true);
         checkIndexNoComment();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -158,6 +163,7 @@
         checkSearchOutput(true);
         checkIndexNoDeprecated();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -174,6 +180,7 @@
         checkSearchOutput(true);
         checkSplitIndex();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(true,
                 "member-search-index.zip",
                 "package-search-index.zip",
@@ -189,6 +196,7 @@
         checkSearchOutput(true);
         checkJavaFXOutput();
         checkJqueryAndImageFiles(true);
+        checkSearchJS();
         checkFiles(false,
                 "tag-search-index.zip");
         checkFiles(true,
@@ -420,4 +428,11 @@
                 "resources/x.png",
                 "resources/glass.png");
     }
+
+    void checkSearchJS() {
+        checkOutput("search.js", true,
+                "camelCaseRegexp = ($.ui.autocomplete.escapeRegex(request.term)).split(/(?=[A-Z])/).join(\"([a-z0-9_$]*?)\");",
+                "var camelCaseMatcher = new RegExp(\"^\" + camelCaseRegexp);",
+                "camelCaseMatcher.test(item.l)");
+    }
 }