addon-sdk/source/lib/sdk/util/match-pattern.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 module.metadata = {
michael@0 8 "stability": "unstable"
michael@0 9 };
michael@0 10
michael@0 11 const { URL } = require('../url');
michael@0 12 const cache = {};
michael@0 13
michael@0 14 function MatchPattern(pattern) {
michael@0 15 if (cache[pattern]) return cache[pattern];
michael@0 16
michael@0 17 if (typeof pattern.test == "function") {
michael@0 18
michael@0 19 // For compatibility with -moz-document rules, we require the RegExp's
michael@0 20 // global, ignoreCase, and multiline flags to be set to false.
michael@0 21 if (pattern.global) {
michael@0 22 throw new Error("A RegExp match pattern cannot be set to `global` " +
michael@0 23 "(i.e. //g).");
michael@0 24 }
michael@0 25 if (pattern.ignoreCase) {
michael@0 26 throw new Error("A RegExp match pattern cannot be set to `ignoreCase` " +
michael@0 27 "(i.e. //i).");
michael@0 28 }
michael@0 29 if (pattern.multiline) {
michael@0 30 throw new Error("A RegExp match pattern cannot be set to `multiline` " +
michael@0 31 "(i.e. //m).");
michael@0 32 }
michael@0 33
michael@0 34 this.regexp = pattern;
michael@0 35 }
michael@0 36 else {
michael@0 37 let firstWildcardPosition = pattern.indexOf("*");
michael@0 38 let lastWildcardPosition = pattern.lastIndexOf("*");
michael@0 39 if (firstWildcardPosition != lastWildcardPosition)
michael@0 40 throw new Error("There can be at most one '*' character in a wildcard.");
michael@0 41
michael@0 42 if (firstWildcardPosition == 0) {
michael@0 43 if (pattern.length == 1)
michael@0 44 this.anyWebPage = true;
michael@0 45 else if (pattern[1] != ".")
michael@0 46 throw new Error("Expected a *.<domain name> string, got: " + pattern);
michael@0 47 else
michael@0 48 this.domain = pattern.substr(2);
michael@0 49 }
michael@0 50 else {
michael@0 51 if (pattern.indexOf(":") == -1) {
michael@0 52 throw new Error("When not using *.example.org wildcard, the string " +
michael@0 53 "supplied is expected to be either an exact URL to " +
michael@0 54 "match or a URL prefix. The provided string ('" +
michael@0 55 pattern + "') is unlikely to match any pages.");
michael@0 56 }
michael@0 57
michael@0 58 if (firstWildcardPosition == -1)
michael@0 59 this.exactURL = pattern;
michael@0 60 else if (firstWildcardPosition == pattern.length - 1)
michael@0 61 this.urlPrefix = pattern.substr(0, pattern.length - 1);
michael@0 62 else {
michael@0 63 throw new Error("The provided wildcard ('" + pattern + "') has a '*' " +
michael@0 64 "in an unexpected position. It is expected to be the " +
michael@0 65 "first or the last character in the wildcard.");
michael@0 66 }
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 cache[pattern] = this;
michael@0 71 }
michael@0 72
michael@0 73 MatchPattern.prototype = {
michael@0 74
michael@0 75 test: function MatchPattern_test(urlStr) {
michael@0 76 try {
michael@0 77 var url = URL(urlStr);
michael@0 78 }
michael@0 79 catch (err) {
michael@0 80 return false;
michael@0 81 }
michael@0 82
michael@0 83 // Test the URL against a RegExp pattern. For compatibility with
michael@0 84 // -moz-document rules, we require the RegExp to match the entire URL,
michael@0 85 // so we not only test for a match, we also make sure the matched string
michael@0 86 // is the entire URL string.
michael@0 87 //
michael@0 88 // Assuming most URLs don't match most match patterns, we call `test` for
michael@0 89 // speed when determining whether or not the URL matches, then call `exec`
michael@0 90 // for the small subset that match to make sure the entire URL matches.
michael@0 91 //
michael@0 92 if (this.regexp && this.regexp.test(urlStr) &&
michael@0 93 this.regexp.exec(urlStr)[0] == urlStr)
michael@0 94 return true;
michael@0 95
michael@0 96 if (this.anyWebPage && /^(https?|ftp)$/.test(url.scheme))
michael@0 97 return true;
michael@0 98 if (this.exactURL && this.exactURL == urlStr)
michael@0 99 return true;
michael@0 100
michael@0 101 // Tests the urlStr against domain and check if
michael@0 102 // wildcard submitted (*.domain.com), it only allows
michael@0 103 // subdomains (sub.domain.com) or from the root (http://domain.com)
michael@0 104 // and reject non-matching domains (otherdomain.com)
michael@0 105 // bug 856913
michael@0 106 if (this.domain && url.host &&
michael@0 107 (url.host === this.domain ||
michael@0 108 url.host.slice(-this.domain.length - 1) === "." + this.domain))
michael@0 109 return true;
michael@0 110 if (this.urlPrefix && 0 == urlStr.indexOf(this.urlPrefix))
michael@0 111 return true;
michael@0 112
michael@0 113 return false;
michael@0 114 },
michael@0 115
michael@0 116 toString: function () '[object MatchPattern]'
michael@0 117
michael@0 118 };
michael@0 119
michael@0 120 exports.MatchPattern = MatchPattern;

mercurial