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

Unified Diff: include.preload.js

Issue 29710595: Issue 6433 - Use generators in getURLsFromElement (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Feb. 28, 2018, 12:54 a.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 | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -30,21 +30,24 @@
["audio", "MEDIA"],
["video", "MEDIA"],
["frame", "SUBDOCUMENT"],
["iframe", "SUBDOCUMENT"],
["object", "OBJECT"],
["embed", "OBJECT"]
]);
-function getURLsFromObjectElement(element)
+function* getURLsFromObjectElement(element)
{
let url = element.getAttribute("data");
if (url)
- return [url];
+ {
+ yield url;
+ return;
+ }
for (let child of element.children)
{
if (child.localName != "param")
continue;
let name = child.getAttribute("name");
if (name != "movie" && // Adobe Flash
@@ -52,85 +55,55 @@
name != "src" && // Real Media + Quicktime
name != "FileName") // Windows Media
continue;
let value = child.getAttribute("value");
if (!value)
continue;
- return [value];
+ yield value;
+ return;
}
-
- return [];
}
-function getURLsFromAttributes(element)
+function* getURLsFromAttributes({src = "", srcset = ""})
{
- let urls = [];
-
- if (element.src)
- urls.push(element.src);
+ yield src;
- if (element.srcset)
- {
- for (let candidate of element.srcset.split(","))
- {
- let url = candidate.trim().replace(/\s+\S+$/, "");
- if (url)
- urls.push(url);
- }
- }
-
- return urls;
+ for (let candidate of srcset.split(","))
+ yield candidate.trim().replace(/\s+\S+$/, "");
}
-function getURLsFromMediaElement(element)
+function* getURLsFromMediaElement(element)
{
- let urls = getURLsFromAttributes(element);
+ yield* getURLsFromAttributes(element);
for (let child of element.children)
{
if (child.localName == "source" || child.localName == "track")
- urls.push(...getURLsFromAttributes(child));
+ yield* getURLsFromAttributes(child);
}
- if (element.poster)
- urls.push(element.poster);
-
- return urls;
+ yield element.poster;
}
function getURLsFromElement(element)
{
- let urls;
- switch (element.localName)
- {
- case "object":
- urls = getURLsFromObjectElement(element);
- break;
-
- case "video":
- case "audio":
- case "picture":
- urls = getURLsFromMediaElement(element);
- break;
+ let urls = null;
+ if (element.localName == "object")
+ urls = getURLsFromObjectElement(element);
+ else if (["video", "audio", "picture"].includes(element.localName))
+ urls = getURLsFromMediaElement(element);
+ else
+ urls = getURLsFromAttributes(element);
- default:
- urls = getURLsFromAttributes(element);
- break;
- }
-
- for (let i = 0; i < urls.length; i++)
- {
- if (/^(?!https?:)[\w-]+:/i.test(urls[i]))
- urls.splice(i--, 1);
- }
-
- return urls;
+ return Array.from(urls).filter(
+ url => url && !/^(?!https?:)[\w-]+:/i.test(url)
+ );
}
function hideElement(element)
{
function doHide()
{
let propertyName = "display";
let propertyValue = "none";
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld