| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 /** | 61 /** |
| 62 * Generates filters to block an element. | 62 * Generates filters to block an element. |
| 63 * | 63 * @param {Object} details |
| 64 * @param {string} tagName The element's tag name | 64 * @param {string} details.tagName The element's tag name |
| 65 * @param {Object} attributes The element's "id", "src" and "style attreibute | 65 * @param {string} detials.id The element's "id" attribute |
| 66 * @param {string[]} classes The classes given by the element's "class" attr ibute | 66 * @param {string} details.src The element's "src" attribute |
| 67 * @param {string[]} urls The URLs considered when loading the element | 67 * @param {string} details.style The element's "style" attribute |
| 68 * @param {string} [type] The request type (will be ignored if there are no URLs) | 68 * @param {string[]} details.classes The classes given by the element's "class" attribute |
| 69 * @param {string} baseURL The URL of the document containing the element | 69 * @param {string[]} details.urls The URLs considered when loading the eleme nt |
| 70 * @param {Page} page The page containing the element | 70 * @param {string} details.type The request type (will be ignored if there are no URLs) |
| 71 * @param {Frame} frame The frame containing the element | 71 * @param {string} details.baseURL The URL of the document containing the ele ment |
| 72 * @param {Page} details.page The page 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:
| |
| 72 * | 74 * |
| 73 * @return {object} An object holding the list of generated filters and | 75 * @return {object} An object holding the list of generated filters and |
| 74 * the list of CSS selectors for the included element | 76 * the list of CSS selectors for the included element |
| 75 * hiding filters: {filters: [...], selectors: [...]} | 77 * hiding filters: {filters: [...], selectors: [...]} |
| 76 */ | 78 */ |
| 77 function composeFilters(tagName, attributes, classes, urls, type, baseURL, page, frame) | 79 function composeFilters(details) |
| 78 { | 80 { |
| 79 let filters = []; | 81 let filters = []; |
| 80 let selectors = []; | 82 let selectors = []; |
| 83 | |
| 84 let page = details.page; | |
| 85 let frame = details.frame; | |
| 81 | 86 |
| 82 if (!isFrameWhitelisted(page, frame, "DOCUMENT")) | 87 if (!isFrameWhitelisted(page, frame, "DOCUMENT")) |
| 83 { | 88 { |
| 84 let docDomain = extractHostFromFrame(frame); | 89 let docDomain = extractHostFromFrame(frame); |
| 85 | 90 |
| 86 // Add a blocking filter for each URL of the element that can be blocked | 91 // Add a blocking filter for each URL of the element that can be blocked |
| 87 for (let url of urls) | 92 for (let url of details.urls) |
| 88 { | 93 { |
| 89 let urlObj = new URL(url, baseURL); | 94 let urlObj = new URL(url, details.baseURL); |
| 90 | 95 |
| 91 if (url.protocol == "http:" || url.protocol == "https:") | 96 if (urlObj.protocol == "http:" || urlObj.protocol == "https:") |
| 92 { | 97 { |
| 93 url = stringifyURL(urlObj); | 98 url = stringifyURL(urlObj); |
| 94 | 99 |
| 95 let filter = defaultMatcher.matchesAny( | 100 let filter = defaultMatcher.matchesAny( |
| 96 url, type, docDomain, | 101 url, details.type, docDomain, |
| 97 isThirdParty(urlObj, docDomain), | 102 isThirdParty(urlObj, docDomain), |
| 98 getKey(page, frame) | 103 getKey(page, frame) |
| 99 ); | 104 ); |
| 100 | 105 |
| 101 if (!(filter instanceof WhitelistFilter)) | 106 if (!(filter instanceof WhitelistFilter)) |
| 102 { | 107 { |
| 103 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); | 108 let filterText = url.replace(/^[\w\-]+:\/+(?:www\.)?/, "||"); |
| 104 | 109 |
| 105 if (filters.indexOf(filterText) == -1) | 110 if (filters.indexOf(filterText) == -1) |
| 106 filters.push(filterText); | 111 filters.push(filterText); |
| 107 } | 112 } |
| 108 } | 113 } |
| 109 } | 114 } |
| 110 | 115 |
| 111 // If we couldn't generate any blocking filters, fallback to element hiding | 116 // If we couldn't generate any blocking filters, fallback to element hiding |
| 112 let selectors = []; | 117 let selectors = []; |
| 113 if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE")) | 118 if (filters.length == 0 && !isFrameWhitelisted(page, frame, "ELEMHIDE")) |
| 114 { | 119 { |
| 115 // Generate CSS selectors based on the element's "id" and "class" attribut e | 120 // Generate CSS selectors based on the element's "id" and "class" attribut e |
| 116 if (attributes.id) | 121 if (details.id) |
| 117 selectors.push("#" + escapeCSS(attributes.id)); | 122 selectors.push("#" + escapeCSS(details.id)); |
| 118 if (classes.length > 0) | 123 if (details.classes.length > 0) |
| 119 selectors.push(classes.map(c => "." + escapeCSS(c)).join("")); | 124 selectors.push(details.classes.map(c => "." + escapeCSS(c)).join("")); |
| 120 | 125 |
| 121 // 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, |
| 122 // generate a CSS selector matching the "src" attribute | 127 // generate a CSS selector matching the "src" attribute |
| 123 if (attributes.src) | 128 if (details.src) |
| 124 selectors.push(escapeCSS(tagName) + "[src=" + quoteCSS(attributes.src) + "]"); | 129 selectors.push(escapeCSS(details.tagName) + "[src=" + quoteCSS(details.s rc) + "]"); |
| 125 | 130 |
| 126 // As last resort, if there is a "style" attribute, and we couldn't genera te | 131 // As last resort, if there is a "style" attribute, and we couldn't genera te |
| 127 // any filters so far, generate a CSS selector matching the "style" attrib ute | 132 // any filters so far, generate a CSS selector matching the "style" attrib ute |
| 128 if (attributes.style && selectors.length == 0 && filters.length == 0) | 133 if (details.style && selectors.length == 0 && filters.length == 0) |
| 129 selectors.push(escapeCSS(tagName) + "[style=" + quoteCSS(attributes.styl e) + "]"); | 134 selectors.push(escapeCSS(details.tagName) + "[style=" + quoteCSS(details .style) + "]"); |
| 130 | 135 |
| 131 // Add an element hiding filter for each generated CSS selector | 136 // Add an element hiding filter for each generated CSS selector |
| 132 for (let selector of selectors) | 137 for (let selector of selectors) |
| 133 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); | 138 filters.push(docDomain.replace(/^www\./, "") + "##" + selector); |
| 134 } | 139 } |
| 135 } | 140 } |
| 136 | 141 |
| 137 return {filters: filters, selectors: selectors}; | 142 return {filters: filters, selectors: selectors}; |
| 138 } | 143 } |
| 139 exports.composeFilters = composeFilters; | 144 exports.composeFilters = composeFilters; |
| LEFT | RIGHT |