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: Rework the query selector all Created Jan. 25, 2018, 9:58 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;
139 }
140 return elements;
141 }
142
143 /**
144 * Query selector. If it is relative, will try :scoped.
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()
148 * @returns {Node|NodeList} result of the query. null in case of error.
149 */
150 function scopedQuerySelector(subtree, selector, all)
151 {
152 if (relativeSelectorRegexp.test(selector))
153 {
154 selector = ":scope" + selector;
155 if (scopeSupported)
Manish Jethani 2018/01/26 08:41:32 I think since the code in this if block spans mult
hub 2018/01/26 14:44:45 Done.
156 return all ? subtree.querySelectorAll(selector) :
157 subtree.querySelector(selector);
158 if (scopeSupported == null)
159 return tryQuerySelector(subtree, selector, all);
160 return null;
161 }
162 return all ? subtree.querySelectorAll(selector) :
163 subtree.querySelector(selector);
164 }
165
166 function scopedQuerySelectorAll(subtree, selector)
167 {
168 return scopedQuerySelector(subtree, selector, true);
169 }
170
125 function* evaluate(chain, index, prefix, subtree, styles) 171 function* evaluate(chain, index, prefix, subtree, styles)
126 { 172 {
127 if (index >= chain.length) 173 if (index >= chain.length)
128 { 174 {
129 yield prefix; 175 yield prefix;
130 return; 176 return;
131 } 177 }
132 for (let [selector, element] of 178 for (let [selector, element] of
133 chain[index].getSelectors(prefix, subtree, styles)) 179 chain[index].getSelectors(prefix, subtree, styles))
134 { 180 {
(...skipping 21 matching lines...) Expand all
156 * @param {Node} subtree the subtree we work on. 202 * @param {Node} subtree the subtree we work on.
157 * @param {StringifiedStyle[]} styles the stringified style objects. 203 * @param {StringifiedStyle[]} styles the stringified style objects.
158 */ 204 */
159 *getSelectors(prefix, subtree, styles) 205 *getSelectors(prefix, subtree, styles)
160 { 206 {
161 yield [prefix + this._selector, subtree]; 207 yield [prefix + this._selector, subtree];
162 } 208 }
163 }; 209 };
164 210
165 const incompletePrefixRegexp = /[\s>+~]$/; 211 const incompletePrefixRegexp = /[\s>+~]$/;
166 const relativeSelectorRegexp = /^[>+~]/; 212 const relativeSelectorRegexp = /^[>]/;
Manish Jethani 2018/01/26 08:41:32 Since there's only one character in the group, we
hub 2018/01/26 14:44:45 Done.
167 213
168 function HasSelector(selectors) 214 function HasSelector(selectors)
169 { 215 {
170 this._innerSelectors = selectors; 216 this._innerSelectors = selectors;
171 } 217 }
172 218
173 HasSelector.prototype = { 219 HasSelector.prototype = {
174 requiresHiding: true, 220 requiresHiding: true,
175 221
176 get dependsOnStyles() 222 get dependsOnStyles()
(...skipping 10 matching lines...) Expand all
187 /** 233 /**
188 * Generator function returning selected elements. 234 * Generator function returning selected elements.
189 * @param {string} prefix the prefix for the selector. 235 * @param {string} prefix the prefix for the selector.
190 * @param {Node} subtree the subtree we work on. 236 * @param {Node} subtree the subtree we work on.
191 * @param {StringifiedStyle[]} styles the stringified style objects. 237 * @param {StringifiedStyle[]} styles the stringified style objects.
192 */ 238 */
193 *getElements(prefix, subtree, styles) 239 *getElements(prefix, subtree, styles)
194 { 240 {
195 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
196 prefix + "*" : prefix; 242 prefix + "*" : prefix;
197 let elements = subtree.querySelectorAll(actualPrefix); 243 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
198 for (let element of elements) 244 if (elements)
199 { 245 {
200 let iter = evaluate(this._innerSelectors, 0, "", element, styles); 246 for (let element of elements)
201 for (let selector of iter)
202 { 247 {
203 if (selector == null) 248 let iter = evaluate(this._innerSelectors, 0, "", element, styles);
249 for (let selector of iter)
204 { 250 {
205 yield null; 251 if (selector == null)
206 continue; 252 yield null;
207 } 253 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; 254 yield element;
214 } 255 }
215 catch (e) 256 yield null;
216 {
217 // :scope isn't supported on Edge, ignore error caused by it.
218 }
219 } 257 }
220 yield null;
221 } 258 }
222 } 259 }
223 }; 260 };
224 261
225 function ContainsSelector(textContent) 262 function ContainsSelector(textContent)
226 { 263 {
227 this._text = textContent; 264 this._text = textContent;
228 } 265 }
229 266
230 ContainsSelector.prototype = { 267 ContainsSelector.prototype = {
231 requiresHiding: true, 268 requiresHiding: true,
232 269
233 *getSelectors(prefix, subtree, stylesheet) 270 *getSelectors(prefix, subtree, stylesheet)
234 { 271 {
235 for (let element of this.getElements(prefix, subtree, stylesheet)) 272 for (let element of this.getElements(prefix, subtree, stylesheet))
236 yield [makeSelector(element, ""), subtree]; 273 yield [makeSelector(element, ""), subtree];
237 }, 274 },
238 275
239 *getElements(prefix, subtree, stylesheet) 276 *getElements(prefix, subtree, stylesheet)
240 { 277 {
241 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ? 278 let actualPrefix = (!prefix || incompletePrefixRegexp.test(prefix)) ?
242 prefix + "*" : prefix; 279 prefix + "*" : prefix;
243 let elements = subtree.querySelectorAll(actualPrefix);
244 280
245 for (let element of elements) 281 let elements = scopedQuerySelectorAll(subtree, actualPrefix);
282 if (elements)
246 { 283 {
247 if (element.textContent.includes(this._text)) 284 for (let element of elements)
248 yield element; 285 {
249 else 286 if (element.textContent.includes(this._text))
250 yield null; 287 yield element;
288 else
289 yield null;
290 }
251 } 291 }
252 } 292 }
253 }; 293 };
254 294
255 function PropsSelector(propertyExpression) 295 function PropsSelector(propertyExpression)
256 { 296 {
257 let regexpString; 297 let regexpString;
258 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 298 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
259 propertyExpression[propertyExpression.length - 1] == "/") 299 propertyExpression[propertyExpression.length - 1] == "/")
260 { 300 {
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 characterData: true, 644 characterData: true,
605 subtree: true 645 subtree: true
606 } 646 }
607 ); 647 );
608 this.document.addEventListener("load", this.onLoad.bind(this), true); 648 this.document.addEventListener("load", this.onLoad.bind(this), true);
609 } 649 }
610 } 650 }
611 }; 651 };
612 652
613 exports.ElemHideEmulation = ElemHideEmulation; 653 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