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

Delta Between Two Patch Sets: lib/filterComposer.js

Issue 5225119261655040: Issue 1282 - Don't generate filters conflicting with existing exception rules (Closed)
Left Patch Set: Rebased Created Feb. 13, 2015, 4:13 p.m.
Right Patch Set: Addressed comment Created March 3, 2015, 2:59 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « include.preload.js ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 19 matching lines...) Expand all
30 // char code as well. 30 // char code as well.
31 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr)) 31 if (code <= 0x1F || code == 0x7F || /[\d\{\}]/.test(chr))
32 return "\\" + code.toString(16) + " "; 32 return "\\" + code.toString(16) + " ";
33 33
34 return "\\" + chr; 34 return "\\" + chr;
35 } 35 }
36 36
37 /** 37 /**
38 * Escapes a token (e.g. tag, id, class or attribute) to be used in CSS selector s. 38 * Escapes a token (e.g. tag, id, class or attribute) to be used in CSS selector s.
39 * 39 *
40 * @param {string} [s] 40 * @param {string} s
41 * @return {string} 41 * @return {string}
42 */ 42 */
43 function escapeCSS(s) 43 function escapeCSS(s)
44 { 44 {
45 return s.replace(/^[\d\-]|[^\w\-\u0080-\uFFFF]/g, escapeChar); 45 return s.replace(/^[\d\-]|[^\w\-\u0080-\uFFFF]/g, escapeChar);
46 } 46 }
47 exports.escapeCSS = escapeCSS; 47 exports.escapeCSS = escapeCSS;
48 48
49 /** 49 /**
50 * Quotes a string to be used as attribute value in CSS selectors. 50 * Quotes a string to be used as attribute value in CSS selectors.
51 * 51 *
52 * @param {string} [value] 52 * @param {string} value
53 * @return {string} 53 * @return {string}
54 */ 54 */
55 function quoteCSS(value) 55 function quoteCSS(value)
56 { 56 {
57 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"'; 57 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"';
58 } 58 }
59 exports.quoteCSS = quoteCSS; 59 exports.quoteCSS = quoteCSS;
60 60
61 function canBlockURL(url, type, page, frame)
62 {
63 if (url.protocol != "http:" && url.protocol != "https:")
64 return false;
65
66 let docDomain = extractHostFromFrame(frame);
67 let filter = defaultMatcher.matchesAny(
68 stringifyURL(url), type, docDomain,
69 isThirdParty(url, docDomain), getKey(page, frame)
70 );
71
72 return !(filter instanceof WhitelistFilter);
73 }
74
75 /** 61 /**
76 * Generates filters to block an element. 62 * Generates filters to block an element.
77 * 63 * @param {Object} details
78 * @param {string} [tagName] The element's tag name 64 * @param {string} details.tagName The element's tag name
79 * @param {string} [src] The element's "src" attribute (can be null) 65 * @param {string} detials.id The element's "id" attribute
80 * @param {string} [id] The element's "id" attribute (can be null) 66 * @param {string} details.src The element's "src" attribute
81 * @param {string} [style] The element's "style" attribute (can be null) 67 * @param {string} details.style The element's "style" attribute
82 * @param {string[]} [classes] The classes given by the element's "class" attrib ute 68 * @param {string[]} details.classes The classes given by the element's "class" attribute
83 * @param {string[]} [urls] The URLs considered when loading the element 69 * @param {string[]} details.urls The URLs considered when loading the eleme nt
84 * @param {string} [type] The request type (will be ignored if there are no URLs) 70 * @param {string} details.type The request type (will be ignored if there are no URLs)
85 * @param {string} [baseURL] The URL of the document containing the element 71 * @param {string} details.baseURL The URL of the document containing the ele ment
86 * @param {Page} [page] The page containing the element 72 * @param {Page} details.page The page containing the element
87 * @param {Frame} [frame] The frame containing the element 73 * @param {Frame} details.frame The frame containing the element
Wladimir Palant 2015/03/03 19:17:01 I wonder whether JsDoc Toolkit will actually accep
Sebastian Noack 2015/03/03 19:18:54 Why not? This is official JSDoc syntax: http://use
Wladimir Palant 2015/03/03 20:11:47 Yes, seems to be supported by our version as well:
88 * 74 *
89 * @return {object} An object holding the list of generated filters and 75 * @return {object} An object holding the list of generated filters and
90 * the list of CSS selectors for the included element 76 * the list of CSS selectors for the included element
91 * hiding filters: {filters: [...], selectors: [...]} 77 * hiding filters: {filters: [...], selectors: [...]}
92 */ 78 */
93 function composeFilters(tagName, id, src, style, classes, urls, type, baseURL, p age, frame) 79 function composeFilters(details)
94 { 80 {
95 // Add a blocking filter for each URL of the element that can be blocked
96 let filters = []; 81 let filters = [];
97 for (let url of urls) 82 let selectors = [];
83
84 let page = details.page;
85 let frame = details.frame;
86
87 if (!isFrameWhitelisted(page, frame, "DOCUMENT"))
98 { 88 {
99 let urlObj = new URL(url, baseURL); 89 let docDomain = extractHostFromFrame(frame);
100 if (canBlockURL(urlObj, type, page, frame)) 90
91 // Add a blocking filter for each URL of the element that can be blocked
92 for (let url of details.urls)
101 { 93 {
102 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); 94 let urlObj = new URL(url, details.baseURL);
103 95
104 if (filters.indexOf(filter) == -1) 96 if (urlObj.protocol == "http:" || urlObj.protocol == "https:")
105 filters.push(filter); 97 {
98 url = stringifyURL(urlObj);
99
100 let filter = defaultMatcher.matchesAny(
101 url, details.type, docDomain,
102 isThirdParty(urlObj, docDomain),
103 getKey(page, frame)
104 );
105
106 if (!(filter instanceof WhitelistFilter))
107 {
108 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
109
110 if (filters.indexOf(filterText) == -1)
111 filters.push(filterText);
112 }
113 }
106 } 114 }
107 }
108 115
109 let selectors = []; 116 // If we couldn't generate any blocking filters, fallback to element hiding
110 if (!isFrameWhitelisted(page, frame, "ELEMHIDE")) 117 let selectors = [];
111 { 118 if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE"))
112 // Generate CSS selectors based on the element's "id" and "class" attribute 119 {
113 if (id) 120 // Generate CSS selectors based on the element's "id" and "class" attribut e
114 selectors.push("#" + escapeCSS(id)); 121 if (details.id)
115 if (classes.length > 0) 122 selectors.push("#" + escapeCSS(details.id));
116 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); 123 if (details.classes.length > 0)
124 selectors.push(details.classes.map(c => "." + escapeCSS(c)).join(""));
117 125
118 // If there is a "src" attribute, specifiying a URL that we can't block, 126 // If there is a "src" attribute, specifiying a URL that we can't block,
119 // generate a CSS selector matching the "src" attribute 127 // generate a CSS selector matching the "src" attribute
120 if (src && !canBlockURL(new URL(src, baseURL), type, page, frame)) 128 if (details.src)
121 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]"); 129 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s rc) + "]");
122 130
123 // As last resort, if there is a "style" attribute, and we couldn't generate 131 // As last resort, if there is a "style" attribute, and we couldn't genera te
124 // any filters so far, generate a CSS selector matching the "style" attribut e 132 // any filters so far, generate a CSS selector matching the "style" attrib ute
125 if (style && selectors.length == 0 && filters.length == 0) 133 if (details.style && selectors.length == 0 && filters.length == 0)
126 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]"); 134 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details .style) + "]");
127 135
128 // Add an element hiding filter for each generated CSS selector 136 // Add an element hiding filter for each generated CSS selector
129 if (selectors.length > 0)
130 {
131 let domain = extractHostFromFrame(frame).replace(/^www\./, "");
132
133 for (let selector of selectors) 137 for (let selector of selectors)
134 filters.push(domain + "##" + selector); 138 filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
135 } 139 }
136 } 140 }
137 141
138 return {filters: filters, selectors: selectors}; 142 return {filters: filters, selectors: selectors};
139 } 143 }
140 exports.composeFilters = composeFilters; 144 exports.composeFilters = composeFilters;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld