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

Unified Diff: lib/filterComposer.js

Issue 5225119261655040: Issue 1282 - Don't generate filters conflicting with existing exception rules (Closed)
Patch Set: Addressed comment Created March 3, 2015, 2:59 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterComposer.js
===================================================================
--- a/lib/filterComposer.js
+++ b/lib/filterComposer.js
@@ -15,7 +15,10 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-let {getDecodedHostname, stringifyURL} = require("url");
+let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url");
+let {getKey, isFrameWhitelisted} = require("whitelisting");
+let {defaultMatcher} = require("matcher");
+let {WhitelistFilter} = require("filterClasses");
function escapeChar(chr)
{
@@ -57,62 +60,82 @@
/**
* Generates filters to block an element.
- *
- * @param {string} tagName The element's tag name
- * @param {string} [src] The element's "src" attribute
- * @param {string} [id] The element's "id" attribute
- * @param {string} [style] The element's "style" attribute
- * @param {string[]} classes The classes given by the element's "class" attribute
- * @param {string[]} urls The URLs considered when loading the element
- * @param {URL} baseURL The URL of the document containing the element
+ * @param {Object} details
+ * @param {string} details.tagName The element's tag name
+ * @param {string} detials.id The element's "id" attribute
+ * @param {string} details.src The element's "src" attribute
+ * @param {string} details.style The element's "style" attribute
+ * @param {string[]} details.classes The classes given by the element's "class" attribute
+ * @param {string[]} details.urls The URLs considered when loading the element
+ * @param {string} details.type The request type (will be ignored if there are no URLs)
+ * @param {string} details.baseURL The URL of the document containing the element
+ * @param {Page} details.page The page containing the element
+ * @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:
*
* @return {object} An object holding the list of generated filters and
* the list of CSS selectors for the included element
* hiding filters: {filters: [...], selectors: [...]}
*/
-function composeFilters(tagName, id, src, style, classes, urls, baseURL)
+function composeFilters(details)
{
- // Add a blocking filter for each HTTP(S) URL associated with the element
let filters = [];
- for (let url of urls)
+ let selectors = [];
+
+ let page = details.page;
+ let frame = details.frame;
+
+ if (!isFrameWhitelisted(page, frame, "DOCUMENT"))
{
- let urlObj = new URL(url, baseURL);
- if (urlObj.protocol == "http:" || urlObj.protocol == "https:")
+ let docDomain = extractHostFromFrame(frame);
+
+ // Add a blocking filter for each URL of the element that can be blocked
+ for (let url of details.urls)
{
- let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
+ let urlObj = new URL(url, details.baseURL);
- if (filters.indexOf(filter) == -1)
- filters.push(filter);
+ if (urlObj.protocol == "http:" || urlObj.protocol == "https:")
+ {
+ url = stringifyURL(urlObj);
+
+ let filter = defaultMatcher.matchesAny(
+ url, details.type, docDomain,
+ isThirdParty(urlObj, docDomain),
+ getKey(page, frame)
+ );
+
+ if (!(filter instanceof WhitelistFilter))
+ {
+ let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
+
+ if (filters.indexOf(filterText) == -1)
+ filters.push(filterText);
+ }
+ }
}
- }
- // If we couldn't generate any blocking filters, fallback to element hiding
- let selectors = [];
- if (filters.length == 0)
- {
- // Generate CSS selectors based on the element's "id" and "class" attribute
- if (id)
- selectors.push("#" + escapeCSS(id));
- if (classes.length > 0)
- selectors.push(classes.map(c => "." + escapeCSS(c)).join(""));
+ // If we couldn't generate any blocking filters, fallback to element hiding
+ let selectors = [];
+ if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE"))
+ {
+ // Generate CSS selectors based on the element's "id" and "class" attribute
+ if (details.id)
+ selectors.push("#" + escapeCSS(details.id));
+ if (details.classes.length > 0)
+ selectors.push(details.classes.map(c => "." + escapeCSS(c)).join(""));
- // If there is a "src" attribute, specifiying a URL that we can't block,
- // generate a CSS selector matching the "src" attribute
- if (src)
- selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(src) + "]");
+ // If there is a "src" attribute, specifiying a URL that we can't block,
+ // generate a CSS selector matching the "src" attribute
+ if (details.src)
+ selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.src) + "]");
- // As last resort, if there is a "style" attribute, and we couldn't generate
- // any filters so far, generate a CSS selector matching the "style" attribute
- if (style && selectors.length == 0 && filters.length == 0)
- selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(style) + "]");
+ // As last resort, if there is a "style" attribute, and we couldn't generate
+ // any filters so far, generate a CSS selector matching the "style" attribute
+ if (details.style && selectors.length == 0 && filters.length == 0)
+ selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details.style) + "]");
- // Add an element hiding filter for each generated CSS selector
- if (selectors.length > 0)
- {
- let domain = getDecodedHostname(baseURL).replace(/^www\./, "");
-
+ // Add an element hiding filter for each generated CSS selector
for (let selector of selectors)
- filters.push(domain + "##" + selector);
+ filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
}
}
« no previous file with comments | « include.preload.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld