forked from jeresig/env-js
/
location.js
277 lines (249 loc) · 8.45 KB
/
location.js
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
/**
* Location
*
* Mozilla MDC:
* https://developer.mozilla.org/En/DOM/Window.location
* https://developer.mozilla.org/en/DOM/document.location
*
* HTML5: 6.10.4 The Location interface
* http://dev.w3.org/html5/spec/Overview.html#location
*
* HTML5: 2.5.3 Interfaces for URL manipulation
* http://dev.w3.org/html5/spec/Overview.html#url-decomposition-idl-attributes
* All of section 2.5 is worth reading, but 2.5.3 contains very
* detailed information on how getters/setter should work
*
* NOT IMPLEMENTED:
* HTML5: Section 6.10.4.1 Security -- prevents scripts from another domain
* from accessing most of the 'Location'
* Not sure if anyone implements this in HTML4
*/
Location = function(url, doc, history) {
//console.log('Location url %s', url);
var $url = url,
$document = doc ? doc : null,
$history = history ? history : null;
var parts = urlparse.urlsplit($url);
return {
get hash() {
return parts.fragment ? '#' + parts.fragment : parts.fragment;
},
set hash(s) {
if (s[0] === '#') {
parts.fragment = s.substr(1);
} else {
parts.fragment = s;
}
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add($url, 'hash');
}
},
get host() {
return parts.netloc;
},
set host(s) {
if (!s || s === '') {
return;
}
parts.netloc = s;
$url = urlparse.urlunsplit(parts);
// this regenerates hostname & port
parts = urlparse.urlsplit($url);
if ($history) {
$history.add( $url, 'host');
}
this.assign($url);
},
get hostname() {
return parts.hostname;
},
set hostname(s) {
if (!s || s === '') {
return;
}
parts.netloc = s;
if (parts.port != '') {
parts.netloc += ':' + parts.port;
}
parts.hostname = s;
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add( $url, 'hostname');
}
this.assign($url);
},
get href() {
return $url;
},
set href(url) {
$url = url;
if ($history) {
$history.add($url, 'href');
}
this.assign($url);
},
get pathname() {
return parts.path;
},
set pathname(s) {
if (s[0] === '/') {
parts.path = s;
} else {
parts.path = '/' + s;
}
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add($url, 'pathname');
}
this.assign($url);
},
get port() {
// make sure it's a string
return '' + parts.port;
},
set port(p) {
// make a string
var s = '' + p;
parts.port = s;
parts.netloc = parts.hostname + ':' + parts.port;
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add( $url, 'port');
}
this.assign($url);
},
get protocol() {
return parts.scheme + ':';
},
set protocol(s) {
var i = s.indexOf(':');
if (i != -1) {
s = s.substr(0,i);
}
parts.scheme = s;
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add($url, 'protocol');
}
this.assign($url);
},
get search() {
return (parts.query) ? '?' + parts.query : parts.query;
},
set search(s) {
if (s[0] == '?') {
s = s.substr(1);
}
parts.query = s;
$url = urlparse.urlunsplit(parts);
if ($history) {
$history.add($url, 'search');
}
this.assign($url);
},
toString: function() {
return $url;
},
assign: function(url) {
var _this = this;
var xhr;
var event;
//console.log('assigning %s',url);
$url = url;
//we can only assign if this Location is associated with a document
if ($document) {
//console.log('fetching %s (async? %s)', url, $document.async);
xhr = new XMLHttpRequest();
// TODO: make async flag a Envjs paramter
xhr.open('GET', url, false);//$document.async);
// TODO: is there a better way to test if a node is an HTMLDocument?
if ($document.toString() === '[object HTMLDocument]') {
//tell the xhr to not parse the document as XML
//console.log('loading html document');
xhr.onreadystatechange = function() {
//console.log('readyState %s', xhr.readyState);
if (xhr.readyState === 4) {
$document.baseURI = new Location(url, $document);
//console.log('new document baseURI %s', $document.baseURI);
__exchangeHTMLDocument__($document, xhr.responseText, url);
}
};
xhr.send(null, false);
} else {
//Treat as an XMLDocument
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
$document = xhr.responseXML;
$document.baseURI = $url;
if ($document.createEvent) {
event = $document.createEvent('Event');
event.initEvent('DOMContentLoaded');
$document.dispatchEvent( event, false );
}
}
};
xhr.send();
}
};
},
reload: function(forceget) {
//for now we have no caching so just proxy to assign
//console.log('reloading %s',$url);
this.assign($url);
},
replace: function(url) {
this.assign(url);
}
};
};
var __exchangeHTMLDocument__ = function(doc, text, url) {
var html, head, title, body, event, e;
try {
doc.baseURI = url;
HTMLParser.parseDocument(text, doc);
Envjs.wait();
} catch (e) {
console.log('parsererror %s', e);
try {
console.log('document \n %s', doc.documentElement.outerHTML);
} catch (e) {
// swallow
}
doc = new HTMLDocument(new DOMImplementation(), doc.ownerWindow);
html = doc.createElement('html');
head = doc.createElement('head');
title = doc.createElement('title');
body = doc.createElement('body');
title.appendChild(doc.createTextNode('Error'));
body.appendChild(doc.createTextNode('' + e));
head.appendChild(title);
html.appendChild(head);
html.appendChild(body);
doc.appendChild(html);
//console.log('default error document \n %s', doc.documentElement.outerHTML);
//DOMContentLoaded event
if (doc.createEvent) {
event = doc.createEvent('Event');
event.initEvent('DOMContentLoaded', false, false);
doc.dispatchEvent( event, false );
event = doc.createEvent('HTMLEvents');
event.initEvent('load', false, false);
doc.dispatchEvent( event, false );
}
//finally fire the window.onload event
//TODO: this belongs in window.js which is a event
// event handler for DOMContentLoaded on document
try {
if (doc === window.document) {
console.log('triggering window.load');
event = doc.createEvent('HTMLEvents');
event.initEvent('load', false, false);
window.dispatchEvent( event, false );
}
} catch (e) {
//console.log('window load event failed %s', e);
//swallow
}
}; /* closes return {... */
};