Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/common.js

Issue 29912636: Issue 7052 - Use string-based matching for literal patterns (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Remove unnecessary comment Created Oct. 19, 2018, 9:52 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/common.js
===================================================================
--- a/lib/common.js
+++ b/lib/common.js
@@ -13,16 +13,63 @@
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
/**
+ * Regular expression used to match the <code>||</code> prefix in an otherwise
+ * literal pattern.
+ * @type {RegExp}
+ */
+let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$");
+
+/**
+ * Checks whether the given pattern is a string of literal characters with no
+ * wildcards or any other special characters. If the pattern is prefixed with a
+ * <code>||</code> but otherwise contains no special characters, it is still
+ * considered to be a literal pattern.
+ * @param {string} pattern
+ * @returns {boolean}
+ */
+function isLiteralPattern(pattern)
+{
+ return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
+}
+
+exports.isLiteralPattern = isLiteralPattern;
+
+/**
+ * Checks whether the given URL matches the given pattern.
+ * @param {string} location The URL to check.
+ * @param {string} pattern The pattern to match. This may be prefixed with a
+ * <code>||</code> to match the beginning of the URL. Any other characters
+ * are treated literally.
+ * @returns {boolean}
+ */
+function locationMatchesPattern(location, pattern)
+{
+ if (pattern[0] == "|" && pattern[1] == "|")
+ {
+ let index = location.indexOf(pattern.substring(2));
+
+ // The "||" prefix requires that the text that follows does not start with
+ // a forward slash.
+ return index != -1 && location[index] != "/" &&
+ doubleAnchorRegExp.test(location.substring(0, index));
+ }
+
+ return location.includes(pattern);
+}
+
+exports.locationMatchesPattern = locationMatchesPattern;
+
+/**
* Converts raw text into a regular expression string
* @param {string} text the string to convert
* @return {string} regular expression representation of the text
*/
function textToRegExp(text)
{
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
}
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld