Skip to content

Commit 150c231

Browse files
committed
searcher.js: handle missing RegExp.escape
That API is more recent than I expected, so there is still a small minority of browsers that may be missing it. But this escaping is only needed when the search context special chars, which too is a minority of searches. Given that, I think it's better to just not escape if support is missing.
1 parent 386baad commit 150c231

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

lib/rdoc/generator/template/json_index/js/searcher.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ Searcher.prototype = new function() {
5757
}
5858

5959
function buildRegexps(queries) {
60+
// A small minority of older browsers don't have RegExp.escape
61+
// but it's not worth including a complex polyfill.
62+
var escape = RegExp.escape || function(s) { return s };
63+
6064
return queries.map(function(query) {
6165
var pattern = [];
6266
for (var i = 0; i < query.length; i++) {
63-
var char = RegExp.escape(query[i]);
67+
var char = escape(query[i]);
6468
pattern.push('([' + char + '])([^' + char + ']*?)');
6569
}
6670
return new RegExp(pattern.join(''), 'i');

test/rdoc/rdoc_generator_json_index_searcher_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ class RDocGeneratorJsonIndexSearcherTest < Test::Unit::TestCase
1616
def setup
1717
@context = MiniRacer::Context.new
1818

19-
# Add RegExp.escape polyfill to avoid `RegExp.escape is not a function` error
20-
@context.eval(<<~JS)
21-
RegExp.escape = function(string) {
22-
return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');
23-
};
24-
JS
25-
2619
searcher_js_path = File.expand_path(
2720
'../../lib/rdoc/generator/template/json_index/js/searcher.js',
2821
__dir__

0 commit comments

Comments
 (0)