From 150c231eab817c4e7c3175ef010efc1f44e24d7a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Sat, 1 Nov 2025 08:50:06 +0100 Subject: [PATCH] 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. --- lib/rdoc/generator/template/json_index/js/searcher.js | 6 +++++- test/rdoc/rdoc_generator_json_index_searcher_test.rb | 7 ------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/rdoc/generator/template/json_index/js/searcher.js b/lib/rdoc/generator/template/json_index/js/searcher.js index 8f03f67a93..74accb7460 100644 --- a/lib/rdoc/generator/template/json_index/js/searcher.js +++ b/lib/rdoc/generator/template/json_index/js/searcher.js @@ -57,10 +57,14 @@ Searcher.prototype = new function() { } function buildRegexps(queries) { + // A small minority of older browsers don't have RegExp.escape + // but it's not worth including a complex polyfill. + var escape = RegExp.escape || function(s) { return s }; + return queries.map(function(query) { var pattern = []; for (var i = 0; i < query.length; i++) { - var char = RegExp.escape(query[i]); + var char = escape(query[i]); pattern.push('([' + char + '])([^' + char + ']*?)'); } return new RegExp(pattern.join(''), 'i'); diff --git a/test/rdoc/rdoc_generator_json_index_searcher_test.rb b/test/rdoc/rdoc_generator_json_index_searcher_test.rb index 36613e57c4..065ea04e17 100644 --- a/test/rdoc/rdoc_generator_json_index_searcher_test.rb +++ b/test/rdoc/rdoc_generator_json_index_searcher_test.rb @@ -16,13 +16,6 @@ class RDocGeneratorJsonIndexSearcherTest < Test::Unit::TestCase def setup @context = MiniRacer::Context.new - # Add RegExp.escape polyfill to avoid `RegExp.escape is not a function` error - @context.eval(<<~JS) - RegExp.escape = function(string) { - return string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'); - }; - JS - searcher_js_path = File.expand_path( '../../lib/rdoc/generator/template/json_index/js/searcher.js', __dir__