@xmldom/xmldom 0.8.2 → 0.9.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/index.d.ts +42 -40
- package/lib/conventions.js +183 -9
- package/lib/dom-parser.js +237 -52
- package/lib/dom.js +262 -79
- package/lib/entities.js +2 -0
- package/lib/index.js +2 -0
- package/lib/sax.js +33 -22
- package/package.json +6 -6
package/lib/dom.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
var conventions = require("./conventions");
|
|
2
|
-
|
|
2
|
+
var isHTMLRawTextElement = conventions.isHTMLRawTextElement;
|
|
3
|
+
var isHTMLVoidElement = conventions.isHTMLVoidElement;
|
|
4
|
+
var MIME_TYPE = conventions.MIME_TYPE;
|
|
3
5
|
var NAMESPACE = conventions.NAMESPACE;
|
|
4
6
|
|
|
5
7
|
/**
|
|
@@ -156,23 +158,23 @@ NodeList.prototype = {
|
|
|
156
158
|
* The number of nodes in the list. The range of valid child node indices is 0 to length-1 inclusive.
|
|
157
159
|
* @standard level1
|
|
158
160
|
*/
|
|
159
|
-
length:0,
|
|
161
|
+
length:0,
|
|
160
162
|
/**
|
|
161
163
|
* Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.
|
|
162
164
|
* @standard level1
|
|
163
|
-
* @param index unsigned long
|
|
165
|
+
* @param index unsigned long
|
|
164
166
|
* Index into the collection.
|
|
165
167
|
* @return Node
|
|
166
|
-
* The node at the indexth position in the NodeList, or null if that is not a valid index.
|
|
168
|
+
* The node at the indexth position in the NodeList, or null if that is not a valid index.
|
|
167
169
|
*/
|
|
168
170
|
item: function(index) {
|
|
169
171
|
return this[index] || null;
|
|
170
172
|
},
|
|
171
|
-
toString:function(
|
|
173
|
+
toString: function (nodeFilter) {
|
|
172
174
|
for(var buf = [], i = 0;i<this.length;i++){
|
|
173
|
-
serializeToString(this[i],buf,
|
|
175
|
+
serializeToString(this[i], buf, nodeFilter)
|
|
174
176
|
}
|
|
175
|
-
return buf.join('')
|
|
177
|
+
return buf.join('')
|
|
176
178
|
}
|
|
177
179
|
};
|
|
178
180
|
|
|
@@ -207,7 +209,7 @@ _extends(LiveNodeList,NodeList);
|
|
|
207
209
|
* but this is simply to allow convenient enumeration of the contents of a NamedNodeMap,
|
|
208
210
|
* and does not imply that the DOM specifies an order to these Nodes.
|
|
209
211
|
* NamedNodeMap objects in the DOM are live.
|
|
210
|
-
* used for attributes or DocumentType entities
|
|
212
|
+
* used for attributes or DocumentType entities
|
|
211
213
|
*/
|
|
212
214
|
function NamedNodeMap() {
|
|
213
215
|
};
|
|
@@ -296,10 +298,10 @@ NamedNodeMap.prototype = {
|
|
|
296
298
|
var attr = this.getNamedItem(key);
|
|
297
299
|
_removeNamedNode(this._ownerElement,this,attr);
|
|
298
300
|
return attr;
|
|
299
|
-
|
|
300
|
-
|
|
301
|
+
|
|
302
|
+
|
|
301
303
|
},// raises: NOT_FOUND_ERR,NO_MODIFICATION_ALLOWED_ERR
|
|
302
|
-
|
|
304
|
+
|
|
303
305
|
//for level2
|
|
304
306
|
removeNamedItemNS:function(namespaceURI,localName){
|
|
305
307
|
var attr = this.getNamedItemNS(namespaceURI,localName);
|
|
@@ -359,15 +361,17 @@ DOMImplementation.prototype = {
|
|
|
359
361
|
* Creates an XML Document object of the specified type with its document element.
|
|
360
362
|
*
|
|
361
363
|
* __It behaves slightly different from the description in the living standard__:
|
|
362
|
-
* - There is no interface/class `XMLDocument`, it returns a `Document` instance.
|
|
363
|
-
* - `
|
|
364
|
-
* - this implementation
|
|
365
|
-
* (
|
|
364
|
+
* - There is no interface/class `XMLDocument`, it returns a `Document` instance (with it's `type` set to `'xml'`).
|
|
365
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
366
|
+
* - The methods provided by this implementation are not validating names or qualified names.
|
|
367
|
+
* (They are only validated by the SAX parser when calling `DOMParser.parseFromString`)
|
|
366
368
|
*
|
|
367
|
-
* @param {string|null} namespaceURI
|
|
369
|
+
* @param {string | null} namespaceURI
|
|
368
370
|
* @param {string} qualifiedName
|
|
369
|
-
* @param {DocumentType
|
|
370
|
-
* @returns {Document}
|
|
371
|
+
* @param {DocumentType | null} [doctype=null]
|
|
372
|
+
* @returns {Document} the XML document
|
|
373
|
+
*
|
|
374
|
+
* @see #createHTMLDocument
|
|
371
375
|
*
|
|
372
376
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createDocument MDN
|
|
373
377
|
* @see https://www.w3.org/TR/DOM-Level-2-Core/core.html#Level-2-Core-DOM-createDocument DOM Level 2 Core (initial)
|
|
@@ -378,7 +382,13 @@ DOMImplementation.prototype = {
|
|
|
378
382
|
* @see https://www.w3.org/TR/xml-names/#ns-qualnames XML Namespaces: Qualified names
|
|
379
383
|
*/
|
|
380
384
|
createDocument: function(namespaceURI, qualifiedName, doctype){
|
|
381
|
-
var
|
|
385
|
+
var contentType = MIME_TYPE.XML_APPLICATION;
|
|
386
|
+
if (namespaceURI === NAMESPACE.HTML) {
|
|
387
|
+
contentType = MIME_TYPE.XML_XHTML_APPLICATION;
|
|
388
|
+
} else if (namespaceURI === NAMESPACE.SVG) {
|
|
389
|
+
contentType = MIME_TYPE.XML_SVG_IMAGE
|
|
390
|
+
}
|
|
391
|
+
var doc = new Document({contentType: contentType});
|
|
382
392
|
doc.implementation = this;
|
|
383
393
|
doc.childNodes = new NodeList();
|
|
384
394
|
doc.doctype = doctype || null;
|
|
@@ -397,6 +407,7 @@ DOMImplementation.prototype = {
|
|
|
397
407
|
* __This behavior is slightly different from the in the specs__:
|
|
398
408
|
* - this implementation is not validating names or qualified names
|
|
399
409
|
* (when parsing XML strings, the SAX parser takes care of that)
|
|
410
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
400
411
|
*
|
|
401
412
|
* @param {string} qualifiedName
|
|
402
413
|
* @param {string} [publicId]
|
|
@@ -420,6 +431,40 @@ DOMImplementation.prototype = {
|
|
|
420
431
|
node.systemId = systemId || '';
|
|
421
432
|
|
|
422
433
|
return node;
|
|
434
|
+
},
|
|
435
|
+
/**
|
|
436
|
+
* Returns an HTML document, that might already have a basic DOM structure.
|
|
437
|
+
*
|
|
438
|
+
* __It behaves slightly different from the description in the living standard__:
|
|
439
|
+
* - If the first argument is `false` no initial nodes are added (steps 3-7 in the specs are omitted)
|
|
440
|
+
* - `encoding`, `mode`, `origin`, `url` fields are currently not declared.
|
|
441
|
+
*
|
|
442
|
+
* @param {string | false} [title]
|
|
443
|
+
* @returns {Document} The HTML document
|
|
444
|
+
*
|
|
445
|
+
* @see https://dom.spec.whatwg.org/#dom-domimplementation-createhtmldocument
|
|
446
|
+
* @see https://dom.spec.whatwg.org/#html-document
|
|
447
|
+
*/
|
|
448
|
+
createHTMLDocument: function(title) {
|
|
449
|
+
var doc = new Document({contentType: MIME_TYPE.HTML})
|
|
450
|
+
doc.implementation = this
|
|
451
|
+
doc.childNodes = new NodeList()
|
|
452
|
+
if (title !== false) {
|
|
453
|
+
doc.doctype = this.createDocumentType('html')
|
|
454
|
+
doc.doctype.ownerDocument = this;
|
|
455
|
+
doc.appendChild(doc.doctype);
|
|
456
|
+
var htmlNode = doc.createElement('html')
|
|
457
|
+
doc.appendChild(htmlNode)
|
|
458
|
+
var headNode = doc.createElement('head')
|
|
459
|
+
htmlNode.appendChild(headNode)
|
|
460
|
+
if (typeof title === 'string') {
|
|
461
|
+
var titleNode = doc.createElement('title');
|
|
462
|
+
titleNode.appendChild(doc.createTextNode(title))
|
|
463
|
+
headNode.appendChild(titleNode)
|
|
464
|
+
}
|
|
465
|
+
htmlNode.appendChild(doc.createElement('body'))
|
|
466
|
+
}
|
|
467
|
+
return doc
|
|
423
468
|
}
|
|
424
469
|
};
|
|
425
470
|
|
|
@@ -427,7 +472,6 @@ DOMImplementation.prototype = {
|
|
|
427
472
|
/**
|
|
428
473
|
* @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1950641247
|
|
429
474
|
*/
|
|
430
|
-
|
|
431
475
|
function Node() {
|
|
432
476
|
};
|
|
433
477
|
|
|
@@ -445,10 +489,10 @@ Node.prototype = {
|
|
|
445
489
|
prefix : null,
|
|
446
490
|
localName : null,
|
|
447
491
|
// Modified in DOM Level 2:
|
|
448
|
-
insertBefore:function(newChild, refChild){//raises
|
|
492
|
+
insertBefore:function(newChild, refChild){//raises
|
|
449
493
|
return _insertBefore(this,newChild,refChild);
|
|
450
494
|
},
|
|
451
|
-
replaceChild:function(newChild, oldChild){//raises
|
|
495
|
+
replaceChild:function(newChild, oldChild){//raises
|
|
452
496
|
this.insertBefore(newChild,oldChild);
|
|
453
497
|
if(oldChild){
|
|
454
498
|
this.removeChild(oldChild);
|
|
@@ -568,9 +612,48 @@ function _visitNode(node,callback){
|
|
|
568
612
|
}
|
|
569
613
|
}
|
|
570
614
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
615
|
+
/**
|
|
616
|
+
* @typedef DocumentOptions
|
|
617
|
+
* @property {string} [contentType=MIME_TYPE.XML_APPLICATION]
|
|
618
|
+
*/
|
|
619
|
+
/**
|
|
620
|
+
* The Document interface describes the common properties and methods for any kind of document.
|
|
621
|
+
*
|
|
622
|
+
* It should usually be created using `new DOMImplementation().createDocument(...)`
|
|
623
|
+
* or `new DOMImplementation().createHTMLDocument(...)`.
|
|
624
|
+
*
|
|
625
|
+
* The constructor is considered a private API and offers to initially set the `contentType` property
|
|
626
|
+
* via it's options parameter.
|
|
627
|
+
*
|
|
628
|
+
* @param {DocumentOptions} [options]
|
|
629
|
+
* @private
|
|
630
|
+
* @constructor
|
|
631
|
+
*
|
|
632
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document
|
|
633
|
+
* @see https://dom.spec.whatwg.org/#interface-document
|
|
634
|
+
*/
|
|
635
|
+
function Document(options){
|
|
636
|
+
var opt = options || {};
|
|
637
|
+
/**
|
|
638
|
+
* The mime type of the document is determined at creation time and can not be modified.
|
|
639
|
+
*
|
|
640
|
+
* @type {string}
|
|
641
|
+
* @readonly
|
|
642
|
+
*
|
|
643
|
+
* @see https://dom.spec.whatwg.org/#concept-document-content-type
|
|
644
|
+
* @see DOMImplementation
|
|
645
|
+
* @see MIME_TYPE
|
|
646
|
+
*/
|
|
647
|
+
this.contentType = opt.contentType || MIME_TYPE.XML_APPLICATION
|
|
648
|
+
/**
|
|
649
|
+
*
|
|
650
|
+
* @type {'html' | 'xml'}
|
|
651
|
+
* @readonly
|
|
652
|
+
*
|
|
653
|
+
* @see https://dom.spec.whatwg.org/#concept-document-type
|
|
654
|
+
* @see DOMImplementation
|
|
655
|
+
*/
|
|
656
|
+
this.type = MIME_TYPE.isHTML(this.contentType) ? 'html' : 'xml'
|
|
574
657
|
}
|
|
575
658
|
|
|
576
659
|
function _onAddAttribute(doc,el,newAttr){
|
|
@@ -675,8 +758,8 @@ function _insertBefore(parentNode,newChild,nextChild){
|
|
|
675
758
|
|
|
676
759
|
newFirst.previousSibling = pre;
|
|
677
760
|
newLast.nextSibling = nextChild;
|
|
678
|
-
|
|
679
|
-
|
|
761
|
+
|
|
762
|
+
|
|
680
763
|
if(pre){
|
|
681
764
|
pre.nextSibling = newFirst;
|
|
682
765
|
}else{
|
|
@@ -727,7 +810,12 @@ function _appendSingleChild (parentNode, newChild) {
|
|
|
727
810
|
}
|
|
728
811
|
|
|
729
812
|
Document.prototype = {
|
|
730
|
-
|
|
813
|
+
/**
|
|
814
|
+
* The implementation that created this document
|
|
815
|
+
* @readonly
|
|
816
|
+
* @type DOMImplementation
|
|
817
|
+
*/
|
|
818
|
+
implementation : null,
|
|
731
819
|
nodeName : '#document',
|
|
732
820
|
nodeType : DOCUMENT_NODE,
|
|
733
821
|
/**
|
|
@@ -824,10 +912,34 @@ Document.prototype = {
|
|
|
824
912
|
});
|
|
825
913
|
},
|
|
826
914
|
|
|
827
|
-
|
|
915
|
+
/**
|
|
916
|
+
* Creates a new `Element` that is owned by this `Document`.
|
|
917
|
+
* In HTML Documents `localName` is the lower cased `tagName`,
|
|
918
|
+
* otherwise no transformation is being applied.
|
|
919
|
+
* When `contentType` implies the HTML namespace, it will be set as `namespaceURI`.
|
|
920
|
+
*
|
|
921
|
+
* __This implementation differs from the specification:__
|
|
922
|
+
* - The provided name is not checked against the `Name` production,
|
|
923
|
+
* so no related error will be thrown.
|
|
924
|
+
* - There is no interface `HTMLElement`, it is always an `Element`.
|
|
925
|
+
* - There is no support for a second argument to indicate using custom elements.
|
|
926
|
+
*
|
|
927
|
+
* @param {string} tagName
|
|
928
|
+
* @return {Element}
|
|
929
|
+
*
|
|
930
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
|
|
931
|
+
* @see https://dom.spec.whatwg.org/#dom-document-createelement
|
|
932
|
+
* @see https://dom.spec.whatwg.org/#concept-create-element
|
|
933
|
+
*/
|
|
828
934
|
createElement : function(tagName){
|
|
829
935
|
var node = new Element();
|
|
830
936
|
node.ownerDocument = this;
|
|
937
|
+
if (this.type === 'html') {
|
|
938
|
+
tagName = tagName.toLowerCase()
|
|
939
|
+
}
|
|
940
|
+
if (MIME_TYPE.hasDefaultHTMLNamespace(this.contentType)) {
|
|
941
|
+
node.namespaceURI = NAMESPACE.HTML
|
|
942
|
+
}
|
|
831
943
|
node.nodeName = tagName;
|
|
832
944
|
node.tagName = tagName;
|
|
833
945
|
node.localName = tagName;
|
|
@@ -867,9 +979,30 @@ Document.prototype = {
|
|
|
867
979
|
node.nodeValue= node.data = data;
|
|
868
980
|
return node;
|
|
869
981
|
},
|
|
870
|
-
|
|
982
|
+
/**
|
|
983
|
+
* Creates an `Attr` node that is owned by this document.
|
|
984
|
+
* In HTML Documents `localName` is the lower cased `name`,
|
|
985
|
+
* otherwise no transformation is being applied.
|
|
986
|
+
*
|
|
987
|
+
* __This implementation differs from the specification:__
|
|
988
|
+
* - The provided name is not checked against the `Name` production,
|
|
989
|
+
* so no related error will be thrown.
|
|
990
|
+
*
|
|
991
|
+
* @param {string} name
|
|
992
|
+
* @return {Attr}
|
|
993
|
+
*
|
|
994
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createAttribute
|
|
995
|
+
* @see https://dom.spec.whatwg.org/#dom-document-createattribute
|
|
996
|
+
*/
|
|
997
|
+
createAttribute: function(name){
|
|
998
|
+
if (this.type === 'html') {
|
|
999
|
+
name = name.toLowerCase()
|
|
1000
|
+
}
|
|
1001
|
+
return this._createAttribute(name);
|
|
1002
|
+
},
|
|
1003
|
+
_createAttribute: function(name){
|
|
871
1004
|
var node = new Attr();
|
|
872
|
-
node.ownerDocument
|
|
1005
|
+
node.ownerDocument = this;
|
|
873
1006
|
node.name = name;
|
|
874
1007
|
node.nodeName = name;
|
|
875
1008
|
node.localName = name;
|
|
@@ -929,6 +1062,12 @@ function Element() {
|
|
|
929
1062
|
};
|
|
930
1063
|
Element.prototype = {
|
|
931
1064
|
nodeType : ELEMENT_NODE,
|
|
1065
|
+
getQualifiedName: function () {
|
|
1066
|
+
return this.prefix ? this.prefix+':'+this.localName : this.localName
|
|
1067
|
+
},
|
|
1068
|
+
_isInHTMLDocumentAndNamespace: function () {
|
|
1069
|
+
return this.ownerDocument.type === 'html' && this.namespaceURI === NAMESPACE.HTML;
|
|
1070
|
+
},
|
|
932
1071
|
hasAttribute : function(name){
|
|
933
1072
|
return this.getAttributeNode(name)!=null;
|
|
934
1073
|
},
|
|
@@ -937,10 +1076,16 @@ Element.prototype = {
|
|
|
937
1076
|
return attr && attr.value || '';
|
|
938
1077
|
},
|
|
939
1078
|
getAttributeNode : function(name){
|
|
1079
|
+
if (this._isInHTMLDocumentAndNamespace()) {
|
|
1080
|
+
name = name.toLowerCase()
|
|
1081
|
+
}
|
|
940
1082
|
return this.attributes.getNamedItem(name);
|
|
941
1083
|
},
|
|
942
1084
|
setAttribute : function(name, value){
|
|
943
|
-
|
|
1085
|
+
if (this._isInHTMLDocumentAndNamespace()) {
|
|
1086
|
+
name = name.toLowerCase()
|
|
1087
|
+
}
|
|
1088
|
+
var attr = this.ownerDocument._createAttribute(name)
|
|
944
1089
|
attr.value = attr.nodeValue = "" + value;
|
|
945
1090
|
this.setAttributeNode(attr)
|
|
946
1091
|
},
|
|
@@ -948,8 +1093,8 @@ Element.prototype = {
|
|
|
948
1093
|
var attr = this.getAttributeNode(name)
|
|
949
1094
|
attr && this.removeAttributeNode(attr);
|
|
950
1095
|
},
|
|
951
|
-
|
|
952
|
-
//four real
|
|
1096
|
+
|
|
1097
|
+
// four real operation method
|
|
953
1098
|
appendChild:function(newChild){
|
|
954
1099
|
if(newChild.nodeType === DOCUMENT_FRAGMENT_NODE){
|
|
955
1100
|
return this.insertBefore(newChild,null);
|
|
@@ -972,7 +1117,7 @@ Element.prototype = {
|
|
|
972
1117
|
var old = this.getAttributeNodeNS(namespaceURI, localName);
|
|
973
1118
|
old && this.removeAttributeNode(old);
|
|
974
1119
|
},
|
|
975
|
-
|
|
1120
|
+
|
|
976
1121
|
hasAttributeNS : function(namespaceURI, localName){
|
|
977
1122
|
return this.getAttributeNodeNS(namespaceURI, localName)!=null;
|
|
978
1123
|
},
|
|
@@ -988,13 +1133,51 @@ Element.prototype = {
|
|
|
988
1133
|
getAttributeNodeNS : function(namespaceURI, localName){
|
|
989
1134
|
return this.attributes.getNamedItemNS(namespaceURI, localName);
|
|
990
1135
|
},
|
|
991
|
-
|
|
992
|
-
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* Returns a LiveNodeList of elements with the given qualifiedName.
|
|
1139
|
+
* Searching for all descendants can be done by passing `*` as `qualifiedName`.
|
|
1140
|
+
*
|
|
1141
|
+
* All descendants of the specified element are searched, but not the element itself.
|
|
1142
|
+
* The returned list is live, which means it updates itself with the DOM tree automatically.
|
|
1143
|
+
* Therefore, there is no need to call `Element.getElementsByTagName()`
|
|
1144
|
+
* with the same element and arguments repeatedly if the DOM changes in between calls.
|
|
1145
|
+
*
|
|
1146
|
+
* When called on an HTML element in an HTML document,
|
|
1147
|
+
* `getElementsByTagName` lower-cases the argument before searching for it.
|
|
1148
|
+
* This is undesirable when trying to match camel-cased SVG elements
|
|
1149
|
+
* (such as `<linearGradient>`) in an HTML document.
|
|
1150
|
+
* Instead, use `Element.getElementsByTagNameNS()`,
|
|
1151
|
+
* which preserves the capitalization of the tag name.
|
|
1152
|
+
*
|
|
1153
|
+
* `Element.getElementsByTagName` is similar to `Document.getElementsByTagName()`,
|
|
1154
|
+
* except that it only searches for elements that are descendants of the specified element.
|
|
1155
|
+
*
|
|
1156
|
+
* @param {string} qualifiedName
|
|
1157
|
+
* @return {LiveNodeList}
|
|
1158
|
+
*
|
|
1159
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
|
|
1160
|
+
* @see https://dom.spec.whatwg.org/#concept-getelementsbytagname
|
|
1161
|
+
*/
|
|
1162
|
+
getElementsByTagName : function(qualifiedName){
|
|
1163
|
+
var isHTMLDocument = (this.nodeType === DOCUMENT_NODE ? this : this.ownerDocument).type === 'html'
|
|
1164
|
+
var lowerQualifiedName = qualifiedName.toLowerCase()
|
|
993
1165
|
return new LiveNodeList(this,function(base){
|
|
994
1166
|
var ls = [];
|
|
995
|
-
_visitNode(base,function(node){
|
|
996
|
-
if(node
|
|
1167
|
+
_visitNode(base, function(node) {
|
|
1168
|
+
if (node === base || node.nodeType !== ELEMENT_NODE) {
|
|
1169
|
+
return
|
|
1170
|
+
}
|
|
1171
|
+
if (qualifiedName === '*') {
|
|
997
1172
|
ls.push(node);
|
|
1173
|
+
} else {
|
|
1174
|
+
var nodeQualifiedName = node.getQualifiedName();
|
|
1175
|
+
var matchingQName = isHTMLDocument && node.namespaceURI === NAMESPACE.HTML
|
|
1176
|
+
? lowerQualifiedName
|
|
1177
|
+
: qualifiedName
|
|
1178
|
+
if(nodeQualifiedName === matchingQName){
|
|
1179
|
+
ls.push(node);
|
|
1180
|
+
}
|
|
998
1181
|
}
|
|
999
1182
|
});
|
|
1000
1183
|
return ls;
|
|
@@ -1009,7 +1192,7 @@ Element.prototype = {
|
|
|
1009
1192
|
}
|
|
1010
1193
|
});
|
|
1011
1194
|
return ls;
|
|
1012
|
-
|
|
1195
|
+
|
|
1013
1196
|
});
|
|
1014
1197
|
}
|
|
1015
1198
|
};
|
|
@@ -1038,7 +1221,7 @@ CharacterData.prototype = {
|
|
|
1038
1221
|
},
|
|
1039
1222
|
insertData: function(offset,text) {
|
|
1040
1223
|
this.replaceData(offset,0,text);
|
|
1041
|
-
|
|
1224
|
+
|
|
1042
1225
|
},
|
|
1043
1226
|
appendChild:function(newChild){
|
|
1044
1227
|
throw new Error(ExceptionMessage[HIERARCHY_REQUEST_ERR])
|
|
@@ -1123,29 +1306,26 @@ function ProcessingInstruction() {
|
|
|
1123
1306
|
ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
|
|
1124
1307
|
_extends(ProcessingInstruction,Node);
|
|
1125
1308
|
function XMLSerializer(){}
|
|
1126
|
-
XMLSerializer.prototype.serializeToString = function(node,
|
|
1127
|
-
return nodeSerializeToString.call(node,
|
|
1309
|
+
XMLSerializer.prototype.serializeToString = function (node, nodeFilter) {
|
|
1310
|
+
return nodeSerializeToString.call(node, nodeFilter);
|
|
1128
1311
|
}
|
|
1129
1312
|
Node.prototype.toString = nodeSerializeToString;
|
|
1130
|
-
function nodeSerializeToString(
|
|
1313
|
+
function nodeSerializeToString(nodeFilter) {
|
|
1131
1314
|
var buf = [];
|
|
1132
|
-
var refNode = this.nodeType
|
|
1315
|
+
var refNode = this.nodeType === DOCUMENT_NODE && this.documentElement || this;
|
|
1133
1316
|
var prefix = refNode.prefix;
|
|
1134
1317
|
var uri = refNode.namespaceURI;
|
|
1135
|
-
|
|
1318
|
+
|
|
1136
1319
|
if(uri && prefix == null){
|
|
1137
|
-
//console.log(prefix)
|
|
1138
1320
|
var prefix = refNode.lookupPrefix(uri);
|
|
1139
1321
|
if(prefix == null){
|
|
1140
|
-
//isHTML = true;
|
|
1141
1322
|
var visibleNamespaces=[
|
|
1142
1323
|
{namespace:uri,prefix:null}
|
|
1143
1324
|
//{namespace:uri,prefix:''}
|
|
1144
1325
|
]
|
|
1145
1326
|
}
|
|
1146
1327
|
}
|
|
1147
|
-
serializeToString(this,buf,
|
|
1148
|
-
//console.log('###',this.nodeType,uri,prefix,buf.join(''))
|
|
1328
|
+
serializeToString(this,buf,nodeFilter,visibleNamespaces);
|
|
1149
1329
|
return buf.join('');
|
|
1150
1330
|
}
|
|
1151
1331
|
|
|
@@ -1165,8 +1345,8 @@ function needNamespaceDefine(node, isHTML, visibleNamespaces) {
|
|
|
1165
1345
|
if (prefix === "xml" && uri === NAMESPACE.XML || uri === NAMESPACE.XMLNS) {
|
|
1166
1346
|
return false;
|
|
1167
1347
|
}
|
|
1168
|
-
|
|
1169
|
-
var i = visibleNamespaces.length
|
|
1348
|
+
|
|
1349
|
+
var i = visibleNamespaces.length
|
|
1170
1350
|
while (i--) {
|
|
1171
1351
|
var ns = visibleNamespaces[i];
|
|
1172
1352
|
// get namespace prefix
|
|
@@ -1193,10 +1373,12 @@ function addSerializedAttribute(buf, qualifiedName, value) {
|
|
|
1193
1373
|
buf.push(' ', qualifiedName, '="', value.replace(/[<>&"\t\n\r]/g, _xmlEncoder), '"')
|
|
1194
1374
|
}
|
|
1195
1375
|
|
|
1196
|
-
function serializeToString(node,buf,
|
|
1376
|
+
function serializeToString (node, buf, nodeFilter, visibleNamespaces) {
|
|
1197
1377
|
if (!visibleNamespaces) {
|
|
1198
1378
|
visibleNamespaces = [];
|
|
1199
1379
|
}
|
|
1380
|
+
var doc = node.nodeType === DOCUMENT_NODE ? node : node.ownerDocument
|
|
1381
|
+
var isHTML = doc.type === 'html'
|
|
1200
1382
|
|
|
1201
1383
|
if(nodeFilter){
|
|
1202
1384
|
node = nodeFilter(node);
|
|
@@ -1217,8 +1399,6 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
|
|
1217
1399
|
var len = attrs.length;
|
|
1218
1400
|
var child = node.firstChild;
|
|
1219
1401
|
var nodeName = node.tagName;
|
|
1220
|
-
|
|
1221
|
-
isHTML = NAMESPACE.isHTML(node.namespaceURI) || isHTML
|
|
1222
1402
|
|
|
1223
1403
|
var prefixedNodeName = nodeName
|
|
1224
1404
|
if (!isHTML && !node.prefix && node.namespaceURI) {
|
|
@@ -1273,39 +1453,43 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
|
|
1273
1453
|
addSerializedAttribute(buf, prefix ? 'xmlns:' + prefix : "xmlns", uri);
|
|
1274
1454
|
visibleNamespaces.push({ prefix: prefix, namespace:uri });
|
|
1275
1455
|
}
|
|
1276
|
-
serializeToString(attr,buf,
|
|
1456
|
+
serializeToString(attr,buf,nodeFilter,visibleNamespaces);
|
|
1277
1457
|
}
|
|
1278
1458
|
|
|
1279
|
-
// add namespace for current node
|
|
1459
|
+
// add namespace for current node
|
|
1280
1460
|
if (nodeName === prefixedNodeName && needNamespaceDefine(node, isHTML, visibleNamespaces)) {
|
|
1281
1461
|
var prefix = node.prefix||'';
|
|
1282
1462
|
var uri = node.namespaceURI;
|
|
1283
1463
|
addSerializedAttribute(buf, prefix ? 'xmlns:' + prefix : "xmlns", uri);
|
|
1284
1464
|
visibleNamespaces.push({ prefix: prefix, namespace:uri });
|
|
1285
1465
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1466
|
+
// in XML elements can be closed when they have no children
|
|
1467
|
+
var canCloseTag = !child;
|
|
1468
|
+
if (canCloseTag && (isHTML || NAMESPACE.isHTML(node.namespaceURI))) {
|
|
1469
|
+
// in HTML (doc or ns) only void elements can be closed right away
|
|
1470
|
+
canCloseTag = isHTMLVoidElement(nodeName)
|
|
1471
|
+
}
|
|
1472
|
+
if (canCloseTag) {
|
|
1473
|
+
buf.push("/>");
|
|
1474
|
+
} else {
|
|
1475
|
+
buf.push(">");
|
|
1289
1476
|
//if is cdata child node
|
|
1290
|
-
if(isHTML &&
|
|
1477
|
+
if (isHTML && isHTMLRawTextElement(nodeName)) {
|
|
1291
1478
|
while(child){
|
|
1292
1479
|
if(child.data){
|
|
1293
1480
|
buf.push(child.data);
|
|
1294
1481
|
}else{
|
|
1295
|
-
serializeToString(child, buf,
|
|
1482
|
+
serializeToString(child, buf, nodeFilter, visibleNamespaces.slice());
|
|
1296
1483
|
}
|
|
1297
1484
|
child = child.nextSibling;
|
|
1298
1485
|
}
|
|
1299
|
-
}else
|
|
1300
|
-
{
|
|
1486
|
+
} else {
|
|
1301
1487
|
while(child){
|
|
1302
|
-
serializeToString(child, buf,
|
|
1488
|
+
serializeToString(child, buf, nodeFilter, visibleNamespaces.slice());
|
|
1303
1489
|
child = child.nextSibling;
|
|
1304
1490
|
}
|
|
1305
1491
|
}
|
|
1306
|
-
buf.push(
|
|
1307
|
-
}else{
|
|
1308
|
-
buf.push('/>');
|
|
1492
|
+
buf.push("</", prefixedNodeName, ">");
|
|
1309
1493
|
}
|
|
1310
1494
|
// remove added visible namespaces
|
|
1311
1495
|
//visibleNamespaces.length = startVisibleNamespaces;
|
|
@@ -1314,7 +1498,7 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
|
|
1314
1498
|
case DOCUMENT_FRAGMENT_NODE:
|
|
1315
1499
|
var child = node.firstChild;
|
|
1316
1500
|
while(child){
|
|
1317
|
-
serializeToString(child, buf,
|
|
1501
|
+
serializeToString(child, buf, nodeFilter, visibleNamespaces.slice());
|
|
1318
1502
|
child = child.nextSibling;
|
|
1319
1503
|
}
|
|
1320
1504
|
return;
|
|
@@ -1496,7 +1680,7 @@ try{
|
|
|
1496
1680
|
}
|
|
1497
1681
|
}
|
|
1498
1682
|
})
|
|
1499
|
-
|
|
1683
|
+
|
|
1500
1684
|
function getTextContent(node){
|
|
1501
1685
|
switch(node.nodeType){
|
|
1502
1686
|
case ELEMENT_NODE:
|
|
@@ -1523,12 +1707,11 @@ try{
|
|
|
1523
1707
|
}catch(e){//ie8
|
|
1524
1708
|
}
|
|
1525
1709
|
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
//}
|
|
1710
|
+
exports.Document = Document;
|
|
1711
|
+
exports.DocumentType = DocumentType;
|
|
1712
|
+
exports.DOMException = DOMException;
|
|
1713
|
+
exports.DOMImplementation = DOMImplementation;
|
|
1714
|
+
exports.Element = Element;
|
|
1715
|
+
exports.Node = Node;
|
|
1716
|
+
exports.NodeList = NodeList;
|
|
1717
|
+
exports.XMLSerializer = XMLSerializer;
|
package/lib/entities.js
CHANGED