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

Side by Side Diff: lib/compat.js

Issue 29371763: Issue 4795 - Use modern JavaScript syntax (Closed)
Patch Set: Addressed some more feedback Created Jan. 18, 2017, 11:44 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 | « include.preload.js ('k') | lib/csp.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict";
19
18 // 20 //
19 // Module framework stuff 21 // Module framework stuff
20 // 22 //
21 23
22 function require(module) 24 function require(module)
23 { 25 {
24 if (!(module in require.scopes)) 26 if (!(module in require.scopes))
25 { 27 {
26 let scope = {exports: {}}; 28 let scope = {exports: {}};
27 require.scopes[module] = require.modules[module](scope, scope.exports); 29 require.scopes[module] = require.modules[module](scope, scope.exports);
28 } 30 }
29 return require.scopes[module]; 31 return require.scopes[module];
30 } 32 }
31 require.modules = Object.create(null); 33 require.modules = Object.create(null);
32 require.scopes = Object.create(null); 34 require.scopes = Object.create(null);
33 35
34 function importAll(module, globalObj) 36 function importAll(module, globalObj)
35 { 37 {
36 var exports = require(module); 38 let exports = require(module);
37 for (var key in exports) 39 for (let key in exports)
38 globalObj[key] = exports[key]; 40 globalObj[key] = exports[key];
39 } 41 }
40 42
41 onShutdown = { 43 let onShutdown = {
42 done: false, 44 done: false,
43 add: function() {}, 45 add() {},
44 remove: function() {} 46 remove() {}
45 }; 47 };
46 48
47 // 49 //
48 // XPCOM emulation 50 // XPCOM emulation
49 // 51 //
50 52
51 var Components = 53 let Components =
52 { 54 {
53 interfaces: 55 interfaces:
54 { 56 {
55 nsIFile: {DIRECTORY_TYPE: 0}, 57 nsIFile: {DIRECTORY_TYPE: 0},
56 nsIFileURL: function() {}, 58 nsIFileURL() {},
57 nsIHttpChannel: function() {}, 59 nsIHttpChannel() {},
58 nsITimer: {TYPE_REPEATING_SLACK: 0}, 60 nsITimer: {TYPE_REPEATING_SLACK: 0},
59 nsIInterfaceRequestor: null, 61 nsIInterfaceRequestor: null,
60 nsIChannelEventSink: null 62 nsIChannelEventSink: null
61 }, 63 },
62 classes: 64 classes:
63 { 65 {
64 "@mozilla.org/timer;1": 66 "@mozilla.org/timer;1":
65 { 67 {
66 createInstance: function() 68 createInstance() { return new FakeTimer(); }
67 {
68 return new FakeTimer();
69 }
70 }, 69 },
71 "@mozilla.org/xmlextras/xmlhttprequest;1": 70 "@mozilla.org/xmlextras/xmlhttprequest;1":
72 { 71 {
73 createInstance: function() 72 createInstance() { return new XMLHttpRequest(); }
74 {
75 return new XMLHttpRequest();
76 }
77 } 73 }
78 }, 74 },
79 results: {}, 75 results: {},
80 utils: { 76 utils: {
81 import: function() 77 import()
82 { 78 {
83 }, 79 },
84 reportError: function(e) 80 reportError(e)
85 { 81 {
86 console.error(e); 82 console.error(e);
87 console.trace(); 83 console.trace();
88 } 84 }
89 }, 85 },
90 manager: null, 86 manager: null,
91 ID: function() 87 ID() { return null; }
92 {
93 return null;
94 }
95 }; 88 };
96 const Cc = Components.classes; 89 const Cc = Components.classes;
97 const Ci = Components.interfaces; 90 const Ci = Components.interfaces;
98 const Cr = Components.results; 91 const Cr = Components.results;
99 const Cu = Components.utils; 92 const Cu = Components.utils;
100 93
101 var XPCOMUtils = 94 let XPCOMUtils =
102 { 95 {
103 generateQI: function() {} 96 generateQI() {}
104 }; 97 };
105 98
106 // 99 //
107 // Fake nsIFile implementation for our I/O 100 // Fake nsIFile implementation for our I/O
108 // 101 //
109 102
110 function FakeFile(path) 103 function FakeFile(path)
111 { 104 {
112 this.path = path; 105 this.path = path;
113 } 106 }
114 FakeFile.prototype = 107 FakeFile.prototype =
115 { 108 {
116 get leafName() 109 get leafName()
117 { 110 {
118 return this.path; 111 return this.path;
119 }, 112 },
120 set leafName(value) 113 set leafName(value)
121 { 114 {
122 this.path = value; 115 this.path = value;
123 }, 116 },
124 append: function(path) 117 append(path)
125 { 118 {
126 this.path += path; 119 this.path += path;
127 }, 120 },
128 clone: function() 121 clone()
129 { 122 {
130 return new FakeFile(this.path); 123 return new FakeFile(this.path);
131 }, 124 },
132 get parent() 125 get parent()
133 { 126 {
134 return {create: function() {}}; 127 return {create() {}};
135 }, 128 },
136 normalize: function() {} 129 normalize() {}
137 }; 130 };
138 131
139 // 132 //
140 // Services.jsm module emulation 133 // Services.jsm module emulation
141 // 134 //
142 135
143 var Services = 136 let Services =
144 { 137 {
145 obs: { 138 obs: {
146 addObserver: function() {}, 139 addObserver() {},
147 removeObserver: function() {} 140 removeObserver() {}
148 }, 141 },
149 vc: { 142 vc: {
150 compare: function(v1, v2) 143 compare(v1, v2)
151 { 144 {
152 function parsePart(s) 145 function parsePart(s)
153 { 146 {
154 if (!s) 147 if (!s)
155 return parsePart("0"); 148 return parsePart("0");
156 149
157 var part = { 150 let part = {
158 numA: 0, 151 numA: 0,
159 strB: "", 152 strB: "",
160 numC: 0, 153 numC: 0,
161 extraD: "" 154 extraD: ""
162 }; 155 };
163 156
164 if (s === "*") 157 if (s === "*")
165 { 158 {
166 part.numA = Number.MAX_VALUE; 159 part.numA = Number.MAX_VALUE;
167 return part; 160 return part;
168 } 161 }
169 162
170 var matches = s.match(/(\d*)(\D*)(\d*)(.*)/); 163 let matches = s.match(/(\d*)(\D*)(\d*)(.*)/);
171 part.numA = parseInt(matches[1], 10) || part.numA; 164 part.numA = parseInt(matches[1], 10) || part.numA;
172 part.strB = matches[2] || part.strB; 165 part.strB = matches[2] || part.strB;
173 part.numC = parseInt(matches[3], 10) || part.numC; 166 part.numC = parseInt(matches[3], 10) || part.numC;
174 part.extraD = matches[4] || part.extraD; 167 part.extraD = matches[4] || part.extraD;
175 168
176 if (part.strB == "+") 169 if (part.strB == "+")
177 { 170 {
178 part.numA++; 171 part.numA++;
179 part.strB = "pre"; 172 part.strB = "pre";
180 } 173 }
181 174
182 return part; 175 return part;
183 } 176 }
184 177
185 function comparePartElement(s1, s2) 178 function comparePartElement(s1, s2)
186 { 179 {
187 if (s1 === "" && s2 !== "") 180 if (s1 === "" && s2 !== "")
188 return 1; 181 return 1;
189 if (s1 !== "" && s2 === "") 182 if (s1 !== "" && s2 === "")
190 return -1; 183 return -1;
191 return s1 === s2 ? 0 : (s1 > s2 ? 1 : -1); 184 return s1 === s2 ? 0 : (s1 > s2 ? 1 : -1);
192 } 185 }
193 186
194 function compareParts(p1, p2) 187 function compareParts(p1, p2)
195 { 188 {
196 var result = 0; 189 let result = 0;
197 var elements = ["numA", "strB", "numC", "extraD"]; 190 let elements = ["numA", "strB", "numC", "extraD"];
198 elements.some(function(element) 191 elements.some(element =>
199 { 192 {
200 result = comparePartElement(p1[element], p2[element]); 193 result = comparePartElement(p1[element], p2[element]);
201 return result; 194 return result;
202 }); 195 });
203 return result; 196 return result;
204 } 197 }
205 198
206 var parts1 = v1.split("."); 199 let parts1 = v1.split(".");
207 var parts2 = v2.split("."); 200 let parts2 = v2.split(".");
208 for (var i = 0; i < Math.max(parts1.length, parts2.length); i++) 201 for (let i = 0; i < Math.max(parts1.length, parts2.length); i++)
209 { 202 {
210 var result = compareParts(parsePart(parts1[i]), parsePart(parts2[i])); 203 let result = compareParts(parsePart(parts1[i]), parsePart(parts2[i]));
211 if (result) 204 if (result)
212 return result; 205 return result;
213 } 206 }
214 return 0; 207 return 0;
215 } 208 }
216 } 209 }
217 } 210 }
218 211
219 // 212 //
220 // FileUtils.jsm module emulation 213 // FileUtils.jsm module emulation
221 // 214 //
222 215
223 var FileUtils = 216 let FileUtils =
224 { 217 {
225 PERMS_DIRECTORY: 0 218 PERMS_DIRECTORY: 0
226 }; 219 };
227 220
228 function FakeTimer() 221 function FakeTimer()
229 { 222 {
230 } 223 }
231 FakeTimer.prototype = 224 FakeTimer.prototype =
232 { 225 {
233 delay: 0, 226 delay: 0,
234 callback: null, 227 callback: null,
235 initWithCallback: function(callback, delay) 228 initWithCallback(callback, delay)
236 { 229 {
237 this.callback = callback; 230 this.callback = callback;
238 this.delay = delay; 231 this.delay = delay;
239 this.scheduleTimeout(); 232 this.scheduleTimeout();
240 }, 233 },
241 scheduleTimeout: function() 234 scheduleTimeout()
242 { 235 {
243 var me = this; 236 window.setTimeout(() =>
244 window.setTimeout(function()
245 { 237 {
246 try 238 try
247 { 239 {
248 me.callback(); 240 this.callback();
249 } 241 }
250 catch(e) 242 catch(e)
251 { 243 {
252 Cu.reportError(e); 244 Cu.reportError(e);
253 } 245 }
254 me.scheduleTimeout(); 246 this.scheduleTimeout();
255 }, this.delay); 247 }, this.delay);
256 } 248 }
257 }; 249 };
258 250
259 // 251 //
260 // Add a channel property to XMLHttpRequest, Synchronizer needs it 252 // Add a channel property to XMLHttpRequest, Synchronizer needs it
261 // 253 //
262 254
263 XMLHttpRequest.prototype.channel = 255 XMLHttpRequest.prototype.channel =
264 { 256 {
265 status: -1, 257 status: -1,
266 notificationCallbacks: {}, 258 notificationCallbacks: {},
267 loadFlags: 0, 259 loadFlags: 0,
268 INHIBIT_CACHING: 0, 260 INHIBIT_CACHING: 0,
269 VALIDATE_ALWAYS: 0, 261 VALIDATE_ALWAYS: 0,
270 QueryInterface: function() 262 QueryInterface()
271 { 263 {
272 return this; 264 return this;
273 } 265 }
274 }; 266 };
OLDNEW
« no previous file with comments | « include.preload.js ('k') | lib/csp.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld