Skip to content

Commit 8ac1bd0

Browse files
committed
HTML Search: Add test for querying
Fix a number of small bugs that this picked up along the way. Intended to eventually be updated after #11942 is landed (but seperating out for now for ease-of-review).
1 parent ceb3b2a commit 8ac1bd0

File tree

3 files changed

+66
-17
lines changed

3 files changed

+66
-17
lines changed

sphinx/themes/basic/static/searchtools.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ const Search = {
187187
if (Search._queued_query !== null) {
188188
const query = Search._queued_query;
189189
Search._queued_query = null;
190-
Search.query(query);
190+
let { results, searchTerms, highlightTerms } = Search.query(query);
191+
_displayNextItem(results, results.length, searchTerms, highlightTerms);
191192
}
192193
},
193194

@@ -235,8 +236,12 @@ const Search = {
235236
Search.startPulse();
236237

237238
// index already loaded, the browser was quick!
238-
if (Search.hasIndex()) Search.query(query);
239-
else Search.deferQuery(query);
239+
if (Search.hasIndex()) {
240+
let { results, searchTerms, highlightTerms } = Search.query(query);
241+
_displayNextItem(results, results.length, searchTerms, highlightTerms);
242+
} else {
243+
Search.deferQuery(query);
244+
}
240245
},
241246

242247
/**
@@ -367,8 +372,7 @@ const Search = {
367372
//Search.lastresults = results.slice(); // a copy
368373
// console.info("search results:", Search.lastresults);
369374

370-
// print the results
371-
_displayNextItem(results, results.length, searchTerms, highlightTerms);
375+
return { results, searchTerms, highlightTerms };
372376
},
373377

374378
/**
@@ -463,17 +467,23 @@ const Search = {
463467
{ files: terms[word], score: Scorer.term },
464468
{ files: titleTerms[word], score: Scorer.title },
465469
];
470+
466471
// add support for partial matches
467472
if (word.length > 2) {
468473
const escapedWord = _escapeRegExp(word);
469-
Object.keys(terms).forEach((term) => {
470-
if (term.match(escapedWord) && !terms[word])
471-
arr.push({ files: terms[term], score: Scorer.partialTerm });
472-
});
473-
Object.keys(titleTerms).forEach((term) => {
474-
if (term.match(escapedWord) && !titleTerms[word])
475-
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
476-
});
474+
if (!terms.hasOwnProperty(word)) {
475+
Object.keys(terms).forEach((term) => {
476+
if (term.match(escapedWord)) {
477+
arr.push({ files: terms[term], score: Scorer.partialTerm });
478+
}
479+
});
480+
}
481+
if (!titleTerms.hasOwnProperty(word)) {
482+
Object.keys(titleTerms).forEach((term) => {
483+
if (term.match(escapedWord))
484+
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
485+
});
486+
}
477487
}
478488

479489
// no match but word was a required one
@@ -496,9 +506,13 @@ const Search = {
496506

497507
// create the mapping
498508
files.forEach((file) => {
499-
if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1)
500-
fileMap.get(file).push(word);
501-
else fileMap.set(file, [word]);
509+
if (fileMap.has(file)) {
510+
if (fileMap.get(file).indexOf(word) === -1) {
511+
fileMap.get(file).push(word);
512+
}
513+
} else {
514+
fileMap.set(file, [word]);
515+
}
502516
});
503517
});
504518

tests/js/documentation_options.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
const DOCUMENTATION_OPTIONS = {};
2+
3+
// stub Stemmer / stopWords
4+
function Stemmer() {
5+
this.stemWord = function (word) {
6+
return word;
7+
}
8+
};
9+
let stopwords = [];

tests/js/searchtools.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,41 @@ describe('Basic html theme search', function() {
2121
"<no title>",
2222
"",
2323
null,
24-
2,
24+
5,
2525
"index.rst"
2626
]];
2727
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
2828
});
2929

3030
});
3131

32+
describe('query', function() {
33+
it("should not duplicate on title and content", function() {
34+
index = {
35+
alltitles: {
36+
'Main Page': [[0, 'main-page']],
37+
},
38+
docnames:["index"],
39+
filenames:["index.rst"],
40+
indexentries:{},
41+
objects:{},
42+
objtypes: {},
43+
objnames: {},
44+
terms:{main:0, page:0},
45+
titles:["Main Page"],
46+
titleterms:{ main:0, page:0 }
47+
}
48+
Search.setIndex(index);
49+
let { results } = Search.query('main page');
50+
// currently the search result is duplicated, see
51+
// https://github.com/sphinx-doc/sphinx/pull/11942
52+
expect(results).toEqual([
53+
[ 'index', 'Main Page', '', null, 15, 'index.rst' ],
54+
[ 'index', 'Main Page', '#main-page', null, 100, 'index.rst' ]
55+
]);
56+
});
57+
})
58+
3259
});
3360

3461
// This is regression test for https://github.com/sphinx-doc/sphinx/issues/3150

0 commit comments

Comments
 (0)