@xmldom/xmldom 0.8.4 → 0.8.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,15 @@ 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.5](https://github.com/xmldom/xmldom/compare/0.8.4...0.8.5)
8
+
9
+ ### Fixed
10
+
11
+ - fix: Restore ES5 compatibility [`#452`](https://github.com/xmldom/xmldom/pull/452) / [`#453`](https://github.com/xmldom/xmldom/issues/453)
12
+
13
+ Thank you, [@fengxinming](https://github.com/fengxinming), for your contributions
14
+
15
+
7
16
  ## [0.8.4](https://github.com/xmldom/xmldom/compare/0.8.3...0.8.4)
8
17
 
9
18
  ### Fixed
@@ -1,5 +1,37 @@
1
1
  'use strict'
2
2
 
3
+ /**
4
+ * Ponyfill for `Array.prototype.find` which is only available in ES6 runtimes.
5
+ *
6
+ * Works with anything that has a `length` property and index access properties, including NodeList.
7
+ *
8
+ * @template {unknown} T
9
+ * @param {Array<T> | ({length:number, [number]: T})} list
10
+ * @param {function (item: T, index: number, list:Array<T> | ({length:number, [number]: T})):boolean} predicate
11
+ * @param {Partial<Pick<ArrayConstructor['prototype'], 'find'>>?} ac `Array.prototype` by default,
12
+ * allows injecting a custom implementation in tests
13
+ * @returns {T | undefined}
14
+ *
15
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
16
+ * @see https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.find
17
+ */
18
+ function find(list, predicate, ac) {
19
+ if (ac === undefined) {
20
+ ac = Array.prototype;
21
+ }
22
+ if (list && typeof ac.find === 'function') {
23
+ return ac.find.call(list, predicate);
24
+ }
25
+ for (var i = 0; i < list.length; i++) {
26
+ if (Object.prototype.hasOwnProperty.call(list, i)) {
27
+ var item = list[i];
28
+ if (predicate.call(undefined, item, i, list)) {
29
+ return item;
30
+ }
31
+ }
32
+ }
33
+ }
34
+
3
35
  /**
4
36
  * "Shallow freezes" an object to render it immutable.
5
37
  * Uses `Object.freeze` if available,
@@ -165,6 +197,7 @@ var NAMESPACE = freeze({
165
197
  })
166
198
 
167
199
  exports.assign = assign;
200
+ exports.find = find;
168
201
  exports.freeze = freeze;
169
202
  exports.MIME_TYPE = MIME_TYPE;
170
203
  exports.NAMESPACE = NAMESPACE;
package/lib/dom.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var conventions = require("./conventions");
2
2
 
3
+ var find = conventions.find;
3
4
  var NAMESPACE = conventions.NAMESPACE;
4
5
 
5
6
  /**
@@ -176,14 +177,6 @@ NodeList.prototype = {
176
177
  }
177
178
  return buf.join('');
178
179
  },
179
- /**
180
- * @private
181
- * @param {function (Node):boolean} predicate
182
- * @returns {Node | undefined}
183
- */
184
- find: function (predicate) {
185
- return Array.prototype.find.call(this, predicate);
186
- },
187
180
  /**
188
181
  * @private
189
182
  * @param {function (Node):boolean} predicate
@@ -748,10 +741,10 @@ function isTextNode(node) {
748
741
  */
749
742
  function isElementInsertionPossible(doc, child) {
750
743
  var parentChildNodes = doc.childNodes || [];
751
- if (parentChildNodes.find(isElementNode) || isDocTypeNode(child)) {
744
+ if (find(parentChildNodes, isElementNode) || isDocTypeNode(child)) {
752
745
  return false;
753
746
  }
754
- var docTypeNode = parentChildNodes.find(isDocTypeNode);
747
+ var docTypeNode = find(parentChildNodes, isDocTypeNode);
755
748
  return !(child && docTypeNode && parentChildNodes.indexOf(docTypeNode) > parentChildNodes.indexOf(child));
756
749
  }
757
750
  /**
@@ -786,8 +779,8 @@ function _insertBefore(parent, node, child) {
786
779
  var nodeChildNodes = node.childNodes || [];
787
780
  if (parent.nodeType === Node.DOCUMENT_NODE) {
788
781
  if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
789
- let nodeChildElements = nodeChildNodes.filter(isElementNode);
790
- if (nodeChildElements.length > 1 || nodeChildNodes.find(isTextNode)) {
782
+ var nodeChildElements = nodeChildNodes.filter(isElementNode);
783
+ if (nodeChildElements.length > 1 || find(nodeChildNodes, isTextNode)) {
791
784
  throw new DOMException(HIERARCHY_REQUEST_ERR, 'More than one element or text in fragment');
792
785
  }
793
786
  if (nodeChildElements.length === 1 && !isElementInsertionPossible(parent, child)) {
@@ -795,15 +788,15 @@ function _insertBefore(parent, node, child) {
795
788
  }
796
789
  }
797
790
  if (isElementNode(node)) {
798
- if (parentChildNodes.find(isElementNode) || !isElementInsertionPossible(parent, child)) {
791
+ if (find(parentChildNodes, isElementNode) || !isElementInsertionPossible(parent, child)) {
799
792
  throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one element can be added and only after doctype');
800
793
  }
801
794
  }
802
795
  if (isDocTypeNode(node)) {
803
- if (parentChildNodes.find(isDocTypeNode)) {
796
+ if (find(parentChildNodes, isDocTypeNode)) {
804
797
  throw new DOMException(HIERARCHY_REQUEST_ERR, 'Only one doctype is allowed');
805
798
  }
806
- let parentElementChild = parentChildNodes.find(isElementNode);
799
+ var parentElementChild = find(parentChildNodes, isElementNode);
807
800
  if (child && parentChildNodes.indexOf(parentElementChild) < parentChildNodes.indexOf(child)) {
808
801
  throw new DOMException(HIERARCHY_REQUEST_ERR, 'Doctype can only be inserted before an element');
809
802
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xmldom/xmldom",
3
- "version": "0.8.4",
3
+ "version": "0.8.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",
@@ -29,13 +29,15 @@
29
29
  ],
30
30
  "scripts": {
31
31
  "lint": "eslint lib test",
32
+ "format": "prettier --write test",
32
33
  "changelog": "auto-changelog --unreleased-only",
33
34
  "start": "nodemon --watch package.json --watch lib --watch test --exec 'npm --silent run test && npm --silent run lint'",
34
35
  "stryker": "stryker run",
35
36
  "stryker:dry-run": "stryker run -m '' --reporters progress",
36
37
  "test": "jest",
38
+ "testrelease": "npm test && npm run lint",
37
39
  "version": "./changelog-has-version.sh",
38
- "release": "np --no-yarn"
40
+ "release": "np --no-yarn --test-script testrelease"
39
41
  },
40
42
  "engines": {
41
43
  "node": ">=10.0.0"