forked from jeresig/env-js
/
env.js
8788 lines (7950 loc) · 525 KB
/
env.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Envjs core-env.1.2.0.0
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
var Envjs = function(){
var i,
name
override = function(){
for(i=0;i<arguments.length;i++){
for ( name in arguments[i] ) {
var g = arguments[i].__lookupGetter__(name),
s = arguments[i].__lookupSetter__(name);
if ( g || s ) {
if ( g ) Envjs.__defineGetter__(name, g);
if ( s ) Envjs.__defineSetter__(name, s);
} else
Envjs[name] = arguments[i][name];
}
}
};
if(arguments.length === 1 && typeof(arguments[0]) == 'string'){
window.location = arguments[0];
}else if (arguments.length === 1 && typeof(arguments[0]) == "object"){
override(arguments[0])
}else if(arguments.length === 2){
override(arguments[1]);
window.location = arguments[0];
}
return;
};
//eg "Mozilla"
Envjs.appCodeName = "Envjs";
//eg "Gecko/20070309 Firefox/2.0.0.3"
Envjs.appName = "Resig/20070309 PilotFish/1.2.0.1";
/*
* Envjs core-env.1.2.0.0
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
(function(){
/**
* Writes message to system out
* @param {String} message
*/
Envjs.log = function(message){};
/**
* Constants providing enumerated levels for logging in modules
*/
Envjs.DEBUG = 1;
Envjs.INFO = 2;
Envjs.WARN = 3;
Envjs.ERROR = 3;
Envjs.NONE = 3;
/**
* Writes error info out to console
* @param {Error} e
*/
Envjs.lineSource = function(e){};
/**
* describes which script src values will trigger Envjs to load
* the script like a browser would
*/
Envjs.scriptTypes = {
"text/javascript" :false,
"text/envjs" :true
};
/**
* will be called when loading a script throws an error
* @param {Object} script
* @param {Object} e
*/
Envjs.onScriptLoadError = function(script, e){};
/**
* load and execute script tag text content
* @param {Object} script
*/
Envjs.loadInlineScript = function(script){
var tmpFile;
try{
if(Envjs.DEBUG_ENABLED){
//
Envjs.writeToTempFile(script.text, 'js') ;
Envjs.load(tmpFile);
}else{
eval(script.text);
}
}catch(e){
Envjs.onScriptLoadError(script, e);
}
};
/**
* Executes a script tag
* @param {Object} script
* @param {Object} parser
*/
Envjs.loadLocalScript = function(script){
console.debug("loading script %s", script);
var types,
src,
i,
base,
filename,
// SMP: see also the note in html/document.js about script.type
script_type = script.type === null ?
"text/javascript" : script.type;
if(script_type){
types = script_type?script_type.split(";"):[];
for(i=0;i<types.length;i++){
if(Envjs.scriptTypes[types[i]]){
//ok this script type is allowed
break;
}
if(i+1 == types.length)
return false;
}
}else{
try{
//handle inline scripts
if(!script.src)
Envjs.loadInlineScript(script);
return true
}catch(e){
//Envjs.error("Error loading script.", e);
Envjs.onScriptLoadError(script, e);
return false;
}
}
if(script.src){
//$env.info("loading allowed external script :" + script.src);
//lets you register a function to execute
//before the script is loaded
if(Envjs.beforeScriptLoad){
for(src in Envjs.beforeScriptLoad){
if(script.src.match(src)){
Envjs.beforeScriptLoad[src](script);
}
}
}
base = "" + script.ownerDocument.location;
//filename = Envjs.location(script.src.match(/([^\?#]*)/)[1], base );
filename = Envjs.location(script.src, base );
try {
load(filename);
console.log('loaded %s', filename);
} catch(e) {
console.log("could not load script %s \n %s", filename, e );
Envjs.onScriptLoadError(script, e);
return false;
}
//lets you register a function to execute
//after the script is loaded
if(Envjs.afterScriptLoad){
for(src in Envjs.afterScriptLoad){
if(script.src.match(src)){
Envjs.afterScriptLoad[src](script);
}
}
}
}
return true;
};
/**
* synchronizes thread modifications
* @param {Function} fn
*/
Envjs.sync = function(fn){}
/**
* sleep thread for specified duration
* @param {Object} millseconds
*/
Envjs.sleep = function(millseconds){};
/**
* Interval to wait on event loop when nothing is happening
*/
Envjs.WAIT_INTERVAL = 100;//milliseconds
/**
* resolves location relative to base or window location
* @param {Object} path
* @param {Object} base
*/
Envjs.location = function(path, base){};
/**
* Used in the XMLHttpRquest implementation to run a
* request in a seperate thread
* @param {Object} fn
*/
Envjs.runAsync = function(fn){};
/**
* Used to write to a local file
* @param {Object} text
* @param {Object} url
*/
Envjs.writeToFile = function(text, url){};
/**
* Used to write to a local file
* @param {Object} text
* @param {Object} suffix
*/
Envjs.writeToTempFile = function(text, suffix){};
/**
* Used to delete a local file
* @param {Object} url
*/
Envjs.deleteFile = function(url){};
/**
* establishes connection and calls responsehandler
* @param {Object} xhr
* @param {Object} responseHandler
* @param {Object} data
*/
Envjs.connection = function(xhr, responseHandler, data){};
/**
* Makes an object window-like by proxying object accessors
* @param {Object} scope
* @param {Object} parent
*/
Envjs.proxy = function(scope, parent, aliasList){};
Envjs.javaEnabled = false;
Envjs.tmpdir = '';
Envjs.os_name = '';
Envjs.os_arch = '';
Envjs.os_version = '';
Envjs.lang = '';
Envjs.platform = '';//how do we get the version
/**
*
* @param {Object} frameElement
* @param {Object} url
*/
Envjs.loadFrame = function(frame, url){
try {
if(frame.contentWindow){
//mark for garbage collection
frame.contentWindow = null;
}
frame.contentWindow = {};
new Window(frame.contentWindow, window);
frame.contentDocument.async = false;
frame.contentWindow.location = url;
} catch(e) {
console.log("failed to load frame content: from %s %s", url, e);
}
};
/**
* @author john resig & the envjs team
* @uri http://www.envjs.com/
* @copyright 2008-2010
* @license MIT
*/
})();
/*
* Envjs dom.1.2.0.0
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*
* Parts of the implementation were originally written by:\
* and Jon van Noort (jon@webarcana.com.au) \
* and David Joham (djoham@yahoo.com)",\
* and Scott Severtson
*
* This file simply provides the global definitions we need to \
* be able to correctly implement to core browser DOM interfaces."
*/
var Attr,
CDATASection,
CharacterData,
Comment,
Document,
DocumentFragment,
DocumentType,
DOMException,
DOMImplementation,
Element,
Entity,
EntityReference,
NamedNodeMap,
Namespace,
Node,
NodeList,
Notation,
ProcessingInstruction,
Text,
Range,
XMLSerializer;
/*
* Envjs dom.1.2.0.0
* Pure JavaScript Browser Environment
* By John Resig <http://ejohn.org/> and the Envjs Team
* Copyright 2008-2010 John Resig, under the MIT License
*/
(function(){
/**
* @author john resig
*/
// Helper method for extending one object with another.
function __extend__(a,b) {
for ( var i in b ) {
var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
if ( g || s ) {
if ( g ) a.__defineGetter__(i, g);
if ( s ) a.__defineSetter__(i, s);
} else
a[i] = b[i];
} return a;
};
/**
* @author john resig
*/
//from jQuery
function __setArray__( target, array ) {
// Resetting the length to 0, then using the native Array push
// is a super-fast way to populate an object with array-like properties
target.length = 0;
Array.prototype.push.apply( target, array );
};
/**
* @class NodeList -
* provides the abstraction of an ordered collection of nodes
*
* @param ownerDocument : Document - the ownerDocument
* @param parentNode : Node - the node that the NodeList is attached to (or null)
*/
NodeList = function(ownerDocument, parentNode) {
this.length = 0;
this.parentNode = parentNode;
this.ownerDocument = ownerDocument;
this._readonly = false;
__setArray__(this, []);
};
__extend__(NodeList.prototype, {
item : function(index) {
var ret = null;
if ((index >= 0) && (index < this.length)) {
// bounds check
ret = this[index];
}
// if the index is out of bounds, default value null is returned
return ret;
},
get xml() {
var ret = "",
i;
// create string containing the concatenation of the string values of each child
for (i=0; i < this.length; i++) {
if(this[i]){
if(this[i].nodeType == Node.TEXT_NODE && i>0 &&
this[i-1].nodeType == Node.TEXT_NODE){
//add a single space between adjacent text nodes
ret += " "+this[i].xml;
}else{
ret += this[i].xml;
}
}
}
return ret;
},
toArray: function () {
var children = [],
i;
for ( i=0; i < this.length; i++) {
children.push (this[i]);
}
return children;
},
toString: function(){
return "[object NodeList]";
}
});
/**
* @method __findItemIndex__
* find the item index of the node
* @author Jon van Noort (jon@webarcana.com.au)
* @param node : Node
* @return : int
*/
var __findItemIndex__ = function (nodelist, node) {
var ret = -1, i;
for (i=0; i<nodelist.length; i++) {
// compare id to each node's _id
if (nodelist[i] === node) {
// found it!
ret = i;
break;
}
}
// if node is not found, default value -1 is returned
return ret;
};
/**
* @method __insertBefore__
* insert the specified Node into the NodeList before the specified index
* Used by Node.insertBefore(). Note: Node.insertBefore() is responsible
* for Node Pointer surgery __insertBefore__ simply modifies the internal
* data structure (Array).
* @param newChild : Node - the Node to be inserted
* @param refChildIndex : int - the array index to insert the Node before
*/
var __insertBefore__ = function(nodelist, newChild, refChildIndex) {
if ((refChildIndex >= 0) && (refChildIndex <= nodelist.length)) {
// bounds check
if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
// node is a DocumentFragment
// append the children of DocumentFragment
Array.prototype.splice.apply(nodelist,
[refChildIndex, 0].concat(newChild.childNodes.toArray()));
}
else {
// append the newChild
Array.prototype.splice.apply(nodelist,[refChildIndex, 0, newChild]);
}
}
};
/**
* @method __replaceChild__
* replace the specified Node in the NodeList at the specified index
* Used by Node.replaceChild(). Note: Node.replaceChild() is responsible
* for Node Pointer surgery __replaceChild__ simply modifies the internal
* data structure (Array).
*
* @param newChild : Node - the Node to be inserted
* @param refChildIndex : int - the array index to hold the Node
*/
var __replaceChild__ = function(nodelist, newChild, refChildIndex) {
var ret = null;
// bounds check
if ((refChildIndex >= 0) && (refChildIndex < nodelist.length)) {
// preserve old child for return
ret = nodelist[refChildIndex];
if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
// node is a DocumentFragment
// get array containing children prior to refChild
Array.prototype.splice.apply(nodelist,
[refChildIndex, 1].concat(newChild.childNodes.toArray()));
}
else {
// simply replace node in array (links between Nodes are
// made at higher level)
nodelist[refChildIndex] = newChild;
}
}
// return replaced node
return ret;
};
/**
* @method __removeChild__
* remove the specified Node in the NodeList at the specified index
* Used by Node.removeChild(). Note: Node.removeChild() is responsible
* for Node Pointer surgery __removeChild__ simply modifies the internal
* data structure (Array).
* @param refChildIndex : int - the array index holding the Node to be removed
*/
var __removeChild__ = function(nodelist, refChildIndex) {
var ret = null;
if (refChildIndex > -1) {
// found it!
// return removed node
ret = nodelist[refChildIndex];
// rebuild array without removed child
Array.prototype.splice.apply(nodelist,[refChildIndex, 1]);
}
// return removed node
return ret;
};
/**
* @method __appendChild__
* append the specified Node to the NodeList. Used by Node.appendChild().
* Note: Node.appendChild() is responsible for Node Pointer surgery
* __appendChild__ simply modifies the internal data structure (Array).
* @param newChild : Node - the Node to be inserted
*/
var __appendChild__ = function(nodelist, newChild) {
if (newChild.nodeType == Node.DOCUMENT_FRAGMENT_NODE) {
// node is a DocumentFragment
// append the children of DocumentFragment
Array.prototype.push.apply(nodelist, newChild.childNodes.toArray() );
} else {
// simply add node to array (links between Nodes are made at higher level)
Array.prototype.push.apply(nodelist, [newChild]);
}
};
/**
* @method __cloneNodes__ -
* Returns a NodeList containing clones of the Nodes in this NodeList
* @param deep : boolean -
* If true, recursively clone the subtree under each of the nodes;
* if false, clone only the nodes themselves (and their attributes,
* if it is an Element).
* @param parentNode : Node - the new parent of the cloned NodeList
* @return : NodeList - NodeList containing clones of the Nodes in this NodeList
*/
var __cloneNodes__ = function(nodelist, deep, parentNode) {
var cloneNodeList = new NodeList(nodelist.ownerDocument, parentNode);
// create list containing clones of each child
for (var i=0; i < nodelist.length; i++) {
__appendChild__(cloneNodeList, nodelist[i].cloneNode(deep));
}
return cloneNodeList;
};
/**
* @class NamedNodeMap -
* used to represent collections of nodes that can be accessed by name
* typically a set of Element attributes
*
* @extends NodeList -
* note W3C spec says that this is not the case, but we need an item()
* method identical to NodeList's, so why not?
* @param ownerDocument : Document - the ownerDocument
* @param parentNode : Node - the node that the NamedNodeMap is attached to (or null)
*/
NamedNodeMap = function(ownerDocument, parentNode) {
this.NodeList = NodeList;
this.NodeList(ownerDocument, parentNode);
__setArray__(this, []);
};
NamedNodeMap.prototype = new NodeList;
__extend__(NamedNodeMap.prototype, {
add: function(name){
this[this.length] = name;
},
getNamedItem : function(name) {
var ret = null;
// test that Named Node exists
var itemIndex = __findNamedItemIndex__(this, name);
if (itemIndex > -1) {
// found it!
ret = this[itemIndex];
}
// if node is not found, default value null is returned
return ret;
},
setNamedItem : function(arg) {
// test for exceptions
if (__ownerDocument__(this).implementation.errorChecking) {
// throw Exception if arg was not created by this Document
if (this.ownerDocument != arg.ownerDocument) {
throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR));
}
// throw Exception if DOMNamedNodeMap is readonly
if (this._readonly || (this.parentNode && this.parentNode._readonly)) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
// throw Exception if arg is already an attribute of another Element object
if (arg.ownerElement && (arg.ownerElement != this.parentNode)) {
throw(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR));
}
}
// get item index
var itemIndex = __findNamedItemIndex__(this, arg.name);
var ret = null;
if (itemIndex > -1) { // found it!
ret = this[itemIndex]; // use existing Attribute
// throw Exception if DOMAttr is readonly
if (__ownerDocument__(this).implementation.errorChecking && ret._readonly) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
} else {
this[itemIndex] = arg; // over-write existing NamedNode
this[arg.name.toLowerCase()] = arg;
}
} else {
// add new NamedNode
Array.prototype.push.apply(this, [arg]);
this[arg.name.toLowerCase()] = arg;
}
arg.ownerElement = this.parentNode; // update ownerElement
return ret; // return old node or null
},
removeNamedItem : function(name) {
var ret = null;
// test for exceptions
// throw Exception if NamedNodeMap is readonly
if (__ownerDocument__(this).implementation.errorChecking &&
(this._readonly || (this.parentNode && this.parentNode._readonly))) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
// get item index
var itemIndex = __findNamedItemIndex__(this, name);
// throw Exception if there is no node named name in this map
if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) {
throw(new DOMException(DOMException.NOT_FOUND_ERR));
}
// get Node
var oldNode = this[itemIndex];
//this[oldNode.name] = undefined;
// throw Exception if Node is readonly
if (__ownerDocument__(this).implementation.errorChecking && oldNode._readonly) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
// return removed node
return __removeChild__(this, itemIndex);
},
getNamedItemNS : function(namespaceURI, localName) {
var ret = null;
// test that Named Node exists
var itemIndex = __findNamedItemNSIndex__(this, namespaceURI, localName);
if (itemIndex > -1) {
// found it! return NamedNode
ret = this[itemIndex];
}
// if node is not found, default value null is returned
return ret;
},
setNamedItemNS : function(arg) {
//console.log('setNamedItemNS %s', arg);
// test for exceptions
if (__ownerDocument__(this).implementation.errorChecking) {
// throw Exception if NamedNodeMap is readonly
if (this._readonly || (this.parentNode && this.parentNode._readonly)) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
// throw Exception if arg was not created by this Document
if (__ownerDocument__(this) != __ownerDocument__(arg)) {
throw(new DOMException(DOMException.WRONG_DOCUMENT_ERR));
}
// throw Exception if arg is already an attribute of another Element object
if (arg.ownerElement && (arg.ownerElement != this.parentNode)) {
throw(new DOMException(DOMException.INUSE_ATTRIBUTE_ERR));
}
}
// get item index
var itemIndex = __findNamedItemNSIndex__(this, arg.namespaceURI, arg.localName);
var ret = null;
if (itemIndex > -1) {
// found it!
// use existing Attribute
ret = this[itemIndex];
// throw Exception if Attr is readonly
if (__ownerDocument__(this).implementation.errorChecking && ret._readonly) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
} else {
// over-write existing NamedNode
this[itemIndex] = arg;
}
}else {
// add new NamedNode
Array.prototype.push.apply(this, [arg]);
}
arg.ownerElement = this.parentNode;
// return old node or null
return ret;
console.log('finished setNamedItemNS %s', arg);
},
removeNamedItemNS : function(namespaceURI, localName) {
var ret = null;
// test for exceptions
// throw Exception if NamedNodeMap is readonly
if (__ownerDocument__(this).implementation.errorChecking && (this._readonly || (this.parentNode && this.parentNode._readonly))) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
// get item index
var itemIndex = __findNamedItemNSIndex__(this, namespaceURI, localName);
// throw Exception if there is no matching node in this map
if (__ownerDocument__(this).implementation.errorChecking && (itemIndex < 0)) {
throw(new DOMException(DOMException.NOT_FOUND_ERR));
}
// get Node
var oldNode = this[itemIndex];
// throw Exception if Node is readonly
if (__ownerDocument__(this).implementation.errorChecking && oldNode._readonly) {
throw(new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR));
}
return __removeChild__(this, itemIndex); // return removed node
},
get xml() {
var ret = "";
// create string containing concatenation of all (but last) Attribute string values (separated by spaces)
for (var i=0; i < this.length -1; i++) {
ret += this[i].xml +" ";
}
// add last Attribute to string (without trailing space)
if (this.length > 0) {
ret += this[this.length -1].xml;
}
return ret;
}
});
/**
* @method __findNamedItemIndex__
* find the item index of the node with the specified name
*
* @param name : string - the name of the required node
* @param isnsmap : if its a NamespaceNodeMap
* @return : int
*/
var __findNamedItemIndex__ = function(namednodemap, name, isnsmap) {
var ret = -1;
// loop through all nodes
for (var i=0; i<namednodemap.length; i++) {
// compare name to each node's nodeName
if(isnsmap){
if (namednodemap[i].localName.toLowerCase() == name.toLowerCase()) {
// found it!
ret = i;
break;
}
}else{
if (namednodemap[i].name.toLowerCase() == name.toLowerCase()) {
// found it!
ret = i;
break;
}
}
}
// if node is not found, default value -1 is returned
return ret;
};
/**
* @method __findNamedItemNSIndex__
* find the item index of the node with the specified
* namespaceURI and localName
*
* @param namespaceURI : string - the namespace URI of the required node
* @param localName : string - the local name of the required node
* @return : int
*/
var __findNamedItemNSIndex__ = function(namednodemap, namespaceURI, localName) {
var ret = -1;
// test that localName is not null
if (localName) {
// loop through all nodes
for (var i=0; i<namednodemap.length; i++) {
if(namednodemap[i].namespaceURI && namednodemap[i].localName){
// compare name to each node's namespaceURI and localName
if ((namednodemap[i].namespaceURI.toLowerCase() == namespaceURI.toLowerCase()) &&
(namednodemap[i].localName.toLowerCase() == localName.toLowerCase())) {
// found it!
ret = i;
break;
}
}
}
}
// if node is not found, default value -1 is returned
return ret;
};
/**
* @method __hasAttribute__
* Returns true if specified node exists
*
* @param name : string - the name of the required node
* @return : boolean
*/
var __hasAttribute__ = function(namednodemap, name) {
var ret = false;
// test that Named Node exists
var itemIndex = __findNamedItemIndex__(namednodemap, name);
if (itemIndex > -1) {
// found it!
ret = true;
}
// if node is not found, default value false is returned
return ret;
}
/**
* @method __hasAttributeNS__
* Returns true if specified node exists
*
* @param namespaceURI : string - the namespace URI of the required node
* @param localName : string - the local name of the required node
* @return : boolean
*/
var __hasAttributeNS__ = function(namednodemap, namespaceURI, localName) {
var ret = false;
// test that Named Node exists
var itemIndex = __findNamedItemNSIndex__(namednodemap, namespaceURI, localName);
if (itemIndex > -1) {
// found it!
ret = true;
}
// if node is not found, default value false is returned
return ret;
}
/**
* @method __cloneNamedNodes__
* Returns a NamedNodeMap containing clones of the Nodes in this NamedNodeMap
*
* @param parentNode : Node - the new parent of the cloned NodeList
* @param isnsmap : bool - is this a NamespaceNodeMap
* @return NamedNodeMap containing clones of the Nodes in this NamedNodeMap
*/
var __cloneNamedNodes__ = function(namednodemap, parentNode, isnsmap) {
var cloneNamedNodeMap = isnsmap?
new NamespaceNodeMap(namednodemap.ownerDocument, parentNode):
new NamedNodeMap(namednodemap.ownerDocument, parentNode);
// create list containing clones of all children
for (var i=0; i < namednodemap.length; i++) {
__appendChild__(cloneNamedNodeMap, namednodemap[i].cloneNode(false));
}
return cloneNamedNodeMap;
};
/**
* @class NamespaceNodeMap -
* used to represent collections of namespace nodes that can be
* accessed by name typically a set of Element attributes
*
* @extends NamedNodeMap
*
* @param ownerDocument : Document - the ownerDocument
* @param parentNode : Node - the node that the NamespaceNodeMap is attached to (or null)
*/
var NamespaceNodeMap = function(ownerDocument, parentNode) {
this.NamedNodeMap = NamedNodeMap;
this.NamedNodeMap(ownerDocument, parentNode);
__setArray__(this, []);
};
NamespaceNodeMap.prototype = new NamedNodeMap;
__extend__(NamespaceNodeMap.prototype, {
get xml() {
var ret = "",
ns,
ind;
// identify namespaces declared local to this Element (ie, not inherited)
for (ind = 0; ind < this.length; ind++) {
// if namespace declaration does not exist in the containing node's, parentNode's namespaces
ns = null;
try {
var ns = this.parentNode.parentNode._namespaces.
getNamedItem(this[ind].localName);
}catch (e) {
//breaking to prevent default namespace being inserted into return value
break;
}
if (!(ns && (""+ ns.nodeValue == ""+ this[ind].nodeValue))) {
// display the namespace declaration
ret += this[ind].xml +" ";
}
}
return ret;
}
});
/**
* @class Node -
* The Node interface is the primary datatype for the entire
* Document Object Model. It represents a single node in the
* document tree.
* @param ownerDocument : Document - The Document object associated with this node.
*/
Node = function(ownerDocument) {
this.baseURI = 'about:blank';
this.namespaceURI = null;
this.nodeName = "";
this.nodeValue = null;
// The parent of this node. All nodes, except Document, DocumentFragment,
// and Attr may have a parent. However, if a node has just been created
// and not yet added to the tree, or if it has been removed from the tree,
// this is null
this.parentNode = null;
// A NodeList that contains all children of this node. If there are no
// children, this is a NodeList containing no nodes. The content of the
// returned NodeList is "live" in the sense that, for instance, changes to
// the children of the node object that it was created from are immediately
// reflected in the nodes returned by the NodeList accessors; it is not a
// static snapshot of the content of the node. This is true for every
// NodeList, including the ones returned by the getElementsByTagName method.
this.childNodes = new NodeList(ownerDocument, this);
// The first child of this node. If there is no such node, this is null
this.firstChild = null;
// The last child of this node. If there is no such node, this is null.
this.lastChild = null;
// The node immediately preceding this node. If there is no such node,
// this is null.
this.previousSibling = null;
// The node immediately following this node. If there is no such node,
// this is null.
this.nextSibling = null;
// The Document object associated with this node
this.ownerDocument = ownerDocument;
this.attributes = null;
// The namespaces in scope for this node
this._namespaces = new NamespaceNodeMap(ownerDocument, this);
this._readonly = false;
};
// nodeType constants
Node.ELEMENT_NODE = 1;
Node.ATTRIBUTE_NODE = 2;
Node.TEXT_NODE = 3;