Skip to content

Commit 96336be

Browse files
committed
HTML Search: Add test, refactoring to make that possible
1 parent aec26d2 commit 96336be

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

sphinx/themes/basic/static/searchtools.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ const Search = {
198198
if (Search._queued_query !== null) {
199199
const query = Search._queued_query;
200200
Search._queued_query = null;
201-
Search.query(query);
201+
let { results, searchTerms, highlightTerms } = Search.query(query);
202+
_displayNextItem(results, results.length, searchTerms, highlightTerms);
202203
}
203204
},
204205

@@ -246,8 +247,12 @@ const Search = {
246247
Search.startPulse();
247248

248249
// index already loaded, the browser was quick!
249-
if (Search.hasIndex()) Search.query(query);
250-
else Search.deferQuery(query);
250+
if (Search.hasIndex()) {
251+
let { results, searchTerms, highlightTerms } = Search.query(query);
252+
_displayNextItem(results, results.length, searchTerms, highlightTerms);
253+
} else {
254+
Search.deferQuery(query);
255+
}
251256
},
252257

253258
/**
@@ -380,8 +385,7 @@ const Search = {
380385
//Search.lastresults = results.slice(); // a copy
381386
// console.info("search results:", Search.lastresults);
382387

383-
// print the results
384-
_displayNextItem(results, results.length, searchTerms, highlightTerms);
388+
return { results, searchTerms, highlightTerms };
385389
},
386390

387391
/**
@@ -476,6 +480,7 @@ const Search = {
476480
{ files: terms[word], score: Scorer.term },
477481
{ files: titleTerms[word], score: Scorer.title },
478482
];
483+
479484
// add support for partial matches
480485
if (word.length > 2) {
481486
const escapedWord = _escapeRegExp(word);

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ describe('Basic html theme search', function() {
5656

5757
});
5858

59+
describe('query', function() {
60+
it("should not duplicate on title and content", function() {
61+
index = {
62+
alltitles: {
63+
'Main Page': [[0, 'main-page']],
64+
},
65+
docnames:["index"],
66+
filenames:["index.rst"],
67+
indexentries:{},
68+
objects:{},
69+
objtypes: {},
70+
objnames: {},
71+
terms:{main:0, page:0},
72+
titles:["Main Page"],
73+
titleterms:{ main:0, page:0 }
74+
}
75+
Search.setIndex(index);
76+
let { results } = Search.query('main page');
77+
// should only be one result
78+
expect(results).toEqual([
79+
[ 'index', 'Main Page', '', null, 100, 'index.rst' ],
80+
]);
81+
});
82+
})
83+
5984
});
6085

6186
describe("htmlToText", function() {

0 commit comments

Comments
 (0)