Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
var events = require("../level2/events"),
2
core = require("../level2/core").dom.level2.core,
3
defineGetter = require('../utils').defineGetter,
4
defineSetter = require('../utils').defineSetter,
5
HtmlToDom = require('../browser/htmltodom').HtmlToDom,
6
domToHtml = require('../browser/domtohtml').domToHtml,
7
htmlencoding = require('../browser/htmlencoding'),
8
HTMLEncode = htmlencoding.HTMLEncode,
9
HTMLDecode = htmlencoding.HTMLDecode;
10
11
// modify cloned instance for more info check: https://github.com/tmpvar/jsdom/issues/325
12
core = Object.create(core);
13
14
/*
15
valuetype DOMString sequence<unsigned short>;
16
typedef unsigned long long DOMTimeStamp;
17
typedef any DOMUserData;
18
typedef Object DOMObject;
19
20
*/
21
// ExceptionCode
22
core.VALIDATION_ERR = 16;
23
core.TYPE_MISMATCH_ERR = 17;
24
25
/*
26
// Introduced in DOM Level 3:
27
interface NameList {
28
DOMString getName(in unsigned long index);
29
DOMString getNamespaceURI(in unsigned long index);
30
readonly attribute unsigned long length;
31
boolean contains(in DOMString str);
32
boolean containsNS(in DOMString namespaceURI,
33
in DOMString name);
34
};
35
36
// Introduced in DOM Level 3:
37
interface DOMImplementationList {
38
DOMImplementation item(in unsigned long index);
39
readonly attribute unsigned long length;
40
};
41
42
// Introduced in DOM Level 3:
43
interface DOMImplementationSource {
44
DOMImplementation getDOMImplementation(in DOMString features);
45
DOMImplementationList getDOMImplementationList(in DOMString features);
46
};
47
*/
48
49
50
core.DOMImplementation.prototype.getFeature = function(feature, version) {
51
52
};
53
54
/*
55
interface Node {
56
// Modified in DOM Level 3:
57
Node insertBefore(in Node newChild,
58
in Node refChild)
59
raises(DOMException);
60
// Modified in DOM Level 3:
61
Node replaceChild(in Node newChild,
62
in Node oldChild)
63
raises(DOMException);
64
// Modified in DOM Level 3:
65
Node removeChild(in Node oldChild)
66
raises(DOMException);
67
// Modified in DOM Level 3:
68
Node appendChild(in Node newChild)
69
raises(DOMException);
70
boolean hasChildNodes();
71
Node cloneNode(in boolean deep);
72
// Modified in DOM Level 3:
73
void normalize();
74
// Introduced in DOM Level 3:
75
readonly attribute DOMString baseURI;
76
*/
77
78
// Compare Document Position
79
var DOCUMENT_POSITION_DISCONNECTED = core.Node.prototype.DOCUMENT_POSITION_DISCONNECTED = 0x01;
80
var DOCUMENT_POSITION_PRECEDING = core.Node.prototype.DOCUMENT_POSITION_PRECEDING = 0x02;
81
var DOCUMENT_POSITION_FOLLOWING = core.Node.prototype.DOCUMENT_POSITION_FOLLOWING = 0x04;
82
var DOCUMENT_POSITION_CONTAINS = core.Node.prototype.DOCUMENT_POSITION_CONTAINS = 0x08;
83
var DOCUMENT_POSITION_CONTAINED_BY = core.Node.prototype.DOCUMENT_POSITION_CONTAINED_BY = 0x10;
84
var DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = core.Node.prototype.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20;
85
var DOCUMENT_TYPE_NODE = core.Node.prototype.DOCUMENT_TYPE_NODE;
86
87
core.Node.prototype.compareDocumentPosition = function compareDocumentPosition( otherNode ) {
88
if( !(otherNode instanceof core.Node) ) {
89
throw Error("Comparing position against non-Node values is not allowed")
90
}
91
var thisOwner, otherOwner;
92
93
if( this.nodeType === this.DOCUMENT_NODE)
94
thisOwner = this
95
else
96
thisOwner = this.ownerDocument
97
98
if( otherNode.nodeType === this.DOCUMENT_NODE)
99
otherOwner = otherNode
100
else
101
otherOwner = otherNode.ownerDocument
102
103
if( this === otherNode ) return 0
104
if( this === otherNode.ownerDocument ) return DOCUMENT_POSITION_FOLLOWING + DOCUMENT_POSITION_CONTAINED_BY
105
if( this.ownerDocument === otherNode ) return DOCUMENT_POSITION_PRECEDING + DOCUMENT_POSITION_CONTAINS
106
if( thisOwner !== otherOwner ) return DOCUMENT_POSITION_DISCONNECTED
107
108
// Text nodes for attributes does not have a _parentNode. So we need to find them as attribute child.
109
if( this.nodeType === this.ATTRIBUTE_NODE && this._childNodes && this._childNodes._toArray().indexOf(otherNode) !== -1)
110
return DOCUMENT_POSITION_FOLLOWING + DOCUMENT_POSITION_CONTAINED_BY
111
112
if( otherNode.nodeType === this.ATTRIBUTE_NODE && otherNode._childNodes && otherNode._childNodes._toArray().indexOf(this) !== -1)
113
return DOCUMENT_POSITION_PRECEDING + DOCUMENT_POSITION_CONTAINS
114
115
var point = this
116
var parents = [ ]
117
var previous = null
118
while( point ) {
119
if( point == otherNode ) return DOCUMENT_POSITION_PRECEDING + DOCUMENT_POSITION_CONTAINS
120
parents.push( point )
121
point = point._parentNode
122
}
123
point = otherNode
124
previous = null
125
while( point ) {
126
if( point == this ) return DOCUMENT_POSITION_FOLLOWING + DOCUMENT_POSITION_CONTAINED_BY
127
var location_index = parents.indexOf( point )
128
if( location_index !== -1) {
129
var smallest_common_ancestor = parents[ location_index ]
130
var this_index = smallest_common_ancestor._childNodes._toArray().indexOf( parents[location_index - 1] )
131
var other_index = smallest_common_ancestor._childNodes._toArray().indexOf( previous )
132
if( this_index > other_index ) {
133
return DOCUMENT_POSITION_PRECEDING
134
}
135
else {
136
return DOCUMENT_POSITION_FOLLOWING
137
}
138
}
139
previous = point
140
point = point._parentNode
141
}
142
return DOCUMENT_POSITION_DISCONNECTED
143
};
144
/*
145
// Introduced in DOM Level 3:
146
attribute DOMString textContent;
147
// raises(DOMException) on setting
148
// raises(DOMException) on retrieval
149
*/
150
core.Node.prototype.isSameNode = function(other) {
151
return (other === this);
152
};
153
154
// @see http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-textContent
155
defineGetter(core.Node.prototype, 'textContent', function() {
156
switch (this.nodeType) {
157
case this.COMMENT_NODE:
158
case this.CDATA_SECTION_NODE:
159
case this.PROCESSING_INSTRUCTION_NODE:
160
case this.TEXT_NODE:
161
return this.nodeValue;
162
163
case this.ATTRIBUTE_NODE:
164
case this.DOCUMENT_FRAGMENT_NODE:
165
case this.ELEMENT_NODE:
166
case this.ENTITY_NODE:
167
case this.ENTITY_REFERENCE_NODE:
168
var out = '';
169
for (var i = 0 ; i < this.childNodes.length ; ++i) {
170
if (this.childNodes[i].nodeType !== this.COMMENT_NODE &&
171
this.childNodes[i].nodeType !== this.PROCESSING_INSTRUCTION_NODE) {
172
out += this.childNodes[i].textContent || '';
173
}
174
}
175
return out;
176
177
default:
178
return null;
179
}
180
});
181
182
defineSetter(core.Node.prototype, 'textContent', function(txt) {
183
for (var i = this.childNodes.length; --i >=0;) {
184
this.removeChild(this.childNodes.item(i));
185
}
186
if (txt !== "" && txt != null) {
187
this.appendChild(this._ownerDocument.createTextNode(txt));
188
}
189
return txt;
190
});
191
192
/*
193
// Introduced in DOM Level 3:
194
DOMString lookupPrefix(in DOMString namespaceURI);
195
// Introduced in DOM Level 3:
196
boolean isDefaultNamespace(in DOMString namespaceURI);
197
// Introduced in DOM Level 3:
198
DOMString lookupNamespaceURI(in DOMString prefix);
199
*/
200
// Introduced in DOM Level 3:
201
core.Node.prototype.isEqualNode = function(other) {
202
var self = this;
203
var diffValues = function() {
204
for (var i=0;i<arguments.length;i++) {
205
var k = arguments[i];
206
if (self[k] != other[k]) return(true);
207
}
208
return(false);
209
};
210
var diffNamedNodeMaps = function(snnm, onnm) {
211
if ((snnm == null) && (onnm == null)) return(false);
212
if ((snnm == null) || (onnm == null)) return(true);
213
if (snnm.length != onnm.length) return(true);
214
var js = [];
215
for (var j=0;j<onnm.length;j++) { js[j] = j }
216
for (var i=0;i<snnm.length;i++) {
217
var found=false;
218
for (var j=0;j<js.length;j++) {
219
if (snnm.item(i).isEqualNode(onnm.item(js[j]))) {
220
found = true;
221
// in order to be 100% accurate, we remove index values from consideration once they've matched
222
js.splice(j,1);
223
break;
224
}
225
}
226
if (!found) return(true);
227
}
228
return(false);
229
};
230
var diffNodeLists = function(snl, onl) {
231
if ((snl == null) && (onl == null)) return(false);
232
if ((snl == null) || (onl == null)) return(true);
233
if (snl.length != onl.length) return(true);
234
for (var i=0;i<snl.length;i++) {
235
if (!snl.item(i).isEqualNode(onl.item(i))) return(true);
236
}
237
return(false);
238
};
239
if (!other) return(false);
240
if (this.isSameNode(other)) return(true);
241
if (this.nodeType != other.nodeType) return(false);
242
if (diffValues('nodeName', 'localName', 'namespaceURI', 'prefix', 'nodeValue')) return(false);
243
if (diffNamedNodeMaps(this.attributes, other.attributes)) return(false);
244
if (diffNodeLists(this.childNodes, other.childNodes)) return(false);
245
if (this.nodeType == DOCUMENT_TYPE_NODE) {
246
if (diffValues('publicId', 'systemId', 'internalSubset')) return(false);
247
if (diffNamedNodeMaps(this.entities, other.entities)) return(false);
248
if (diffNamedNodeMaps(this.notations, other.notations)) return(false);
249
}
250
return (true);
251
};
252
/*
253
// Introduced in DOM Level 3:
254
DOMObject getFeature(in DOMString feature,
255
in DOMString version);
256
*/
257
// Introduced in DOM Level 3:
258
core.Node.prototype.setUserData = function(key, data, handler) {
259
var r = this[key] || null;
260
this[key] = data;
261
return(r);
262
};
263
264
// Introduced in DOM Level 3:
265
core.Node.prototype.getUserData = function(key) {
266
var r = this[key] || null;
267
return(r);
268
};
269
/*
270
interface NodeList {
271
Node item(in unsigned long index);
272
readonly attribute unsigned long length;
273
};
274
275
interface NamedNodeMap {
276
Node getNamedItem(in DOMString name);
277
Node setNamedItem(in Node arg)
278
raises(DOMException);
279
Node removeNamedItem(in DOMString name)
280
raises(DOMException);
281
Node item(in unsigned long index);
282
readonly attribute unsigned long length;
283
// Introduced in DOM Level 2:
284
Node getNamedItemNS(in DOMString namespaceURI,
285
in DOMString localName)
286
raises(DOMException);
287
// Introduced in DOM Level 2:
288
Node setNamedItemNS(in Node arg)
289
raises(DOMException);
290
// Introduced in DOM Level 2:
291
Node removeNamedItemNS(in DOMString namespaceURI,
292
in DOMString localName)
293
raises(DOMException);
294
};
295
296
interface CharacterData : Node {
297
attribute DOMString data;
298
// raises(DOMException) on setting
299
// raises(DOMException) on retrieval
300
301
readonly attribute unsigned long length;
302
DOMString substringData(in unsigned long offset,
303
in unsigned long count)
304
raises(DOMException);
305
void appendData(in DOMString arg)
306
raises(DOMException);
307
void insertData(in unsigned long offset,
308
in DOMString arg)
309
raises(DOMException);
310
void deleteData(in unsigned long offset,
311
in unsigned long count)
312
raises(DOMException);
313
void replaceData(in unsigned long offset,
314
in unsigned long count,
315
in DOMString arg)
316
raises(DOMException);
317
};
318
319
interface Attr : Node {
320
readonly attribute DOMString name;
321
readonly attribute boolean specified;
322
attribute DOMString value;
323
// raises(DOMException) on setting
324
325
// Introduced in DOM Level 2:
326
readonly attribute Element ownerElement;
327
// Introduced in DOM Level 3:
328
readonly attribute TypeInfo schemaTypeInfo;
329
330
*/
331
// Introduced in DOM Level 3:
332
defineGetter(core.Attr.prototype, 'isId', function() {
333
return (this.name.toLowerCase() === 'id');
334
});
335
/*
336
};
337
338
interface Element : Node {
339
readonly attribute DOMString tagName;
340
DOMString getAttribute(in DOMString name);
341
void setAttribute(in DOMString name,
342
in DOMString value)
343
raises(DOMException);
344
void removeAttribute(in DOMString name)
345
raises(DOMException);
346
Attr getAttributeNode(in DOMString name);
347
Attr setAttributeNode(in Attr newAttr)
348
raises(DOMException);
349
Attr removeAttributeNode(in Attr oldAttr)
350
raises(DOMException);
351
NodeList getElementsByTagName(in DOMString name);
352
// Introduced in DOM Level 2:
353
DOMString getAttributeNS(in DOMString namespaceURI,
354
in DOMString localName)
355
raises(DOMException);
356
// Introduced in DOM Level 2:
357
void setAttributeNS(in DOMString namespaceURI,
358
in DOMString qualifiedName,
359
in DOMString value)
360
raises(DOMException);
361
// Introduced in DOM Level 2:
362
void removeAttributeNS(in DOMString namespaceURI,
363
in DOMString localName)
364
raises(DOMException);
365
// Introduced in DOM Level 2:
366
Attr getAttributeNodeNS(in DOMString namespaceURI,
367
in DOMString localName)
368
raises(DOMException);
369
// Introduced in DOM Level 2:
370
Attr setAttributeNodeNS(in Attr newAttr)
371
raises(DOMException);
372
// Introduced in DOM Level 2:
373
NodeList getElementsByTagNameNS(in DOMString namespaceURI,
374
in DOMString localName)
375
raises(DOMException);
376
// Introduced in DOM Level 2:
377
boolean hasAttribute(in DOMString name);
378
// Introduced in DOM Level 2:
379
boolean hasAttributeNS(in DOMString namespaceURI,
380
in DOMString localName)
381
raises(DOMException);
382
// Introduced in DOM Level 3:
383
readonly attribute TypeInfo schemaTypeInfo;
384
// Introduced in DOM Level 3:
385
void setIdAttribute(in DOMString name,
386
in boolean isId)
387
raises(DOMException);
388
// Introduced in DOM Level 3:
389
void setIdAttributeNS(in DOMString namespaceURI,
390
in DOMString localName,
391
in boolean isId)
392
raises(DOMException);
393
// Introduced in DOM Level 3:
394
void setIdAttributeNode(in Attr idAttr,
395
in boolean isId)
396
raises(DOMException);
397
};
398
399
interface Text : CharacterData {
400
Text splitText(in unsigned long offset)
401
raises(DOMException);
402
// Introduced in DOM Level 3:
403
readonly attribute boolean isElementContentWhitespace;
404
// Introduced in DOM Level 3:
405
readonly attribute DOMString wholeText;
406
// Introduced in DOM Level 3:
407
Text replaceWholeText(in DOMString content)
408
raises(DOMException);
409
};
410
411
interface Comment : CharacterData {
412
};
413
414
// Introduced in DOM Level 3:
415
interface TypeInfo {
416
readonly attribute DOMString typeName;
417
readonly attribute DOMString typeNamespace;
418
419
// DerivationMethods
420
const unsigned long DERIVATION_RESTRICTION = 0x00000001;
421
const unsigned long DERIVATION_EXTENSION = 0x00000002;
422
const unsigned long DERIVATION_UNION = 0x00000004;
423
const unsigned long DERIVATION_LIST = 0x00000008;
424
425
boolean isDerivedFrom(in DOMString typeNamespaceArg,
426
in DOMString typeNameArg,
427
in unsigned long derivationMethod);
428
};
429
*/
430
// Introduced in DOM Level 3:
431
core.UserDataHandler = function() {};
432
core.UserDataHandler.prototype.NODE_CLONED = 1;
433
core.UserDataHandler.prototype.NODE_IMPORTED = 2;
434
core.UserDataHandler.prototype.NODE_DELETED = 3;
435
core.UserDataHandler.prototype.NODE_RENAMED = 4;
436
core.UserDataHandler.prototype.NODE_ADOPTED = 5;
437
core.UserDataHandler.prototype.handle = function(operation, key, data, src, dst) {};
438
439
// Introduced in DOM Level 3:
440
core.DOMError = function(severity, message, type, relatedException, relatedData, location) {
441
this._severity = severity;
442
this._message = message;
443
this._type = type;
444
this._relatedException = relatedException;
445
this._relatedData = relatedData;
446
this._location = location;
447
};
448
core.DOMError.prototype = {};
449
core.DOMError.prototype.SEVERITY_WARNING = 1;
450
core.DOMError.prototype.SEVERITY_ERROR = 2;
451
core.DOMError.prototype.SEVERITY_FATAL_ERROR = 3;
452
defineGetter(core.DOMError.prototype, 'severity', function() {
453
return this._severity;
454
});
455
defineGetter(core.DOMError.prototype, 'message', function() {
456
return this._message;
457
});
458
defineGetter(core.DOMError.prototype, 'type', function() {
459
return this._type;
460
});
461
defineGetter(core.DOMError.prototype, 'relatedException', function() {
462
return this._relatedException;
463
});
464
defineGetter(core.DOMError.prototype, 'relatedData', function() {
465
return this._relatedData;
466
});
467
defineGetter(core.DOMError.prototype, 'location', function() {
468
return this._location;
469
});
470
471
/*
472
// Introduced in DOM Level 3:
473
interface DOMErrorHandler {
474
boolean handleError(in DOMError error);
475
};
476
477
// Introduced in DOM Level 3:
478
interface DOMLocator {
479
readonly attribute long lineNumber;
480
readonly attribute long columnNumber;
481
readonly attribute long byteOffset;
482
readonly attribute long utf16Offset;
483
readonly attribute Node relatedNode;
484
readonly attribute DOMString uri;
485
};
486
*/
487
488
// Introduced in DOM Level 3:
489
core.DOMConfiguration = function(){
490
var possibleParameterNames = {
491
'canonical-form': [false, true], // extra rules for true
492
'cdata-sections': [true, false],
493
'check-character-normalization': [false, true],
494
'comments': [true, false],
495
'datatype-normalization': [false, true],
496
'element-content-whitespace': [true, false],
497
'entities': [true, false],
498
// 'error-handler': [],
499
'infoset': [undefined, true, false], // extra rules for true
500
'namespaces': [true, false],
501
'namespace-declarations': [true, false], // only checked if namespaces is true
502
'normalize-characters': [false, true],
503
// 'schema-location': [],
504
// 'schema-type': [],
505
'split-cdata-sections': [true, false],
506
'validate': [false, true],
507
'validate-if-schema': [false, true],
508
'well-formed': [true, false]
509
}
510
};
511
512
core.DOMConfiguration.prototype = {
513
setParameter: function(name, value) {},
514
getParameter: function(name) {},
515
canSetParameter: function(name, value) {},
516
parameterNames: function() {}
517
};
518
519
//core.Document.prototype._domConfig = new core.DOMConfiguration();
520
defineGetter(core.Document.prototype, 'domConfig', function() {
521
return this._domConfig || new core.DOMConfiguration();;
522
});
523
524
// Introduced in DOM Level 3:
525
core.DOMStringList = function() {};
526
527
core.DOMStringList.prototype = {
528
item: function() {},
529
length: function() {},
530
contains: function() {}
531
};
532
533
534
/*
535
interface CDATASection : Text {
536
};
537
538
interface DocumentType : Node {
539
readonly attribute DOMString name;
540
readonly attribute NamedNodeMap entities;
541
readonly attribute NamedNodeMap notations;
542
// Introduced in DOM Level 2:
543
readonly attribute DOMString publicId;
544
// Introduced in DOM Level 2:
545
readonly attribute DOMString systemId;
546
// Introduced in DOM Level 2:
547
readonly attribute DOMString internalSubset;
548
};
549
550
interface Notation : Node {
551
readonly attribute DOMString publicId;
552
readonly attribute DOMString systemId;
553
};
554
555
interface Entity : Node {
556
readonly attribute DOMString publicId;
557
readonly attribute DOMString systemId;
558
readonly attribute DOMString notationName;
559
// Introduced in DOM Level 3:
560
readonly attribute DOMString inputEncoding;
561
// Introduced in DOM Level 3:
562
readonly attribute DOMString xmlEncoding;
563
// Introduced in DOM Level 3:
564
readonly attribute DOMString xmlVersion;
565
};
566
567
interface EntityReference : Node {
568
};
569
570
interface ProcessingInstruction : Node {
571
readonly attribute DOMString target;
572
attribute DOMString data;
573
// raises(DOMException) on setting
574
575
};
576
577
interface DocumentFragment : Node {
578
};
579
580
interface Document : Node {
581
// Modified in DOM Level 3:
582
readonly attribute DocumentType doctype;
583
readonly attribute DOMImplementation implementation;
584
readonly attribute Element documentElement;
585
Element createElement(in DOMString tagName)
586
raises(DOMException);
587
DocumentFragment createDocumentFragment();
588
Text createTextNode(in DOMString data);
589
Comment createComment(in DOMString data);
590
CDATASection createCDATASection(in DOMString data)
591
raises(DOMException);
592
ProcessingInstruction createProcessingInstruction(in DOMString target,
593
in DOMString data)
594
raises(DOMException);
595
Attr createAttribute(in DOMString name)
596
raises(DOMException);
597
EntityReference createEntityReference(in DOMString name)
598
raises(DOMException);
599
NodeList getElementsByTagName(in DOMString tagname);
600
// Introduced in DOM Level 2:
601
Node importNode(in Node importedNode,
602
in boolean deep)
603
raises(DOMException);
604
// Introduced in DOM Level 2:
605
Element createElementNS(in DOMString namespaceURI,
606
in DOMString qualifiedName)
607
raises(DOMException);
608
// Introduced in DOM Level 2:
609
Attr createAttributeNS(in DOMString namespaceURI,
610
in DOMString qualifiedName)
611
raises(DOMException);
612
// Introduced in DOM Level 2:
613
NodeList getElementsByTagNameNS(in DOMString namespaceURI,
614
in DOMString localName);
615
// Introduced in DOM Level 2:
616
Element getElementById(in DOMString elementId);
617
*/
618
619
// Introduced in DOM Level 3:
620
core.Document.prototype._inputEncoding = null;
621
defineGetter(core.Document.prototype, 'inputEncoding', function() {
622
return this._inputEncoding;
623
});
624
/*
625
// Introduced in DOM Level 3:
626
readonly attribute DOMString xmlEncoding;
627
// Introduced in DOM Level 3:
628
attribute boolean xmlStandalone;
629
// raises(DOMException) on setting
630
631
// Introduced in DOM Level 3:
632
attribute DOMString xmlVersion;
633
// raises(DOMException) on setting
634
635
// Introduced in DOM Level 3:
636
attribute boolean strictErrorChecking;
637
// Introduced in DOM Level 3:
638
attribute DOMString documentURI;
639
// Introduced in DOM Level 3:
640
Node adoptNode(in Node source)
641
raises(DOMException);
642
// Introduced in DOM Level 3:
643
readonly attribute DOMConfiguration domConfig;
644
// Introduced in DOM Level 3:
645
void normalizeDocument();
646
// Introduced in DOM Level 3:
647
Node renameNode(in Node n,
648
in DOMString namespaceURI,
649
in DOMString qualifiedName)
650
raises(DOMException);
651
};
652
};
653
654
#endif // _DOM_IDL_
655
*/
656
657
exports.dom = {
658
level3 : {
659
core: core
660
}
661
};
662
663