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

Side by Side Diff: include.preload.js

Issue 29710595: Issue 6433 - Use generators in getURLsFromElement (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Use functional style Created Feb. 28, 2018, 2:24 a.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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 17 matching lines...) Expand all
28 ["input", "IMAGE"], 28 ["input", "IMAGE"],
29 ["picture", "IMAGE"], 29 ["picture", "IMAGE"],
30 ["audio", "MEDIA"], 30 ["audio", "MEDIA"],
31 ["video", "MEDIA"], 31 ["video", "MEDIA"],
32 ["frame", "SUBDOCUMENT"], 32 ["frame", "SUBDOCUMENT"],
33 ["iframe", "SUBDOCUMENT"], 33 ["iframe", "SUBDOCUMENT"],
34 ["object", "OBJECT"], 34 ["object", "OBJECT"],
35 ["embed", "OBJECT"] 35 ["embed", "OBJECT"]
36 ]); 36 ]);
37 37
38 function getURLsFromObjectElement(element) 38 function* getURLsFromObjectElement(element)
39 { 39 {
40 let url = element.getAttribute("data"); 40 const paramNames = [
41 if (url) 41 "movie", // Adobe Flash
42 return [url]; 42 "source", // Silverlight
43 "src", // Real Media + Quicktime
44 "FileName" // Windows Media
45 ];
46
47 let data = element.getAttribute("data");
48 if (data)
49 {
50 yield data;
51 return;
52 }
53
54 let param = [].find.call(
55 element.children,
56 child => child.localName == "param" &&
57 paramNames.includes(child.getAttribute("name")) &&
58 child.getAttribute("value")
59 );
60
61 if (param)
62 yield param.getAttribute("value");
63 }
64
65 function* getURLsFromAttributes({src = "", srcset = ""})
66 {
67 yield src;
68
69 yield* srcset.split(",").map(url => url.trim().replace(/\s+\S+$/, ""));
70 }
71
72 function* getURLsFromMediaElement(element)
73 {
74 yield* getURLsFromAttributes(element);
43 75
44 for (let child of element.children) 76 for (let child of element.children)
45 { 77 {
46 if (child.localName != "param") 78 if (["source", "track"].includes(child.localName))
47 continue; 79 yield* getURLsFromAttributes(child);
48
49 let name = child.getAttribute("name");
50 if (name != "movie" && // Adobe Flash
51 name != "source" && // Silverlight
52 name != "src" && // Real Media + Quicktime
53 name != "FileName") // Windows Media
54 continue;
55
56 let value = child.getAttribute("value");
57 if (!value)
58 continue;
59
60 return [value];
61 } 80 }
62 81
63 return []; 82 yield element.poster;
64 }
65
66 function getURLsFromAttributes(element)
67 {
68 let urls = [];
69
70 if (element.src)
71 urls.push(element.src);
72
73 if (element.srcset)
74 {
75 for (let candidate of element.srcset.split(","))
76 {
77 let url = candidate.trim().replace(/\s+\S+$/, "");
78 if (url)
79 urls.push(url);
80 }
81 }
82
83 return urls;
84 }
85
86 function getURLsFromMediaElement(element)
87 {
88 let urls = getURLsFromAttributes(element);
89
90 for (let child of element.children)
91 {
92 if (child.localName == "source" || child.localName == "track")
93 urls.push(...getURLsFromAttributes(child));
94 }
95
96 if (element.poster)
97 urls.push(element.poster);
98
99 return urls;
100 } 83 }
101 84
102 function getURLsFromElement(element) 85 function getURLsFromElement(element)
103 { 86 {
104 let urls; 87 let urls = null;
105 switch (element.localName)
106 {
107 case "object":
108 urls = getURLsFromObjectElement(element);
109 break;
110 88
111 case "video": 89 let {localName} = element;
112 case "audio":
113 case "picture":
114 urls = getURLsFromMediaElement(element);
115 break;
116 90
117 default: 91 if (localName == "object")
118 urls = getURLsFromAttributes(element); 92 urls = getURLsFromObjectElement(element);
119 break; 93 else if (["video", "audio", "picture"].includes(localName))
120 } 94 urls = getURLsFromMediaElement(element);
95 else
96 urls = getURLsFromAttributes(element);
121 97
122 for (let i = 0; i < urls.length; i++) 98 return Array.from(urls).filter(
123 { 99 url => url && !/^(?!https?:)[\w-]+:/i.test(url)
124 if (/^(?!https?:)[\w-]+:/i.test(urls[i])) 100 );
125 urls.splice(i--, 1);
126 }
127
128 return urls;
129 } 101 }
130 102
131 function hideElement(element) 103 function hideElement(element)
132 { 104 {
133 function doHide() 105 function doHide()
134 { 106 {
135 let propertyName = "display"; 107 let propertyName = "display";
136 let propertyValue = "none"; 108 let propertyValue = "none";
137 if (element.localName == "frame") 109 if (element.localName == "frame")
138 { 110 {
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 let element = event.target; 509 let element = event.target;
538 if (/^i?frame$/.test(element.localName)) 510 if (/^i?frame$/.test(element.localName))
539 checkCollapse(element); 511 checkCollapse(element);
540 }, true); 512 }, true);
541 } 513 }
542 514
543 window.checkCollapse = checkCollapse; 515 window.checkCollapse = checkCollapse;
544 window.elemhide = elemhide; 516 window.elemhide = elemhide;
545 window.typeMap = typeMap; 517 window.typeMap = typeMap;
546 window.getURLsFromElement = getURLsFromElement; 518 window.getURLsFromElement = getURLsFromElement;
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