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

Side by Side Diff: lib/filterComposer.js

Issue 29328834: Issue 3163 - Adapt "Block element" functionality for a change in Chrome dealing with "\0" in CSS se… (Closed)
Patch Set: Created Oct. 5, 2015, 1:30 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 | qunit/tests/cssEscaping.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-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 let {RegExpFilter} = require("filterClasses");
24 24
25 function isValidString(s) {
26 return s && s.indexOf("\0") == -1;
27 }
28
25 function escapeChar(chr) 29 function escapeChar(chr)
26 { 30 {
27 let code = chr.charCodeAt(0); 31 let code = chr.charCodeAt(0);
28 32
29 // Control characters and leading digits must be escaped based on 33 // Control characters and leading digits must be escaped based on
30 // their char code in CSS. Moreover, curly brackets aren't allowed 34 // their char code in CSS. Moreover, curly brackets aren't allowed
31 // in elemhide filters, and therefore must be escaped based on their 35 // in elemhide filters, and therefore must be escaped based on their
32 // char code as well. 36 // char code as well.
33 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr)) 37 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr))
34 return "\\" + code.toString(16) + " "; 38 return "\\" + code.toString(16) + " ";
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 if (filters.indexOf(filterText) == -1) 116 if (filters.indexOf(filterText) == -1)
113 filters.push(filterText); 117 filters.push(filterText);
114 } 118 }
115 } 119 }
116 120
117 // If we couldn't generate any blocking filters, fallback to element hiding 121 // If we couldn't generate any blocking filters, fallback to element hiding
118 let selectors = []; 122 let selectors = [];
119 if (filters.length == 0 && !isFrameWhitelisted(page, frame, RegExpFilter.typ eMap.ELEMHIDE)) 123 if (filters.length == 0 && !isFrameWhitelisted(page, frame, RegExpFilter.typ eMap.ELEMHIDE))
120 { 124 {
121 // Generate CSS selectors based on the element's "id" and "class" attribut e 125 // Generate CSS selectors based on the element's "id" and "class" attribut e
122 if (details.id) 126 if (isValidString(details.id))
123 selectors.push("#" + escapeCSS(details.id)); 127 selectors.push("#" + escapeCSS(details.id));
124 if (details.classes.length > 0) 128
125 selectors.push(details.classes.map(c => "." + escapeCSS(c)).join("")); 129 let classes = details.classes.filter(isValidString);
130 if (classes.length > 0)
131 selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
126 132
127 // If there is a "src" attribute, specifiying a URL that we can't block, 133 // If there is a "src" attribute, specifiying a URL that we can't block,
128 // generate a CSS selector matching the "src" attribute 134 // generate a CSS selector matching the "src" attribute
129 if (details.src) 135 if (isValidString(details.src))
130 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s rc) + "]"); 136 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s rc) + "]");
131 137
132 // As last resort, if there is a "style" attribute, and we couldn't genera te 138 // As last resort, if there is a "style" attribute, and we couldn't genera te
133 // any filters so far, generate a CSS selector matching the "style" attrib ute 139 // any filters so far, generate a CSS selector matching the "style" attrib ute
134 if (details.style && selectors.length == 0 && filters.length == 0) 140 if (isValidString(details.style) && selectors.length == 0 && filters.lengt h == 0)
135 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details .style) + "]"); 141 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details .style) + "]");
136 142
137 // Add an element hiding filter for each generated CSS selector 143 // Add an element hiding filter for each generated CSS selector
138 for (let selector of selectors) 144 for (let selector of selectors)
139 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); 145 filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
140 } 146 }
141 } 147 }
142 148
143 return {filters: filters, selectors: selectors}; 149 return {filters: filters, selectors: selectors};
144 }; 150 };
OLDNEW
« no previous file with comments | « no previous file | qunit/tests/cssEscaping.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld