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: addressed comments Created Jan. 26, 2018, 2:44 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 scopeSupported = false;
Manish Jethani 2018/01/30 05:53:41 Maybe we should add that comment here saying that
hub 2018/01/30 16:03:57 Done.
139 }
140 return elements;
141 }
142
143 /**
144 * Query selector. If it is relative, will try :scoped.
Manish Jethani 2018/01/30 05:53:41 You meant ":scope" (without the D)?
hub 2018/01/30 16:03:57 Done.
145 * @param {Node} subtree the element to query selector
146 * @param {string} selector the selector to query
147 * @param {bool} all True to perform querySelectorAll()
Manish Jethani 2018/01/30 05:53:41 Since all is optional and defaults to false, I gue
hub 2018/01/30 16:03:57 Done.
148 * @returns {Node|NodeList} result of the query. null in case of error.
Manish Jethani 2018/01/30 05:53:41 Since this can return null I think it should be "{
lainverse 2018/01/30 15:28:29 BTW, wouldn't it be better to return empty array w
hub 2018/01/30 16:03:57 Done.
149 */
150 function scopedQuerySelector(subtree, selector, all)
151 {
152 if (relativeSelectorRegexp.test(selector))
153 {
154 selector = ":scope" + selector;
155 if (scopeSupported)
156 {
157 return all ? subtree.querySelectorAll(selector) :
158 subtree.querySelector(selector);
159 }
160 if (scopeSupported == null)
161 return tryQuerySelector(subtree, selector, all);
162 return null;
163 }
164 return all ? subtree.querySelectorAll(selector) :
165 subtree.querySelector(selector);
166 }
167
168 function scopedQuerySelectorAll(subtree, selector)
169 {
170 return scopedQuerySelector(subtree, selector, true);
171 }
172
125 function* evaluate(chain, index, prefix, subtree, styles) 173 function* evaluate(chain, index, prefix, subtree, styles)
126 { 174 {
127 if (index >= chain.length) 175 if (index >= chain.length)
128 { 176 {
129 yield prefix; 177 yield prefix;
130 return; 178 return;
131 } 179 }
132 for (let [selector, element] of 180 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 181 chain[index].getSelectors(prefix, subtree, styles))
134 { 182 {
(...skipping 21 matching lines...) Expand all
156 * @param {Node} subtree the subtree we work on. 204 * @param {Node} subtree the subtree we work on.
157 * @param {StringifiedStyle[]} styles the stringified style objects. 205 * @param {StringifiedStyle[]} styles the stringified style objects.
158 */ 206 */
159 *getSelectors(prefix, subtree, styles) 207 *getSelectors(prefix, subtree, styles)
160 { 208 {
161 yield [prefix + this._selector, subtree]; 209 yield [prefix + this._selector, subtree];
162 } 210 }
163 }; 211 };
164 212
165 const incompletePrefixRegexp = /[\s>+~]$/; 213 const incompletePrefixRegexp = /[\s>+~]$/;
166 const relativeSelectorRegexp = /^[>+~]/; 214 const relativeSelectorRegexp = /^>/;
Manish Jethani 2018/01/30 06:13:06 So I ran this on Chrome and Firefox: { let u =
hub 2018/01/30 16:03:57 Done.
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