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

Side by Side Diff: lib/matcher.js

Issue 29965585: Issue 6992 - Try all keyword candidates when removing a filter (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Dec. 19, 2018, 5:56 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 | « no previous file | test/matcher.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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 { 103 {
104 set.add(filter); 104 set.add(filter);
105 } 105 }
106 } 106 }
107 107
108 /** 108 /**
109 * Removes a filter by a given keyword from a map. 109 * Removes a filter by a given keyword from a map.
110 * @param {RegExpFilter} filter 110 * @param {RegExpFilter} filter
111 * @param {string} keyword 111 * @param {string} keyword
112 * @param {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} map 112 * @param {Map.<string,(RegExpFilter|Set.<RegExpFilter>)>} map
113 * @returns {boolean} Whether the filter was removed.
113 */ 114 */
114 function removeFilterByKeyword(filter, keyword, map) 115 function removeFilterByKeyword(filter, keyword, map)
115 { 116 {
116 let set = map.get(keyword); 117 let set = map.get(keyword);
117 if (typeof set == "undefined") 118 if (typeof set == "undefined")
118 return; 119 return false;
119 120
120 if (set.size == 1) 121 if (set.size == 1)
121 { 122 {
122 if (filter == set) 123 if (filter != set)
123 map.delete(keyword); 124 return false;
125
126 map.delete(keyword);
124 } 127 }
125 else 128 else
126 { 129 {
130 let count = set.size;
131
127 set.delete(filter); 132 set.delete(filter);
128 133
134 if (set.size == count)
135 return false;
136
129 if (set.size == 1) 137 if (set.size == 1)
130 map.set(keyword, [...set][0]); 138 map.set(keyword, [...set][0]);
131 } 139 }
140
141 return true;
132 } 142 }
133 143
134 /** 144 /**
135 * Checks whether a particular filter is slow. 145 * Checks whether a particular filter is slow.
136 * @param {RegExpFilter} filter 146 * @param {RegExpFilter} filter
137 * @returns {boolean} 147 * @returns {boolean}
138 */ 148 */
139 function isSlowFilter(filter) 149 function isSlowFilter(filter)
140 { 150 {
141 return !filter.pattern || !keywordRegExp.test(filter.pattern); 151 return !filter.pattern || !keywordRegExp.test(filter.pattern);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 addFilterByKeyword(filter, keyword, map); 219 addFilterByKeyword(filter, keyword, map);
210 } 220 }
211 } 221 }
212 222
213 /** 223 /**
214 * Removes a filter from the matcher 224 * Removes a filter from the matcher
215 * @param {RegExpFilter} filter 225 * @param {RegExpFilter} filter
216 */ 226 */
217 remove(filter) 227 remove(filter)
218 { 228 {
219 let keyword = this.findKeyword(filter); 229 let keyword = null;
230 let candidates = null;
231
232 let {pattern} = filter;
233 if (pattern == null)
234 {
235 candidates = [""];
236 }
237 else
238 {
239 candidates = pattern.toLowerCase().match(allKeywordsRegExp);
240 if (!candidates)
241 candidates = [""];
242 }
243
220 let locationOnly = filter.isLocationOnly(); 244 let locationOnly = filter.isLocationOnly();
221 245
222 removeFilterByKeyword(filter, keyword, 246 // The keyword used to add a filter depends on the current list of filters
223 locationOnly ? this._simpleFiltersByKeyword : 247 // at the time of addition. At this point we don't know what keyword was
224 this._complexFiltersByKeyword); 248 // used to add the filter. We must try all the candidates.
249 for (let candidate of candidates)
250 {
251 candidate = candidate.substring(1);
225 252
226 if (locationOnly) 253 if (removeFilterByKeyword(filter, candidate,
254 locationOnly ? this._simpleFiltersByKeyword :
255 this._complexFiltersByKeyword))
256 {
257 keyword = candidate;
258 break;
259 }
260 }
261
262 if (locationOnly || keyword == null)
227 return; 263 return;
228 264
229 for (let type of nonDefaultTypes(filter.contentType)) 265 for (let type of nonDefaultTypes(filter.contentType))
230 { 266 {
231 let map = this._filterMapsByType.get(type); 267 let map = this._filterMapsByType.get(type);
232 if (map) 268 if (map)
233 removeFilterByKeyword(filter, keyword, map); 269 removeFilterByKeyword(filter, keyword, map);
234 } 270 }
235 } 271 }
236 272
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 588
553 exports.CombinedMatcher = CombinedMatcher; 589 exports.CombinedMatcher = CombinedMatcher;
554 590
555 /** 591 /**
556 * Shared {@link CombinedMatcher} instance that should usually be used. 592 * Shared {@link CombinedMatcher} instance that should usually be used.
557 * @type {CombinedMatcher} 593 * @type {CombinedMatcher}
558 */ 594 */
559 let defaultMatcher = new CombinedMatcher(); 595 let defaultMatcher = new CombinedMatcher();
560 596
561 exports.defaultMatcher = defaultMatcher; 597 exports.defaultMatcher = defaultMatcher;
OLDNEW
« no previous file with comments | « no previous file | test/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld