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

Side by Side Diff: lib/matcher.js

Issue 29807560: Issue 6745 - Prefer strict equality operator (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created June 14, 2018, 4:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/filterStorage.js ('k') | lib/notification.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 * @param {RegExpFilter} filter 61 * @param {RegExpFilter} filter
62 */ 62 */
63 add(filter) 63 add(filter)
64 { 64 {
65 if (this.keywordByFilter.has(filter)) 65 if (this.keywordByFilter.has(filter))
66 return; 66 return;
67 67
68 // Look for a suitable keyword 68 // Look for a suitable keyword
69 let keyword = this.findKeyword(filter); 69 let keyword = this.findKeyword(filter);
70 let oldEntry = this.filterByKeyword.get(keyword); 70 let oldEntry = this.filterByKeyword.get(keyword);
71 if (typeof oldEntry == "undefined") 71 if (typeof oldEntry === "undefined")
72 this.filterByKeyword.set(keyword, filter); 72 this.filterByKeyword.set(keyword, filter);
73 else if (oldEntry.length == 1) 73 else if (oldEntry.length === 1)
74 this.filterByKeyword.set(keyword, [oldEntry, filter]); 74 this.filterByKeyword.set(keyword, [oldEntry, filter]);
75 else 75 else
76 oldEntry.push(filter); 76 oldEntry.push(filter);
77 this.keywordByFilter.set(filter, keyword); 77 this.keywordByFilter.set(filter, keyword);
78 }, 78 },
79 79
80 /** 80 /**
81 * Removes a filter from the matcher 81 * Removes a filter from the matcher
82 * @param {RegExpFilter} filter 82 * @param {RegExpFilter} filter
83 */ 83 */
84 remove(filter) 84 remove(filter)
85 { 85 {
86 let keyword = this.keywordByFilter.get(filter); 86 let keyword = this.keywordByFilter.get(filter);
87 if (typeof keyword == "undefined") 87 if (typeof keyword === "undefined")
88 return; 88 return;
89 89
90 let list = this.filterByKeyword.get(keyword); 90 let list = this.filterByKeyword.get(keyword);
91 if (list.length <= 1) 91 if (list.length <= 1)
92 this.filterByKeyword.delete(keyword); 92 this.filterByKeyword.delete(keyword);
93 else 93 else
94 { 94 {
95 let index = list.indexOf(filter); 95 let index = list.indexOf(filter);
96 if (index >= 0) 96 if (index >= 0)
97 { 97 {
98 list.splice(index, 1); 98 list.splice(index, 1);
99 if (list.length == 1) 99 if (list.length === 1)
100 this.filterByKeyword.set(keyword, list[0]); 100 this.filterByKeyword.set(keyword, list[0]);
101 } 101 }
102 } 102 }
103 103
104 this.keywordByFilter.delete(filter); 104 this.keywordByFilter.delete(filter);
105 }, 105 },
106 106
107 /** 107 /**
108 * Chooses a keyword to be associated with the filter 108 * Chooses a keyword to be associated with the filter
109 * @param {Filter} filter 109 * @param {Filter} filter
110 * @return {string} keyword or an empty string if no keyword could be found 110 * @return {string} keyword or an empty string if no keyword could be found
111 */ 111 */
112 findKeyword(filter) 112 findKeyword(filter)
113 { 113 {
114 let result = ""; 114 let result = "";
115 let {text} = filter; 115 let {text} = filter;
116 if (Filter.regexpRegExp.test(text)) 116 if (Filter.regexpRegExp.test(text))
117 return result; 117 return result;
118 118
119 // Remove options 119 // Remove options
120 let match = Filter.optionsRegExp.exec(text); 120 let match = Filter.optionsRegExp.exec(text);
121 if (match) 121 if (match)
122 text = match.input.substr(0, match.index); 122 text = match.input.substr(0, match.index);
123 123
124 // Remove whitelist marker 124 // Remove whitelist marker
125 if (text[0] == "@" && text[1] == "@") 125 if (text[0] === "@" && text[1] === "@")
126 text = text.substr(2); 126 text = text.substr(2);
127 127
128 let candidates = text.toLowerCase().match( 128 let candidates = text.toLowerCase().match(
129 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g 129 /[^a-z0-9%*][a-z0-9%]{3,}(?=[^a-z0-9%*])/g
130 ); 130 );
131 if (!candidates) 131 if (!candidates)
132 return result; 132 return result;
133 133
134 let hash = this.filterByKeyword; 134 let hash = this.filterByKeyword;
135 let resultCount = 0xFFFFFF; 135 let resultCount = 0xFFFFFF;
136 let resultLength = 0; 136 let resultLength = 0;
137 for (let i = 0, l = candidates.length; i < l; i++) 137 for (let i = 0, l = candidates.length; i < l; i++)
138 { 138 {
139 let candidate = candidates[i].substr(1); 139 let candidate = candidates[i].substr(1);
140 let filters = hash.get(candidate); 140 let filters = hash.get(candidate);
141 let count = typeof filters != "undefined" ? filters.length : 0; 141 let count = typeof filters !== "undefined" ? filters.length : 0;
142 if (count < resultCount || 142 if (count < resultCount ||
143 (count == resultCount && candidate.length > resultLength)) 143 (count === resultCount && candidate.length > resultLength))
144 { 144 {
145 result = candidate; 145 result = candidate;
146 resultCount = count; 146 resultCount = count;
147 resultLength = candidate.length; 147 resultLength = candidate.length;
148 } 148 }
149 } 149 }
150 return result; 150 return result;
151 }, 151 },
152 152
153 /** 153 /**
154 * Checks whether a particular filter is being matched against. 154 * Checks whether a particular filter is being matched against.
155 * @param {RegExpFilter} filter 155 * @param {RegExpFilter} filter
156 * @return {boolean} 156 * @return {boolean}
157 */ 157 */
158 hasFilter(filter) 158 hasFilter(filter)
159 { 159 {
160 return this.keywordByFilter.has(filter); 160 return this.keywordByFilter.has(filter);
161 }, 161 },
162 162
163 /** 163 /**
164 * Returns the keyword used for a filter, null for unknown filters. 164 * Returns the keyword used for a filter, null for unknown filters.
165 * @param {RegExpFilter} filter 165 * @param {RegExpFilter} filter
166 * @return {?string} 166 * @return {?string}
167 */ 167 */
168 getKeywordForFilter(filter) 168 getKeywordForFilter(filter)
169 { 169 {
170 let keyword = this.keywordByFilter.get(filter); 170 let keyword = this.keywordByFilter.get(filter);
171 return typeof keyword != "undefined" ? keyword : null; 171 return typeof keyword !== "undefined" ? keyword : null;
172 }, 172 },
173 173
174 /** 174 /**
175 * Checks whether the entries for a particular keyword match a URL 175 * Checks whether the entries for a particular keyword match a URL
176 * @param {string} keyword 176 * @param {string} keyword
177 * @param {string} location 177 * @param {string} location
178 * @param {number} typeMask 178 * @param {number} typeMask
179 * @param {string} docDomain 179 * @param {string} docDomain
180 * @param {boolean} thirdParty 180 * @param {boolean} thirdParty
181 * @param {string} sitekey 181 * @param {string} sitekey
182 * @param {boolean} specificOnly 182 * @param {boolean} specificOnly
183 * @return {?Filter} 183 * @return {?Filter}
184 */ 184 */
185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, 185 _checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey,
186 specificOnly) 186 specificOnly)
187 { 187 {
188 let list = this.filterByKeyword.get(keyword); 188 let list = this.filterByKeyword.get(keyword);
189 if (typeof list == "undefined") 189 if (typeof list === "undefined")
190 return null; 190 return null;
191 for (let i = 0; i < list.length; i++) 191 for (let i = 0; i < list.length; i++)
192 { 192 {
193 let filter = list[i]; 193 let filter = list[i];
194 194
195 if (specificOnly && filter.isGeneric() && 195 if (specificOnly && filter.isGeneric() &&
196 !(filter instanceof WhitelistFilter)) 196 !(filter instanceof WhitelistFilter))
197 continue; 197 continue;
198 198
199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) 199 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey))
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 /** 405 /**
406 * @see Matcher#matchesAny 406 * @see Matcher#matchesAny
407 * @inheritdoc 407 * @inheritdoc
408 */ 408 */
409 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) 409 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly)
410 { 410 {
411 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + 411 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty +
412 " " + sitekey + " " + specificOnly; 412 " " + sitekey + " " + specificOnly;
413 413
414 let result = this.resultCache.get(key); 414 let result = this.resultCache.get(key);
415 if (typeof result != "undefined") 415 if (typeof result !== "undefined")
416 return result; 416 return result;
417 417
418 result = this.matchesAnyInternal(location, typeMask, docDomain, 418 result = this.matchesAnyInternal(location, typeMask, docDomain,
419 thirdParty, sitekey, specificOnly); 419 thirdParty, sitekey, specificOnly);
420 420
421 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries) 421 if (this.resultCache.size >= CombinedMatcher.maxCacheEntries)
422 this.resultCache.clear(); 422 this.resultCache.clear();
423 423
424 this.resultCache.set(key, result); 424 this.resultCache.set(key, result);
425 425
426 return result; 426 return result;
427 } 427 }
428 }; 428 };
429 429
430 /** 430 /**
431 * Shared CombinedMatcher instance that should usually be used. 431 * Shared CombinedMatcher instance that should usually be used.
432 * @type {CombinedMatcher} 432 * @type {CombinedMatcher}
433 */ 433 */
434 exports.defaultMatcher = new CombinedMatcher(); 434 exports.defaultMatcher = new CombinedMatcher();
OLDNEW
« no previous file with comments | « lib/filterStorage.js ('k') | lib/notification.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld