@xmldom/xmldom 0.7.2 → 0.8.0
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 +65 -0
- package/lib/dom-parser.js +77 -12
- package/lib/dom.js +101 -37
- package/lib/index.js +4 -0
- package/lib/sax.js +16 -8
- package/package.json +17 -20
- package/readme.md +82 -73
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,71 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [0.8.0](https://github.com/xmldom/xmldom/compare/0.7.5...0.8.0)
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- Normalize all line endings according to XML specs [1.0](https://w3.org/TR/xml/#sec-line-ends) and [1.1](https://www.w3.org/TR/xml11/#sec-line-ends) \
|
|
11
|
+
BREAKING CHANGE: Certain combination of line break characters are normalized to a single `\n` before parsing takes place and will no longer be preserved.
|
|
12
|
+
- [`#303`](https://github.com/xmldom/xmldom/issues/303) / [`#307`](https://github.com/xmldom/xmldom/pull/307)
|
|
13
|
+
- [`#49`](https://github.com/xmldom/xmldom/issues/49), [`#97`](https://github.com/xmldom/xmldom/issues/97), [`#324`](https://github.com/xmldom/xmldom/issues/324) / [`#314`](https://github.com/xmldom/xmldom/pull/314)
|
|
14
|
+
- XMLSerializer: Preserve whitespace character references [`#284`](https://github.com/xmldom/xmldom/issues/284) / [`#310`](https://github.com/xmldom/xmldom/pull/310) \
|
|
15
|
+
BREAKING CHANGE: If you relied on the not spec compliant preservation of literal `\t`, `\n` or `\r` in **attribute values**.
|
|
16
|
+
To preserve those you will have to create XML that instead contains the correct numerical (or hexadecimal) equivalent (e.g. `	`, `
`, `
`).
|
|
17
|
+
- Drop deprecated exports `DOMImplementation` and `XMLSerializer` from `lib/dom-parser.js` [#53](https://github.com/xmldom/xmldom/issues/53) / [`#309`](https://github.com/xmldom/xmldom/pull/309)
|
|
18
|
+
BREAKING CHANGE: Use the one provided by the main package export.
|
|
19
|
+
- dom: Remove all links as part of `removeChild` [`#343`](https://github.com/xmldom/xmldom/issues/343) / [`#355`](https://github.com/xmldom/xmldom/pull/355)
|
|
20
|
+
|
|
21
|
+
### Chore
|
|
22
|
+
- ci: Restore latest tested node version to 16.x [`#325`](https://github.com/xmldom/xmldom/pull/325)
|
|
23
|
+
- ci: Split test and lint steps into jobs [`#111`](https://github.com/xmldom/xmldom/issues/111) / [`#304`](https://github.com/xmldom/xmldom/pull/304)
|
|
24
|
+
- Pinned and updated devDependencies
|
|
25
|
+
|
|
26
|
+
Thank you [@marrus-sh](https://github.com/marrus-sh), [@victorandree](https://github.com/victorandree), [@mdierolf](https://github.com/mdierolf), [@tsabbay](https://github.com/tsabbay), [@fatihpense](https://github.com/fatihpense) for your contributions
|
|
27
|
+
|
|
28
|
+
## 0.7.5
|
|
29
|
+
|
|
30
|
+
[Commits](https://github.com/xmldom/xmldom/compare/0.7.4...0.7.5)
|
|
31
|
+
|
|
32
|
+
### Fixes:
|
|
33
|
+
|
|
34
|
+
- Preserve default namespace when serializing [`#319`](https://github.com/xmldom/xmldom/issues/319) / [`#321`](https://github.com/xmldom/xmldom/pull/321)
|
|
35
|
+
Thank you [@lupestro](https://github.com/lupestro)
|
|
36
|
+
|
|
37
|
+
## 0.7.4
|
|
38
|
+
|
|
39
|
+
[Commits](https://github.com/xmldom/xmldom/compare/0.7.3...0.7.4)
|
|
40
|
+
|
|
41
|
+
### Fixes:
|
|
42
|
+
|
|
43
|
+
- Restore ability to parse `__prototype__` attributes [`#315`](https://github.com/xmldom/xmldom/pull/315)
|
|
44
|
+
Thank you [@dsimsonOMF](https://github.com/dsimsonOMF)
|
|
45
|
+
|
|
46
|
+
## 0.7.3
|
|
47
|
+
|
|
48
|
+
[Commits](https://github.com/xmldom/xmldom/compare/0.7.2...0.7.3)
|
|
49
|
+
|
|
50
|
+
### Fixes:
|
|
51
|
+
|
|
52
|
+
- Add doctype when parsing from string [`#277`](https://github.com/xmldom/xmldom/issues/277) / [`#301`](https://github.com/xmldom/xmldom/pull/301)
|
|
53
|
+
- Correct typo in error message [`#294`](https://github.com/xmldom/xmldom/pull/294)
|
|
54
|
+
Thank you [@rrthomas](https://github.com/rrthomas)
|
|
55
|
+
|
|
56
|
+
### Refactor:
|
|
57
|
+
|
|
58
|
+
- Improve exports & require statements, new main package entry [`#233`](https://github.com/xmldom/xmldom/pull/233)
|
|
59
|
+
|
|
60
|
+
### Docs:
|
|
61
|
+
|
|
62
|
+
- Fix Stryker badge [`#298`](https://github.com/xmldom/xmldom/pull/298)
|
|
63
|
+
- Fix link to help-wanted issues [`#299`](https://github.com/xmldom/xmldom/pull/299)
|
|
64
|
+
|
|
65
|
+
### Chore:
|
|
66
|
+
|
|
67
|
+
- Execute stryker:dry-run on branches [`#302`](https://github.com/xmldom/xmldom/pull/302)
|
|
68
|
+
- Fix stryker config [`#300`](https://github.com/xmldom/xmldom/pull/300)
|
|
69
|
+
- Split test and lint scripts [`#297`](https://github.com/xmldom/xmldom/pull/297)
|
|
70
|
+
- Switch to stryker dashboard owned by org [`#292`](https://github.com/xmldom/xmldom/pull/292)
|
|
71
|
+
|
|
7
72
|
## 0.7.2
|
|
8
73
|
|
|
9
74
|
[Commits](https://github.com/xmldom/xmldom/compare/0.7.1...0.7.2)
|
package/lib/dom-parser.js
CHANGED
|
@@ -1,8 +1,73 @@
|
|
|
1
1
|
var conventions = require("./conventions");
|
|
2
|
+
var dom = require('./dom')
|
|
2
3
|
var entities = require('./entities');
|
|
4
|
+
var sax = require('./sax');
|
|
5
|
+
|
|
6
|
+
var DOMImplementation = dom.DOMImplementation;
|
|
3
7
|
|
|
4
8
|
var NAMESPACE = conventions.NAMESPACE;
|
|
5
9
|
|
|
10
|
+
var ParseError = sax.ParseError;
|
|
11
|
+
var XMLReader = sax.XMLReader;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Normalizes line ending according to https://www.w3.org/TR/xml11/#sec-line-ends:
|
|
15
|
+
*
|
|
16
|
+
* > XML parsed entities are often stored in computer files which,
|
|
17
|
+
* > for editing convenience, are organized into lines.
|
|
18
|
+
* > These lines are typically separated by some combination
|
|
19
|
+
* > of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).
|
|
20
|
+
* >
|
|
21
|
+
* > To simplify the tasks of applications, the XML processor must behave
|
|
22
|
+
* > as if it normalized all line breaks in external parsed entities (including the document entity)
|
|
23
|
+
* > on input, before parsing, by translating all of the following to a single #xA character:
|
|
24
|
+
* >
|
|
25
|
+
* > 1. the two-character sequence #xD #xA
|
|
26
|
+
* > 2. the two-character sequence #xD #x85
|
|
27
|
+
* > 3. the single character #x85
|
|
28
|
+
* > 4. the single character #x2028
|
|
29
|
+
* > 5. any #xD character that is not immediately followed by #xA or #x85.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} input
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
function normalizeLineEndings(input) {
|
|
35
|
+
return input
|
|
36
|
+
.replace(/\r[\n\u0085]/g, '\n')
|
|
37
|
+
.replace(/[\r\u0085\u2028]/g, '\n')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @typedef Locator
|
|
42
|
+
* @property {number} [columnNumber]
|
|
43
|
+
* @property {number} [lineNumber]
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef DOMParserOptions
|
|
48
|
+
* @property {DOMHandler} [domBuilder]
|
|
49
|
+
* @property {Function} [errorHandler]
|
|
50
|
+
* @property {(string) => string} [normalizeLineEndings] used to replace line endings before parsing
|
|
51
|
+
* defaults to `normalizeLineEndings`
|
|
52
|
+
* @property {Locator} [locator]
|
|
53
|
+
* @property {Record<string, string>} [xmlns]
|
|
54
|
+
*
|
|
55
|
+
* @see normalizeLineEndings
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The DOMParser interface provides the ability to parse XML or HTML source code
|
|
60
|
+
* from a string into a DOM `Document`.
|
|
61
|
+
*
|
|
62
|
+
* _xmldom is different from the spec in that it allows an `options` parameter,
|
|
63
|
+
* to override the default behavior._
|
|
64
|
+
*
|
|
65
|
+
* @param {DOMParserOptions} [options]
|
|
66
|
+
* @constructor
|
|
67
|
+
*
|
|
68
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
|
|
69
|
+
* @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-parsing-and-serialization
|
|
70
|
+
*/
|
|
6
71
|
function DOMParser(options){
|
|
7
72
|
this.options = options ||{locator:{}};
|
|
8
73
|
}
|
|
@@ -26,10 +91,15 @@ DOMParser.prototype.parseFromString = function(source,mimeType){
|
|
|
26
91
|
defaultNSMap[''] = NAMESPACE.HTML;
|
|
27
92
|
}
|
|
28
93
|
defaultNSMap.xml = defaultNSMap.xml || NAMESPACE.XML;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
94
|
+
var normalize = options.normalizeLineEndings || normalizeLineEndings;
|
|
95
|
+
if (source && typeof source === 'string') {
|
|
96
|
+
sax.parse(
|
|
97
|
+
normalize(source),
|
|
98
|
+
defaultNSMap,
|
|
99
|
+
entityMap
|
|
100
|
+
)
|
|
101
|
+
} else {
|
|
102
|
+
sax.errorHandler.error('invalid doc source')
|
|
33
103
|
}
|
|
34
104
|
return domBuilder.doc;
|
|
35
105
|
}
|
|
@@ -170,6 +240,7 @@ DOMHandler.prototype = {
|
|
|
170
240
|
var dt = impl.createDocumentType(name, publicId, systemId);
|
|
171
241
|
this.locator && position(this.locator,dt)
|
|
172
242
|
appendElement(this, dt);
|
|
243
|
+
this.doc.doctype = dt;
|
|
173
244
|
}
|
|
174
245
|
},
|
|
175
246
|
/**
|
|
@@ -246,12 +317,6 @@ function appendElement (hander,node) {
|
|
|
246
317
|
}
|
|
247
318
|
}//appendChild and setAttributeNS are preformance key
|
|
248
319
|
|
|
249
|
-
//if(typeof require == 'function'){
|
|
250
|
-
var sax = require('./sax');
|
|
251
|
-
var XMLReader = sax.XMLReader;
|
|
252
|
-
var ParseError = sax.ParseError;
|
|
253
|
-
var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
|
|
254
|
-
exports.XMLSerializer = require('./dom').XMLSerializer ;
|
|
255
|
-
exports.DOMParser = DOMParser;
|
|
256
320
|
exports.__DOMHandler = DOMHandler;
|
|
257
|
-
|
|
321
|
+
exports.normalizeLineEndings = normalizeLineEndings;
|
|
322
|
+
exports.DOMParser = DOMParser;
|
package/lib/dom.js
CHANGED
|
@@ -81,7 +81,7 @@ function _extends(Class,Super){
|
|
|
81
81
|
}
|
|
82
82
|
if(pt.constructor != Class){
|
|
83
83
|
if(typeof Class != 'function'){
|
|
84
|
-
console.error("
|
|
84
|
+
console.error("unknown Class:"+Class)
|
|
85
85
|
}
|
|
86
86
|
pt.constructor = Class
|
|
87
87
|
}
|
|
@@ -488,6 +488,20 @@ Node.prototype = {
|
|
|
488
488
|
hasAttributes:function(){
|
|
489
489
|
return this.attributes.length>0;
|
|
490
490
|
},
|
|
491
|
+
/**
|
|
492
|
+
* Look up the prefix associated to the given namespace URI, starting from this node.
|
|
493
|
+
* **The default namespace declarations are ignored by this method.**
|
|
494
|
+
* See Namespace Prefix Lookup for details on the algorithm used by this method.
|
|
495
|
+
*
|
|
496
|
+
* _Note: The implementation seems to be incomplete when compared to the algorithm described in the specs._
|
|
497
|
+
*
|
|
498
|
+
* @param {string | null} namespaceURI
|
|
499
|
+
* @returns {string | null}
|
|
500
|
+
* @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-lookupNamespacePrefix
|
|
501
|
+
* @see https://www.w3.org/TR/DOM-Level-3-Core/namespaces-algorithms.html#lookupNamespacePrefixAlgo
|
|
502
|
+
* @see https://dom.spec.whatwg.org/#dom-node-lookupprefix
|
|
503
|
+
* @see https://github.com/xmldom/xmldom/issues/322
|
|
504
|
+
*/
|
|
491
505
|
lookupPrefix:function(namespaceURI){
|
|
492
506
|
var el = this;
|
|
493
507
|
while(el){
|
|
@@ -577,48 +591,67 @@ function _onRemoveAttribute(doc,el,newAttr,remove){
|
|
|
577
591
|
}
|
|
578
592
|
}
|
|
579
593
|
|
|
580
|
-
|
|
594
|
+
/**
|
|
595
|
+
* Updates `el.childNodes`, updating the indexed items and it's `length`.
|
|
596
|
+
* Passing `newChild` means it will be appended.
|
|
597
|
+
* Otherwise it's assumed that an item has been removed,
|
|
598
|
+
* and `el.firstNode` and it's `.nextSibling` are used
|
|
599
|
+
* to walk the current list of child nodes.
|
|
600
|
+
*
|
|
601
|
+
* @param {Document} doc
|
|
602
|
+
* @param {Node} el
|
|
603
|
+
* @param {Node} [newChild]
|
|
604
|
+
* @private
|
|
605
|
+
*/
|
|
606
|
+
function _onUpdateChild (doc, el, newChild) {
|
|
581
607
|
if(doc && doc._inc){
|
|
582
608
|
doc._inc++;
|
|
583
609
|
//update childNodes
|
|
584
610
|
var cs = el.childNodes;
|
|
585
|
-
if(newChild){
|
|
611
|
+
if (newChild) {
|
|
586
612
|
cs[cs.length++] = newChild;
|
|
587
|
-
}else{
|
|
588
|
-
//console.log(1)
|
|
613
|
+
} else {
|
|
589
614
|
var child = el.firstChild;
|
|
590
615
|
var i = 0;
|
|
591
|
-
while(child){
|
|
616
|
+
while (child) {
|
|
592
617
|
cs[i++] = child;
|
|
593
|
-
child =child.nextSibling;
|
|
618
|
+
child = child.nextSibling;
|
|
594
619
|
}
|
|
595
620
|
cs.length = i;
|
|
621
|
+
delete cs[cs.length];
|
|
596
622
|
}
|
|
597
623
|
}
|
|
598
624
|
}
|
|
599
625
|
|
|
600
626
|
/**
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
*
|
|
627
|
+
* Removes the connections between `parentNode` and `child`
|
|
628
|
+
* and any existing `child.previousSibling` or `child.nextSibling`.
|
|
629
|
+
*
|
|
630
|
+
* @see https://github.com/xmldom/xmldom/issues/135
|
|
631
|
+
* @see https://github.com/xmldom/xmldom/issues/145
|
|
632
|
+
*
|
|
633
|
+
* @param {Node} parentNode
|
|
634
|
+
* @param {Node} child
|
|
635
|
+
* @returns {Node} the child that was removed.
|
|
636
|
+
* @private
|
|
607
637
|
*/
|
|
608
|
-
function _removeChild(parentNode,child){
|
|
638
|
+
function _removeChild (parentNode, child) {
|
|
609
639
|
var previous = child.previousSibling;
|
|
610
640
|
var next = child.nextSibling;
|
|
611
|
-
if(previous){
|
|
641
|
+
if (previous) {
|
|
612
642
|
previous.nextSibling = next;
|
|
613
|
-
}else{
|
|
614
|
-
parentNode.firstChild = next
|
|
643
|
+
} else {
|
|
644
|
+
parentNode.firstChild = next;
|
|
615
645
|
}
|
|
616
|
-
if(next){
|
|
646
|
+
if (next) {
|
|
617
647
|
next.previousSibling = previous;
|
|
618
|
-
}else{
|
|
648
|
+
} else {
|
|
619
649
|
parentNode.lastChild = previous;
|
|
620
650
|
}
|
|
621
|
-
|
|
651
|
+
child.parentNode = null;
|
|
652
|
+
child.previousSibling = null;
|
|
653
|
+
child.nextSibling = null;
|
|
654
|
+
_onUpdateChild(parentNode.ownerDocument, parentNode);
|
|
622
655
|
return child;
|
|
623
656
|
}
|
|
624
657
|
/**
|
|
@@ -664,31 +697,45 @@ function _insertBefore(parentNode,newChild,nextChild){
|
|
|
664
697
|
}
|
|
665
698
|
return newChild;
|
|
666
699
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Appends `newChild` to `parentNode`.
|
|
703
|
+
* If `newChild` is already connected to a `parentNode` it is first removed from it.
|
|
704
|
+
*
|
|
705
|
+
* @see https://github.com/xmldom/xmldom/issues/135
|
|
706
|
+
* @see https://github.com/xmldom/xmldom/issues/145
|
|
707
|
+
* @param {Node} parentNode
|
|
708
|
+
* @param {Node} newChild
|
|
709
|
+
* @returns {Node}
|
|
710
|
+
* @private
|
|
711
|
+
*/
|
|
712
|
+
function _appendSingleChild (parentNode, newChild) {
|
|
713
|
+
if (newChild.parentNode) {
|
|
714
|
+
newChild.parentNode.removeChild(newChild);
|
|
673
715
|
}
|
|
674
|
-
var pre = parentNode.lastChild;
|
|
675
716
|
newChild.parentNode = parentNode;
|
|
676
|
-
newChild.previousSibling =
|
|
717
|
+
newChild.previousSibling = parentNode.lastChild;
|
|
677
718
|
newChild.nextSibling = null;
|
|
678
|
-
if(
|
|
679
|
-
|
|
680
|
-
}else{
|
|
719
|
+
if (newChild.previousSibling) {
|
|
720
|
+
newChild.previousSibling.nextSibling = newChild;
|
|
721
|
+
} else {
|
|
681
722
|
parentNode.firstChild = newChild;
|
|
682
723
|
}
|
|
683
724
|
parentNode.lastChild = newChild;
|
|
684
|
-
_onUpdateChild(parentNode.ownerDocument,parentNode,newChild);
|
|
725
|
+
_onUpdateChild(parentNode.ownerDocument, parentNode, newChild);
|
|
685
726
|
return newChild;
|
|
686
|
-
//console.log("__aa",parentNode.lastChild.nextSibling == null)
|
|
687
727
|
}
|
|
728
|
+
|
|
688
729
|
Document.prototype = {
|
|
689
730
|
//implementation : null,
|
|
690
731
|
nodeName : '#document',
|
|
691
732
|
nodeType : DOCUMENT_NODE,
|
|
733
|
+
/**
|
|
734
|
+
* The DocumentType node of the document.
|
|
735
|
+
*
|
|
736
|
+
* @readonly
|
|
737
|
+
* @type DocumentType
|
|
738
|
+
*/
|
|
692
739
|
doctype : null,
|
|
693
740
|
documentElement : null,
|
|
694
741
|
_inc : 1,
|
|
@@ -1131,12 +1178,18 @@ function needNamespaceDefine(node, isHTML, visibleNamespaces) {
|
|
|
1131
1178
|
}
|
|
1132
1179
|
/**
|
|
1133
1180
|
* Well-formed constraint: No < in Attribute Values
|
|
1134
|
-
* The replacement text of any entity referred to directly or indirectly
|
|
1135
|
-
*
|
|
1136
|
-
* @see https://www.w3.org/TR/
|
|
1181
|
+
* > The replacement text of any entity referred to directly or indirectly
|
|
1182
|
+
* > in an attribute value must not contain a <.
|
|
1183
|
+
* @see https://www.w3.org/TR/xml11/#CleanAttrVals
|
|
1184
|
+
* @see https://www.w3.org/TR/xml11/#NT-AttValue
|
|
1185
|
+
*
|
|
1186
|
+
* Literal whitespace other than space that appear in attribute values
|
|
1187
|
+
* are serialized as their entity references, so they will be preserved.
|
|
1188
|
+
* (In contrast to whitespace literals in the input which are normalized to spaces)
|
|
1189
|
+
* @see https://www.w3.org/TR/xml11/#AVNormalize
|
|
1137
1190
|
*/
|
|
1138
1191
|
function addSerializedAttribute(buf, qualifiedName, value) {
|
|
1139
|
-
buf.push(' ', qualifiedName, '="', value.replace(/[<&"]/g,_xmlEncoder), '"')
|
|
1192
|
+
buf.push(' ', qualifiedName, '="', value.replace(/[<&"\t\n\r]/g, _xmlEncoder), '"')
|
|
1140
1193
|
}
|
|
1141
1194
|
|
|
1142
1195
|
function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
|
@@ -1169,12 +1222,23 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
|
|
|
1169
1222
|
var prefixedNodeName = nodeName
|
|
1170
1223
|
if (!isHTML && !node.prefix && node.namespaceURI) {
|
|
1171
1224
|
var defaultNS
|
|
1225
|
+
// lookup current default ns from `xmlns` attribute
|
|
1172
1226
|
for (var ai = 0; ai < attrs.length; ai++) {
|
|
1173
1227
|
if (attrs.item(ai).name === 'xmlns') {
|
|
1174
1228
|
defaultNS = attrs.item(ai).value
|
|
1175
1229
|
break
|
|
1176
1230
|
}
|
|
1177
1231
|
}
|
|
1232
|
+
if (!defaultNS) {
|
|
1233
|
+
// lookup current default ns in visibleNamespaces
|
|
1234
|
+
for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
|
|
1235
|
+
var namespace = visibleNamespaces[nsi]
|
|
1236
|
+
if (namespace.prefix === '' && namespace.namespace === node.namespaceURI) {
|
|
1237
|
+
defaultNS = namespace.namespace
|
|
1238
|
+
break
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1178
1242
|
if (defaultNS !== node.namespaceURI) {
|
|
1179
1243
|
for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
|
|
1180
1244
|
var namespace = visibleNamespaces[nsi]
|
package/lib/index.js
ADDED
package/lib/sax.js
CHANGED
|
@@ -230,8 +230,18 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
|
|
|
230
230
|
* @param {number} startIndex
|
|
231
231
|
*/
|
|
232
232
|
function addAttribute(qname, value, startIndex) {
|
|
233
|
-
if (
|
|
234
|
-
|
|
233
|
+
if (el.attributeNames.hasOwnProperty(qname)) {
|
|
234
|
+
errorHandler.fatalError('Attribute ' + qname + ' redefined')
|
|
235
|
+
}
|
|
236
|
+
el.addValue(
|
|
237
|
+
qname,
|
|
238
|
+
// @see https://www.w3.org/TR/xml/#AVNormalize
|
|
239
|
+
// since the xmldom sax parser does not "interpret" DTD the following is not implemented:
|
|
240
|
+
// - recursive replacement of (DTD) entity references
|
|
241
|
+
// - trimming and collapsing multiple spaces into a single one for attributes that are not of type CDATA
|
|
242
|
+
value.replace(/[\t\n\r]/g, ' ').replace(/&#?\w+;/g, entityReplacer),
|
|
243
|
+
startIndex
|
|
244
|
+
)
|
|
235
245
|
}
|
|
236
246
|
var attrName;
|
|
237
247
|
var value;
|
|
@@ -262,7 +272,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
|
|
|
262
272
|
start = p+1;
|
|
263
273
|
p = source.indexOf(c,start)
|
|
264
274
|
if(p>0){
|
|
265
|
-
value = source.slice(start,p)
|
|
275
|
+
value = source.slice(start, p);
|
|
266
276
|
addAttribute(attrName, value, start-1);
|
|
267
277
|
s = S_ATTR_END;
|
|
268
278
|
}else{
|
|
@@ -270,10 +280,8 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
|
|
|
270
280
|
throw new Error('attribute value no end \''+c+'\' match');
|
|
271
281
|
}
|
|
272
282
|
}else if(s == S_ATTR_NOQUOT_VALUE){
|
|
273
|
-
value = source.slice(start,p)
|
|
274
|
-
//console.log(attrName,value,start,p)
|
|
283
|
+
value = source.slice(start, p);
|
|
275
284
|
addAttribute(attrName, value, start);
|
|
276
|
-
//console.dir(el)
|
|
277
285
|
errorHandler.warning('attribute "'+attrName+'" missed start quot('+c+')!!');
|
|
278
286
|
start = p+1;
|
|
279
287
|
s = S_ATTR_END
|
|
@@ -327,7 +335,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
|
|
|
327
335
|
}
|
|
328
336
|
if(s == S_ATTR_NOQUOT_VALUE){
|
|
329
337
|
errorHandler.warning('attribute "'+value+'" missed quot(")!');
|
|
330
|
-
addAttribute(attrName, value
|
|
338
|
+
addAttribute(attrName, value, start)
|
|
331
339
|
}else{
|
|
332
340
|
if(!NAMESPACE.isHTML(currentNSMap['']) || !value.match(/^(?:disabled|checked|selected)$/i)){
|
|
333
341
|
errorHandler.warning('attribute "'+value+'" missed value!! "'+value+'" instead!!')
|
|
@@ -355,7 +363,7 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
|
|
|
355
363
|
s = S_ATTR_SPACE;
|
|
356
364
|
break;
|
|
357
365
|
case S_ATTR_NOQUOT_VALUE:
|
|
358
|
-
var value = source.slice(start,p)
|
|
366
|
+
var value = source.slice(start, p);
|
|
359
367
|
errorHandler.warning('attribute "'+value+'" missed quot(")!!');
|
|
360
368
|
addAttribute(attrName, value, start)
|
|
361
369
|
case S_ATTR_END:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmldom/xmldom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"w3c",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "git://github.com/xmldom/xmldom.git"
|
|
19
19
|
},
|
|
20
|
-
"main": "lib/
|
|
20
|
+
"main": "lib/index.js",
|
|
21
21
|
"types": "index.d.ts",
|
|
22
22
|
"files": [
|
|
23
23
|
"CHANGELOG.md",
|
|
@@ -27,31 +27,28 @@
|
|
|
27
27
|
"lib"
|
|
28
28
|
],
|
|
29
29
|
"scripts": {
|
|
30
|
-
"lint": "
|
|
31
|
-
"
|
|
32
|
-
"start": "nodemon --watch package.json --watch lib --watch test --exec 'npm --silent run test'",
|
|
30
|
+
"lint": "eslint lib test",
|
|
31
|
+
"start": "nodemon --watch package.json --watch lib --watch test --exec 'npm --silent run test && npm --silent run lint'",
|
|
33
32
|
"stryker": "stryker run",
|
|
34
|
-
"
|
|
35
|
-
"test
|
|
36
|
-
"test:jest": "jest"
|
|
33
|
+
"stryker:dry-run": "stryker run -m '' --reporters progress",
|
|
34
|
+
"test": "jest"
|
|
37
35
|
},
|
|
38
36
|
"engines": {
|
|
39
37
|
"node": ">=10.0.0"
|
|
40
38
|
},
|
|
41
39
|
"dependencies": {},
|
|
42
40
|
"devDependencies": {
|
|
43
|
-
"@stryker-mutator/core": "
|
|
44
|
-
"eslint": "
|
|
45
|
-
"eslint-config-prettier": "
|
|
46
|
-
"eslint-plugin-es5": "
|
|
47
|
-
"eslint-plugin-prettier": "
|
|
48
|
-
"get-stream": "
|
|
49
|
-
"jest": "
|
|
50
|
-
"nodemon": "
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"yauzl": "^2.10.0"
|
|
41
|
+
"@stryker-mutator/core": "5.5.1",
|
|
42
|
+
"eslint": "8.5.0",
|
|
43
|
+
"eslint-config-prettier": "8.3.0",
|
|
44
|
+
"eslint-plugin-es5": "1.5.0",
|
|
45
|
+
"eslint-plugin-prettier": "4.0.0",
|
|
46
|
+
"get-stream": "6.0.1",
|
|
47
|
+
"jest": "27.4.5",
|
|
48
|
+
"nodemon": "2.0.15",
|
|
49
|
+
"prettier": "2.5.1",
|
|
50
|
+
"xmltest": "1.5.0",
|
|
51
|
+
"yauzl": "2.10.0"
|
|
55
52
|
},
|
|
56
53
|
"bugs": {
|
|
57
54
|
"url": "https://github.com/xmldom/xmldom/issues"
|
package/readme.md
CHANGED
|
@@ -1,85 +1,36 @@
|
|
|
1
1
|
# @xmldom/xmldom
|
|
2
2
|
|
|
3
|
+
***Since version 0.7.0 this package is published to npm as [`@xmldom/xmldom`](https://www.npmjs.com/package/@xmldom/xmldom) and no longer as [`xmldom`](https://www.npmjs.com/package/xmldom), because [we are no longer able to publish `xmldom`](https://github.com/xmldom/xmldom/issues/271).***
|
|
4
|
+
*For better readability in the docs we will continue to talk about this library as "xmldom".*
|
|
5
|
+
|
|
3
6
|
[](LICENSE)
|
|
4
7
|
[](https://www.npmjs.com/package/@xmldom/xmldom)
|
|
5
8
|
[](https://github.com/xmldom/xmldom/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
|
|
6
|
-
[.**
|
|
10
|
-
|
|
11
|
-
*For better readability in the docs we will continue to talk about this library as "xmldom".*
|
|
12
|
-
|
|
13
|
-
xmldom is a javascript [ponyfill](https://ponyfill.com/) for the following APIs supported in browsers:
|
|
14
|
-
- convert an XML string into a DOM tree (`new DOMParser().parseFromString(xml, mimeType)` => `Document`)
|
|
15
|
-
- create, access and modify a DOM tree (`new DOMImplementation().createDocument(...)` => `Document`, )
|
|
16
|
-
- serialize a DOM tree back into an XML string (`new XMLSerializer().serializeToString(node)` => `string`)
|
|
17
|
-
|
|
18
|
-
Note that this `xmldom` library is not required if your code targets a modern browser. But this library is recommended if your code needs to also work in other runtimes like NodeJS or Rhino.
|
|
19
|
-
|
|
20
|
-
## Specs
|
|
9
|
+
[](https://github.com/xmldom/xmldom/issues?q=is%3Aissue+is%3Aopen+label%3Ahelp-wanted)
|
|
10
|
+
[](https://dashboard.stryker-mutator.io/reports/github.com/xmldom/xmldom/master)
|
|
21
11
|
|
|
22
|
-
The implementation is based on several specifications:
|
|
23
12
|
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
xmldom is a javascript [ponyfill](https://ponyfill.com/) to provide the following APIs [that are present in modern browsers](https://caniuse.com/xml-serializer) to other runtimes:
|
|
14
|
+
- convert an XML string into a DOM tree
|
|
15
|
+
```
|
|
16
|
+
new DOMParser().parseFromString(xml, mimeType) => Document
|
|
17
|
+
```
|
|
18
|
+
- create, access and modify a DOM tree
|
|
19
|
+
```
|
|
20
|
+
new DOMImplementation().createDocument(...) => Document
|
|
21
|
+
```
|
|
22
|
+
- serialize a DOM tree back into an XML string
|
|
23
|
+
```
|
|
24
|
+
new XMLSerializer().serializeToString(node) => string
|
|
25
|
+
```
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
The target runtimes `xmldom` supports are currently Node >= v10 (ES5) and Rhino ([not tested as part of CI](https://github.com/xmldom/xmldom/discussions/214)).
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
Note that there are some known deviations between this implementation and the W3 specifications.
|
|
34
|
-
|
|
35
|
-
Note: [The latest version of this spec](https://w3c.github.io/DOM-Parsing/) has the status "Editors Draft", since it is under active development. One major change is that [the definition of the `DOMParser` interface has been moved to the HTML spec](https://w3c.github.io/DOM-Parsing/#the-domparser-interface)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
### DOM
|
|
39
|
-
|
|
40
|
-
The original author claims that xmldom implements [DOM Level 2] in a "fully compatible" way and some parts of [DOM Level 3], but there are not enough tests to prove this. Both Specifications are now superseded by the [DOM Level 4 aka Living standard] wich has a much broader scope than xmldom.
|
|
41
|
-
|
|
42
|
-
xmldom implements the following interfaces (most constructors are currently not exposed):
|
|
43
|
-
- `Attr`
|
|
44
|
-
- `CDATASection`
|
|
45
|
-
- `CharacterData`
|
|
46
|
-
- `Comment`
|
|
47
|
-
- `Document`
|
|
48
|
-
- `DocumentFragment`
|
|
49
|
-
- `DocumentType`
|
|
50
|
-
- `DOMException` (constructor exposed)
|
|
51
|
-
- `DOMImplementation` (constructor exposed)
|
|
52
|
-
- `Element`
|
|
53
|
-
- `Entity`
|
|
54
|
-
- `EntityReference`
|
|
55
|
-
- `LiveNodeList`
|
|
56
|
-
- `NamedNodeMap`
|
|
57
|
-
- `Node` (constructor exposed)
|
|
58
|
-
- `NodeList`
|
|
59
|
-
- `Notation`
|
|
60
|
-
- `ProcessingInstruction`
|
|
61
|
-
- `Text`
|
|
62
|
-
|
|
63
|
-
more details are available in the (incomplete) [API Reference](#api-reference) section.
|
|
64
|
-
|
|
65
|
-
### HTML
|
|
66
|
-
|
|
67
|
-
xmldom does not have any goal of supporting the full spec, but it has some capability to parse, report and serialize things differently when "detecting HTML" (by checking the default namespace).
|
|
68
|
-
There is an upcoming change to better align the implementation with the latest specs, related to <https://github.com/xmldom/xmldom/issues/203>.
|
|
69
|
-
|
|
70
|
-
### SAX, XML, XMLNS
|
|
71
|
-
|
|
72
|
-
xmldom has an own SAX parser implementation to do the actual parsing, which implements some interfaces in alignment with the Java interfaces SAX defines:
|
|
73
|
-
- `XMLReader`
|
|
74
|
-
- `DOMHandler`
|
|
75
|
-
|
|
76
|
-
There is an idea/proposal to make ti possible to replace it with something else in <https://github.com/xmldom/xmldom/issues/55>
|
|
29
|
+
When deciding how to fix bugs or implement features, `xmldom` tries to stay as close as possible to the various [related specifications/standards](#specs).
|
|
30
|
+
As indicated by the version starting with `0.`, this implementation is not feature complete and some implemented features differ from what the specifications describe.
|
|
31
|
+
**Issues and PRs for such differences are always welcome, even when they only provide a failing test case.**
|
|
77
32
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
**Original project location:** <https://github.com/jindw/xmldom>
|
|
81
|
-
|
|
82
|
-
More details about the transition can be found in the [CHANGELOG](CHANGELOG.md#maintainer-changes) and in <https://github.com/xmldom/xmldom/issues/62>
|
|
33
|
+
This project was forked from it's [original source](https://github.com/jindw/xmldom) in 2019, more details about that transition can be found in the [CHANGELOG](CHANGELOG.md#maintainer-changes).
|
|
83
34
|
|
|
84
35
|
## Usage
|
|
85
36
|
|
|
@@ -121,7 +72,7 @@ import { DOMParser } from '@xmldom/xmldom'
|
|
|
121
72
|
```javascript
|
|
122
73
|
parseFromString(xmlsource,mimeType)
|
|
123
74
|
```
|
|
124
|
-
* **options extension** _by xmldom_(not
|
|
75
|
+
* **options extension** _by xmldom_ (not DOM standard!!)
|
|
125
76
|
|
|
126
77
|
```javascript
|
|
127
78
|
//added the options argument
|
|
@@ -327,3 +278,61 @@ import { DOMParser } from '@xmldom/xmldom'
|
|
|
327
278
|
lineNumber
|
|
328
279
|
//Numbered starting from '1'
|
|
329
280
|
columnNumber
|
|
281
|
+
|
|
282
|
+
## Specs
|
|
283
|
+
|
|
284
|
+
The implementation is based on several specifications:
|
|
285
|
+
|
|
286
|
+
<!-- Should open in new tab and the links in the SVG should be clickable there! -->
|
|
287
|
+
<a href="https://raw.githubusercontent.com/xmldom/xmldom/master/docs/specs.svg" target="_blank" rel="noopener noreferrer nofollow" ></a>
|
|
288
|
+
|
|
289
|
+
### DOM Parsing and Serialization
|
|
290
|
+
|
|
291
|
+
From the [W3C DOM Parsing and Serialization (WD 2016)](https://www.w3.org/TR/2016/WD-DOM-Parsing-20160517/) `xmldom` provides an implementation for the interfaces:
|
|
292
|
+
- `DOMParser`
|
|
293
|
+
- `XMLSerializer`
|
|
294
|
+
|
|
295
|
+
Note that there are some known deviations between this implementation and the W3 specifications.
|
|
296
|
+
|
|
297
|
+
Note: [The latest version of this spec](https://w3c.github.io/DOM-Parsing/) has the status "Editors Draft", since it is under active development. One major change is that [the definition of the `DOMParser` interface has been moved to the HTML spec](https://w3c.github.io/DOM-Parsing/#the-domparser-interface)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
### DOM
|
|
301
|
+
|
|
302
|
+
The original author claims that xmldom implements [DOM Level 2] in a "fully compatible" way and some parts of [DOM Level 3], but there are not enough tests to prove this. Both Specifications are now superseded by the [DOM Level 4 aka Living standard] wich has a much broader scope than xmldom.
|
|
303
|
+
|
|
304
|
+
xmldom implements the following interfaces (most constructors are currently not exposed):
|
|
305
|
+
- `Attr`
|
|
306
|
+
- `CDATASection`
|
|
307
|
+
- `CharacterData`
|
|
308
|
+
- `Comment`
|
|
309
|
+
- `Document`
|
|
310
|
+
- `DocumentFragment`
|
|
311
|
+
- `DocumentType`
|
|
312
|
+
- `DOMException` (constructor exposed)
|
|
313
|
+
- `DOMImplementation` (constructor exposed)
|
|
314
|
+
- `Element`
|
|
315
|
+
- `Entity`
|
|
316
|
+
- `EntityReference`
|
|
317
|
+
- `LiveNodeList`
|
|
318
|
+
- `NamedNodeMap`
|
|
319
|
+
- `Node` (constructor exposed)
|
|
320
|
+
- `NodeList`
|
|
321
|
+
- `Notation`
|
|
322
|
+
- `ProcessingInstruction`
|
|
323
|
+
- `Text`
|
|
324
|
+
|
|
325
|
+
more details are available in the (incomplete) [API Reference](#api-reference) section.
|
|
326
|
+
|
|
327
|
+
### HTML
|
|
328
|
+
|
|
329
|
+
xmldom does not have any goal of supporting the full spec, but it has some capability to parse, report and serialize things differently when "detecting HTML" (by checking the default namespace).
|
|
330
|
+
There is an upcoming change to better align the implementation with the latest specs, related to <https://github.com/xmldom/xmldom/issues/203>.
|
|
331
|
+
|
|
332
|
+
### SAX, XML, XMLNS
|
|
333
|
+
|
|
334
|
+
xmldom has an own SAX parser implementation to do the actual parsing, which implements some interfaces in alignment with the Java interfaces SAX defines:
|
|
335
|
+
- `XMLReader`
|
|
336
|
+
- `DOMHandler`
|
|
337
|
+
|
|
338
|
+
There is an idea/proposal to make ti possible to replace it with something else in <https://github.com/xmldom/xmldom/issues/55>
|