Blame view

static/plugins/webshim-gh-pages/js-webshim/dev/shims/url.js 8.72 KB
831eac332   zhuzhenchao   add file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  webshim.register('url', function($, webshims, window, document, undefined, options){
  'use strict';
  
  // URL Polyfill
  // Draft specification: http://url.spec.whatwg.org
  
  // Notes:
  // - Primarily useful for parsing URLs and modifying query parameters
  // - Should work in IE8+ and everything more modern
  
  	(function (global) {
  		// Browsers may have:
  		// * No global URL object
  		// * URL with static methods only - may have a dummy constructor
  		// * URL with members except searchParams
  		// * Full URL API support
  		var origURL = global.URL;
  		var nativeURL;
  		try {
  			if (origURL) {
  				nativeURL = new global.URL('http://example.com');
  				if ('searchParams' in nativeURL)
  					return;
  				if (!('href' in nativeURL))
  					nativeURL = undefined;
  			}
  		} catch (_) {}
  
  		function URLUtils(url) {
  			if (nativeURL)
  				return new origURL(url);
  			var anchor = document.createElement('a');
  			anchor.href = url;
  			return anchor;
  		}
  
  		global.URL = function URL(url, base) {
  			if (!(this instanceof global.URL))
  				throw new TypeError("Failed to construct 'URL': Please use the 'new' operator.");
  
  			if (base) {
  				url = (function () {
  					if (nativeURL) return new origURL(url, base).href;
  
  					var doc;
  					// Use another document/base tag/anchor for relative URL resolution, if possible
  					if (document.implementation && document.implementation.createHTMLDocument) {
  						doc = document.implementation.createHTMLDocument('');
  					} else if (document.implementation && document.implementation.createDocument) {
  						doc = document.implementation.createElement('http://www.w3.org/1999/xhtml', 'html', null);
  						doc.documentElement.appendChild(doc.createElement('head'));
  						doc.documentElement.appendChild(doc.createElement('body'));
  					} else if (window.ActiveXObject) {
  						doc = new window.ActiveXObject('htmlfile');
  						doc.write('<head></head><body></body>');
  						doc.close();
  					}
  
  					if (!doc) throw Error('base not supported');
  
  					var baseTag = doc.createElement('base');
  					baseTag.href = base;
  					doc.getElementsByTagName('head')[0].appendChild(baseTag);
  					var anchor = doc.createElement('a');
  					anchor.href = url;
  					return anchor.href;
  				}());
  			}
  
  			// An inner object implementing URLUtils (either a native URL
  			// object or an HTMLAnchorElement instance) is used to perform the
  			// URL algorithms. With full ES5 getter/setter support, return a
  			// regular object For IE8's limited getter/setter support, a
  			// different HTMLAnchorElement is returned with properties
  			// overridden
  
  			var instance = URLUtils(url || '');
  
  			// Detect for ES5 getter/setter support
  			var ES5_GET_SET = (Object.defineProperties && (function () {
  				var o = {}; Object.defineProperties(o, { p: { 'get': function () { return true; } } }); return o.p;
  			}()));
  
  			var self = ES5_GET_SET ? this : document.createElement('a');
  
  			// NOTE: Doesn't do the encoding/decoding dance
  			function parse(input, isindex) {
  				var sequences = input.split('&');
  				if (isindex && sequences[0].indexOf('=') === -1)
  					sequences[0] = '=' + sequences[0];
  				var pairs = [];
  				sequences.forEach(function (bytes) {
  					if (bytes.length === 0) return;
  					var index = bytes.indexOf('=');
  					if (index !== -1) {
  						var name = bytes.substring(0, index);
  						var value = bytes.substring(index + 1);
  					} else {
  						name = bytes;
  						value = '';
  					}
  					name = name.replace(/\+/g, ' ');
  					value = value.replace(/\+/g, ' ');
  					pairs.push({ name: name, value: value });
  				});
  				var output = [];
  				pairs.forEach(function (pair) {
  					output.push({
  						name: decodeURIComponent(pair.name),
  						value: decodeURIComponent(pair.value)
  					});
  				});
  				return output;
  			}
  
  			function URLSearchParams(url_object, init) {
  				var pairs = [];
  				if (init)
  					pairs = parse(init);
  
  				this._setPairs = function (list) { pairs = list; };
  				this._updateSteps = function () { updateSteps(); };
  
  				var updating = false;
  				function updateSteps() {
  					if (updating) return;
  					updating = true;
  
  					// TODO: For all associated url objects
  					url_object.search = serialize(pairs);
  
  					updating = false;
  				}
  
  				// NOTE: Doesn't do the encoding/decoding dance
  				function serialize(pairs) {
  					var output = '', first = true;
  					pairs.forEach(function (pair) {
  						var name = encodeURIComponent(pair.name);
  						var value = encodeURIComponent(pair.value);
  						if (!first) output += '&';
  						output += name + '=' + value;
  						first = false;
  					});
  					return output.replace(/%20/g, '+');
  				}
  
  				Object.defineProperties(this, {
  					append: {
  						value: function (name, value) {
  							pairs.push({ name: name, value: value });
  							updateSteps();
  						}
  					},
  
  					'delete': {
  						value: function (name) {
  							for (var i = 0; i < pairs.length;) {
  								if (pairs[i].name === name)
  									pairs.splice(i, 1);
  								else
  									++i;
  							}
  							updateSteps();
  						}
  					},
  
  					get: {
  						value: function (name) {
  							for (var i = 0; i < pairs.length; ++i) {
  								if (pairs[i].name === name)
  									return pairs[i].value;
  							}
  							return null;
  						}
  					},
  
  					getAll: {
  						value: function (name) {
  							var result = [];
  							for (var i = 0; i < pairs.length; ++i) {
  								if (pairs[i].name === name)
  									result.push(pairs[i].value);
  							}
  							return result;
  						}
  					},
  
  					has: {
  						value: function (name) {
  							for (var i = 0; i < pairs.length; ++i) {
  								if (pairs[i].name === name)
  									return true;
  							}
  							return false;
  						}
  					},
  
  					set: {
  						value: function (name, value) {
  							var found = false;
  							for (var i = 0; i < pairs.length;) {
  								if (pairs[i].name === name) {
  									if (!found) {
  										pairs[i].value = value;
  										found = true;
  										++i;
  									} else {
  										pairs.splice(i, 1);
  									}
  								} else {
  									++i;
  								}
  							}
  
  							if (!found)
  								pairs.push({ name: name, value: value });
  
  							updateSteps();
  						}
  					},
  
  					toString: {
  						value: function () {
  							return serialize(pairs);
  						}
  					}
  				});
  			};
  
  			var queryObject = new URLSearchParams(
  				self, instance.search ? instance.search.substring(1) : null);
  
  			Object.defineProperties(self, {
  				href: {
  					get: function () { return instance.href; },
  					set: function (v) { instance.href = v; tidy_instance(); update_steps(); }
  				},
  				origin: {
  					get: function () {
  						if ('origin' in instance) return instance.origin;
  						return this.protocol + '//' + this.host;
  					}
  				},
  				protocol: {
  					get: function () { return instance.protocol; },
  					set: function (v) { instance.protocol = v; }
  				},
  				username: {
  					get: function () { return instance.username; },
  					set: function (v) { instance.username = v; }
  				},
  				password: {
  					get: function () { return instance.password; },
  					set: function (v) { instance.password = v; }
  				},
  				host: {
  					get: function () {
  						// IE returns default port in |host|
  						var re = {'http:': /:80$/, 'https:': /:443$/, 'ftp:': /:21$/}[instance.protocol];
  						return re ? instance.host.replace(re, '') : instance.host;
  					},
  					set: function (v) { instance.host = v; }
  				},
  				hostname: {
  					get: function () { return instance.hostname; },
  					set: function (v) { instance.hostname = v; }
  				},
  				port: {
  					get: function () { return instance.port; },
  					set: function (v) { instance.port = v; }
  				},
  				pathname: {
  					get: function () {
  						// IE does not include leading '/' in |pathname|
  						if (instance.pathname.charAt(0) !== '/') return '/' + instance.pathname;
  						return instance.pathname;
  					},
  					set: function (v) { instance.pathname = v; }
  				},
  				search: {
  					get: function () { return instance.search; },
  					set: function (v) {
  						if (instance.search === v) return;
  						instance.search = v; tidy_instance(); update_steps();
  					}
  				},
  				searchParams: {
  					get: function () { return queryObject; }
  					// TODO: implement setter
  				},
  				hash: {
  					get: function () { return instance.hash; },
  					set: function (v) { instance.hash = v; tidy_instance(); }
  				},
  				toString: {
  					value: function() { return instance.toString(); }
  				},
  				valueOf: {
  					value: function() { return instance.valueOf(); }
  				}
  			});
  
  			function tidy_instance() {
  				var href = instance.href.replace(/#$|\?$|\?(?=#)/g, '');
  				if (instance.href !== href)
  					instance.href = href;
  			}
  
  			function update_steps() {
  				queryObject._setPairs(instance.search ? parse(instance.search.substring(1)) : []);
  				queryObject._updateSteps();
  			};
  
  			return self;
  		};
  
  		if (origURL) {
  			for (var i in origURL) {
  				if (origURL.hasOwnProperty(i))
  					global.URL[i] = origURL[i];
  			}
  		}
  
  	}(window));
  
  });