Merge branch '8137_urldecode' into 3.x

This commit is contained in:
Takeshi KOMIYA 2021-01-26 02:11:13 +09:00
commit 241577f65e
6 changed files with 55 additions and 3 deletions

View File

@ -80,6 +80,8 @@ Bugs fixed
+ or ^) are used as keystrokes + or ^) are used as keystrokes
* #8629: html: A type warning for html_use_opensearch is shown twice * #8629: html: A type warning for html_use_opensearch is shown twice
* #8714: html: kbd role with "Caps Lock" rendered incorrectly * #8714: html: kbd role with "Caps Lock" rendered incorrectly
* #8123: html search: fix searching for terms containing + (Requires a custom
search language that does not split on +)
* #8665: html theme: Could not override globaltoc_maxdepth in theme.conf * #8665: html theme: Could not override globaltoc_maxdepth in theme.conf
* #4304: linkcheck: Fix race condition that could lead to checking the * #4304: linkcheck: Fix race condition that could lead to checking the
availability of the same URL twice availability of the same URL twice

View File

@ -18,6 +18,7 @@ module.exports = function(config) {
'sphinx/themes/basic/static/underscore.js', 'sphinx/themes/basic/static/underscore.js',
'sphinx/themes/basic/static/jquery.js', 'sphinx/themes/basic/static/jquery.js',
'sphinx/themes/basic/static/doctools.js', 'sphinx/themes/basic/static/doctools.js',
'sphinx/themes/basic/static/searchtools.js',
'tests/js/*.js' 'tests/js/*.js'
], ],

View File

@ -29,9 +29,14 @@ if (!window.console || !console.firebug) {
/** /**
* small helper function to urldecode strings * small helper function to urldecode strings
*
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
*/ */
jQuery.urldecode = function(x) { jQuery.urldecode = function(x) {
return decodeURIComponent(x).replace(/\+/g, ' '); if (!x) {
return x
}
return decodeURIComponent(x.replace(/\+/g, ' '));
}; };
/** /**

View File

@ -379,6 +379,13 @@ var Search = {
return results; return results;
}, },
/**
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
escapeRegExp : function(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
},
/** /**
* search for full-text terms in the index * search for full-text terms in the index
*/ */
@ -402,13 +409,14 @@ var Search = {
]; ];
// add support for partial matches // add support for partial matches
if (word.length > 2) { if (word.length > 2) {
var word_regex = this.escapeRegExp(word);
for (var w in terms) { for (var w in terms) {
if (w.match(word) && !terms[word]) { if (w.match(word_regex) && !terms[word]) {
_o.push({files: terms[w], score: Scorer.partialTerm}) _o.push({files: terms[w], score: Scorer.partialTerm})
} }
} }
for (var w in titleterms) { for (var w in titleterms) {
if (w.match(word) && !titleterms[word]) { if (w.match(word_regex) && !titleterms[word]) {
_o.push({files: titleterms[w], score: Scorer.partialTitle}) _o.push({files: titleterms[w], score: Scorer.partialTitle})
} }
} }

View File

@ -12,6 +12,10 @@ describe('jQuery extensions', function() {
expect(jQuery.urldecode(test_encoded_string)).toEqual(test_decoded_string); expect(jQuery.urldecode(test_encoded_string)).toEqual(test_decoded_string);
}); });
it('+ should result in " "', function() {
expect(jQuery.urldecode('+')).toEqual(' ');
});
}); });
describe('getQueryParameters', function() { describe('getQueryParameters', function() {

32
tests/js/searchtools.js Normal file
View File

@ -0,0 +1,32 @@
describe('Basic html theme search', function() {
describe('terms search', function() {
it('should find "C++" when in index', function() {
index = {
docnames:["index"],
filenames:["index.rst"],
terms:{'c++':0},
titles:["<no title>"],
titleterms:{}
}
Search.setIndex(index);
searchterms = ['c++'];
excluded = [];
terms = index.terms;
titleterms = index.titleterms;
hits = [[
"index",
"<no title>",
"",
null,
2,
"index.rst"
]];
expect(Search.performTermsSearch(searchterms, excluded, terms, titleterms)).toEqual(hits);
});
});
});