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

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

Issue 29383960: Issue 3143 - Filter elements with :-abp-has() (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Created March 14, 2017, 11:20 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 | lib/filterClasses.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 // We are currently limited to ECMAScript 5 in this file, because it is being 1 // We are currently limited to ECMAScript 5 in this file, because it is being
2 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796 2 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796
3 3
4 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/; 4 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/;
5 var pseudoClassHasSelectorRegExp = /:has\((.*)\)/;
5 6
6 function splitSelector(selector) 7 function splitSelector(selector)
7 { 8 {
8 if (selector.indexOf(",") == -1) 9 if (selector.indexOf(",") == -1)
9 return [selector]; 10 return [selector];
10 11
11 var selectors = []; 12 var selectors = [];
12 var start = 0; 13 var start = 0;
13 var level = 0; 14 var level = 0;
14 var sep = ""; 15 var sep = "";
(...skipping 19 matching lines...) Expand all
34 selectors.push(selector.substring(start, i)); 35 selectors.push(selector.substring(start, i));
35 start = i + 1; 36 start = i + 1;
36 } 37 }
37 } 38 }
38 } 39 }
39 40
40 selectors.push(selector.substring(start)); 41 selectors.push(selector.substring(start));
41 return selectors; 42 return selectors;
42 } 43 }
43 44
45 // matcher for the pseudo CSS4 class :has
46 // For those browser that don't have it yet.
47 function PseudoHasMatcher(selector)
48 {
49 this.hasSelector = selector;
50 }
51
52 PseudoHasMatcher.prototype = {
53 match: function(elem)
54 {
55 var matches = [];
56 // look up for all elements that match the :has().
57 var children = elem.children;
58 for (var i = 0; i < children.length; i++)
59 {
60 var hasElem = elem.querySelector(this.hasSelector);
61 if (hasElem != null)
62 {
63 matches.push(hasElem);
64 }
65 }
66 return matches;
67 }
68 };
69
44 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) 70 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc)
45 { 71 {
46 this.window = window; 72 this.window = window;
47 this.getFiltersFunc = getFiltersFunc; 73 this.getFiltersFunc = getFiltersFunc;
48 this.addSelectorsFunc = addSelectorsFunc; 74 this.addSelectorsFunc = addSelectorsFunc;
49 } 75 }
50 76
51 ElemHideEmulation.prototype = { 77 ElemHideEmulation.prototype = {
52 stringifyStyle: function(style) 78 stringifyStyle: function(style)
53 { 79 {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 for (var i = 0; i < rules.length; i++) 116 for (var i = 0; i < rules.length; i++)
91 { 117 {
92 var rule = rules[i]; 118 var rule = rules[i];
93 if (rule.type != rule.STYLE_RULE) 119 if (rule.type != rule.STYLE_RULE)
94 continue; 120 continue;
95 121
96 var style = this.stringifyStyle(rule.style); 122 var style = this.stringifyStyle(rule.style);
97 for (var j = 0; j < this.patterns.length; j++) 123 for (var j = 0; j < this.patterns.length; j++)
98 { 124 {
99 var pattern = this.patterns[j]; 125 var pattern = this.patterns[j];
100 if (pattern.regexp.test(style)) 126 if (pattern.type == "prop" && pattern.regexp.test(style))
101 { 127 {
102 var subSelectors = splitSelector(rule.selectorText); 128 var subSelectors = splitSelector(rule.selectorText);
103 for (var k = 0; k < subSelectors.length; k++) 129 for (var k = 0; k < subSelectors.length; k++)
104 { 130 {
105 var subSelector = subSelectors[k]; 131 var subSelector = subSelectors[k];
106 selectors.push(pattern.prefix + subSelector + pattern.suffix); 132 selectors.push(pattern.prefix + subSelector + pattern.suffix);
107 filters.push(pattern.text); 133 filters.push(pattern.text);
108 } 134 }
109 } 135 }
110 } 136 }
111 } 137 }
138 this.findPseudoClassHasSelectors(selectors, filters);
139 },
140
141 findPseudoClassHasSelectors: function(selectors, filters)
142 {
143 // XXX shall we split the patterns?
144 for (var i = 0; i < this.patterns.length; i++)
145 {
146 var pattern = this.patterns[i];
147 if (pattern.type != ":has")
148 continue;
149
150 var prefixes = document.querySelectorAll(pattern.prefix);
151 for (var j = 0; j < prefixes.length; j++)
152 {
153 var matched = pattern.elementMatcher.match(prefixes[j]);
154 if (matched.length == 0)
155 {
156 continue;
157 }
158 // XXX make sure we don't have performance problems here
159 matched.forEach(function(e)
160 {
161 var subSelector = e.id;
162 if (!subSelector)
163 {
164 var findUniqueId = function()
165 {
166 var id = "elemHideEmulationHide-" + Math.floor(Math.random() * 100 00);
167 if (!document.getElementById(id))
168 return id;
169 return findUniqueId();
170 }
171 subSelector = findUniqueId();
172 e.id = subSelector;
173 }
174 var newSelector = pattern.prefix ? pattern.prefix + "> " : "";
175 newSelector += "#" + subSelector;
176 newSelector += pattern.suffix ? " > " + pattern.suffix : "";
177 selectors.push(newSelector);
178 filters.push(pattern.text);
179 });
180 }
181 }
112 }, 182 },
113 183
114 addSelectors: function(stylesheets) 184 addSelectors: function(stylesheets)
115 { 185 {
116 var selectors = []; 186 var selectors = [];
117 var filters = []; 187 var filters = [];
118 for (var i = 0; i < stylesheets.length; i++) 188 for (var i = 0; i < stylesheets.length; i++)
119 this.findSelectors(stylesheets[i], selectors, filters); 189 this.findSelectors(stylesheets[i], selectors, filters);
120 this.addSelectorsFunc(selectors, filters); 190 this.addSelectorsFunc(selectors, filters);
121 }, 191 },
122 192
123 onLoad: function(event) 193 onLoad: function(event)
124 { 194 {
125 var stylesheet = event.target.sheet; 195 var stylesheet = event.target.sheet;
126 if (stylesheet) 196 if (stylesheet)
127 this.addSelectors([stylesheet]); 197 this.addSelectors([stylesheet]);
128 }, 198 },
129 199
130 apply: function() 200 apply: function()
131 { 201 {
132 this.getFiltersFunc(function(patterns) 202 this.getFiltersFunc(function(patterns)
133 { 203 {
134 this.patterns = []; 204 this.patterns = [];
135 for (var i = 0; i < patterns.length; i++) 205 for (var i = 0; i < patterns.length; i++)
136 { 206 {
207 var regexpMatcher;
208 var elementMatcher;
137 var pattern = patterns[i]; 209 var pattern = patterns[i];
138 var match = propertySelectorRegExp.exec(pattern.selector); 210 var match = propertySelectorRegExp.exec(pattern.selector);
139 if (!match) 211 if (match)
140 continue; 212 {
213 var regexpString;
214 var propertyExpression = match[2];
215 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" &&
216 propertyExpression[propertyExpression.length - 1] == "/")
217 regexpString = propertyExpression.slice(1, -1)
218 .replace("\\x7B ", "{").replace("\\x7D ", "}");
219 else
220 regexpString = filterToRegExp(propertyExpression);
221 regexpMatcher = new RegExp(regexpString, "i");
222 type = 'prop';
223 }
224 else
225 {
226 match = pseudoClassHasSelectorRegExp.exec(pattern.selector);
227 if (!match)
228 continue;
141 229
142 var propertyExpression = match[2]; 230 var pseudoHasSelector = match[1];
143 var regexpString; 231 elementMatcher = new PseudoHasMatcher(pseudoHasSelector);
144 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && 232 type = ':has';
145 propertyExpression[propertyExpression.length - 1] == "/") 233 }
146 regexpString = propertyExpression.slice(1, -1)
147 .replace("\\x7B ", "{").replace("\\x7D ", "}");
148 else
149 regexpString = filterToRegExp(propertyExpression);
150 234
151 this.patterns.push({ 235 this.patterns.push({
236 type: type,
152 text: pattern.text, 237 text: pattern.text,
153 regexp: new RegExp(regexpString, "i"), 238 regexp: regexpMatcher,
239 elementMatcher: elementMatcher,
154 prefix: pattern.selector.substr(0, match.index), 240 prefix: pattern.selector.substr(0, match.index),
155 suffix: pattern.selector.substr(match.index + match[0].length) 241 suffix: pattern.selector.substr(match.index + match[0].length)
156 }); 242 });
157 } 243 }
158 244
159 if (this.patterns.length > 0) 245 if (this.patterns.length > 0)
160 { 246 {
161 var document = this.window.document; 247 var document = this.window.document;
162 this.addSelectors(document.styleSheets); 248 this.addSelectors(document.styleSheets);
163 document.addEventListener("load", this.onLoad.bind(this), true); 249 document.addEventListener("load", this.onLoad.bind(this), true);
164 } 250 }
165 }.bind(this)); 251 }.bind(this));
166 } 252 }
167 }; 253 };
OLDNEW
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld