node-html-parser 5.2.2 → 5.3.0

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
@@ -374,7 +374,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
374
374
  }
375
375
  DOMTokenList.prototype._validate = function (c) {
376
376
  if (/\s/.test(c)) {
377
- throw new Error("DOMException in DOMTokenList.add: The token '" + c + "' contains HTML space characters, which are not valid in tokens.");
377
+ throw new Error("DOMException in DOMTokenList.add: The token '".concat(c, "' contains HTML space characters, which are not valid in tokens."));
378
378
  }
379
379
  };
380
380
  DOMTokenList.prototype.add = function (c) {
@@ -458,14 +458,14 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
458
458
  );
459
459
  if (keyAttrs.id) {
460
460
  if (!rawAttrs) {
461
- _this.rawAttrs = "id=\"" + keyAttrs.id + "\"";
461
+ _this.rawAttrs = "id=\"".concat(keyAttrs.id, "\"");
462
462
  }
463
463
  }
464
464
  if (keyAttrs.class) {
465
465
  if (!rawAttrs) {
466
- var cls = "class=\"" + _this.classList.toString() + "\"";
466
+ var cls = "class=\"".concat(_this.classList.toString(), "\"");
467
467
  if (_this.rawAttrs) {
468
- _this.rawAttrs += " " + cls;
468
+ _this.rawAttrs += " ".concat(cls);
469
469
  }
470
470
  else {
471
471
  _this.rawAttrs = cls;
@@ -610,7 +610,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
610
610
  else {
611
611
  var text = node.trimmedText;
612
612
  if (currentBlock.prependWhitespace) {
613
- text = " " + text;
613
+ text = " ".concat(text);
614
614
  currentBlock.prependWhitespace = false;
615
615
  }
616
616
  currentBlock.push(text);
@@ -631,8 +631,8 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
631
631
  HTMLElement.prototype.toString = function () {
632
632
  var tag = this.rawTagName;
633
633
  if (tag) {
634
- var attrs = this.rawAttrs ? " " + this.rawAttrs : '';
635
- return this.isVoidElement ? "<" + tag + attrs + ">" : "<" + tag + attrs + ">" + this.innerHTML + "</" + tag + ">";
634
+ var attrs = this.rawAttrs ? " ".concat(this.rawAttrs) : '';
635
+ return this.isVoidElement ? "<".concat(tag).concat(attrs, ">") : "<".concat(tag).concat(attrs, ">").concat(this.innerHTML, "</").concat(tag, ">");
636
636
  }
637
637
  return this.innerHTML;
638
638
  };
@@ -728,9 +728,9 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
728
728
  res.push(' '.repeat(indention) + str);
729
729
  }
730
730
  function dfs(node) {
731
- var idStr = node.id ? "#" + node.id : '';
732
- var classStr = node.classList.length ? "." + node.classList.value.join('.') : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
733
- write("" + node.rawTagName + idStr + classStr);
731
+ var idStr = node.id ? "#".concat(node.id) : '';
732
+ var classStr = node.classList.length ? ".".concat(node.classList.value.join('.')) : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
733
+ write("".concat(node.rawTagName).concat(idStr).concat(classStr));
734
734
  indention++;
735
735
  node.childNodes.forEach(function (childNode) {
736
736
  if (childNode.nodeType === type_3.default.ELEMENT_NODE) {
@@ -832,6 +832,43 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
832
832
  }
833
833
  return re;
834
834
  };
835
+ /**
836
+ * find element by it's id
837
+ * @param {string} id the id of the element to select
838
+ */
839
+ HTMLElement.prototype.getElementById = function (id) {
840
+ var stack = [];
841
+ var currentNodeReference = this;
842
+ var index = 0;
843
+ // index turns to undefined once the stack is empty and the first condition occurs
844
+ // which happens once all relevant children are searched through
845
+ while (index !== undefined) {
846
+ var child = void 0;
847
+ // make it work with sparse arrays
848
+ do {
849
+ child = currentNodeReference.childNodes[index++];
850
+ } while (index < currentNodeReference.childNodes.length && child === undefined);
851
+ // if the child does not exist we move on with the last provided index (which belongs to the parentNode)
852
+ if (child === undefined) {
853
+ currentNodeReference = currentNodeReference.parentNode;
854
+ index = stack.pop();
855
+ continue;
856
+ }
857
+ if (child.nodeType === type_3.default.ELEMENT_NODE) {
858
+ if (child.id === id) {
859
+ return child;
860
+ }
861
+ ;
862
+ // if children are existing push the current status to the stack and keep searching for elements in the level below
863
+ if (child.childNodes.length > 0) {
864
+ stack.push(index);
865
+ currentNodeReference = child;
866
+ index = 0;
867
+ }
868
+ }
869
+ }
870
+ return null;
871
+ };
835
872
  /**
836
873
  * 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
874
  * @param selector a DOMString containing a selector list
@@ -989,7 +1026,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
989
1026
  if (val === undefined || val === 'null') {
990
1027
  return name;
991
1028
  }
992
- return name + "=" + val;
1029
+ return "".concat(name, "=").concat(val);
993
1030
  })
994
1031
  .join(' ');
995
1032
  // Update this.id
@@ -1036,7 +1073,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1036
1073
  var val = _this.quoteAttribute(attrs[name]);
1037
1074
  if (val === 'null' || val === '""')
1038
1075
  return name;
1039
- return name + "=" + val;
1076
+ return "".concat(name, "=").concat(val);
1040
1077
  })
1041
1078
  .join(' ');
1042
1079
  // Update this.id
@@ -1064,7 +1101,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1064
1101
  var val = attributes[name];
1065
1102
  if (val === 'null' || val === '""')
1066
1103
  return name;
1067
- return name + "=" + _this.quoteAttribute(String(val));
1104
+ return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
1068
1105
  })
1069
1106
  .join(' ');
1070
1107
  };
@@ -1106,7 +1143,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1106
1143
  });
1107
1144
  }
1108
1145
  else {
1109
- throw new Error("The value provided ('" + where + "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'");
1146
+ throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
1110
1147
  }
1111
1148
  // if (!where || html === undefined || html === null) {
1112
1149
  // return;
@@ -1295,8 +1332,8 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1295
1332
  pre: true,
1296
1333
  };
1297
1334
  var element_names = Object.keys(elements);
1298
- var kBlockTextElements = element_names.map(function (it) { return new RegExp("^" + it + "$", 'i'); });
1299
- var kIgnoreElements = element_names.filter(function (it) { return elements[it]; }).map(function (it) { return new RegExp("^" + it + "$", 'i'); });
1335
+ var kBlockTextElements = element_names.map(function (it) { return new RegExp("^".concat(it, "$"), 'i'); });
1336
+ var kIgnoreElements = element_names.filter(function (it) { return elements[it]; }).map(function (it) { return new RegExp("^".concat(it, "$"), 'i'); });
1300
1337
  function element_should_be_ignore(tag) {
1301
1338
  return kIgnoreElements.some(function (it) { return it.test(tag); });
1302
1339
  }
@@ -1311,7 +1348,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1311
1348
  var noNestedTagIndex = undefined;
1312
1349
  var match;
1313
1350
  // https://github.com/taoqf/node-html-parser/issues/38
1314
- data = "<" + frameflag + ">" + data + "</" + frameflag + ">";
1351
+ data = "<".concat(frameflag, ">").concat(data, "</").concat(frameflag, ">");
1315
1352
  var lowerCaseTagName = options.lowerCaseTagName;
1316
1353
  var dataEndPos = data.length - (frameflag.length + 2);
1317
1354
  var frameFlagOffset = frameflag.length + 2;
@@ -1379,7 +1416,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
1379
1416
  stack.push(currentParent);
1380
1417
  if (is_block_text_element(tagName)) {
1381
1418
  // Find closing tag
1382
- var closeMarkup = "</" + tagName + ">";
1419
+ var closeMarkup = "</".concat(tagName, ">");
1383
1420
  var closeIndex = lowerCaseTagName
1384
1421
  ? data.toLocaleLowerCase().indexOf(closeMarkup, kMarkupPattern.lastIndex)
1385
1422
  : data.indexOf(closeMarkup, kMarkupPattern.lastIndex);
@@ -1515,7 +1552,7 @@ define("nodes/comment", ["require", "exports", "nodes/node", "nodes/type"], func
1515
1552
  configurable: true
1516
1553
  });
1517
1554
  CommentNode.prototype.toString = function () {
1518
- return "<!--" + this.rawText + "-->";
1555
+ return "<!--".concat(this.rawText, "-->");
1519
1556
  };
1520
1557
  return CommentNode;
1521
1558
  }(node_3.default));
@@ -47,7 +47,7 @@ var CommentNode = /** @class */ (function (_super) {
47
47
  configurable: true
48
48
  });
49
49
  CommentNode.prototype.toString = function () {
50
- return "<!--" + this.rawText + "-->";
50
+ return "<!--".concat(this.rawText, "-->");
51
51
  };
52
52
  return CommentNode;
53
53
  }(node_1.default));
@@ -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
@@ -86,7 +86,7 @@ var DOMTokenList = /** @class */ (function () {
86
86
  }
87
87
  DOMTokenList.prototype._validate = function (c) {
88
88
  if (/\s/.test(c)) {
89
- throw new Error("DOMException in DOMTokenList.add: The token '" + c + "' contains HTML space characters, which are not valid in tokens.");
89
+ throw new Error("DOMException in DOMTokenList.add: The token '".concat(c, "' contains HTML space characters, which are not valid in tokens."));
90
90
  }
91
91
  };
92
92
  DOMTokenList.prototype.add = function (c) {
@@ -170,14 +170,14 @@ var HTMLElement = /** @class */ (function (_super) {
170
170
  );
171
171
  if (keyAttrs.id) {
172
172
  if (!rawAttrs) {
173
- _this.rawAttrs = "id=\"" + keyAttrs.id + "\"";
173
+ _this.rawAttrs = "id=\"".concat(keyAttrs.id, "\"");
174
174
  }
175
175
  }
176
176
  if (keyAttrs.class) {
177
177
  if (!rawAttrs) {
178
- var cls = "class=\"" + _this.classList.toString() + "\"";
178
+ var cls = "class=\"".concat(_this.classList.toString(), "\"");
179
179
  if (_this.rawAttrs) {
180
- _this.rawAttrs += " " + cls;
180
+ _this.rawAttrs += " ".concat(cls);
181
181
  }
182
182
  else {
183
183
  _this.rawAttrs = cls;
@@ -322,7 +322,7 @@ var HTMLElement = /** @class */ (function (_super) {
322
322
  else {
323
323
  var text = node.trimmedText;
324
324
  if (currentBlock.prependWhitespace) {
325
- text = " " + text;
325
+ text = " ".concat(text);
326
326
  currentBlock.prependWhitespace = false;
327
327
  }
328
328
  currentBlock.push(text);
@@ -343,8 +343,8 @@ var HTMLElement = /** @class */ (function (_super) {
343
343
  HTMLElement.prototype.toString = function () {
344
344
  var tag = this.rawTagName;
345
345
  if (tag) {
346
- var attrs = this.rawAttrs ? " " + this.rawAttrs : '';
347
- return this.isVoidElement ? "<" + tag + attrs + ">" : "<" + tag + attrs + ">" + this.innerHTML + "</" + tag + ">";
346
+ var attrs = this.rawAttrs ? " ".concat(this.rawAttrs) : '';
347
+ return this.isVoidElement ? "<".concat(tag).concat(attrs, ">") : "<".concat(tag).concat(attrs, ">").concat(this.innerHTML, "</").concat(tag, ">");
348
348
  }
349
349
  return this.innerHTML;
350
350
  };
@@ -440,9 +440,9 @@ var HTMLElement = /** @class */ (function (_super) {
440
440
  res.push(' '.repeat(indention) + str);
441
441
  }
442
442
  function dfs(node) {
443
- var idStr = node.id ? "#" + node.id : '';
444
- var classStr = node.classList.length ? "." + node.classList.value.join('.') : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
445
- write("" + node.rawTagName + idStr + classStr);
443
+ var idStr = node.id ? "#".concat(node.id) : '';
444
+ var classStr = node.classList.length ? ".".concat(node.classList.value.join('.')) : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
445
+ write("".concat(node.rawTagName).concat(idStr).concat(classStr));
446
446
  indention++;
447
447
  node.childNodes.forEach(function (childNode) {
448
448
  if (childNode.nodeType === type_1.default.ELEMENT_NODE) {
@@ -544,6 +544,43 @@ var HTMLElement = /** @class */ (function (_super) {
544
544
  }
545
545
  return re;
546
546
  };
547
+ /**
548
+ * find element by it's id
549
+ * @param {string} id the id of the element to select
550
+ */
551
+ HTMLElement.prototype.getElementById = function (id) {
552
+ var stack = [];
553
+ var currentNodeReference = this;
554
+ var index = 0;
555
+ // index turns to undefined once the stack is empty and the first condition occurs
556
+ // which happens once all relevant children are searched through
557
+ while (index !== undefined) {
558
+ var child = void 0;
559
+ // make it work with sparse arrays
560
+ do {
561
+ child = currentNodeReference.childNodes[index++];
562
+ } while (index < currentNodeReference.childNodes.length && child === undefined);
563
+ // if the child does not exist we move on with the last provided index (which belongs to the parentNode)
564
+ if (child === undefined) {
565
+ currentNodeReference = currentNodeReference.parentNode;
566
+ index = stack.pop();
567
+ continue;
568
+ }
569
+ if (child.nodeType === type_1.default.ELEMENT_NODE) {
570
+ if (child.id === id) {
571
+ return child;
572
+ }
573
+ ;
574
+ // if children are existing push the current status to the stack and keep searching for elements in the level below
575
+ if (child.childNodes.length > 0) {
576
+ stack.push(index);
577
+ currentNodeReference = child;
578
+ index = 0;
579
+ }
580
+ }
581
+ }
582
+ return null;
583
+ };
547
584
  /**
548
585
  * 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
586
  * @param selector a DOMString containing a selector list
@@ -701,7 +738,7 @@ var HTMLElement = /** @class */ (function (_super) {
701
738
  if (val === undefined || val === 'null') {
702
739
  return name;
703
740
  }
704
- return name + "=" + val;
741
+ return "".concat(name, "=").concat(val);
705
742
  })
706
743
  .join(' ');
707
744
  // Update this.id
@@ -748,7 +785,7 @@ var HTMLElement = /** @class */ (function (_super) {
748
785
  var val = _this.quoteAttribute(attrs[name]);
749
786
  if (val === 'null' || val === '""')
750
787
  return name;
751
- return name + "=" + val;
788
+ return "".concat(name, "=").concat(val);
752
789
  })
753
790
  .join(' ');
754
791
  // Update this.id
@@ -776,7 +813,7 @@ var HTMLElement = /** @class */ (function (_super) {
776
813
  var val = attributes[name];
777
814
  if (val === 'null' || val === '""')
778
815
  return name;
779
- return name + "=" + _this.quoteAttribute(String(val));
816
+ return "".concat(name, "=").concat(_this.quoteAttribute(String(val)));
780
817
  })
781
818
  .join(' ');
782
819
  };
@@ -818,7 +855,7 @@ var HTMLElement = /** @class */ (function (_super) {
818
855
  });
819
856
  }
820
857
  else {
821
- throw new Error("The value provided ('" + where + "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'");
858
+ throw new Error("The value provided ('".concat(where, "') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'"));
822
859
  }
823
860
  // if (!where || html === undefined || html === null) {
824
861
  // return;
@@ -1007,8 +1044,8 @@ function base_parse(data, options) {
1007
1044
  pre: true,
1008
1045
  };
1009
1046
  var element_names = Object.keys(elements);
1010
- var kBlockTextElements = element_names.map(function (it) { return new RegExp("^" + it + "$", 'i'); });
1011
- var kIgnoreElements = element_names.filter(function (it) { return elements[it]; }).map(function (it) { return new RegExp("^" + it + "$", 'i'); });
1047
+ var kBlockTextElements = element_names.map(function (it) { return new RegExp("^".concat(it, "$"), 'i'); });
1048
+ var kIgnoreElements = element_names.filter(function (it) { return elements[it]; }).map(function (it) { return new RegExp("^".concat(it, "$"), 'i'); });
1012
1049
  function element_should_be_ignore(tag) {
1013
1050
  return kIgnoreElements.some(function (it) { return it.test(tag); });
1014
1051
  }
@@ -1023,7 +1060,7 @@ function base_parse(data, options) {
1023
1060
  var noNestedTagIndex = undefined;
1024
1061
  var match;
1025
1062
  // https://github.com/taoqf/node-html-parser/issues/38
1026
- data = "<" + frameflag + ">" + data + "</" + frameflag + ">";
1063
+ data = "<".concat(frameflag, ">").concat(data, "</").concat(frameflag, ">");
1027
1064
  var lowerCaseTagName = options.lowerCaseTagName;
1028
1065
  var dataEndPos = data.length - (frameflag.length + 2);
1029
1066
  var frameFlagOffset = frameflag.length + 2;
@@ -1091,7 +1128,7 @@ function base_parse(data, options) {
1091
1128
  stack.push(currentParent);
1092
1129
  if (is_block_text_element(tagName)) {
1093
1130
  // Find closing tag
1094
- var closeMarkup = "</" + tagName + ">";
1131
+ var closeMarkup = "</".concat(tagName, ">");
1095
1132
  var closeIndex = lowerCaseTagName
1096
1133
  ? data.toLocaleLowerCase().indexOf(closeMarkup, kMarkupPattern.lastIndex)
1097
1134
  : data.indexOf(closeMarkup, kMarkupPattern.lastIndex);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-html-parser",
3
- "version": "5.2.2",
3
+ "version": "5.3.0",
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",
@@ -72,7 +72,6 @@
72
72
  "high5": "^1.0.0",
73
73
  "html-dom-parser": "^1.0.4",
74
74
  "html-parser": "^0.11.0",
75
- "html5": "^1.0.5",
76
75
  "html5parser": "^2.0.2",
77
76
  "htmljs-parser": "^2.11.1",
78
77
  "htmlparser": "^1.7.7",