diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index 8bb1af5a448..f83101add89 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -187,7 +187,8 @@ const Search = { if (Search._queued_query !== null) { const query = Search._queued_query; Search._queued_query = null; - Search.query(query); + let { results, searchTerms, highlightTerms } = Search.query(query); + _displayNextItem(results, results.length, searchTerms, highlightTerms); } }, @@ -235,8 +236,12 @@ const Search = { Search.startPulse(); // index already loaded, the browser was quick! - if (Search.hasIndex()) Search.query(query); - else Search.deferQuery(query); + if (Search.hasIndex()) { + let { results, searchTerms, highlightTerms } = Search.query(query); + _displayNextItem(results, results.length, searchTerms, highlightTerms); + } else { + Search.deferQuery(query); + } }, /** @@ -367,8 +372,7 @@ const Search = { //Search.lastresults = results.slice(); // a copy // console.info("search results:", Search.lastresults); - // print the results - _displayNextItem(results, results.length, searchTerms, highlightTerms); + return { results, searchTerms, highlightTerms }; }, /** @@ -463,17 +467,23 @@ const Search = { { files: terms[word], score: Scorer.term }, { files: titleTerms[word], score: Scorer.title }, ]; + // add support for partial matches if (word.length > 2) { const escapedWord = _escapeRegExp(word); - Object.keys(terms).forEach((term) => { - if (term.match(escapedWord) && !terms[word]) - arr.push({ files: terms[term], score: Scorer.partialTerm }); - }); - Object.keys(titleTerms).forEach((term) => { - if (term.match(escapedWord) && !titleTerms[word]) - arr.push({ files: titleTerms[word], score: Scorer.partialTitle }); - }); + if (!terms.hasOwnProperty(word)) { + Object.keys(terms).forEach((term) => { + if (term.match(escapedWord)) { + arr.push({ files: terms[term], score: Scorer.partialTerm }); + } + }); + } + if (!titleTerms.hasOwnProperty(word)) { + Object.keys(titleTerms).forEach((term) => { + if (term.match(escapedWord)) + arr.push({ files: titleTerms[word], score: Scorer.partialTitle }); + }); + } } // no match but word was a required one @@ -496,9 +506,13 @@ const Search = { // create the mapping files.forEach((file) => { - if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1) - fileMap.get(file).push(word); - else fileMap.set(file, [word]); + if (fileMap.has(file)) { + if (fileMap.get(file).indexOf(word) === -1) { + fileMap.get(file).push(word); + } + } else { + fileMap.set(file, [word]); + } }); }); diff --git a/tests/js/documentation_options.js b/tests/js/documentation_options.js index e736460a09f..5ca7f178768 100644 --- a/tests/js/documentation_options.js +++ b/tests/js/documentation_options.js @@ -1 +1,9 @@ const DOCUMENTATION_OPTIONS = {}; + +// stub Stemmer / stopWords +function Stemmer() { + this.stemWord = function (word) { + return word; + } +}; +let stopwords = []; diff --git a/tests/js/searchtools.js b/tests/js/searchtools.js index c9e0c43d3b2..765af56016c 100644 --- a/tests/js/searchtools.js +++ b/tests/js/searchtools.js @@ -21,7 +21,7 @@ describe('Basic html theme search', function() { "<no title>", "", null, - 2, + 5, "index.rst" ]]; expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits); @@ -29,6 +29,33 @@ describe('Basic html theme search', function() { }); + describe('query', function() { + it("should duplicate on title and content", function() { + index = { + alltitles: { + 'Main Page': [[0, 'main-page']], + }, + docnames:["index"], + filenames:["index.rst"], + indexentries:{}, + objects:{}, + objtypes: {}, + objnames: {}, + terms:{main:0, page:0}, + titles:["Main Page"], + titleterms:{ main:0, page:0 } + } + Search.setIndex(index); + let { results } = Search.query('main page'); + // currently the search result is duplicated, see + // https://github.com/sphinx-doc/sphinx/pull/11942 + expect(results).toEqual([ + [ 'index', 'Main Page', '', null, 15, 'index.rst' ], + [ 'index', 'Main Page', '#main-page', null, 100, 'index.rst' ] + ]); + }); + }) + }); // This is regression test for https://github.com/sphinx-doc/sphinx/issues/3150