node-html-parser 5.2.6 → 5.3.2

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
@@ -203,6 +203,10 @@ Get class names.
203
203
 
204
204
  Clone a node.
205
205
 
206
+ #### Node#getElementById(id: string): HTMLElement;
207
+
208
+ Get element by it's ID.
209
+
206
210
  ## HTMLElement Properties
207
211
 
208
212
  ### HTMLElement#text
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 () {
@@ -832,6 +844,43 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
832
844
  }
833
845
  return re;
834
846
  };
847
+ /**
848
+ * find element by it's id
849
+ * @param {string} id the id of the element to select
850
+ */
851
+ HTMLElement.prototype.getElementById = function (id) {
852
+ var stack = [];
853
+ var currentNodeReference = this;
854
+ var index = 0;
855
+ // index turns to undefined once the stack is empty and the first condition occurs
856
+ // which happens once all relevant children are searched through
857
+ while (index !== undefined) {
858
+ var child = void 0;
859
+ // make it work with sparse arrays
860
+ do {
861
+ child = currentNodeReference.childNodes[index++];
862
+ } while (index < currentNodeReference.childNodes.length && child === undefined);
863
+ // if the child does not exist we move on with the last provided index (which belongs to the parentNode)
864
+ if (child === undefined) {
865
+ currentNodeReference = currentNodeReference.parentNode;
866
+ index = stack.pop();
867
+ continue;
868
+ }
869
+ if (child.nodeType === type_3.default.ELEMENT_NODE) {
870
+ if (child.id === id) {
871
+ return child;
872
+ }
873
+ ;
874
+ // if children are existing push the current status to the stack and keep searching for elements in the level below
875
+ if (child.childNodes.length > 0) {
876
+ stack.push(index);
877
+ currentNodeReference = child;
878
+ index = 0;
879
+ }
880
+ }
881
+ }
882
+ return null;
883
+ };
835
884
  /**
836
885
  * traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
837
886
  * @param selector a DOMString containing a selector list
@@ -887,7 +936,8 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
887
936
  * @return {Node} node appended
888
937
  */
889
938
  HTMLElement.prototype.appendChild = function (node) {
890
- // node.parentNode = this;
939
+ // remove the node from it's parent
940
+ node.remove();
891
941
  this.childNodes.push(node);
892
942
  node.parentNode = this;
893
943
  return node;
@@ -996,6 +1046,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
996
1046
  if (key === 'id') {
997
1047
  this.id = '';
998
1048
  }
1049
+ return this;
999
1050
  };
1000
1051
  HTMLElement.prototype.hasAttribute = function (key) {
1001
1052
  return key.toLowerCase() in this.attrs;
@@ -1067,6 +1118,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1067
1118
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
1068
1119
  })
1069
1120
  .join(' ');
1121
+ return this;
1070
1122
  };
1071
1123
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
1072
1124
  var _a, _b, _c;
@@ -1079,14 +1131,11 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1079
1131
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1080
1132
  return child === _this;
1081
1133
  });
1134
+ resetParent(p.childNodes, this.parentNode);
1082
1135
  (_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes, false));
1083
- p.childNodes.forEach(function (n) {
1084
- if (n instanceof HTMLElement) {
1085
- n.parentNode = _this.parentNode;
1086
- }
1087
- });
1088
1136
  }
1089
1137
  else if (where === 'afterbegin') {
1138
+ resetParent(p.childNodes, this);
1090
1139
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
1091
1140
  }
1092
1141
  else if (where === 'beforeend') {
@@ -1098,16 +1147,13 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1098
1147
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1099
1148
  return child === _this;
1100
1149
  });
1150
+ resetParent(p.childNodes, this.parentNode);
1101
1151
  (_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes, false));
1102
- p.childNodes.forEach(function (n) {
1103
- if (n instanceof HTMLElement) {
1104
- n.parentNode = _this.parentNode;
1105
- }
1106
- });
1107
1152
  }
1108
1153
  else {
1109
1154
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
1110
1155
  }
1156
+ return this;
1111
1157
  // if (!where || html === undefined || html === null) {
1112
1158
  // return;
1113
1159
  // }
@@ -1482,6 +1528,12 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1482
1528
  return root;
1483
1529
  }
1484
1530
  exports.parse = parse;
1531
+ function resetParent(nodes, parent) {
1532
+ return nodes.map(function (node) {
1533
+ node.parentNode = parent;
1534
+ return node;
1535
+ });
1536
+ }
1485
1537
  });
1486
1538
  define("nodes/comment", ["require", "exports", "nodes/node", "nodes/type"], function (require, exports, node_3, type_4) {
1487
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?: Options): this;
103
99
  replaceWith(...nodes: (string | Node)[]): void;
104
100
  get outerHTML(): string;
105
101
  /**
@@ -135,6 +131,11 @@ export default class HTMLElement extends Node {
135
131
  * @param {string} tagName the tagName of the elements to select
136
132
  */
137
133
  getElementsByTagName(tagName: string): Array<HTMLElement>;
134
+ /**
135
+ * find element by it's id
136
+ * @param {string} id the id of the element to select
137
+ */
138
+ getElementById(id: string): HTMLElement;
138
139
  /**
139
140
  * traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
140
141
  * @param selector a DOMString containing a selector list
@@ -168,7 +169,7 @@ export default class HTMLElement extends Node {
168
169
  * @return {Object} parsed attributes
169
170
  */
170
171
  get rawAttributes(): RawAttributes;
171
- removeAttribute(key: string): void;
172
+ removeAttribute(key: string): this;
172
173
  hasAttribute(key: string): boolean;
173
174
  /**
174
175
  * Get an attribute
@@ -185,8 +186,8 @@ export default class HTMLElement extends Node {
185
186
  * Replace all the attributes of the HTMLElement by the provided attributes
186
187
  * @param {Attributes} attributes the new attribute set
187
188
  */
188
- setAttributes(attributes: Attributes): void;
189
- insertAdjacentHTML(where: InsertPosition, html: string): void;
189
+ setAttributes(attributes: Attributes): this;
190
+ insertAdjacentHTML(where: InsertPosition, html: string): this;
190
191
  get nextSibling(): Node;
191
192
  get nextElementSibling(): HTMLElement;
192
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 () {
@@ -544,6 +542,43 @@ var HTMLElement = /** @class */ (function (_super) {
544
542
  }
545
543
  return re;
546
544
  };
545
+ /**
546
+ * find element by it's id
547
+ * @param {string} id the id of the element to select
548
+ */
549
+ HTMLElement.prototype.getElementById = function (id) {
550
+ var stack = [];
551
+ var currentNodeReference = this;
552
+ var index = 0;
553
+ // index turns to undefined once the stack is empty and the first condition occurs
554
+ // which happens once all relevant children are searched through
555
+ while (index !== undefined) {
556
+ var child = void 0;
557
+ // make it work with sparse arrays
558
+ do {
559
+ child = currentNodeReference.childNodes[index++];
560
+ } while (index < currentNodeReference.childNodes.length && child === undefined);
561
+ // if the child does not exist we move on with the last provided index (which belongs to the parentNode)
562
+ if (child === undefined) {
563
+ currentNodeReference = currentNodeReference.parentNode;
564
+ index = stack.pop();
565
+ continue;
566
+ }
567
+ if (child.nodeType === type_1.default.ELEMENT_NODE) {
568
+ if (child.id === id) {
569
+ return child;
570
+ }
571
+ ;
572
+ // if children are existing push the current status to the stack and keep searching for elements in the level below
573
+ if (child.childNodes.length > 0) {
574
+ stack.push(index);
575
+ currentNodeReference = child;
576
+ index = 0;
577
+ }
578
+ }
579
+ }
580
+ return null;
581
+ };
547
582
  /**
548
583
  * traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
549
584
  * @param selector a DOMString containing a selector list
@@ -599,7 +634,8 @@ var HTMLElement = /** @class */ (function (_super) {
599
634
  * @return {Node} node appended
600
635
  */
601
636
  HTMLElement.prototype.appendChild = function (node) {
602
- // node.parentNode = this;
637
+ // remove the node from it's parent
638
+ node.remove();
603
639
  this.childNodes.push(node);
604
640
  node.parentNode = this;
605
641
  return node;
@@ -708,6 +744,7 @@ var HTMLElement = /** @class */ (function (_super) {
708
744
  if (key === 'id') {
709
745
  this.id = '';
710
746
  }
747
+ return this;
711
748
  };
712
749
  HTMLElement.prototype.hasAttribute = function (key) {
713
750
  return key.toLowerCase() in this.attrs;
@@ -779,6 +816,7 @@ var HTMLElement = /** @class */ (function (_super) {
779
816
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
780
817
  })
781
818
  .join(' ');
819
+ return this;
782
820
  };
783
821
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
784
822
  var _a, _b, _c;
@@ -791,14 +829,11 @@ var HTMLElement = /** @class */ (function (_super) {
791
829
  var idx = this.parentNode.childNodes.findIndex(function (child) {
792
830
  return child === _this;
793
831
  });
832
+ resetParent(p.childNodes, this.parentNode);
794
833
  (_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes, false));
795
- p.childNodes.forEach(function (n) {
796
- if (n instanceof HTMLElement) {
797
- n.parentNode = _this.parentNode;
798
- }
799
- });
800
834
  }
801
835
  else if (where === 'afterbegin') {
836
+ resetParent(p.childNodes, this);
802
837
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
803
838
  }
804
839
  else if (where === 'beforeend') {
@@ -810,16 +845,13 @@ var HTMLElement = /** @class */ (function (_super) {
810
845
  var idx = this.parentNode.childNodes.findIndex(function (child) {
811
846
  return child === _this;
812
847
  });
848
+ resetParent(p.childNodes, this.parentNode);
813
849
  (_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes, false));
814
- p.childNodes.forEach(function (n) {
815
- if (n instanceof HTMLElement) {
816
- n.parentNode = _this.parentNode;
817
- }
818
- });
819
850
  }
820
851
  else {
821
852
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
822
853
  }
854
+ return this;
823
855
  // if (!where || html === undefined || html === null) {
824
856
  // return;
825
857
  // }
@@ -1194,3 +1226,9 @@ function parse(data, options) {
1194
1226
  return root;
1195
1227
  }
1196
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.2.6",
3
+ "version": "5.3.2",
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",