node-html-parser 5.2.6 → 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
@@ -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
@@ -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
@@ -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
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.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",