node-html-parser 3.2.0 → 3.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
@@ -1,7 +1,7 @@
1
1
  # Fast HTML Parser [![NPM version](https://badge.fury.io/js/node-html-parser.png)](http://badge.fury.io/js/node-html-parser) [![Build Status](https://travis-ci.org/taoqf/node-html-parser.svg?branch=master)](https://travis-ci.org/taoqf/node-html-parser)
2
2
 
3
3
  Fast HTML Parser is a _very fast_ HTML parser. Which will generate a simplified
4
- DOM tree, with basic element query support.
4
+ DOM tree, with element query support.
5
5
 
6
6
  Per the design, it intends to parse massive HTML files in lowest price, thus the
7
7
  performance is the top priority. For this reason, some malformatted HTML may not
@@ -112,6 +112,10 @@ Note: Full css3 selector supported since v3.0.0.
112
112
 
113
113
  Query CSS Selector to find matching node.
114
114
 
115
+ ### HTMLElement#closest(selector)
116
+
117
+ Query closest element by css selector.
118
+
115
119
  ### HTMLElement#appendChild(node)
116
120
 
117
121
  Append a child node to childNodes
@@ -478,6 +478,61 @@ export default class HTMLElement extends Node {
478
478
  // }
479
479
  // return null;
480
480
  }
481
+ /**
482
+ * 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.
483
+ * @param selector a DOMString containing a selector list
484
+ */
485
+ closest(selector) {
486
+ const mapChild = new Map();
487
+ let el = this;
488
+ let old = null;
489
+ function findOne(test, elems) {
490
+ let elem = null;
491
+ for (let i = 0, l = elems.length; i < l && !elem; i++) {
492
+ const el = elems[i];
493
+ if (test(el)) {
494
+ elem = el;
495
+ }
496
+ else {
497
+ const child = mapChild.get(el);
498
+ if (child) {
499
+ elem = findOne(test, [child]);
500
+ }
501
+ }
502
+ }
503
+ return elem;
504
+ }
505
+ while (el) {
506
+ mapChild.set(el, old);
507
+ old = el;
508
+ el = el.parentNode;
509
+ }
510
+ el = this;
511
+ while (el) {
512
+ const e = selectOne(selector, el, {
513
+ xmlMode: true,
514
+ adapter: {
515
+ ...Matcher,
516
+ getChildren(node) {
517
+ const child = mapChild.get(node);
518
+ return child && [child];
519
+ },
520
+ getSiblings(node) {
521
+ return [node];
522
+ },
523
+ findOne,
524
+ findAll() {
525
+ return [];
526
+ }
527
+ }
528
+ });
529
+ if (e) {
530
+ return e;
531
+ }
532
+ el = el.parentNode;
533
+ }
534
+ return null;
535
+ }
481
536
  /**
482
537
  * Append a child node to childNodes
483
538
  * @param {Node} node node to append
package/dist/main.js CHANGED
@@ -16,6 +16,17 @@ var __extends = (this && this.__extends) || (function () {
16
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
+ var __assign = (this && this.__assign) || function () {
20
+ __assign = Object.assign || function(t) {
21
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
22
+ s = arguments[i];
23
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
24
+ t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign.apply(this, arguments);
29
+ };
19
30
  var __spreadArray = (this && this.__spreadArray) || function (to, from) {
20
31
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
21
32
  to[j] = from[i];
@@ -732,6 +743,58 @@ define("nodes/html", ["require", "exports", "he", "css-select", "nodes/node", "n
732
743
  // }
733
744
  // return null;
734
745
  };
746
+ /**
747
+ * 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.
748
+ * @param selector a DOMString containing a selector list
749
+ */
750
+ HTMLElement.prototype.closest = function (selector) {
751
+ var mapChild = new Map();
752
+ var el = this;
753
+ var old = null;
754
+ function findOne(test, elems) {
755
+ var elem = null;
756
+ for (var i = 0, l = elems.length; i < l && !elem; i++) {
757
+ var el_1 = elems[i];
758
+ if (test(el_1)) {
759
+ elem = el_1;
760
+ }
761
+ else {
762
+ var child = mapChild.get(el_1);
763
+ if (child) {
764
+ elem = findOne(test, [child]);
765
+ }
766
+ }
767
+ }
768
+ return elem;
769
+ }
770
+ while (el) {
771
+ mapChild.set(el, old);
772
+ old = el;
773
+ el = el.parentNode;
774
+ }
775
+ el = this;
776
+ while (el) {
777
+ var e = css_select_1.selectOne(selector, el, {
778
+ xmlMode: true,
779
+ adapter: __assign(__assign({}, matcher_1.default), { getChildren: function (node) {
780
+ var child = mapChild.get(node);
781
+ return child && [child];
782
+ },
783
+ getSiblings: function (node) {
784
+ return [node];
785
+ },
786
+ findOne: findOne,
787
+ findAll: function () {
788
+ return [];
789
+ } })
790
+ });
791
+ if (e) {
792
+ return e;
793
+ }
794
+ el = el.parentNode;
795
+ }
796
+ return null;
797
+ };
735
798
  /**
736
799
  * Append a child node to childNodes
737
800
  * @param {Node} node node to append
@@ -122,6 +122,11 @@ export default class HTMLElement extends Node {
122
122
  * @return {HTMLElement} matching node
123
123
  */
124
124
  querySelector(selector: string): HTMLElement;
125
+ /**
126
+ * 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.
127
+ * @param selector a DOMString containing a selector list
128
+ */
129
+ closest(selector: string): Node;
125
130
  /**
126
131
  * Append a child node to childNodes
127
132
  * @param {Node} node node to append
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
17
28
  var __spreadArray = (this && this.__spreadArray) || function (to, from) {
18
29
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
19
30
  to[j] = from[i];
@@ -558,6 +569,58 @@ var HTMLElement = /** @class */ (function (_super) {
558
569
  // }
559
570
  // return null;
560
571
  };
572
+ /**
573
+ * 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.
574
+ * @param selector a DOMString containing a selector list
575
+ */
576
+ HTMLElement.prototype.closest = function (selector) {
577
+ var mapChild = new Map();
578
+ var el = this;
579
+ var old = null;
580
+ function findOne(test, elems) {
581
+ var elem = null;
582
+ for (var i = 0, l = elems.length; i < l && !elem; i++) {
583
+ var el_1 = elems[i];
584
+ if (test(el_1)) {
585
+ elem = el_1;
586
+ }
587
+ else {
588
+ var child = mapChild.get(el_1);
589
+ if (child) {
590
+ elem = findOne(test, [child]);
591
+ }
592
+ }
593
+ }
594
+ return elem;
595
+ }
596
+ while (el) {
597
+ mapChild.set(el, old);
598
+ old = el;
599
+ el = el.parentNode;
600
+ }
601
+ el = this;
602
+ while (el) {
603
+ var e = css_select_1.selectOne(selector, el, {
604
+ xmlMode: true,
605
+ adapter: __assign(__assign({}, matcher_1.default), { getChildren: function (node) {
606
+ var child = mapChild.get(node);
607
+ return child && [child];
608
+ },
609
+ getSiblings: function (node) {
610
+ return [node];
611
+ },
612
+ findOne: findOne,
613
+ findAll: function () {
614
+ return [];
615
+ } })
616
+ });
617
+ if (e) {
618
+ return e;
619
+ }
620
+ el = el.parentNode;
621
+ }
622
+ return null;
623
+ };
561
624
  /**
562
625
  * Append a child node to childNodes
563
626
  * @param {Node} node node to append
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-html-parser",
3
- "version": "3.2.0",
3
+ "version": "3.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
  "module": "dist/esm/index.js",
@@ -15,7 +15,7 @@
15
15
  "build": "npm run lint && npm run clean && npm run ts:cjs && npm run ts:amd && npm run ts:esm",
16
16
  "dev": "tsc -w & mocha -w ./test/*.js",
17
17
  "pretest": "tsc -m commonjs",
18
- "release": "np"
18
+ "release": "yarn build && np"
19
19
  },
20
20
  "keywords": [
21
21
  "parser",