OLD | NEW |
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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** @module filterComposer */ | 18 /** @module filterComposer */ |
19 | 19 |
20 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); | 20 let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url"); |
21 let {getKey, isFrameWhitelisted} = require("whitelisting"); | 21 let {getKey, isFrameWhitelisted} = require("whitelisting"); |
22 let {defaultMatcher} = require("matcher"); | 22 let {defaultMatcher} = require("matcher"); |
| 23 let {RegExpFilter} = require("filterClasses"); |
23 | 24 |
24 function escapeChar(chr) | 25 function escapeChar(chr) |
25 { | 26 { |
26 let code = chr.charCodeAt(0); | 27 let code = chr.charCodeAt(0); |
27 | 28 |
28 // Control characters and leading digits must be escaped based on | 29 // Control characters and leading digits must be escaped based on |
29 // their char code in CSS. Moreover, curly brackets aren't allowed | 30 // their char code in CSS. Moreover, curly brackets aren't allowed |
30 // in elemhide filters, and therefore must be escaped based on their | 31 // in elemhide filters, and therefore must be escaped based on their |
31 // char code as well. | 32 // char code as well. |
32 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr)) | 33 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr)) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 * hiding filters: {filters: [...], selectors: [...]} | 81 * hiding filters: {filters: [...], selectors: [...]} |
81 */ | 82 */ |
82 exports.composeFilters = function(details) | 83 exports.composeFilters = function(details) |
83 { | 84 { |
84 let filters = []; | 85 let filters = []; |
85 let selectors = []; | 86 let selectors = []; |
86 | 87 |
87 let page = details.page; | 88 let page = details.page; |
88 let frame = details.frame; | 89 let frame = details.frame; |
89 | 90 |
90 if (!isFrameWhitelisted(page, frame, "DOCUMENT")) | 91 if (!isFrameWhitelisted(page, frame, RegExpFilter.typeMap.DOCUMENT)) |
91 { | 92 { |
| 93 let typeMask = RegExpFilter.typeMap[details.type]; |
92 let docDomain = extractHostFromFrame(frame); | 94 let docDomain = extractHostFromFrame(frame); |
93 | 95 |
94 // Add a blocking filter for each URL of the element that can be blocked | 96 // Add a blocking filter for each URL of the element that can be blocked |
95 for (let url of details.urls) | 97 for (let url of details.urls) |
96 { | 98 { |
97 let urlObj = new URL(url, details.baseURL); | 99 let urlObj = new URL(url, details.baseURL); |
98 url = stringifyURL(urlObj); | 100 url = stringifyURL(urlObj); |
99 | 101 |
100 let filter = defaultMatcher.whitelist.matchesAny( | 102 let filter = defaultMatcher.whitelist.matchesAny( |
101 url, details.type, docDomain, | 103 url, typeMask, docDomain, |
102 isThirdParty(urlObj, docDomain), | 104 isThirdParty(urlObj, docDomain), |
103 getKey(page, frame) | 105 getKey(page, frame) |
104 ); | 106 ); |
105 | 107 |
106 if (!filter) | 108 if (!filter) |
107 { | 109 { |
108 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); | 110 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
109 | 111 |
110 if (filters.indexOf(filterText) == -1) | 112 if (filters.indexOf(filterText) == -1) |
111 filters.push(filterText); | 113 filters.push(filterText); |
112 } | 114 } |
113 } | 115 } |
114 | 116 |
115 // If we couldn't generate any blocking filters, fallback to element hiding | 117 // If we couldn't generate any blocking filters, fallback to element hiding |
116 let selectors = []; | 118 let selectors = []; |
117 if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE")) | 119 if (filters.length == 0 && !isFrameWhitelisted(page, frame, RegExpFilter.typ
eMap.ELEMHIDE)) |
118 { | 120 { |
119 // Generate CSS selectors based on the element's "id" and "class" attribut
e | 121 // Generate CSS selectors based on the element's "id" and "class" attribut
e |
120 if (details.id) | 122 if (details.id) |
121 selectors.push("#" + escapeCSS(details.id)); | 123 selectors.push("#" + escapeCSS(details.id)); |
122 if (details.classes.length > 0) | 124 if (details.classes.length > 0) |
123 selectors.push(details.classes.map(c => "." + escapeCSS(c)).join("")); | 125 selectors.push(details.classes.map(c => "." + escapeCSS(c)).join("")); |
124 | 126 |
125 // If there is a "src" attribute, specifiying a URL that we can't block, | 127 // If there is a "src" attribute, specifiying a URL that we can't block, |
126 // generate a CSS selector matching the "src" attribute | 128 // generate a CSS selector matching the "src" attribute |
127 if (details.src) | 129 if (details.src) |
128 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s
rc) + "]"); | 130 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s
rc) + "]"); |
129 | 131 |
130 // As last resort, if there is a "style" attribute, and we couldn't genera
te | 132 // As last resort, if there is a "style" attribute, and we couldn't genera
te |
131 // any filters so far, generate a CSS selector matching the "style" attrib
ute | 133 // any filters so far, generate a CSS selector matching the "style" attrib
ute |
132 if (details.style && selectors.length == 0 && filters.length == 0) | 134 if (details.style && selectors.length == 0 && filters.length == 0) |
133 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details
.style) + "]"); | 135 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details
.style) + "]"); |
134 | 136 |
135 // Add an element hiding filter for each generated CSS selector | 137 // Add an element hiding filter for each generated CSS selector |
136 for (let selector of selectors) | 138 for (let selector of selectors) |
137 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); | 139 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); |
138 } | 140 } |
139 } | 141 } |
140 | 142 |
141 return {filters: filters, selectors: selectors}; | 143 return {filters: filters, selectors: selectors}; |
142 }; | 144 }; |
OLD | NEW |