node-html-parser 5.2.5 → 5.3.1

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
@@ -495,7 +495,9 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
495
495
  this.parentNode.childNodes = children.filter(function (child) {
496
496
  return _this !== child;
497
497
  });
498
+ this.parentNode = null;
498
499
  }
500
+ return this;
499
501
  };
500
502
  /**
501
503
  * Remove Child element from childNodes array
@@ -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
@@ -996,6 +1045,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
996
1045
  if (key === 'id') {
997
1046
  this.id = '';
998
1047
  }
1048
+ return this;
999
1049
  };
1000
1050
  HTMLElement.prototype.hasAttribute = function (key) {
1001
1051
  return key.toLowerCase() in this.attrs;
@@ -1067,6 +1117,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1067
1117
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
1068
1118
  })
1069
1119
  .join(' ');
1120
+ return this;
1070
1121
  };
1071
1122
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
1072
1123
  var _a, _b, _c;
@@ -1079,14 +1130,11 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1079
1130
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1080
1131
  return child === _this;
1081
1132
  });
1133
+ resetParent(p.childNodes, this.parentNode);
1082
1134
  (_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
1135
  }
1089
1136
  else if (where === 'afterbegin') {
1137
+ resetParent(p.childNodes, this);
1090
1138
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
1091
1139
  }
1092
1140
  else if (where === 'beforeend') {
@@ -1098,16 +1146,13 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1098
1146
  var idx = this.parentNode.childNodes.findIndex(function (child) {
1099
1147
  return child === _this;
1100
1148
  });
1149
+ resetParent(p.childNodes, this.parentNode);
1101
1150
  (_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
1151
  }
1108
1152
  else {
1109
1153
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
1110
1154
  }
1155
+ return this;
1111
1156
  // if (!where || html === undefined || html === null) {
1112
1157
  // return;
1113
1158
  // }
@@ -1482,6 +1527,12 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1482
1527
  return root;
1483
1528
  }
1484
1529
  exports.parse = parse;
1530
+ function resetParent(nodes, parent) {
1531
+ return nodes.map(function (node) {
1532
+ node.parentNode = parent;
1533
+ return node;
1534
+ });
1535
+ }
1485
1536
  });
1486
1537
  define("nodes/comment", ["require", "exports", "nodes/node", "nodes/type"], function (require, exports, node_3, type_4) {
1487
1538
  "use strict";
@@ -63,18 +63,18 @@ export default class HTMLElement extends Node {
63
63
  /**
64
64
  * Remove current element
65
65
  */
66
- remove(): void;
66
+ remove(): this;
67
67
  /**
68
68
  * Remove Child element from childNodes array
69
69
  * @param {HTMLElement} node node to remove
70
70
  */
71
- removeChild(node: Node): void;
71
+ removeChild(node: Node): this;
72
72
  /**
73
73
  * Exchanges given child with new child
74
74
  * @param {HTMLElement} oldNode node to exchange
75
75
  * @param {HTMLElement} newNode new node
76
76
  */
77
- exchangeChild(oldNode: Node, newNode: Node): void;
77
+ exchangeChild(oldNode: Node, newNode: Node): this;
78
78
  get tagName(): string;
79
79
  set tagName(newname: string);
80
80
  get localName(): string;
@@ -99,7 +99,7 @@ export default class HTMLElement extends Node {
99
99
  toString(): string;
100
100
  get innerHTML(): string;
101
101
  set innerHTML(content: string);
102
- set_content(content: string | Node | Node[], options?: Options): void;
102
+ set_content(content: string | Node | Node[], options?: Options): this;
103
103
  replaceWith(...nodes: (string | Node)[]): void;
104
104
  get outerHTML(): string;
105
105
  /**
@@ -135,6 +135,11 @@ export default class HTMLElement extends Node {
135
135
  * @param {string} tagName the tagName of the elements to select
136
136
  */
137
137
  getElementsByTagName(tagName: string): Array<HTMLElement>;
138
+ /**
139
+ * find element by it's id
140
+ * @param {string} id the id of the element to select
141
+ */
142
+ getElementById(id: string): HTMLElement;
138
143
  /**
139
144
  * 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
145
  * @param selector a DOMString containing a selector list
@@ -168,7 +173,7 @@ export default class HTMLElement extends Node {
168
173
  * @return {Object} parsed attributes
169
174
  */
170
175
  get rawAttributes(): RawAttributes;
171
- removeAttribute(key: string): void;
176
+ removeAttribute(key: string): this;
172
177
  hasAttribute(key: string): boolean;
173
178
  /**
174
179
  * Get an attribute
@@ -185,8 +190,8 @@ export default class HTMLElement extends Node {
185
190
  * Replace all the attributes of the HTMLElement by the provided attributes
186
191
  * @param {Attributes} attributes the new attribute set
187
192
  */
188
- setAttributes(attributes: Attributes): void;
189
- insertAdjacentHTML(where: InsertPosition, html: string): void;
193
+ setAttributes(attributes: Attributes): this;
194
+ insertAdjacentHTML(where: InsertPosition, html: string): this;
190
195
  get nextSibling(): Node;
191
196
  get nextElementSibling(): HTMLElement;
192
197
  get previousSibling(): Node;
@@ -207,7 +207,9 @@ var HTMLElement = /** @class */ (function (_super) {
207
207
  this.parentNode.childNodes = children.filter(function (child) {
208
208
  return _this !== child;
209
209
  });
210
+ this.parentNode = null;
210
211
  }
212
+ return this;
211
213
  };
212
214
  /**
213
215
  * Remove Child element from childNodes array
@@ -217,6 +219,7 @@ var HTMLElement = /** @class */ (function (_super) {
217
219
  this.childNodes = this.childNodes.filter(function (child) {
218
220
  return child !== node;
219
221
  });
222
+ return this;
220
223
  };
221
224
  /**
222
225
  * Exchanges given child with new child
@@ -231,6 +234,7 @@ var HTMLElement = /** @class */ (function (_super) {
231
234
  }
232
235
  return child;
233
236
  });
237
+ return this;
234
238
  };
235
239
  Object.defineProperty(HTMLElement.prototype, "tagName", {
236
240
  get: function () {
@@ -359,7 +363,10 @@ var HTMLElement = /** @class */ (function (_super) {
359
363
  set: function (content) {
360
364
  //const r = parse(content, global.options); // TODO global.options ?
361
365
  var r = parse(content);
362
- this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
366
+ var nodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
367
+ resetParent(nodes, this);
368
+ resetParent(this.childNodes, null);
369
+ this.childNodes = nodes;
363
370
  },
364
371
  enumerable: false,
365
372
  configurable: true
@@ -373,7 +380,10 @@ var HTMLElement = /** @class */ (function (_super) {
373
380
  var r = parse(content, options);
374
381
  content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
375
382
  }
383
+ resetParent(this.childNodes, null);
384
+ resetParent(content, this);
376
385
  this.childNodes = content;
386
+ return this;
377
387
  };
378
388
  HTMLElement.prototype.replaceWith = function () {
379
389
  var _this = this;
@@ -381,6 +391,7 @@ var HTMLElement = /** @class */ (function (_super) {
381
391
  for (var _i = 0; _i < arguments.length; _i++) {
382
392
  nodes[_i] = arguments[_i];
383
393
  }
394
+ var parent = this.parentNode;
384
395
  var content = nodes
385
396
  .map(function (node) {
386
397
  if (node instanceof node_1.default) {
@@ -394,10 +405,11 @@ var HTMLElement = /** @class */ (function (_super) {
394
405
  return [];
395
406
  })
396
407
  .flat();
397
- var idx = this.parentNode.childNodes.findIndex(function (child) {
408
+ var idx = parent.childNodes.findIndex(function (child) {
398
409
  return child === _this;
399
410
  });
400
- this.parentNode.childNodes = __spreadArray(__spreadArray(__spreadArray([], this.parentNode.childNodes.slice(0, idx), true), content, true), this.parentNode.childNodes.slice(idx + 1), true);
411
+ resetParent([this], null);
412
+ parent.childNodes = __spreadArray(__spreadArray(__spreadArray([], parent.childNodes.slice(0, idx), true), resetParent(content, parent), true), parent.childNodes.slice(idx + 1), true);
401
413
  };
402
414
  Object.defineProperty(HTMLElement.prototype, "outerHTML", {
403
415
  get: function () {
@@ -544,6 +556,43 @@ var HTMLElement = /** @class */ (function (_super) {
544
556
  }
545
557
  return re;
546
558
  };
559
+ /**
560
+ * find element by it's id
561
+ * @param {string} id the id of the element to select
562
+ */
563
+ HTMLElement.prototype.getElementById = function (id) {
564
+ var stack = [];
565
+ var currentNodeReference = this;
566
+ var index = 0;
567
+ // index turns to undefined once the stack is empty and the first condition occurs
568
+ // which happens once all relevant children are searched through
569
+ while (index !== undefined) {
570
+ var child = void 0;
571
+ // make it work with sparse arrays
572
+ do {
573
+ child = currentNodeReference.childNodes[index++];
574
+ } while (index < currentNodeReference.childNodes.length && child === undefined);
575
+ // if the child does not exist we move on with the last provided index (which belongs to the parentNode)
576
+ if (child === undefined) {
577
+ currentNodeReference = currentNodeReference.parentNode;
578
+ index = stack.pop();
579
+ continue;
580
+ }
581
+ if (child.nodeType === type_1.default.ELEMENT_NODE) {
582
+ if (child.id === id) {
583
+ return child;
584
+ }
585
+ ;
586
+ // if children are existing push the current status to the stack and keep searching for elements in the level below
587
+ if (child.childNodes.length > 0) {
588
+ stack.push(index);
589
+ currentNodeReference = child;
590
+ index = 0;
591
+ }
592
+ }
593
+ }
594
+ return null;
595
+ };
547
596
  /**
548
597
  * 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
598
  * @param selector a DOMString containing a selector list
@@ -708,6 +757,7 @@ var HTMLElement = /** @class */ (function (_super) {
708
757
  if (key === 'id') {
709
758
  this.id = '';
710
759
  }
760
+ return this;
711
761
  };
712
762
  HTMLElement.prototype.hasAttribute = function (key) {
713
763
  return key.toLowerCase() in this.attrs;
@@ -779,6 +829,7 @@ var HTMLElement = /** @class */ (function (_super) {
779
829
  return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
780
830
  })
781
831
  .join(' ');
832
+ return this;
782
833
  };
783
834
  HTMLElement.prototype.insertAdjacentHTML = function (where, html) {
784
835
  var _a, _b, _c;
@@ -791,14 +842,11 @@ var HTMLElement = /** @class */ (function (_super) {
791
842
  var idx = this.parentNode.childNodes.findIndex(function (child) {
792
843
  return child === _this;
793
844
  });
845
+ resetParent(p.childNodes, this.parentNode);
794
846
  (_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
847
  }
801
848
  else if (where === 'afterbegin') {
849
+ resetParent(p.childNodes, this);
802
850
  (_b = this.childNodes).unshift.apply(_b, p.childNodes);
803
851
  }
804
852
  else if (where === 'beforeend') {
@@ -810,16 +858,13 @@ var HTMLElement = /** @class */ (function (_super) {
810
858
  var idx = this.parentNode.childNodes.findIndex(function (child) {
811
859
  return child === _this;
812
860
  });
861
+ resetParent(p.childNodes, this.parentNode);
813
862
  (_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
863
  }
820
864
  else {
821
865
  throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
822
866
  }
867
+ return this;
823
868
  // if (!where || html === undefined || html === null) {
824
869
  // return;
825
870
  // }
@@ -1194,3 +1239,9 @@ function parse(data, options) {
1194
1239
  return root;
1195
1240
  }
1196
1241
  exports.parse = parse;
1242
+ function resetParent(nodes, parent) {
1243
+ return nodes.map(function (node) {
1244
+ node.parentNode = parent;
1245
+ return node;
1246
+ });
1247
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-html-parser",
3
- "version": "5.2.5",
3
+ "version": "5.3.1",
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",