1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Page Modifications code.
16 * The Initial Developer of the Original Code is Mozilla.
17 * Portions created by the Initial Developer are Copyright (C) 2009
18 * the Initial Developer. All Rights Reserved.
21 * David Dahl <ddahl@mozilla.com>
22 * Drew Willcoxon <adw@mozilla.com>
23 * Erik Vold <erikvvold@gmail.com>
24 * Nils Maier <maierman@web.de>
25 * Anthony Lieuallen <arantius@gmail.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
40 ////////////////////////////////////////////////////////////////////////////////
41 let validSchemes = ["http", "https", "ftp", "file"];
42 let REG_HOST = /^(?:\*\.)?[^*\/]+$|^\*$|^$/;
44 let iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
47 ////////////////////////////////////////////////////////////////////////////////
48 function uri2re (uri) {
49 uri = uri.replace(/^\s+/, "").replace(/\s+$/, "");
50 if (uri == "") return null;
51 if (uri == "*") return null;
53 if (uri.length >= 2 && uri.charCodeAt(0) == 47 && uri.charCodeAt(uri.length-1) == 47) {
54 if (uri.length < 3) return null;
55 return new RegExp(uri.substring(1, uri.length-1), "i");
57 // convert glob to regexp
59 for (let f = 0; f < uri.length; ++f) {
61 case "*": re += ".*?"; break; // any, non-greedy
62 case ".": case "?": case "^": case "$": case "+":
63 case "{": case "}": case "[": case "]": case "|":
64 case "(": case ")": case "\\":
67 case " ": break; // ignore spaces
68 default: re += uri[f]; break;
72 //if (addonOptions.debugCache) conlog("uri:["+uri+"] re: ["+re+"]");
73 return new RegExp(re, "i");
75 exports.uri2re = uri2re;
78 ////////////////////////////////////////////////////////////////////////////////
79 // For the format of "pattern", see:
80 // https://developer.chrome.com/extensions/match_patterns
81 function MatchPattern (pattern) {
82 this._pattern = pattern;
84 // Special case "<all_urls>".
85 if (pattern === "<all_urls>") {
87 this._scheme = "all_urls";
92 // special case wild scheme
93 if (pattern[0] === "*") {
94 this._wildScheme = true;
95 // forge http, to satisfy the URI parser, and get a host
96 pattern = "http"+pattern.substr(1);
101 uri = iosvc.newURI(pattern, null, null);
105 if (!uri) throw new Error("invalid URI in @match");
107 let scheme = (this._wildScheme ? "all" : uri.scheme);
108 if (scheme != "all" && validSchemes.indexOf(scheme) == -1) throw new Error("invalid scheme in @match");
111 if (!REG_HOST.test(host)) throw new Error("invalid host in @match");
114 if (path[0] !== "/") throw new Error("invalid path in @match");
116 this._scheme = scheme;
119 // We have to manually create the hostname regexp (instead of using
120 // GM_convert2RegExp) to properly handle *.example.tld, which should match
121 // example.tld and any of its subdomains, but not anotherexample.tld.
122 this._hostExpr = new RegExp("^"+
123 // Two characters in the host portion need special treatment:
124 // - ". should not be treated as a wildcard, so we escape it to \.
125 // - if the hostname only consists of "*" (i.e. full wildcard),
126 // replace it with .*
127 host.replace(/\./g, "\\.").replace(/^\*$/, ".*").
128 // Then, handle the special case of "*." (any or no subdomain) for match
129 // patterns. "*." has been escaped to "*\." by the replace above.
130 replace("*\\.", "(.*\\.)?")+"$", "i");
132 // if omitted, then it means "", an alias for localhost
133 this._hostExpr = /^$/;
135 this._pathExpr = uri2re(path);
138 MatchPattern.prototype = {
139 get pattern () (""+this._pattern),
140 get all () this._all,
142 doMatch: function (uriSpec) {
143 if (this._all) return true;
145 let uri = iosvc.newURI(pattern, null, null);
147 if (!this._wildScheme && this._scheme != uri.scheme) return false;
148 return (this._hostExpr.test(uri.host) && this._pathExpr.test(uri.path));
150 return false; // any error? no match
153 exports.MatchPattern = MatchPattern;