8141636: Javadoc search should support camelCase search

Reviewed-by: jjg
This commit is contained in:
Bhavesh Patel 2016-10-13 10:48:09 -07:00
parent 440c2fb03c
commit 1b3ae71f98
2 changed files with 52 additions and 19 deletions

View File

@ -30,6 +30,9 @@ var catPackages = "Packages";
var catTypes = "Types"; var catTypes = "Types";
var catMembers = "Members"; var catMembers = "Members";
var catSearchTags = "SearchTags"; var catSearchTags = "SearchTags";
var highlight = "<span class=\"resultHighlight\">$&</span>";
var camelCaseRegexp = "";
var secondaryMatcher = "";
function getName(name) { function getName(name) {
var anchor = ""; var anchor = "";
var ch = ''; var ch = '';
@ -65,27 +68,35 @@ function getName(name) {
} }
return anchor; 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'; var watermark = 'Search';
$(function() { $(function() {
$("#search").prop("disabled", false); $("#search").prop("disabled", false);
$("#reset").prop("disabled", false); $("#reset").prop("disabled", false);
$("#search").val(watermark).addClass('watermark'); $("#search").val(watermark).addClass('watermark');
$("#search").blur(function(){ $("#search").blur(function() {
if ($(this).val().length == 0) { if ($(this).val().length == 0) {
$(this).val(watermark).addClass('watermark'); $(this).val(watermark).addClass('watermark');
} }
}); });
$("#search").keydown(function(){ $("#search").keydown(function() {
if ($(this).val() == watermark) { if ($(this).val() == watermark) {
$(this).val('').removeClass('watermark'); $(this).val('').removeClass('watermark');
} }
}); });
$("#reset").click(function(){ $("#reset").click(function() {
$("#search").val(''); $("#search").val('');
$("#search").focus(); $("#search").focus();
}); });
$("#search").focus(); $("#search").focus();
$("#search")[0].setSelectionRange(0,0); $("#search")[0].setSelectionRange(0, 0);
}); });
$.widget("custom.catcomplete", $.ui.autocomplete, { $.widget("custom.catcomplete", $.ui.autocomplete, {
_create: function() { _create: function() {
@ -112,22 +123,19 @@ $.widget("custom.catcomplete", $.ui.autocomplete, {
}); });
}, },
_renderItem: function(ul, item) { _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 = ""; var label = "";
if (item.category === catModules) { if (item.category === catModules) {
label = item.l.replace(regexp, highlight); label = getHighlightedText(item.l);
} else if (item.category === catPackages) { } else if (item.category === catPackages) {
label = (item.m) label = (item.m)
? (item.m + "/" + item.l).replace(regexp, highlight) ? getHighlightedText(item.m + "/" + item.l)
: item.l.replace(regexp, highlight); : getHighlightedText(item.l);
} else if (item.category === catTypes) { } else if (item.category === catTypes) {
label += (item.p + "." + item.l).replace(regexp, highlight); label = getHighlightedText(item.p + "." + item.l);
} else if (item.category === catMembers) { } 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) { } else if (item.category === catSearchTags) {
label = item.l.replace(regexp, highlight); label = getHighlightedText(item.l);
} else { } else {
label = item.l; label = item.l;
} }
@ -163,7 +171,9 @@ $(function() {
var tgresult = new Array(); var tgresult = new Array();
var displayCount = 0; var displayCount = 0;
var exactMatcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term) + "$", "i"); 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) { if (moduleSearchIndex) {
var mdleCount = 0; var mdleCount = 0;
$.each(moduleSearchIndex, function(index, item) { $.each(moduleSearchIndex, function(index, item) {
@ -171,6 +181,8 @@ $(function() {
if (exactMatcher.test(item.l)) { if (exactMatcher.test(item.l)) {
result.unshift(item); result.unshift(item);
mdleCount++; mdleCount++;
} else if (camelCaseMatcher.test(item.l)) {
result.unshift(item);
} else if (secondaryMatcher.test(item.l)) { } else if (secondaryMatcher.test(item.l)) {
result.push(item); result.push(item);
} }
@ -188,6 +200,8 @@ $(function() {
if (exactMatcher.test(item.l)) { if (exactMatcher.test(item.l)) {
presult.unshift(item); presult.unshift(item);
pCount++; pCount++;
} else if (camelCaseMatcher.test(pkg)) {
presult.unshift(item);
} else if (secondaryMatcher.test(pkg)) { } else if (secondaryMatcher.test(pkg)) {
presult.push(item); presult.push(item);
} }
@ -202,6 +216,8 @@ $(function() {
if (exactMatcher.test(item.l)) { if (exactMatcher.test(item.l)) {
tresult.unshift(item); tresult.unshift(item);
tCount++; tCount++;
} else if (camelCaseMatcher.test(item.l)) {
tresult.unshift(item);
} else if (secondaryMatcher.test(item.p + "." + item.l)) { } else if (secondaryMatcher.test(item.p + "." + item.l)) {
tresult.push(item); tresult.push(item);
} }
@ -216,6 +232,8 @@ $(function() {
if (exactMatcher.test(item.l)) { if (exactMatcher.test(item.l)) {
mresult.unshift(item); mresult.unshift(item);
mCount++; mCount++;
} else if (camelCaseMatcher.test(item.l)) {
mresult.unshift(item);
} else if (secondaryMatcher.test(item.c + "." + item.l)) { } else if (secondaryMatcher.test(item.c + "." + item.l)) {
mresult.push(item); mresult.push(item);
} }
@ -294,4 +312,4 @@ $(function() {
} }
} }
}); });
}); });

View File

@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 8141492 8071982 * @bug 8141492 8071982 8141636
* @summary Test the search feature of javadoc. * @summary Test the search feature of javadoc.
* @author bpatel * @author bpatel
* @library ../lib * @library ../lib
@ -45,6 +45,7 @@ public class TestSearch extends JavadocTester {
checkExit(Exit.OK); checkExit(Exit.OK);
checkSearchOutput("UnnamedPkgClass.html", true); checkSearchOutput("UnnamedPkgClass.html", true);
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(false, checkFiles(false,
"package-search-index.zip", "package-search-index.zip",
"tag-search-index.zip"); "tag-search-index.zip");
@ -62,6 +63,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkSingleIndex(true); checkSingleIndex(true);
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -78,6 +80,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkSingleIndex(true); checkSingleIndex(true);
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -110,6 +113,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkSingleIndex(true); checkSingleIndex(true);
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -142,6 +146,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkIndexNoComment(); checkIndexNoComment();
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -158,6 +163,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkIndexNoDeprecated(); checkIndexNoDeprecated();
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -174,6 +180,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkSplitIndex(); checkSplitIndex();
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(true, checkFiles(true,
"member-search-index.zip", "member-search-index.zip",
"package-search-index.zip", "package-search-index.zip",
@ -189,6 +196,7 @@ public class TestSearch extends JavadocTester {
checkSearchOutput(true); checkSearchOutput(true);
checkJavaFXOutput(); checkJavaFXOutput();
checkJqueryAndImageFiles(true); checkJqueryAndImageFiles(true);
checkSearchJS();
checkFiles(false, checkFiles(false,
"tag-search-index.zip"); "tag-search-index.zip");
checkFiles(true, checkFiles(true,
@ -420,4 +428,11 @@ public class TestSearch extends JavadocTester {
"resources/x.png", "resources/x.png",
"resources/glass.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)");
}
} }