@xmldom/xmldom 0.7.1 → 0.7.5

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 CHANGED
@@ -4,6 +4,59 @@ 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.7.5
8
+
9
+ [Commits](https://github.com/xmldom/xmldom/compare/0.7.4...0.7.5)
10
+
11
+ ### Fixes:
12
+
13
+ - Preserve default namespace when serializing [`#319`](https://github.com/xmldom/xmldom/issues/319) / [`#321`](https://github.com/xmldom/xmldom/pull/321)
14
+ Thank you [@lupestro](https://github.com/lupestro)
15
+
16
+ ## 0.7.4
17
+
18
+ [Commits](https://github.com/xmldom/xmldom/compare/0.7.3...0.7.4)
19
+
20
+ ### Fixes:
21
+
22
+ - Restore ability to parse `__prototype__` attributes [`#315`](https://github.com/xmldom/xmldom/pull/315)
23
+ Thank you [@dsimsonOMF](https://github.com/dsimsonOMF)
24
+
25
+ ## 0.7.3
26
+
27
+ [Commits](https://github.com/xmldom/xmldom/compare/0.7.2...0.7.3)
28
+
29
+ ### Fixes:
30
+
31
+ - Add doctype when parsing from string [`#277`](https://github.com/xmldom/xmldom/issues/277) / [`#301`](https://github.com/xmldom/xmldom/pull/301)
32
+ - Correct typo in error message [`#294`](https://github.com/xmldom/xmldom/pull/294)
33
+ Thank you [@rrthomas](https://github.com/rrthomas)
34
+
35
+ ### Refactor:
36
+
37
+ - Improve exports & require statements, new main package entry [`#233`](https://github.com/xmldom/xmldom/pull/233)
38
+
39
+ ### Docs:
40
+
41
+ - Fix Stryker badge [`#298`](https://github.com/xmldom/xmldom/pull/298)
42
+ - Fix link to help-wanted issues [`#299`](https://github.com/xmldom/xmldom/pull/299)
43
+
44
+ ### Chore:
45
+
46
+ - Execute stryker:dry-run on branches [`#302`](https://github.com/xmldom/xmldom/pull/302)
47
+ - Fix stryker config [`#300`](https://github.com/xmldom/xmldom/pull/300)
48
+ - Split test and lint scripts [`#297`](https://github.com/xmldom/xmldom/pull/297)
49
+ - Switch to stryker dashboard owned by org [`#292`](https://github.com/xmldom/xmldom/pull/292)
50
+
51
+ ## 0.7.2
52
+
53
+ [Commits](https://github.com/xmldom/xmldom/compare/0.7.1...0.7.2)
54
+
55
+ ### Fixes:
56
+
57
+ - Types: Add index.d.ts to packaged files [`#288`](https://github.com/xmldom/xmldom/pull/288)
58
+ Thank you [@forty](https://github.com/forty)
59
+
7
60
  ## 0.7.1
8
61
 
9
62
  [Commits](https://github.com/xmldom/xmldom/compare/0.7.0...0.7.1)
package/index.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /// <reference lib="dom" />
2
+
3
+ declare module "@xmldom/xmldom" {
4
+ var DOMParser: DOMParserStatic;
5
+ var XMLSerializer: XMLSerializerStatic;
6
+ var DOMImplementation: DOMImplementationStatic;
7
+
8
+ interface DOMImplementationStatic {
9
+ new(): DOMImplementation;
10
+ }
11
+
12
+ interface DOMParserStatic {
13
+ new (): DOMParser;
14
+ new (options: Options): DOMParser;
15
+ }
16
+
17
+ interface XMLSerializerStatic {
18
+ new (): XMLSerializer;
19
+ }
20
+
21
+ interface DOMParser {
22
+ parseFromString(xmlsource: string, mimeType?: string): Document;
23
+ }
24
+
25
+ interface XMLSerializer {
26
+ serializeToString(node: Node): string;
27
+ }
28
+
29
+ interface Options {
30
+ locator?: any;
31
+ errorHandler?: ErrorHandlerFunction | ErrorHandlerObject | undefined;
32
+ }
33
+
34
+ interface ErrorHandlerFunction {
35
+ (level: string, msg: any): any;
36
+ }
37
+
38
+ interface ErrorHandlerObject {
39
+ warning?: ((msg: any) => any) | undefined;
40
+ error?: ((msg: any) => any) | undefined;
41
+ fatalError?: ((msg: any) => any) | undefined;
42
+ }
43
+ }
package/lib/dom-parser.js CHANGED
@@ -1,8 +1,15 @@
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
+
6
13
  function DOMParser(options){
7
14
  this.options = options ||{locator:{}};
8
15
  }
@@ -170,6 +177,7 @@ DOMHandler.prototype = {
170
177
  var dt = impl.createDocumentType(name, publicId, systemId);
171
178
  this.locator && position(this.locator,dt)
172
179
  appendElement(this, dt);
180
+ this.doc.doctype = dt;
173
181
  }
174
182
  },
175
183
  /**
@@ -246,12 +254,15 @@ function appendElement (hander,node) {
246
254
  }
247
255
  }//appendChild and setAttributeNS are preformance key
248
256
 
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
257
  exports.__DOMHandler = DOMHandler;
257
- //}
258
+ exports.DOMParser = DOMParser;
259
+
260
+ /**
261
+ * @deprecated Import/require from main entry point instead
262
+ */
263
+ exports.DOMImplementation = dom.DOMImplementation;
264
+
265
+ /**
266
+ * @deprecated Import/require from main entry point instead
267
+ */
268
+ exports.XMLSerializer = dom.XMLSerializer;
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("unknow Class:"+Class)
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){
@@ -689,6 +703,12 @@ Document.prototype = {
689
703
  //implementation : null,
690
704
  nodeName : '#document',
691
705
  nodeType : DOCUMENT_NODE,
706
+ /**
707
+ * The DocumentType node of the document.
708
+ *
709
+ * @readonly
710
+ * @type DocumentType
711
+ */
692
712
  doctype : null,
693
713
  documentElement : null,
694
714
  _inc : 1,
@@ -1169,12 +1189,23 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
1169
1189
  var prefixedNodeName = nodeName
1170
1190
  if (!isHTML && !node.prefix && node.namespaceURI) {
1171
1191
  var defaultNS
1192
+ // lookup current default ns from `xmlns` attribute
1172
1193
  for (var ai = 0; ai < attrs.length; ai++) {
1173
1194
  if (attrs.item(ai).name === 'xmlns') {
1174
1195
  defaultNS = attrs.item(ai).value
1175
1196
  break
1176
1197
  }
1177
1198
  }
1199
+ if (!defaultNS) {
1200
+ // lookup current default ns in visibleNamespaces
1201
+ for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
1202
+ var namespace = visibleNamespaces[nsi]
1203
+ if (namespace.prefix === '' && namespace.namespace === node.namespaceURI) {
1204
+ defaultNS = namespace.namespace
1205
+ break
1206
+ }
1207
+ }
1208
+ }
1178
1209
  if (defaultNS !== node.namespaceURI) {
1179
1210
  for (var nsi = visibleNamespaces.length - 1; nsi >= 0; nsi--) {
1180
1211
  var namespace = visibleNamespaces[nsi]
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ var dom = require('./dom')
2
+ exports.DOMImplementation = dom.DOMImplementation
3
+ exports.XMLSerializer = dom.XMLSerializer
4
+ exports.DOMParser = require('./dom-parser').DOMParser
package/lib/sax.js CHANGED
@@ -230,7 +230,9 @@ function parseElementStartPart(source,start,el,currentNSMap,entityReplacer,error
230
230
  * @param {number} startIndex
231
231
  */
232
232
  function addAttribute(qname, value, startIndex) {
233
- if (qname in el.attributeNames) errorHandler.fatalError('Attribute ' + qname + ' redefined')
233
+ if (el.attributeNames.hasOwnProperty(qname)) {
234
+ errorHandler.fatalError('Attribute ' + qname + ' redefined')
235
+ }
234
236
  el.addValue(qname, value, startIndex)
235
237
  }
236
238
  var attrName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmldom/xmldom",
3
- "version": "0.7.1",
3
+ "version": "0.7.5",
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,22 +17,21 @@
17
17
  "type": "git",
18
18
  "url": "git://github.com/xmldom/xmldom.git"
19
19
  },
20
- "main": "lib/dom-parser.js",
20
+ "main": "lib/index.js",
21
21
  "types": "index.d.ts",
22
22
  "files": [
23
23
  "CHANGELOG.md",
24
24
  "LICENSE",
25
25
  "readme.md",
26
+ "index.d.ts",
26
27
  "lib"
27
28
  ],
28
29
  "scripts": {
29
- "lint": "npm-run-all lint:eslint",
30
- "lint:eslint": "eslint lib test",
31
- "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'",
32
32
  "stryker": "stryker run",
33
- "test": "npm-run-all test:unit lint",
34
- "test:unit": "npm-run-all test:jest",
35
- "test:jest": "jest"
33
+ "stryker:dry-run": "stryker run -m '' --reporters progress",
34
+ "test": "jest"
36
35
  },
37
36
  "engines": {
38
37
  "node": ">=10.0.0"
@@ -43,11 +42,10 @@
43
42
  "eslint": "^7.32.0",
44
43
  "eslint-config-prettier": "^8.3.0",
45
44
  "eslint-plugin-es5": "^1.5.0",
46
- "eslint-plugin-prettier": "^3.4.0",
45
+ "eslint-plugin-prettier": "^3.4.1",
47
46
  "get-stream": "^6.0.1",
48
47
  "jest": "^27.0.6",
49
48
  "nodemon": "^2.0.12",
50
- "npm-run-all": "^4.1.5",
51
49
  "prettier": "^2.3.2",
52
50
  "xmltest": "^1.5.0",
53
51
  "yauzl": "^2.10.0"
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](https://img.shields.io/npm/l/@xmldom/xmldom?color=blue&style=flat-square)](LICENSE)
4
7
  [![npm](https://img.shields.io/npm/v/@xmldom/xmldom?style=flat-square)](https://www.npmjs.com/package/@xmldom/xmldom)
5
8
  [![bug issues](https://img.shields.io/github/issues/xmldom/xmldom/bug?color=red&style=flat-square)](https://github.com/xmldom/xmldom/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
6
- [!["help wanted" issues](https://img.shields.io/github/issues/xmldom/xmldom/help%20wanted?color=darkgreen&style=flat-square)](https://github.com/xmldom/xmldom/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)
7
- [![Mutation report](https://camo.githubusercontent.com/ee312c4ebce7784ce9f785757eba5d6e33e6d950/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d25324662726f647962697473253246786d6c646f6d2532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/brodybits/xmldom/master)
8
-
9
- **The currently active maintainers decided to publish this code as `@xmldom/xmldom` because [the npm library `xmldom` contains security issues but can currently not be published by us](https://github.com/xmldom/xmldom/issues/271).**
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
+ [![help-wanted issues](https://img.shields.io/github/issues/xmldom/xmldom/help-wanted?color=darkgreen&style=flat-square)](https://github.com/xmldom/xmldom/issues?q=is%3Aissue+is%3Aopen+label%3Ahelp-wanted)
10
+ [![Mutation report](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fxmldom%2Fxmldom%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/xmldom/xmldom/master)
21
11
 
22
- The implementation is based on several specifications:
23
12
 
24
- <!-- Should open in new tab and the links in the SVG should be clickable there! -->
25
- <a href="https://raw.githubusercontent.com/xmldom/xmldom/master/docs/specs.svg" target="_blank" rel="noopener noreferrer nofollow" >![Overview of related specifications and their relations](docs/specs.svg)</a>
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
- ### DOM Parsing and Serialization
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
- 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:
30
- - `DOMParser`
31
- - `XMLSerializer`
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
- ## Forked
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 BOM standard!!)
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" >![Overview of related specifications and their relations](docs/specs.svg)</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>