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

Side by Side Diff: lib/content/elemHideEmulation.js

Issue 29676761: Issue 6296 - Handle relative prefix in :-abp-has() (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: No longer use regexp for relative selector. Created Jan. 30, 2018, 4:03 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/browser/elemHideEmulation.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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 let priority = rule.style.getPropertyPriority(property); 115 let priority = rule.style.getPropertyPriority(property);
116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`); 116 styles.push(`${property}: ${value}${priority ? " !" + priority : ""};`);
117 } 117 }
118 styles.sort(); 118 styles.sort();
119 return { 119 return {
120 style: styles.join(" "), 120 style: styles.join(" "),
121 subSelectors: splitSelector(rule.selectorText) 121 subSelectors: splitSelector(rule.selectorText)
122 }; 122 };
123 } 123 }
124 124
125 let scopeSupported = null;
126
127 function tryQuerySelector(subtree, selector, all)
128 {
129 let elements = null;
130 try
131 {
132 elements = all ? subtree.querySelectorAll(selector) :
133 subtree.querySelector(selector);
134 scopeSupported = true;
135 }
136 catch (e)
137 {
138 // Edge doesn't support ":scope"
139 scopeSupported = false;
140 }
141 return elements;
142 }
143
144 /**
145 * Query selector. If it is relative, will try :scope.
146 * @param {Node} subtree the element to query selector
147 * @param {string} selector the selector to query
148 * @param {bool} [all=false] true to perform querySelectorAll()
149 * @returns {?(Node|NodeList)} result of the query. null in case of error.
150 */
151 function scopedQuerySelector(subtree, selector, all)
152 {
153 if (selector[0] == ">")
154 {
155 selector = ":scope" + selector;
156 if (scopeSupported)
157 {
158 return all ? subtree.querySelectorAll(selector) :
159 subtree.querySelector(selector);
160 }
161 if (scopeSupported == null)
162 return tryQuerySelector(subtree, selector, all);
163 return null;
164 }
165 return all ? subtree.querySelectorAll(selector) :
166 subtree.querySelector(selector);
167 }
168
169 function scopedQuerySelectorAll(subtree, selector)
170 {
171 return scopedQuerySelector(subtree, selector, true);
172 }
173
125 function* evaluate(chain, index, prefix, subtree, styles) 174 function* evaluate(chain, index, prefix, subtree, styles)
126 { 175 {
127 if (index >= chain.length) 176 if (index >= chain.length)
128 { 177 {
129 yield prefix; 178 yield prefix;
130 return; 179 return;
131 } 180 }
132 for (let [selector, element] of 181 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 182 chain[index].getSelectors(prefix, subtree, styles))
134 { 183 {
(...skipping 20 matching lines...) Expand all
155 * @param {string} prefix the prefix for the selector. 204 * @param {string} prefix the prefix for the selector.
156 * @param {Node} subtree the subtree we work on. 205 * @param {Node} subtree the subtree we work on.
157 * @param {StringifiedStyle[]} styles the stringified style objects. 206 * @param {StringifiedStyle[]} styles the stringified style objects.
158 */ 207 */
159 *getSelectors(prefix, subtree, styles) 208 *getSelectors(prefix, subtree, styles)
160 { 209 {
161 yield [prefix + this._selector, subtree]; 210 yield [prefix + this._selector, subtree];
162 } 211 }
163 }; 212 };
164 213
165 const incompletePrefixRegexp = /[\s>+~]$/; 214 const incompletePrefixRegexp = /[\s>+~]$/;
lainverse 2018/01/30 18:07:28 Since that other regexp were replaced with string
Manish Jethani 2018/01/30 20:23:20 If we do this then I think that it should be a sep
166 const relativeSelectorRegexp = /^[>+~]/;
167 215
168 function HasSelector(selectors) 216 function HasSelector(selectors)
169 { 217 {
170 this._innerSelectors = selectors; 218 this._innerSelectors = selectors;
171 } 219 }
172 220
173 HasSelector.prototype = { 221 HasSelector.prototype = {
174 requiresHiding: true, 222 requiresHiding: true,
175 223
176 get dependsOnStyles() 224 get dependsOnStyles()
(...skipping 10 matching lines...) Expand all
187 /** 235 /**
188 * Generator function returning selected elements. 236 * Generator function returning selected elements.
189 * @param {string} prefix the prefix for the selector. 237 * @param {string} prefix the prefix for the selector.
190 * @param {Node} subtree the subtree we work on. 238 * @param {Node} subtree the subtree we work on.
191 * @param {StringifiedStyle[]} styles the stringified style objects. 239 * @param {StringifiedStyle[]} styles the stringified style objects.
192 */ 240 */
193 *getElements(prefix, subtree, styles) 241 *getElements(prefix, subtree, styles)
194 { 242 {
195 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 243 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
196 prefix + "*" : prefix; 244 prefix + "*" : prefix;
197 let elements = subtree.querySelectorAll(actualPrefix); 245 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
198 for (let element of elements) 246 if (elements)
199 { 247 {
200 let iter = evaluate(this._innerSelectors, 0, "", element, styles); 248 for (let element of elements)
201 for (let selector of iter)
202 { 249 {
203 if (selector == null) 250 let iter = evaluate(this._innerSelectors, 0, "", element, styles);
251 for (let selector of iter)
204 { 252 {
205 yield null; 253 if (selector == null)
206 continue; 254 yield null;
207 } 255 else if (scopedQuerySelector(element, selector))
208 if (relativeSelectorRegexp.test(selector))
209 selector = ":scope" + selector;
210 try
211 {
212 if (element.querySelector(selector))
213 yield element; 256 yield element;
214 } 257 }
215 catch (e) 258 yield null;
216 {
217 // :scope isn't supported on Edge, ignore error caused by it.
218 }
219 } 259 }
220 yield null;
221 } 260 }
222 } 261 }
223 }; 262 };
224 263
225 function ContainsSelector(textContent) 264 function ContainsSelector(textContent)
226 { 265 {
227 this._text = textContent; 266 this._text = textContent;
228 } 267 }
229 268
230 ContainsSelector.prototype = { 269 ContainsSelector.prototype = {
231 requiresHiding: true, 270 requiresHiding: true,
232 271
233 *getSelectors(prefix, subtree, stylesheet) 272 *getSelectors(prefix, subtree, stylesheet)
234 { 273 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 274 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 275 yield [makeSelector(element, ""), subtree];
237 }, 276 },
238 277
239 *getElements(prefix, subtree, stylesheet) 278 *getElements(prefix, subtree, stylesheet)
240 { 279 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 280 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 281 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix);
244 282
245 for (let element of elements) 283 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
284 if (elements)
246 { 285 {
247 if (element.textContent.includes(this._text)) 286 for (let element of elements)
248 yield element; 287 {
249 else 288 if (element.textContent.includes(this._text))
250 yield null; 289 yield element;
290 else
291 yield null;
292 }
251 } 293 }
252 } 294 }
253 }; 295 };
254 296
255 function PropsSelector(propertyExpression) 297 function PropsSelector(propertyExpression)
256 { 298 {
257 let regexpString; 299 let regexpString;
258 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 300 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
259 propertyExpression[propertyExpression.length - 1] == "/") 301 propertyExpression[propertyExpression.length - 1] == "/")
260 { 302 {
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 characterData: true, 646 characterData: true,
605 subtree: true 647 subtree: true
606 } 648 }
607 ); 649 );
608 this.document.addEventListener("load", this.onLoad.bind(this), true); 650 this.document.addEventListener("load", this.onLoad.bind(this), true);
609 } 651 }
610 } 652 }
611 }; 653 };
612 654
613 exports.ElemHideEmulation = ElemHideEmulation; 655 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« no previous file with comments | « no previous file | test/browser/elemHideEmulation.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld