From 1e4f80d7a48e18b69c70ac16d533a56ad31d1dc5 Mon Sep 17 00:00:00 2001 From: Will Lachance Date: Sun, 3 Mar 2024 09:18:07 -0500 Subject: [PATCH] [search] fix partial matches overwriting full matches (#11958) --- CHANGES.rst | 2 ++ sphinx/themes/basic/static/searchtools.js | 20 ++++++++++++-------- tests/js/searchtools.js | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index af56692f7..64ce57723 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -33,6 +33,8 @@ Features added Bugs fixed ---------- +* #11958: HTML Search: Fix partial matches overwriting full matches. + Patch by William Lachance. * #11944: Use anchor in search preview. Patch by Will Lachance. * #11668: Raise a useful error when ``theme.conf`` is missing. diff --git a/sphinx/themes/basic/static/searchtools.js b/sphinx/themes/basic/static/searchtools.js index e9a43fa5d..c5826b137 100644 --- a/sphinx/themes/basic/static/searchtools.js +++ b/sphinx/themes/basic/static/searchtools.js @@ -477,14 +477,18 @@ const Search = { // 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 diff --git a/tests/js/searchtools.js b/tests/js/searchtools.js index b70c2d325..82282e3e8 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);