forked from jeresig/env-js
/
document.js
638 lines (610 loc) · 22 KB
/
document.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/**
* @class HTMLDocument
* The Document interface represents the entire HTML or XML document.
* Conceptually, it is the root of the document tree, and provides
* the primary access to the document's data.
*
* @extends Document
*/
HTMLDocument = function(implementation, ownerWindow, referrer) {
Document.apply(this, arguments);
this.referrer = referrer || '';
this.baseURI = "about:blank";
this.ownerWindow = ownerWindow;
};
HTMLDocument.prototype = new Document();
__extend__(HTMLDocument.prototype, {
createElement: function(tagName){
var node;
tagName = tagName.toUpperCase();
// create Element specifying 'this' as ownerDocument
// This is an html document so we need to use explicit interfaces per the
//TODO: would be much faster as a big switch
switch(tagName){
case "A":
node = new HTMLAnchorElement(this);break;
case "AREA":
node = new HTMLAreaElement(this);break;
case "BASE":
node = new HTMLBaseElement(this);break;
case "BLOCKQUOTE":
node = new HTMLQuoteElement(this);break;
case "CANVAS":
node = new HTMLCanvasElement(this);break;
case "Q":
node = new HTMLQuoteElement(this);break;
case "BODY":
node = new HTMLBodyElement(this);break;
case "BR":
node = new HTMLBRElement(this);break;
case "BUTTON":
node = new HTMLButtonElement(this);break;
case "CAPTION":
node = new HTMLElement(this);break;
case "COL":
node = new HTMLTableColElement(this);break;
case "COLGROUP":
node = new HTMLTableColElement(this);break;
case "DEL":
node = new HTMLModElement(this);break;
case "INS":
node = new HTMLModElement(this);break;
case "DIV":
node = new HTMLDivElement(this);break;
case "DL":
node = new HTMLDListElement(this);break;
case "DT":
node = new HTMLElement(this); break;
case "FIELDSET":
node = new HTMLFieldSetElement(this);break;
case "FORM":
node = new HTMLFormElement(this);break;
case "FRAME":
node = new HTMLFrameElement(this);break;
case "H1":
node = new HTMLHeadingElement(this);break;
case "H2":
node = new HTMLHeadingElement(this);break;
case "H3":
node = new HTMLHeadingElement(this);break;
case "H4":
node = new HTMLHeadingElement(this);break;
case "H5":
node = new HTMLHeadingElement(this);break;
case "H6":
node = new HTMLHeadingElement(this);break;
case "HEAD":
node = new HTMLHeadElement(this);break;
case "HR":
node = new HTMLHRElement(this);break;
case "HTML":
node = new HTMLHtmlElement(this);break;
case "IFRAME":
node = new HTMLIFrameElement(this);break;
case "IMG":
node = new HTMLImageElement(this);break;
case "INPUT":
node = new HTMLInputElement(this);break;
case "LABEL":
node = new HTMLLabelElement(this);break;
case "LEGEND":
node = new HTMLLegendElement(this);break;
case "LI":
node = new HTMLLIElement(this);break;
case "LINK":
node = new HTMLLinkElement(this);break;
case "MAP":
node = new HTMLMapElement(this);break;
case "META":
node = new HTMLMetaElement(this);break;
case "NOSCRIPT":
node = new HTMLElement(this);break;
case "OBJECT":
node = new HTMLObjectElement(this);break;
case "OPTGROUP":
node = new HTMLOptGroupElement(this);break;
case "OL":
node = new HTMLOListElement(this); break;
case "OPTION":
node = new HTMLOptionElement(this);break;
case "P":
node = new HTMLParagraphElement(this);break;
case "PARAM":
node = new HTMLParamElement(this);break;
case "PRE":
node = new HTMLPreElement(this);break;
case "SCRIPT":
node = new HTMLScriptElement(this);break;
case "SELECT":
node = new HTMLSelectElement(this);break;
case "SMALL":
node = new HTMLElement(this);break;
case "SPAN":
node = new HTMLSpanElement(this);break;
case "STRONG":
node = new HTMLElement(this);break;
case "STYLE":
node = new HTMLStyleElement(this);break;
case "TABLE":
node = new HTMLTableElement(this);break;
case "TBODY":
node = new HTMLTableSectionElement(this);break;
case "TFOOT":
node = new HTMLTableSectionElement(this);break;
case "THEAD":
node = new HTMLTableSectionElement(this);break;
case "TD":
node = new HTMLTableDataCellElement(this);break;
case "TH":
node = new HTMLTableHeaderCellElement(this);break;
case "TEXTAREA":
node = new HTMLTextAreaElement(this);break;
case "TITLE":
node = new HTMLTitleElement(this);break;
case "TR":
node = new HTMLTableRowElement(this);break;
case "UL":
node = new HTMLUListElement(this);break;
default:
node = new HTMLUnknownElement(this);
}
// assign values to properties (and aliases)
node.nodeName = tagName;
return node;
},
createElementNS : function (uri, local) {
//print('createElementNS :'+uri+" "+local);
if(!uri){
return this.createElement(local);
}else if ("http://www.w3.org/1999/xhtml" == uri) {
return this.createElement(local);
} else if ("http://www.w3.org/1998/Math/MathML" == uri) {
return this.createElement(local);
} else {
return Document.prototype.createElementNS.apply(this,[uri, local]);
}
},
get anchors(){
return new HTMLCollection(this.getElementsByTagName('a'));
},
get applets(){
return new HTMLCollection(this.getElementsByTagName('applet'));
},
get documentElement(){
var html = Document.prototype.__lookupGetter__('documentElement').apply(this,[]);
if( html === null){
html = this.createElement('html');
this.appendChild(html);
html.appendChild(this.createElement('head'));
html.appendChild(this.createElement('body'));
}
return html;
},
//document.head is non-standard
get head(){
//console.log('get head');
if (!this.documentElement) {
this.appendChild(this.createElement('html'));
}
var element = this.documentElement,
length = element.childNodes.length,
i;
//check for the presence of the head element in this html doc
for(i=0;i<length;i++){
if(element.childNodes[i].nodeType === Node.ELEMENT_NODE){
if(element.childNodes[i].tagName.toLowerCase() === 'head'){
return element.childNodes[i];
}
}
}
//no head? ugh bad news html.. I guess we'll force the issue?
var head = element.appendChild(this.createElement('head'));
return head;
},
get title(){
//console.log('get title');
if (!this.documentElement) {
this.appendChild(this.createElement('html'));
}
var title,
head = this.head,
length = head.childNodes.length,
i;
//check for the presence of the title element in this head element
for(i=0;i<length;i++){
if(head.childNodes[i].nodeType === Node.ELEMENT_NODE){
if(head.childNodes[i].tagName.toLowerCase() === 'title'){
return head.childNodes[i].textContent;
}
}
}
//no title? ugh bad news html.. I guess we'll force the issue?
title = head.appendChild(this.createElement('title'));
return title.appendChild(this.createTextNode('Untitled Document')).nodeValue;
},
set title(titleStr){
//console.log('set title %s', titleStr);
if (!this.documentElement) {
this.appendChild(this.createElement('html'));
}
var title = this.title;
title.textContent = titleStr;
},
get body() {
var element = this.documentElement,
length = element.childNodes.length,
i;
for (i=0; i<length; i++) {
if (element.childNodes[i].nodeType === Node.ELEMENT_NODE &&
element.childNodes[i].tagName === 'BODY') {
return element.childNodes[i];
}
}
return null;
},
set body() {
/* in firefox this is a benevolent do nothing*/
console.log('set body');
},
get cookie(){
return Envjs.getCookies(this.location+'');
},
set cookie(cookie){
return Envjs.setCookie(this.location+'', cookie);
},
/**
* document.location
*
* should be identical to window.location
*
* HTML5:
* http://dev.w3.org/html5/spec/Overview.html#the-location-interface
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.location
*
*/
get location() {
if (this.ownerWindow) {
return this.ownerWindow.location;
} else {
return this.baseURI;
}
},
set location(url) {
this.baseURI = url;
if (this.ownerWindow) {
this.ownerWindow.location = url;
}
},
/**
* document.URL (read-only)
*
* HTML DOM Level 2:
* http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-46183437
*
* HTML5:
* http://dev.w3.org/html5/spec/Overview.html#dom-document-url
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.URL
*/
get URL() {
return this.location.href;
},
/**
* document.domain
*
* HTML5 Spec:
* http://dev.w3.org/html5/spec/Overview.html#dom-document-domain
*
* Mozilla MDC:
* https://developer.mozilla.org/en/DOM/document.domain
*
*/
get domain(){
var HOSTNAME = new RegExp('\/\/([^\:\/]+)'),
matches = HOSTNAME.exec(this.baseURI);
return matches&&matches.length>1?matches[1]:"";
},
set domain(value){
var i,
domainParts = this.domain.split('.').reverse(),
newDomainParts = value.split('.').reverse();
if(newDomainParts.length > 1){
for(i=0;i<newDomainParts.length;i++){
if(!(newDomainParts[i] === domainParts[i])){
return;
}
}
this.baseURI = this.baseURI.replace(domainParts.join('.'), value);
}
},
get forms(){
return new HTMLCollection(this.getElementsByTagName('form'));
},
get images(){
return new HTMLCollection(this.getElementsByTagName('img'));
},
get lastModified(){
/* TODO */
return this._lastModified;
},
get links(){
return new HTMLCollection(this.getElementsByTagName('a'));
},
getElementsByName : function(name){
//returns a real Array + the NodeList
var retNodes = __extend__([],new NodeList(this, this.documentElement)),
node;
// loop through all Elements
var all = this.getElementsByTagName('*');
for (var i=0; i < all.length; i++) {
node = all[i];
if (node.nodeType === Node.ELEMENT_NODE &&
node.getAttribute('name') == name) {
retNodes.push(node);
}
}
return retNodes;
},
toString: function(){
return "[object HTMLDocument]";
},
get innerHTML(){
return this.documentElement.outerHTML;
}
});
Aspect.around({
target: Node,
method:"appendChild"
}, function(invocation) {
var event,
okay,
node = invocation.proceed(),
doc = node.ownerDocument;
//console.log('element appended: %s %s %s', node+'', node.nodeName, node.namespaceURI);
if((node.nodeType !== Node.ELEMENT_NODE)){
//for now we are only handling element insertions. probably
//we will need to handle text node changes to script tags and
//changes to src attributes
return node;
}
//console.log('appended html element %s %s %s',
// node.namespaceURI, node.nodeName, node);
switch(doc.parsing){
case true:
/**
* Very special case. While in parsing mode, in head, a
* script can add another script tag to the head, and it will
* be evaluated. This is tested in 'ant fulldoc-spec' tests.
*
* Not quite sure if the require that the new script tag must
* be in the head is correct or not. NamespaceURI == null
* might also need to corrected too.
*/
if (node.namespaceURI === null &&
node.tagName === 'SCRIPT' &&
this.tagName == 'HEAD') {
okay = Envjs.loadLocalScript(node, null);
}
break;
case false:
switch(node.namespaceURI){
case null:
//fall through
case "":
//fall through
case "http://www.w3.org/1999/xhtml":
switch(node.tagName.toLowerCase()){
case 'style':
document.styleSheets.push(CSSStyleSheet(node));
break;
case 'script':
if((this.nodeName.toLowerCase() === 'head')){
try{
okay = Envjs.loadLocalScript(node, null);
//console.log('loaded script? %s %s', node.uuid, okay);
// only fire event if we actually had something to load
if (node.src && node.src.length > 0){
event = doc.createEvent('HTMLEvents');
event.initEvent( okay ? "load" : "error", false, false );
node.dispatchEvent( event, false );
}
}catch(e){
console.log('error loading html element %s %e', node, e.toString());
}
}
break;
case 'frame':
case 'iframe':
node.contentWindow = { };
node.contentDocument = new HTMLDocument(new DOMImplementation(), node.contentWindow);
node.contentWindow.document = node.contentDocument;
try{
Window;
}catch(e){
node.contentDocument.addEventListener('DOMContentLoaded', function(){
event = node.contentDocument.createEvent('HTMLEvents');
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
});
}
try{
if (node.src && node.src.length > 0){
//console.log("getting content document for (i)frame from %s", node.src);
Envjs.loadFrame(node, Envjs.uri(node.src));
event = node.contentDocument.createEvent('HTMLEvents');
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}else{
//I dont like this being here:
//TODO: better mix-in strategy so the try/catch isnt required
try{
if(Window){
Envjs.loadFrame(node);
//console.log('src/html/document.js: triggering frame load');
event = node.contentDocument.createEvent('HTMLEvents');
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}
}catch(e){}
}
}catch(e){
console.log('error loading html element %s %e', node, e.toString());
}
break;
case 'link':
if (node.href && node.href.length > 0) {
__loadLink__(node, node.href);
}
break;
/*
case 'img':
if (node.src && node.src.length > 0){
// don't actually load anything, so we're "done" immediately:
event = doc.createEvent('HTMLEvents');
event.initEvent("load", false, false);
node.dispatchEvent( event, false );
}
break;
*/
case 'option':
node._updateoptions();
break;
default:
if(node.getAttribute('onload')){
console.log('calling attribute onload %s | %s', node.onload, node.tagName);
node.onload();
}
break;
}//switch on name
default:
break;
}//switch on ns
break;
default:
// console.log('element appended: %s %s', node+'', node.namespaceURI);
}//switch on doc.parsing
return node;
});
Aspect.around({
target: Node,
method:"removeChild"
}, function(invocation) {
var event,
okay,
node = invocation.proceed(),
doc = node.ownerDocument;
if((node.nodeType !== Node.ELEMENT_NODE)){
//for now we are only handling element insertions. probably we will need
//to handle text node changes to script tags and changes to src
//attributes
if(node.nodeType !== Node.DOCUMENT_NODE && node.uuid){
//console.log('removing event listeners, %s', node, node.uuid);
node.removeEventListener('*', null, null);
}
return node;
}
//console.log('appended html element %s %s %s', node.namespaceURI, node.nodeName, node);
switch(doc.parsing){
case true:
//handled by parser if included
break;
case false:
switch(node.namespaceURI){
case null:
//fall through
case "":
//fall through
case "http://www.w3.org/1999/xhtml":
//this is interesting dillema since our event engine is
//storing the registered events in an array accessed
//by the uuid property of the node. unforunately this
//means listeners hang out way after(forever ;)) the node
//has been removed and gone out of scope.
//console.log('removing event listeners, %s', node, node.uuid);
node.removeEventListener('*', null, null);
switch(node.tagName.toLowerCase()){
case 'frame':
case 'iframe':
try{
//console.log('removing iframe document');
try{
Envjs.unloadFrame(node);
}catch(e){
console.log('error freeing resources from frame %s', e);
}
node.contentWindow = null;
node.contentDocument = null;
}catch(e){
console.log('error unloading html element %s %e', node, e.toString());
}
break;
default:
break;
}//switch on name
default:
break;
}//switch on ns
break;
default:
console.log('element appended: %s %s', node+'', node.namespaceURI);
}//switch on doc.parsing
return node;
});
/**
* Named Element Support
*
*
*/
/*
*
* @returns 'name' if the node has a appropriate name
* null if node does not have a name
*/
var __isNamedElement__ = function(node) {
if (node.nodeType !== Node.ELEMENT_NODE) {
return null;
}
var tagName = node.tagName.toLowerCase();
var nodename = null;
switch (tagName) {
case 'embed':
case 'form':
case 'iframe':
nodename = node.getAttribute('name');
break;
case 'applet':
nodename = node.id;
break;
case 'object':
// TODO: object needs to be 'fallback free'
nodename = node.id;
break;
case 'img':
nodename = node.id;
if (!nodename || ! node.getAttribute('name')) {
nodename = null;
}
break;
}
return (nodename) ? nodename : null;
};
var __addNamedMap__ = function(target, node) {
var nodename = __isNamedElement__(node);
if (nodename) {
target.__defineGetter__(nodename, function() {
return node;
});
}
};
var __removeNamedMap__ = function(target, node) {
if (!node) {
return;
}
var nodename = __isNamedElement__(node);
if (nodename) {
delete target[nodename];
}
};