 Issue 29361668:
  Issue 4394 - Create a filter class for element hiding emulation filters  (Closed) 
  Base URL: https://bitbucket.org/fhd/adblockpluscore
    
  
    Issue 29361668:
  Issue 4394 - Create a filter class for element hiding emulation filters  (Closed) 
  Base URL: https://bitbucket.org/fhd/adblockpluscore| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 22 matching lines...) Expand all Loading... | |
| 33 | 33 | 
| 34 let t = null; | 34 let t = null; | 
| 35 let defaultTypes = null; | 35 let defaultTypes = null; | 
| 36 | 36 | 
| 37 exports.setUp = function(callback) | 37 exports.setUp = function(callback) | 
| 38 { | 38 { | 
| 39 let sandboxedRequire = createSandbox(); | 39 let sandboxedRequire = createSandbox(); | 
| 40 ( | 40 ( | 
| 41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 
| 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 
| 43 ElemHideException, ElemHideEmulationFilter} = sandboxedRequire("../lib/filt erClasses") | 43 ElemHideException, | 
| 
kzar
2016/11/04 15:45:56
Nit: Also mind fixing some of these long lines? (N
 
Felix Dahlke
2016/11/04 16:43:35
See my comment in elemHideEmulation.js.
 | |
| 44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses") | |
| 44 ); | 45 ); | 
| 45 t = RegExpFilter.typeMap; | 46 t = RegExpFilter.typeMap; | 
| 46 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | | 47 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | | 
| 47 t.GENERICHIDE | t.GENERICBLOCK); | 48 t.GENERICHIDE | t.GENERICBLOCK); | 
| 48 | 49 | 
| 49 callback(); | 50 callback(); | 
| 50 }; | 51 }; | 
| 51 | 52 | 
| 52 function serializeFilter(filter) | 53 function serializeFilter(filter) | 
| 53 { | 54 { | 
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 result.push("type=whitelist"); | 99 result.push("type=whitelist"); | 
| 99 } | 100 } | 
| 100 } | 101 } | 
| 101 else if (filter instanceof ElemHideBase) | 102 else if (filter instanceof ElemHideBase) | 
| 102 { | 103 { | 
| 103 if (filter instanceof ElemHideFilter) | 104 if (filter instanceof ElemHideFilter) | 
| 104 result.push("type=elemhide"); | 105 result.push("type=elemhide"); | 
| 105 else if (filter instanceof ElemHideException) | 106 else if (filter instanceof ElemHideException) | 
| 106 result.push("type=elemhideexception"); | 107 result.push("type=elemhideexception"); | 
| 107 else if (filter instanceof ElemHideEmulationFilter) | 108 else if (filter instanceof ElemHideEmulationFilter) | 
| 108 { | |
| 109 result.push("type=elemhideemulation"); | 109 result.push("type=elemhideemulation"); | 
| 110 result.push("features=" + filter.features); | |
| 111 } | |
| 112 | 110 | 
| 113 result.push("selectorDomain=" + (filter.selectorDomain || "")); | 111 result.push("selectorDomain=" + (filter.selectorDomain || "")); | 
| 114 result.push("selector=" + filter.selector); | 112 result.push("selector=" + filter.selector); | 
| 115 } | 113 } | 
| 116 } | 114 } | 
| 117 return result; | 115 return result; | 
| 118 } | 116 } | 
| 119 | 117 | 
| 120 function addDefaults(expected) | 118 function addDefaults(expected) | 
| 121 { | 119 { | 
| 122 let type = null; | 120 let type = null; | 
| 123 let hasProperty = {}; | 121 let hasProperty = {}; | 
| 124 for (let entry of expected) | 122 for (let entry of expected) | 
| 125 { | 123 { | 
| 126 if (/^type=(.*)/.test(entry)) | 124 if (/^type=(.*)/.test(entry)) | 
| 127 type = RegExp.$1; | 125 type = RegExp.$1; | 
| 128 else if (/^(\w+)/.test(entry)) | 126 else if (/^(\w+)/.test(entry)) | 
| 129 hasProperty[RegExp.$1] = true; | 127 hasProperty[RegExp.$1] = true; | 
| 130 } | 128 } | 
| 131 | 129 | 
| 132 function addProperty(prop, value) | 130 function addProperty(prop, value) | 
| 133 { | 131 { | 
| 134 if (!(prop in hasProperty)) | 132 if (!(prop in hasProperty)) | 
| 135 expected.push(prop + "=" + value); | 133 expected.push(prop + "=" + value); | 
| 136 } | 134 } | 
| 137 | 135 | 
| 138 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "elemhideemulation") | 136 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || | 
| 137 type == "elemhideexception" || type == "elemhideemulation") | |
| 139 { | 138 { | 
| 140 addProperty("disabled", "false"); | 139 addProperty("disabled", "false"); | 
| 141 addProperty("lastHit", "0"); | 140 addProperty("lastHit", "0"); | 
| 142 addProperty("hitCount", "0"); | 141 addProperty("hitCount", "0"); | 
| 143 } | 142 } | 
| 144 if (type == "whitelist" || type == "filterlist") | 143 if (type == "whitelist" || type == "filterlist") | 
| 145 { | 144 { | 
| 146 addProperty("contentType", 0x7FFFFFFF & ~( | 145 addProperty("contentType", 0x7FFFFFFF & ~( | 
| 147 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | | 146 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | | 
| 148 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | | 147 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | | 
| 149 RegExpFilter.typeMap.GENERICBLOCK | 148 RegExpFilter.typeMap.GENERICBLOCK | 
| 150 )); | 149 )); | 
| 151 addProperty("matchCase", "false"); | 150 addProperty("matchCase", "false"); | 
| 152 addProperty("thirdParty", "null"); | 151 addProperty("thirdParty", "null"); | 
| 153 addProperty("domains", ""); | 152 addProperty("domains", ""); | 
| 154 addProperty("sitekeys", ""); | 153 addProperty("sitekeys", ""); | 
| 155 } | 154 } | 
| 156 if (type == "filterlist") | 155 if (type == "filterlist") | 
| 157 { | 156 { | 
| 158 addProperty("collapse", "null"); | 157 addProperty("collapse", "null"); | 
| 159 } | 158 } | 
| 160 if (type == "elemhide" || type == "elemhideexception" || type == "elemhideemul ation") | 159 if (type == "elemhide" || type == "elemhideexception" || | 
| 160 type == "elemhideemulation") | |
| 161 { | 161 { | 
| 162 addProperty("selectorDomain", ""); | 162 addProperty("selectorDomain", ""); | 
| 163 addProperty("domains", ""); | 163 addProperty("domains", ""); | 
| 164 } | |
| 165 if (type == "elemhideemulation") | |
| 166 { | |
| 167 addProperty("features", "0"); | |
| 168 } | 164 } | 
| 169 } | 165 } | 
| 170 | 166 | 
| 171 function compareFilter(test, text, expected, postInit) | 167 function compareFilter(test, text, expected, postInit) | 
| 172 { | 168 { | 
| 173 addDefaults(expected); | 169 addDefaults(expected); | 
| 174 | 170 | 
| 175 let filter = Filter.fromText(text); | 171 let filter = Filter.fromText(text); | 
| 176 if (postInit) | 172 if (postInit) | 
| 177 postInit(filter) | 173 postInit(filter) | 
| (...skipping 27 matching lines...) Expand all Loading... | |
| 205 test.equal(typeof Filter, "function", "typeof Filter"); | 201 test.equal(typeof Filter, "function", "typeof Filter"); | 
| 206 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); | 202 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); | 
| 207 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); | 203 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); | 
| 208 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); | 204 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); | 
| 209 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); | 205 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); | 
| 210 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); | 206 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); | 
| 211 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); | 207 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); | 
| 212 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); | 208 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); | 
| 213 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); | 209 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); | 
| 214 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); | 210 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); | 
| 215 test.equal(typeof ElemHideEmulationFilter, "function", "typeof ElemHideEmulati onFilter"); | 211 test.equal(typeof ElemHideEmulationFilter, "function", | 
| 212 "typeof ElemHideEmulationFilter"); | |
| 216 | 213 | 
| 217 test.done(); | 214 test.done(); | 
| 218 }; | 215 }; | 
| 219 | 216 | 
| 220 exports.testComments = function(test) | 217 exports.testComments = function(test) | 
| 221 { | 218 { | 
| 222 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); | 219 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); | 
| 223 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); | 220 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); | 
| 224 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); | 221 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); | 
| 225 | 222 | 
| 226 test.done(); | 223 test.done(); | 
| 227 }; | 224 }; | 
| 228 | 225 | 
| 229 exports.testInvalidFilters = function(test) | 226 exports.testInvalidFilters = function(test) | 
| 230 { | 227 { | 
| 231 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); | 228 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); | 
| 232 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); | 229 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); | 
| 233 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]); | 230 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]); | 
| 234 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]); | 231 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]); | 
| 235 | 232 | 
| 236 function checkElemHideEmulationFilterInvalid(domains) | 233 function checkElemHideEmulationFilterInvalid(domains) | 
| 237 { | 234 { | 
| 238 let filterText = domains + "##[-abp-properties='abc']"; | 235 let filterText = domains + "##[-abp-properties='abc']"; | 
| 239 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_elemhideemulation_nodomain"]); | 236 compareFilter(test, filterText, | 
| 237 ["type=invalid", "text=" + filterText, | |
| 238 "reason=filter_elemhideemulation_nodomain"]); | |
| 240 } | 239 } | 
| 241 checkElemHideEmulationFilterInvalid(""); | 240 checkElemHideEmulationFilterInvalid(""); | 
| 242 checkElemHideEmulationFilterInvalid("~foo.com"); | 241 checkElemHideEmulationFilterInvalid("~foo.com"); | 
| 243 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com"); | 242 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com"); | 
| 244 checkElemHideEmulationFilterInvalid("foo"); | 243 checkElemHideEmulationFilterInvalid("foo"); | 
| 245 checkElemHideEmulationFilterInvalid("~foo.com,bar"); | 244 checkElemHideEmulationFilterInvalid("~foo.com,bar"); | 
| 246 | 245 | 
| 247 test.done(); | 246 test.done(); | 
| 248 }; | 247 }; | 
| 249 | 248 | 
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); | 342 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); | 
| 344 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); | 343 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); | 
| 345 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]); | 344 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]); | 
| 346 | 345 | 
| 347 test.done(); | 346 test.done(); | 
| 348 }; | 347 }; | 
| 349 | 348 | 
| 350 exports.testElemHideEmulationFilters = function(test) | 349 exports.testElemHideEmulationFilters = function(test) | 
| 351 { | 350 { | 
| 352 // Check valid domain combinations | 351 // Check valid domain combinations | 
| 353 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM", "features=1"]); | 352 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM"]); | 
| 354 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain= foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM", "featu res=1"]); | 353 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain= foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM"]); | 
| 355 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=elemhideem ulation", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com" , "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR", "features=1"]); | 354 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=elemhideem ulation", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com" , "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR"]); | 
| 356 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain= bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM", "featu res=1"]); | 355 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain= bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM"]); | 
| 357 | 356 | 
| 358 compareFilter(test, "##[-abp-properties='']", ["type=invalid", "text=##[-abp-p roperties='']", "reason=filter_elemhideemulation_nodomain"]); | 357 compareFilter(test, "##[-abp-properties='']", ["type=invalid", "text=##[-abp-p roperties='']", "reason=filter_elemhideemulation_nodomain"]); | 
| 359 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); | 358 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); | 
| 360 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=fo o.com", "selector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM", "features =1"]); | 359 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=fo o.com", "selector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM"]); | 
| 361 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=elemhideemulation", "text=foo.com##[-abp-properties='|background-ima ge: url(data:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|backg round-image: url(data:*)']", "domains=FOO.COM", "features=1"]); | 360 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=elemhideemulation", "text=foo.com##[-abp-properties='|background-ima ge: url(data:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|backg round-image: url(data:*)']", "domains=FOO.COM"]); | 
| 362 | |
| 363 // Test feature detection | |
| 364 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM", "features=1"]); | |
| 365 compareFilter(test, "foo.com##:has(#ddd)", ["type=elemhideemulation", "text=fo o.com##:has(#ddd)", "selectorDomain=foo.com", "selector=:has(#ddd)", "domains=FO O.COM", "features=2"]); | |
| 366 compareFilter(test, "foo.com##:has([-abp-properties='abc'])", ["type=elemhidee mulation", "text=foo.com##:has([-abp-properties='abc'])", "selectorDomain=foo.co m", "selector=:has([-abp-properties='abc'])", "domains=FOO.COM", "features=3"]); | |
| 
kzar
2016/11/04 15:45:56
Please don't hardcode the feature flag numbers, be
 
Felix Dahlke
2016/11/04 16:43:35
Good point, will do.
 
Felix Dahlke
2016/11/11 11:51:42
Done.
 | |
| 367 | 361 | 
| 368 test.done(); | 362 test.done(); | 
| 369 }; | 363 }; | 
| 370 | 364 | 
| 371 exports.testEmptyElemHideDomains = function(test) | 365 exports.testEmptyElemHideDomains = function(test) | 
| 372 { | 366 { | 
| 373 let emptyDomainFilters = [ | 367 let emptyDomainFilters = [ | 
| 374 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", | 368 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", | 
| 375 ",foo.com##selector", "foo.com,~##selector", | 369 ",foo.com##selector", "foo.com,~##selector", | 
| 376 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" | 370 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" | 
| 377 ]; | 371 ]; | 
| 378 | 372 | 
| 379 for (let filterText of emptyDomainFilters) | 373 for (let filterText of emptyDomainFilters) | 
| 380 { | 374 { | 
| 381 let filter = Filter.fromText(filterText); | 375 let filter = Filter.fromText(filterText); | 
| 382 test.ok(filter instanceof InvalidFilter); | 376 test.ok(filter instanceof InvalidFilter); | 
| 383 test.equal(filter.reason, "filter_invalid_domain"); | 377 test.equal(filter.reason, "filter_invalid_domain"); | 
| 384 } | 378 } | 
| 385 | 379 | 
| 386 test.done(); | 380 test.done(); | 
| 387 }; | 381 }; | 
| LEFT | RIGHT |