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

Delta Between Two Patch Sets: include.preload.js

Issue 29710595: Issue 6433 - Use generators in getURLsFromElement (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Extract localName Created Feb. 28, 2018, 1:37 a.m.
Right Patch Set: Refactor (fixed) Created March 1, 2018, 9:37 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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* getURLsFromObjectParamChildren({children: [...children]})
39 {
40 const names = [
41 "movie", // Adobe Flash
42 "source", // Silverlight
43 "src", // Real Media + Quicktime
44 "FileName" // Windows Media
45 ];
46
47 let param = children.find(
48 child => child.localName == "param" &&
49 names.includes(child.getAttribute("name")) &&
50 child.getAttribute("value")
51 );
52
53 if (param)
54 yield param.getAttribute("value");
55 }
56
38 function* getURLsFromObjectElement(element) 57 function* getURLsFromObjectElement(element)
39 { 58 {
40 let url = element.getAttribute("data"); 59 let data = element.getAttribute("data");
41 if (url) 60 if (data)
42 { 61 yield data;
43 yield url; 62 else
44 return; 63 yield* getURLsFromObjectParamChildren(element);
45 } 64 }
46 65
47 for (let child of element.children) 66 function* getURLsFromMediaSourceChildren({children: [...children]})
48 { 67 {
49 if (child.localName != "param") 68 let sources = children.filter(
50 continue; 69 ({localName}) => ["source", "track"].includes(localName)
51 70 );
52 let name = child.getAttribute("name"); 71
53 if (name != "movie" && // Adobe Flash 72 for (let source of sources)
54 name != "source" && // Silverlight 73 yield* getURLsFromAttributes(source);
55 name != "src" && // Real Media + Quicktime 74 }
56 name != "FileName") // Windows Media 75
57 continue; 76 function* getURLsFromMediaElement(element)
58 77 {
59 let value = child.getAttribute("value"); 78 yield* getURLsFromAttributes(element);
60 if (!value) 79 yield* getURLsFromMediaSourceChildren(element);
61 continue; 80
62 81 yield element.poster;
63 yield value; 82 }
64 return; 83
65 } 84 function* getURLsFromAttributes({src, srcset = ""})
66 }
67
68 function* getURLsFromAttributes({src = "", srcset = ""})
69 { 85 {
70 yield src; 86 yield src;
71 87
72 for (let candidate of srcset.split(",")) 88 yield* srcset.split(",").map(url => url.trim().replace(/\s+\S+$/, ""));
73 yield candidate.trim().replace(/\s+\S+$/, "");
74 }
75
76 function* getURLsFromMediaElement(element)
77 {
78 yield* getURLsFromAttributes(element);
79
80 for (let child of element.children)
81 {
82 if (child.localName == "source" || child.localName == "track")
83 yield* getURLsFromAttributes(child);
84 }
85
86 yield element.poster;
87 } 89 }
88 90
89 function getURLsFromElement(element) 91 function getURLsFromElement(element)
90 { 92 {
91 let urls = null; 93 let urls = null;
92 94
93 let {localName} = element; 95 let {localName} = element;
94 96
95 if (localName == "object") 97 if (localName == "object")
96 urls = getURLsFromObjectElement(element); 98 urls = getURLsFromObjectElement(element);
97 else if (["video", "audio", "picture"].includes(localName)) 99 else if (["video", "audio", "picture"].includes(localName))
98 urls = getURLsFromMediaElement(element); 100 urls = getURLsFromMediaElement(element);
99 else 101 else
100 urls = getURLsFromAttributes(element); 102 urls = getURLsFromAttributes(element);
101 103
102 return Array.from(urls).filter( 104 return [...urls].filter(url => url && !/^(?!https?:)[\w-]+:/i.test(url));
103 url => url && !/^(?!https?:)[\w-]+:/i.test(url)
104 );
105 } 105 }
106 106
107 function hideElement(element) 107 function hideElement(element)
108 { 108 {
109 function doHide() 109 function doHide()
110 { 110 {
111 let propertyName = "display"; 111 let propertyName = "display";
112 let propertyValue = "none"; 112 let propertyValue = "none";
113 if (element.localName == "frame") 113 if (element.localName == "frame")
114 { 114 {
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 let element = event.target; 513 let element = event.target;
514 if (/^i?frame$/.test(element.localName)) 514 if (/^i?frame$/.test(element.localName))
515 checkCollapse(element); 515 checkCollapse(element);
516 }, true); 516 }, true);
517 } 517 }
518 518
519 window.checkCollapse = checkCollapse; 519 window.checkCollapse = checkCollapse;
520 window.elemhide = elemhide; 520 window.elemhide = elemhide;
521 window.typeMap = typeMap; 521 window.typeMap = typeMap;
522 window.getURLsFromElement = getURLsFromElement; 522 window.getURLsFromElement = getURLsFromElement;
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld