code review: use regex to speed up CSS selector validation

This commit is contained in:
Raymond Hill 2018-04-05 09:45:11 -04:00
parent f55144a179
commit c1d3b6222e
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 5 additions and 1 deletions

View File

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uBlock Origin - a browser extension to block requests. uBlock Origin - a browser extension to block requests.
Copyright (C) 2017 Raymond Hill Copyright (C) 2017-2018 Raymond Hill
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -94,7 +94,11 @@
} catch (ex) { } catch (ex) {
matchesFn = div.querySelector.bind(div); matchesFn = div.querySelector.bind(div);
} }
// Quick regex-based validation -- most cosmetic filters are of the
// simple form and in such case a regex is much faster.
var reSimple = /^[#.][\w-]+$/;
return function(s) { return function(s) {
if ( reSimple.test(s) ) { return true; }
try { try {
matchesFn(s + ', ' + s + ':not(#foo)'); matchesFn(s + ', ' + s + ':not(#foo)');
} catch (ex) { } catch (ex) {