|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 module.metadata = { |
|
8 "stability": "unstable" |
|
9 }; |
|
10 |
|
11 const { URL } = require('../url'); |
|
12 const cache = {}; |
|
13 |
|
14 function MatchPattern(pattern) { |
|
15 if (cache[pattern]) return cache[pattern]; |
|
16 |
|
17 if (typeof pattern.test == "function") { |
|
18 |
|
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 } |
|
33 |
|
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."); |
|
41 |
|
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 } |
|
57 |
|
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 } |
|
69 |
|
70 cache[pattern] = this; |
|
71 } |
|
72 |
|
73 MatchPattern.prototype = { |
|
74 |
|
75 test: function MatchPattern_test(urlStr) { |
|
76 try { |
|
77 var url = URL(urlStr); |
|
78 } |
|
79 catch (err) { |
|
80 return false; |
|
81 } |
|
82 |
|
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; |
|
95 |
|
96 if (this.anyWebPage && /^(https?|ftp)$/.test(url.scheme)) |
|
97 return true; |
|
98 if (this.exactURL && this.exactURL == urlStr) |
|
99 return true; |
|
100 |
|
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; |
|
112 |
|
113 return false; |
|
114 }, |
|
115 |
|
116 toString: function () '[object MatchPattern]' |
|
117 |
|
118 }; |
|
119 |
|
120 exports.MatchPattern = MatchPattern; |