node-html-parser 5.3.0 → 5.3.3

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/README.md CHANGED
@@ -20,17 +20,17 @@ npm install --save node-html-parser
20
20
  ## Performance
21
21
 
22
22
  ```shell
23
- cheerio :12.0726 ms/file ± 7.31605
24
- parse5 :8.18615 ms/file ± 6.15337
25
- node-html-parser (last release):2.16533 ms/file ± 1.56924
26
- htmlparser :17.0658 ms/file ± 120.901
27
- htmlparser2 :2.62695 ms/file ± 4.17579
28
- node-html-parser:2.14907 ms/file ± 1.66632
29
- html-parser :24.6505 ms/file ± 18.9996
30
- htmljs-parser :5.81797 ms/file ± 6.55537
31
- html-dom-parser :2.52265 ms/file ± 3.54858
32
- html5parser :2.01144 ms/file ± 2.53570
33
- high5 :3.91342 ms/file ± 2.65563
23
+ html-parser :24.2329 ms/file ± 18.8092
24
+ htmljs-parser :4.78952 ms/file ± 5.50403
25
+ html-dom-parser :2.19594 ms/file ± 3.07470
26
+ html5parser :1.72007 ms/file ± 2.22713
27
+ cheerio :12.2220 ms/file ± 8.14063
28
+ parse5 :6.77691 ms/file ± 4.12002
29
+ htmlparser2 :2.33526 ms/file ± 3.43847
30
+ htmlparser :17.6260 ms/file ± 122.314
31
+ high5 :3.85676 ms/file ± 2.48878
32
+ node-html-parser:2.04585 ms/file ± 1.23787
33
+ node-html-parser (last release):2.00236 ms/file ± 1.22263
34
34
  ```
35
35
 
36
36
  Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-benchmark).
package/dist/main.js CHANGED
@@ -73,6 +73,20 @@ define("nodes/node", ["require", "exports", "he"], function (require, exports, h
73
73
  value: range !== null && range !== void 0 ? range : [-1, -1]
74
74
  });
75
75
  }
76
+ /**
77
+ * Remove current node
78
+ */
79
+ Node.prototype.remove = function () {
80
+ var _this = this;
81
+ if (this.parentNode) {
82
+ var children = this.parentNode.childNodes;
83
+ this.parentNode.childNodes = children.filter(function (child) {
84
+ return _this !== child;
85
+ });
86
+ this.parentNode = null;
87
+ }
88
+ return this;
89
+ };
76
90
  Object.defineProperty(Node.prototype, "innerText", {
77
91
  get: function () {
78
92
  return this.rawText;
@@ -485,18 +499,6 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
485
499
  }
486
500
  return JSON.stringify(attr.replace(/"/g, '"'));
487
501
  };
488
- /**
489
- * Remove current element
490
- */
491
- HTMLElement.prototype.remove = function () {
492
- var _this = this;
493
- if (this.parentNode) {
494
- var children = this.parentNode.childNodes;
495
- this.parentNode.childNodes = children.filter(function (child) {
496
- return _this !== child;
497
- });
498
- }
499
- };
500
502
  /**
501
503
  * Remove Child element from childNodes array
502
504
  * @param {HTMLElement} node node to remove
@@ -505,6 +507,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
505
507
  this.childNodes = this.childNodes.filter(function (child) {
506
508
  return child !== node;
507
509
  });
510
+ return this;
508
511
  };
509
512
  /**
510
513
  * Exchanges given child with new child
@@ -519,6 +522,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
519
522
  }
520
523
  return child;
521
524
  });
525
+ return this;
522
526
  };
523
527
  Object.defineProperty(HTMLElement.prototype, "tagName", {
524
528
  get: function () {
@@ -647,7 +651,10 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
647
651
  set: function (content) {
648
652
  //const r = parse(content, global.options); // TODO global.options ?
649
653
  var r = parse(content);
650
- this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
654
+ var nodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
655
+ resetParent(nodes, this);
656
+ resetParent(this.childNodes, null);
657
+ this.childNodes = nodes;
651
658
  },
652
659
  enumerable: false,
653
660
  configurable: true
@@ -661,7 +668,10 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
661
668
  var r = parse(content, options);
662
669
  content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
663
670
  }
671
+ resetParent(this.childNodes, null);
672
+ resetParent(content, this);
664
673
  this.childNodes = content;
674
+ return this;
665
675
  };
666
676
  HTMLElement.prototype.replaceWith = function () {
667
677
  var _this = this;
@@ -669,6 +679,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
669
679
  for (var _i = 0; _i < arguments.length; _i++) {
670
680
  nodes[_i] = arguments[_i];
671
681
  }
682
+ var parent = this.parentNode;
672
683
  var content = nodes
673
684
  .map(function (node) {
674
685
  if (node instanceof node_2.default) {
@@ -682,10 +693,11 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
682
693
  return [];
683
694
  })
684
695
  .flat();
685
- var idx = this.parentNode.childNodes.findIndex(function (child) {
696
+ var idx = parent.childNodes.findIndex(function (child) {
686
697
  return child === _this;
687
698
  });
688
- this.parentNode.childNodes = __spreadArray(__spreadArray(__spreadArray([], this.parentNode.childNodes.slice(0, idx), true), content, true), this.parentNode.childNodes.slice(idx + 1), true);
699
+ resetParent([this], null);
700
+ parent.childNodes = __spreadArray(__spreadArray(__spreadArray([], parent.childNodes.slice(0, idx), true), resetParent(content, parent), true), parent.childNodes.slice(idx + 1), true);
689
701
  };
690
702
  Object.defineProperty(HTMLElement.prototype, "outerHTML", {
691
703
  get: function () {
@@ -924,7 +936,8 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
924
936
  * @return {Node} node appended
925
937
  */
926
938
  HTMLElement.prototype.appendChild = function (node) {
927
- // node.parentNode = this;
939
+ // remove the node from it's parent
940
+ node.remove();
928
941
  this.childNodes.push(node);
929
942
  node.parentNode = this;
930
943
  return node;
@@ -1033,6 +1046,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1033
1046
  if (key === 'id') {
1034
1047
  this.id = '';
1035
1048
  }
1049
+ return this;
1036
1050
  };
1037
1051
  HTMLElement.prototype.hasAttribute = function (key) {
1038
1052
  return key.toLowerCase() in this.attrs;
@@ -1104,6 +1118,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1104
1118
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
1105
1119
  })
1106
1120
  .join(' ');
1121
+ return this;
1107
1122
  };
1108
1123
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
1109
1124
  var _a, _b, _c;
@@ -1116,14 +1131,11 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1116
1131
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1117
1132
  return child === _this;
1118
1133
  });
1134
+ resetParent(p.childNodes, this.parentNode);
1119
1135
  (_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes, false));
1120
- p.childNodes.forEach(function (n) {
1121
- if (n instanceof HTMLElement) {
1122
- n.parentNode = _this.parentNode;
1123
- }
1124
- });
1125
1136
  }
1126
1137
  else if (where === 'afterbegin') {
1138
+ resetParent(p.childNodes, this);
1127
1139
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
1128
1140
  }
1129
1141
  else if (where === 'beforeend') {
@@ -1135,16 +1147,13 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1135
1147
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1136
1148
  return child === _this;
1137
1149
  });
1150
+ resetParent(p.childNodes, this.parentNode);
1138
1151
  (_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes, false));
1139
- p.childNodes.forEach(function (n) {
1140
- if (n instanceof HTMLElement) {
1141
- n.parentNode = _this.parentNode;
1142
- }
1143
- });
1144
1152
  }
1145
1153
  else {
1146
1154
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
1147
1155
  }
1156
+ return this;
1148
1157
  // if (!where || html === undefined || html === null) {
1149
1158
  // return;
1150
1159
  // }
@@ -1519,6 +1528,12 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1519
1528
  return root;
1520
1529
  }
1521
1530
  exports.parse = parse;
1531
+ function resetParent(nodes, parent) {
1532
+ return nodes.map(function (node) {
1533
+ node.parentNode = parent;
1534
+ return node;
1535
+ });
1536
+ }
1522
1537
  });
1523
1538
  define("nodes/comment", ["require", "exports", "nodes/node", "nodes/type"], function (require, exports, node_3, type_4) {
1524
1539
  "use strict";
@@ -60,21 +60,17 @@ export default class HTMLElement extends Node {
60
60
  * @memberof HTMLElement
61
61
  */
62
62
  constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs: string, parentNode: HTMLElement | null, range?: [number, number]);
63
- /**
64
- * Remove current element
65
- */
66
- remove(): void;
67
63
  /**
68
64
  * Remove Child element from childNodes array
69
65
  * @param {HTMLElement} node node to remove
70
66
  */
71
- removeChild(node: Node): void;
67
+ removeChild(node: Node): this;
72
68
  /**
73
69
  * Exchanges given child with new child
74
70
  * @param {HTMLElement} oldNode node to exchange
75
71
  * @param {HTMLElement} newNode new node
76
72
  */
77
- exchangeChild(oldNode: Node, newNode: Node): void;
73
+ exchangeChild(oldNode: Node, newNode: Node): this;
78
74
  get tagName(): string;
79
75
  set tagName(newname: string);
80
76
  get localName(): string;
@@ -99,7 +95,7 @@ export default class HTMLElement extends Node {
99
95
  toString(): string;
100
96
  get innerHTML(): string;
101
97
  set innerHTML(content: string);
102
- set_content(content: string | Node | Node[], options?: Options): void;
98
+ set_content(content: string | Node | Node[], options?: Partial<Options>): this;
103
99
  replaceWith(...nodes: (string | Node)[]): void;
104
100
  get outerHTML(): string;
105
101
  /**
@@ -173,7 +169,7 @@ export default class HTMLElement extends Node {
173
169
  * @return {Object} parsed attributes
174
170
  */
175
171
  get rawAttributes(): RawAttributes;
176
- removeAttribute(key: string): void;
172
+ removeAttribute(key: string): this;
177
173
  hasAttribute(key: string): boolean;
178
174
  /**
179
175
  * Get an attribute
@@ -190,8 +186,8 @@ export default class HTMLElement extends Node {
190
186
  * Replace all the attributes of the HTMLElement by the provided attributes
191
187
  * @param {Attributes} attributes the new attribute set
192
188
  */
193
- setAttributes(attributes: Attributes): void;
194
- insertAdjacentHTML(where: InsertPosition, html: string): void;
189
+ setAttributes(attributes: Attributes): this;
190
+ insertAdjacentHTML(where: InsertPosition, html: string): this;
195
191
  get nextSibling(): Node;
196
192
  get nextElementSibling(): HTMLElement;
197
193
  get previousSibling(): Node;
@@ -197,18 +197,6 @@ var HTMLElement = /** @class */ (function (_super) {
197
197
  }
198
198
  return JSON.stringify(attr.replace(/"/g, '&quot;'));
199
199
  };
200
- /**
201
- * Remove current element
202
- */
203
- HTMLElement.prototype.remove = function () {
204
- var _this = this;
205
- if (this.parentNode) {
206
- var children = this.parentNode.childNodes;
207
- this.parentNode.childNodes = children.filter(function (child) {
208
- return _this !== child;
209
- });
210
- }
211
- };
212
200
  /**
213
201
  * Remove Child element from childNodes array
214
202
  * @param {HTMLElement} node node to remove
@@ -217,6 +205,7 @@ var HTMLElement = /** @class */ (function (_super) {
217
205
  this.childNodes = this.childNodes.filter(function (child) {
218
206
  return child !== node;
219
207
  });
208
+ return this;
220
209
  };
221
210
  /**
222
211
  * Exchanges given child with new child
@@ -231,6 +220,7 @@ var HTMLElement = /** @class */ (function (_super) {
231
220
  }
232
221
  return child;
233
222
  });
223
+ return this;
234
224
  };
235
225
  Object.defineProperty(HTMLElement.prototype, "tagName", {
236
226
  get: function () {
@@ -359,7 +349,10 @@ var HTMLElement = /** @class */ (function (_super) {
359
349
  set: function (content) {
360
350
  //const r = parse(content, global.options); // TODO global.options ?
361
351
  var r = parse(content);
362
- this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
352
+ var nodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
353
+ resetParent(nodes, this);
354
+ resetParent(this.childNodes, null);
355
+ this.childNodes = nodes;
363
356
  },
364
357
  enumerable: false,
365
358
  configurable: true
@@ -373,7 +366,10 @@ var HTMLElement = /** @class */ (function (_super) {
373
366
  var r = parse(content, options);
374
367
  content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
375
368
  }
369
+ resetParent(this.childNodes, null);
370
+ resetParent(content, this);
376
371
  this.childNodes = content;
372
+ return this;
377
373
  };
378
374
  HTMLElement.prototype.replaceWith = function () {
379
375
  var _this = this;
@@ -381,6 +377,7 @@ var HTMLElement = /** @class */ (function (_super) {
381
377
  for (var _i = 0; _i < arguments.length; _i++) {
382
378
  nodes[_i] = arguments[_i];
383
379
  }
380
+ var parent = this.parentNode;
384
381
  var content = nodes
385
382
  .map(function (node) {
386
383
  if (node instanceof node_1.default) {
@@ -394,10 +391,11 @@ var HTMLElement = /** @class */ (function (_super) {
394
391
  return [];
395
392
  })
396
393
  .flat();
397
- var idx = this.parentNode.childNodes.findIndex(function (child) {
394
+ var idx = parent.childNodes.findIndex(function (child) {
398
395
  return child === _this;
399
396
  });
400
- this.parentNode.childNodes = __spreadArray(__spreadArray(__spreadArray([], this.parentNode.childNodes.slice(0, idx), true), content, true), this.parentNode.childNodes.slice(idx + 1), true);
397
+ resetParent([this], null);
398
+ parent.childNodes = __spreadArray(__spreadArray(__spreadArray([], parent.childNodes.slice(0, idx), true), resetParent(content, parent), true), parent.childNodes.slice(idx + 1), true);
401
399
  };
402
400
  Object.defineProperty(HTMLElement.prototype, "outerHTML", {
403
401
  get: function () {
@@ -636,7 +634,8 @@ var HTMLElement = /** @class */ (function (_super) {
636
634
  * @return {Node} node appended
637
635
  */
638
636
  HTMLElement.prototype.appendChild = function (node) {
639
- // node.parentNode = this;
637
+ // remove the node from it's parent
638
+ node.remove();
640
639
  this.childNodes.push(node);
641
640
  node.parentNode = this;
642
641
  return node;
@@ -745,6 +744,7 @@ var HTMLElement = /** @class */ (function (_super) {
745
744
  if (key === 'id') {
746
745
  this.id = '';
747
746
  }
747
+ return this;
748
748
  };
749
749
  HTMLElement.prototype.hasAttribute = function (key) {
750
750
  return key.toLowerCase() in this.attrs;
@@ -816,6 +816,7 @@ var HTMLElement = /** @class */ (function (_super) {
816
816
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
817
817
  })
818
818
  .join(' ');
819
+ return this;
819
820
  };
820
821
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
821
822
  var _a, _b, _c;
@@ -828,14 +829,11 @@ var HTMLElement = /** @class */ (function (_super) {
828
829
  var idx = this.parentNode.childNodes.findIndex(function (child) {
829
830
  return child === _this;
830
831
  });
832
+ resetParent(p.childNodes, this.parentNode);
831
833
  (_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes, false));
832
- p.childNodes.forEach(function (n) {
833
- if (n instanceof HTMLElement) {
834
- n.parentNode = _this.parentNode;
835
- }
836
- });
837
834
  }
838
835
  else if (where === 'afterbegin') {
836
+ resetParent(p.childNodes, this);
839
837
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
840
838
  }
841
839
  else if (where === 'beforeend') {
@@ -847,16 +845,13 @@ var HTMLElement = /** @class */ (function (_super) {
847
845
  var idx = this.parentNode.childNodes.findIndex(function (child) {
848
846
  return child === _this;
849
847
  });
848
+ resetParent(p.childNodes, this.parentNode);
850
849
  (_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes, false));
851
- p.childNodes.forEach(function (n) {
852
- if (n instanceof HTMLElement) {
853
- n.parentNode = _this.parentNode;
854
- }
855
- });
856
850
  }
857
851
  else {
858
852
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
859
853
  }
854
+ return this;
860
855
  // if (!where || html === undefined || html === null) {
861
856
  // return;
862
857
  // }
@@ -1231,3 +1226,9 @@ function parse(data, options) {
1231
1226
  return root;
1232
1227
  }
1233
1228
  exports.parse = parse;
1229
+ function resetParent(nodes, parent) {
1230
+ return nodes.map(function (node) {
1231
+ node.parentNode = parent;
1232
+ return node;
1233
+ });
1234
+ }
@@ -13,6 +13,10 @@ export default abstract class Node {
13
13
  abstract toString(): string;
14
14
  abstract clone(): Node;
15
15
  constructor(parentNode?: HTMLElement, range?: [number, number]);
16
+ /**
17
+ * Remove current node
18
+ */
19
+ remove(): this;
16
20
  get innerText(): string;
17
21
  get textContent(): string;
18
22
  set textContent(val: string);
@@ -16,6 +16,20 @@ var Node = /** @class */ (function () {
16
16
  value: range !== null && range !== void 0 ? range : [-1, -1]
17
17
  });
18
18
  }
19
+ /**
20
+ * Remove current node
21
+ */
22
+ Node.prototype.remove = function () {
23
+ var _this = this;
24
+ if (this.parentNode) {
25
+ var children = this.parentNode.childNodes;
26
+ this.parentNode.childNodes = children.filter(function (child) {
27
+ return _this !== child;
28
+ });
29
+ this.parentNode = null;
30
+ }
31
+ return this;
32
+ };
19
33
  Object.defineProperty(Node.prototype, "innerText", {
20
34
  get: function () {
21
35
  return this.rawText;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-html-parser",
3
- "version": "5.3.0",
3
+ "version": "5.3.3",
4
4
  "description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -31,8 +31,8 @@
31
31
  "prepare": "cd test && yarn install"
32
32
  },
33
33
  "keywords": [
34
- "parser",
35
34
  "html",
35
+ "parser",
36
36
  "nodejs",
37
37
  "typescript"
38
38
  ],