Left: | ||
Right: |
OLD | NEW |
---|---|
1 function splitSelector(selector) | 1 function splitSelector(selector) |
2 { | 2 { |
3 if (selector.indexOf(",") == -1) | 3 if (selector.indexOf(",") == -1) |
4 return [selector]; | 4 return [selector]; |
5 | 5 |
6 var selectors = []; | 6 var selectors = []; |
7 var start = 0; | 7 var start = 0; |
8 var level = 0; | 8 var level = 0; |
9 var sep = ""; | 9 var sep = ""; |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... | |
29 selectors.push(selector.substring(start, i)); | 29 selectors.push(selector.substring(start, i)); |
30 start = i + 1; | 30 start = i + 1; |
31 } | 31 } |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 selectors.push(selector.substring(start)); | 35 selectors.push(selector.substring(start)); |
36 return selectors; | 36 return selectors; |
37 } | 37 } |
38 | 38 |
39 function CSSPropertyFilters(window, getFiltersFunc, addSelectorsFunc) | 39 function ElemHideEmulation(window, getFiltersFunc, addSelectorsFunc) |
40 { | 40 { |
41 this.window = window; | 41 this.window = window; |
42 this.getFiltersFunc = getFiltersFunc; | 42 this.getFiltersFunc = getFiltersFunc; |
43 this.addSelectorsFunc = addSelectorsFunc; | 43 this.addSelectorsFunc = addSelectorsFunc; |
44 } | 44 } |
45 | 45 |
46 CSSPropertyFilters.prototype = { | 46 ElemHideEmulation.prototype = { |
47 stringifyStyle: function(style) | 47 stringifyStyle: function(style) |
48 { | 48 { |
49 var styles = []; | 49 var styles = []; |
50 for (var i = 0; i < style.length; i++) | 50 for (var i = 0; i < style.length; i++) |
51 { | 51 { |
52 var property = style.item(i); | 52 var property = style.item(i); |
53 var value = style.getPropertyValue(property); | 53 var value = style.getPropertyValue(property); |
54 var priority = style.getPropertyPriority(property); | 54 var priority = style.getPropertyPriority(property); |
55 styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";"); | 55 styles.push(property + ": " + value + (priority ? " !" + priority : "") + ";"); |
56 } | 56 } |
(...skipping 28 matching lines...) Expand all Loading... | |
85 for (var i = 0; i < rules.length; i++) | 85 for (var i = 0; i < rules.length; i++) |
86 { | 86 { |
87 var rule = rules[i]; | 87 var rule = rules[i]; |
88 if (rule.type != rule.STYLE_RULE) | 88 if (rule.type != rule.STYLE_RULE) |
89 continue; | 89 continue; |
90 | 90 |
91 var style = this.stringifyStyle(rule.style); | 91 var style = this.stringifyStyle(rule.style); |
92 for (var j = 0; j < this.patterns.length; j++) | 92 for (var j = 0; j < this.patterns.length; j++) |
93 { | 93 { |
94 var pattern = this.patterns[j]; | 94 var pattern = this.patterns[j]; |
95 var regexp = pattern.regexp; | 95 if (pattern.regexp.test(style)) |
96 | |
97 if (typeof regexp == "string") | |
98 regexp = pattern.regexp = new RegExp(regexp, "i"); | |
99 | |
100 if (regexp.test(style)) | |
101 { | 96 { |
102 var subSelectors = splitSelector(rule.selectorText); | 97 var subSelectors = splitSelector(rule.selectorText); |
103 for (var k = 0; k < subSelectors.length; k++) | 98 for (var k = 0; k < subSelectors.length; k++) |
104 selectors.push(pattern.prefix + subSelectors[k] + pattern.suffix); | 99 selectors.push(pattern.prefix + subSelectors[k] + pattern.suffix); |
105 filters[pattern.text] = true; | 100 filters[pattern.text] = true; |
106 } | 101 } |
107 } | 102 } |
108 } | 103 } |
109 }, | 104 }, |
110 | 105 |
(...skipping 10 matching lines...) Expand all Loading... | |
121 { | 116 { |
122 var stylesheet = event.target.sheet; | 117 var stylesheet = event.target.sheet; |
123 if (stylesheet) | 118 if (stylesheet) |
124 this.addSelectors([stylesheet]); | 119 this.addSelectors([stylesheet]); |
125 }, | 120 }, |
126 | 121 |
127 load: function(callback) | 122 load: function(callback) |
128 { | 123 { |
129 this.getFiltersFunc(function(patterns) | 124 this.getFiltersFunc(function(patterns) |
130 { | 125 { |
131 this.patterns = patterns; | 126 this.patterns = []; |
127 for (var i = 0; i < patterns.length; i++) | |
128 { | |
129 var pattern = patterns[i]; | |
130 | |
131 // We currently don't support any element hiding emulation feature | |
132 // except for property selectors, so we remove rules that use other | |
133 // features. See https://issues.adblockplus.org/ticket/3143. | |
134 if (pattern.features != elemHideEmulationFeatureMap.PROPERTY_SELECTOR) | |
135 continue; | |
136 | |
137 var propertySelectorRegExp = /\[\-abp\-properties=(["'])([^"']+)\1\]/; | |
Wladimir Palant
2016/11/21 11:39:15
This regexp should be created outside the loop, id
Felix Dahlke
2016/11/21 14:38:59
Done.
| |
138 var match = propertySelectorRegExp.exec(pattern.selector); | |
139 if (!match) | |
140 continue; | |
141 | |
142 var propertyExpression = match[2]; | |
143 var regexpString; | |
144 if (propertyExpression.length >= 2 && propertyExpression[0] == "/" && | |
145 propertyExpression[propertyExpression.length - 1] == "/") | |
146 regexpString = propertyExpression.slice(1, -1); | |
147 else | |
148 regexpString = filterToRegExp(propertyExpression); | |
149 | |
150 this.patterns.push({ | |
151 text: pattern.text, | |
152 regexp: new RegExp(regexpString, "i"), | |
153 prefix: pattern.selector.substr(0, match.index), | |
154 suffix: pattern.selector.substr(match.index + match[0].length) | |
155 }); | |
156 } | |
157 | |
132 callback(); | 158 callback(); |
133 }.bind(this)); | 159 }.bind(this)); |
134 }, | 160 }, |
135 | 161 |
136 apply: function() | 162 apply: function() |
137 { | 163 { |
138 if (this.patterns.length > 0) | 164 if (this.patterns.length > 0) |
139 { | 165 { |
140 var document = this.window.document; | 166 var document = this.window.document; |
141 this.addSelectors(document.styleSheets); | 167 this.addSelectors(document.styleSheets); |
142 document.addEventListener("load", this.onLoad.bind(this), true); | 168 document.addEventListener("load", this.onLoad.bind(this), true); |
143 } | 169 } |
144 } | 170 } |
145 }; | 171 }; |
OLD | NEW |