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

Side by Side Diff: lib/filterComposer.js

Issue 5358312170192896: Issue 2076 - Only generate element hiding filters if there are no blocking filters (Closed)
Patch Set: Created March 2, 2015, 4:18 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 | no next file » | 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
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 * 48 *
49 * @param {string} value 49 * @param {string} value
50 * @return {string} 50 * @return {string}
51 */ 51 */
52 function quoteCSS(value) 52 function quoteCSS(value)
53 { 53 {
54 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"'; 54 return '"' + value.replace(/["\\\{\}\x00-\x1F\x7F]/g, escapeChar) + '"';
55 } 55 }
56 exports.quoteCSS = quoteCSS; 56 exports.quoteCSS = quoteCSS;
57 57
58 function canBlockURL(url)
59 {
60 return url.protocol == "http:" || url.protocol == "https:";
61 }
62
63 /** 58 /**
64 * Generates filters to block an element. 59 * Generates filters to block an element.
65 * 60 *
66 * @param {string} tagName The element's tag name 61 * @param {string} tagName The element's tag name
67 * @param {string} [src] The element's "src" attribute 62 * @param {string} [src] The element's "src" attribute
68 * @param {string} [id] The element's "id" attribute 63 * @param {string} [id] The element's "id" attribute
69 * @param {string} [style] The element's "style" attribute 64 * @param {string} [style] The element's "style" attribute
70 * @param {string[]} classes The classes given by the element's "class" attribu te 65 * @param {string[]} classes The classes given by the element's "class" attribu te
71 * @param {string[]} urls The URLs considered when loading the element 66 * @param {string[]} urls The URLs considered when loading the element
72 * @param {URL} baseURL The URL of the document containing the element 67 * @param {URL} baseURL The URL of the document containing the element
73 * 68 *
74 * @return {object} An object holding the list of generated filters and 69 * @return {object} An object holding the list of generated filters and
75 * the list of CSS selectors for the included element 70 * the list of CSS selectors for the included element
76 * hiding filters: {filters: [...], selectors: [...]} 71 * hiding filters: {filters: [...], selectors: [...]}
77 */ 72 */
78 function composeFilters(tagName, id, src, style, classes, urls, baseURL) 73 function composeFilters(tagName, id, src, style, classes, urls, baseURL)
79 { 74 {
80 // Add a blocking filter for each HTTP(S) URL associated with the element 75 // Add a blocking filter for each HTTP(S) URL associated with the element
81 let filters = []; 76 let filters = [];
82 for (let url of urls) 77 for (let url of urls)
83 { 78 {
84 let urlObj = new URL(url, baseURL); 79 let urlObj = new URL(url, baseURL);
85 if (canBlockURL(urlObj)) 80 if (urlObj.protocol == "http:" || urlObj.protocol == "https:")
86 { 81 {
87 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); 82 let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
88 83
89 if (filters.indexOf(filter) == -1) 84 if (filters.indexOf(filter) == -1)
90 filters.push(filter); 85 filters.push(filter);
91 } 86 }
92 } 87 }
93 88
94 // Generate CSS selectors based on the element's "id" and "class" attribute 89 // If we couldn't generate any blocking filters, fallback to element hiding
95 let selectors = []; 90 let selectors = [];
96 if (id) 91 if (filters.length == 0)
97 selectors.push("#" + escapeCSS(id)); 92 {
98 if (classes.length > 0) 93 // Generate CSS selectors based on the element's "id" and "class" attribute
99 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); 94 if (id)
95 selectors.push("#" + escapeCSS(id));
96 if (classes.length > 0)
97 selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
100 98
101 // If there is a "src" attribute, specifiying a URL that we can't block, 99 // If there is a "src" attribute, specifiying a URL that we can't block,
102 // generate a CSS selector matching the "src" attribute 100 // generate a CSS selector matching the "src" attribute
103 if (src && !canBlockURL(new URL(src, baseURL))) 101 if (src)
104 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]"); 102 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]");
105 103
106 // As last resort, if there is a "style" attribute, and we couldn't generate 104 // As last resort, if there is a "style" attribute, and we couldn't generate
107 // any filters so far, generate a CSS selector matching the "style" attribute 105 // any filters so far, generate a CSS selector matching the "style" attribut e
108 if (style && selectors.length == 0 && filters.length == 0) 106 if (style && selectors.length == 0 && filters.length == 0)
109 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]"); 107 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]");
110 108
111 // Add an element hiding filter for each generated CSS selector 109 // Add an element hiding filter for each generated CSS selector
112 if (selectors.length > 0) 110 if (selectors.length > 0)
113 { 111 {
114 let domain = getDecodedHostname(baseURL).replace(/^www\./, ""); 112 let domain = getDecodedHostname(baseURL).replace(/^www\./, "");
115 113
116 for (let selector of selectors) 114 for (let selector of selectors)
117 filters.push(domain + "##" + selector); 115 filters.push(domain + "##" + selector);
116 }
118 } 117 }
119 118
120 return {filters: filters, selectors: selectors}; 119 return {filters: filters, selectors: selectors};
121 } 120 }
122 exports.composeFilters = composeFilters; 121 exports.composeFilters = composeFilters;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld