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.

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

mercurial