Skip to content

Commit

Permalink
Catch errors when search term is an invalid regular expression
Browse files Browse the repository at this point in the history
On error search escape special characters, with only '.' matching
any characters.
  • Loading branch information
MoonE committed Feb 21, 2020
1 parent d56513b commit 2b8582f
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion config/jsdoc/api/template/static/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
$(function () {

// Allow user configuration?
const allowRegex = true;

// Search Items
$('#include_modules').change(function (e) {
console.log('change');
Expand All @@ -9,6 +13,29 @@ $(function () {
}
});

const reNotCache = {};
function escapeRegexp(s, not) {
if (not) {
let re = reNotCache[not];
if (!re) {
const characters = '-\/\\^$*+?.()|[\]{}'.replace(new RegExp('[' + escapeRegexp(not) + ']', 'g'), '');
re = reNotCache[not] = new RegExp('[' + escapeRegexp(characters) + ']', 'g');
}
return s.replace(re, '\\$&');
}
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

function constructRegex(searchTerm, makeRe, allowRegex) {
try {
if (allowRegex) {
return makeRe(searchTerm);
}
} catch (e) {
}
return makeRe(escapeRegexp(searchTerm, '.'));
}

var getSearchWeight = function (searchTerm, $matchedItem) {
let weight = 0;
// We could get smarter on the weight here
Expand Down Expand Up @@ -46,7 +73,9 @@ $(function () {
var $el = $('.navigation');

if (searchTerm.length > 1) {
var regexp = new RegExp(searchTerm, 'i');
const regexp = constructRegex(searchTerm, function (searchTerm) {
return new RegExp(searchTerm, 'i');
}, allowRegex);
$el.find('li, .member-list').hide();

$el.find('li').each(function (i, v) {
Expand Down

0 comments on commit 2b8582f

Please sign in to comment.