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

Unified Diff: lib/filterComposer.js

Issue 5693109165883392: Issue 2040 - Replaced localStorage with chrome.storage.local (Closed)
Patch Set: Rebased Created March 3, 2015, 3:11 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
Index: lib/filterComposer.js
===================================================================
--- a/lib/filterComposer.js
+++ b/lib/filterComposer.js
@@ -15,10 +15,7 @@
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-let {extractHostFromFrame, stringifyURL, isThirdParty} = require("url");
-let {getKey, isFrameWhitelisted} = require("whitelisting");
-let {defaultMatcher} = require("matcher");
-let {WhitelistFilter} = require("filterClasses");
+let {getDecodedHostname, stringifyURL} = require("url");
function escapeChar(chr)
{
@@ -60,82 +57,62 @@
/**
* Generates filters to block an 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
+ *
+ * @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
*
* @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(details)
+function composeFilters(tagName, id, src, style, classes, urls, baseURL)
{
+ // Add a blocking filter for each HTTP(S) URL associated with the element
let filters = [];
+ for (let url of urls)
+ {
+ let urlObj = new URL(url, baseURL);
+ if (urlObj.protocol == "http:" || urlObj.protocol == "https:")
+ {
+ let filter = stringifyURL(urlObj).replace(/^[\w\-]+:\/+(?:www\.)?/, "||");
+
+ if (filters.indexOf(filter) == -1)
+ filters.push(filter);
+ }
+ }
+
+ // 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(""));
- let page = details.page;
- let frame = details.frame;
+ // 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 (!isFrameWhitelisted(page, frame, "DOCUMENT"))
- {
- let docDomain = extractHostFromFrame(frame);
+ // 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) + "]");
- // Add a blocking filter for each URL of the element that can be blocked
- for (let url of details.urls)
+ // Add an element hiding filter for each generated CSS selector
+ if (selectors.length > 0)
{
- let urlObj = new URL(url, details.baseURL);
+ let domain = getDecodedHostname(baseURL).replace(/^www\./, "");
- 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 && !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 (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 (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
for (let selector of selectors)
- filters.push(docDomain.replace(/^www\./, "") + "##" + selector);
+ filters.push(domain + "##" + selector);
}
}

Powered by Google App Engine
This is Rietveld