node-html-parser 2.0.1 → 2.2.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 +17 -5
- package/dist/esm/nodes/html.js +33 -7
- package/dist/esm/nodes/node.js +6 -0
- package/dist/main.js +60 -15
- package/dist/nodes/comment.d.ts +1 -1
- package/dist/nodes/comment.js +2 -0
- package/dist/nodes/html.d.ts +5 -1
- package/dist/nodes/html.js +50 -16
- package/dist/nodes/node.d.ts +2 -0
- package/dist/nodes/node.js +10 -0
- package/dist/nodes/text.js +2 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -10,20 +10,24 @@ no closing `<li>`, `<td>` etc).
|
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
13
|
+
|
|
13
14
|
```shell
|
|
14
15
|
npm install --save node-html-parser
|
|
15
16
|
```
|
|
16
17
|
|
|
18
|
+
> Note: when using Fast HTML Parser in a Typescript project the minimum Typescript version supported is `^4.1.2`.
|
|
19
|
+
|
|
17
20
|
## Performance
|
|
18
21
|
|
|
19
22
|
Faster than htmlparser2!
|
|
20
23
|
|
|
21
24
|
```shell
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
htmlparser :26.7111 ms/file ± 170.066
|
|
26
|
+
cheerio :24.2480 ms/file ± 17.1711
|
|
27
|
+
parse5 :13.7239 ms/file ± 8.68561
|
|
28
|
+
high5 :7.75466 ms/file ± 5.33549
|
|
29
|
+
htmlparser2 :5.27376 ms/file ± 8.68456
|
|
30
|
+
node-html-parser:2.85768 ms/file ± 2.87784
|
|
27
31
|
```
|
|
28
32
|
|
|
29
33
|
Tested with [htmlparser-benchmark](https://github.com/AndreasMadsen/htmlparser-benchmark).
|
|
@@ -162,6 +166,10 @@ Get unescaped text value of current node and its children. Like `innerText`.
|
|
|
162
166
|
Get escpaed (as-it) text value of current node and its children. May have
|
|
163
167
|
`&` in it. (fast)
|
|
164
168
|
|
|
169
|
+
### HTMLElement#tagName
|
|
170
|
+
|
|
171
|
+
Get tag name of HTMLElement. Notice: the returned value would be an uppercase string.
|
|
172
|
+
|
|
165
173
|
### HTMLElement#structuredText
|
|
166
174
|
|
|
167
175
|
Get structured Text
|
|
@@ -193,3 +201,7 @@ Returns a reference to the next child node of the current element's parent.
|
|
|
193
201
|
### HTMLElement#nextElementSibling
|
|
194
202
|
|
|
195
203
|
Returns a reference to the next child element of the current element's parent.
|
|
204
|
+
|
|
205
|
+
### HTMLElement#textContent
|
|
206
|
+
|
|
207
|
+
Get or Set textContent of current element, more efficient than [set_content](#htmlelementset_contentcontent-string--node--node).
|
package/dist/esm/nodes/html.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import he from 'he';
|
|
2
2
|
import Node from './node';
|
|
3
3
|
import NodeType from './type';
|
|
4
4
|
import TextNode from './text';
|
|
@@ -6,6 +6,7 @@ import Matcher from '../matcher';
|
|
|
6
6
|
import arr_back from '../back';
|
|
7
7
|
import CommentNode from './comment';
|
|
8
8
|
import parse from '../parse';
|
|
9
|
+
const { decode } = he;
|
|
9
10
|
const kBlockElements = new Map();
|
|
10
11
|
kBlockElements.set('DIV', true);
|
|
11
12
|
kBlockElements.set('div', true);
|
|
@@ -117,6 +118,13 @@ export default class HTMLElement extends Node {
|
|
|
117
118
|
return (pre += cur.rawText);
|
|
118
119
|
}, '');
|
|
119
120
|
}
|
|
121
|
+
get textContent() {
|
|
122
|
+
return this.rawText;
|
|
123
|
+
}
|
|
124
|
+
set textContent(val) {
|
|
125
|
+
const content = [new TextNode(val)];
|
|
126
|
+
this.childNodes = content;
|
|
127
|
+
}
|
|
120
128
|
/**
|
|
121
129
|
* Get unescaped text value of current node and its children.
|
|
122
130
|
* @return {string} text content
|
|
@@ -233,7 +241,7 @@ export default class HTMLElement extends Node {
|
|
|
233
241
|
function dfs(node) {
|
|
234
242
|
const idStr = node.id ? (`#${node.id}`) : '';
|
|
235
243
|
const classStr = node.classNames.length ? (`.${node.classNames.join('.')}`) : '';
|
|
236
|
-
write(node.rawTagName
|
|
244
|
+
write(`${node.rawTagName}${idStr}${classStr}`);
|
|
237
245
|
indention++;
|
|
238
246
|
node.childNodes.forEach((childNode) => {
|
|
239
247
|
if (childNode.nodeType === NodeType.ELEMENT_NODE) {
|
|
@@ -409,9 +417,10 @@ export default class HTMLElement extends Node {
|
|
|
409
417
|
}
|
|
410
418
|
/**
|
|
411
419
|
* Get attributes
|
|
420
|
+
* @access private
|
|
412
421
|
* @return {Object} parsed and unescaped attributes
|
|
413
422
|
*/
|
|
414
|
-
get
|
|
423
|
+
get attrs() {
|
|
415
424
|
if (this._attrs) {
|
|
416
425
|
return this._attrs;
|
|
417
426
|
}
|
|
@@ -419,10 +428,19 @@ export default class HTMLElement extends Node {
|
|
|
419
428
|
const attrs = this.rawAttributes;
|
|
420
429
|
for (const key in attrs) {
|
|
421
430
|
const val = attrs[key] || '';
|
|
422
|
-
this._attrs[key] = decode(val);
|
|
431
|
+
this._attrs[key.toLowerCase()] = decode(val);
|
|
423
432
|
}
|
|
424
433
|
return this._attrs;
|
|
425
434
|
}
|
|
435
|
+
get attributes() {
|
|
436
|
+
const ret_attrs = {};
|
|
437
|
+
const attrs = this.rawAttributes;
|
|
438
|
+
for (const key in attrs) {
|
|
439
|
+
const val = attrs[key] || '';
|
|
440
|
+
ret_attrs[key] = decode(val);
|
|
441
|
+
}
|
|
442
|
+
return ret_attrs;
|
|
443
|
+
}
|
|
426
444
|
/**
|
|
427
445
|
* Get escaped (as-it) attributes
|
|
428
446
|
* @return {Object} parsed attributes
|
|
@@ -459,14 +477,14 @@ export default class HTMLElement extends Node {
|
|
|
459
477
|
}).join(' ');
|
|
460
478
|
}
|
|
461
479
|
hasAttribute(key) {
|
|
462
|
-
return key in this.
|
|
480
|
+
return key.toLowerCase() in this.attrs;
|
|
463
481
|
}
|
|
464
482
|
/**
|
|
465
483
|
* Get an attribute
|
|
466
484
|
* @return {string} value of the attribute
|
|
467
485
|
*/
|
|
468
486
|
getAttribute(key) {
|
|
469
|
-
return this.
|
|
487
|
+
return this.attrs[key.toLowerCase()];
|
|
470
488
|
}
|
|
471
489
|
/**
|
|
472
490
|
* Set an attribute value to the HTMLElement
|
|
@@ -477,10 +495,18 @@ export default class HTMLElement extends Node {
|
|
|
477
495
|
if (arguments.length < 2) {
|
|
478
496
|
throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
|
|
479
497
|
}
|
|
498
|
+
const k2 = key.toLowerCase();
|
|
480
499
|
const attrs = this.rawAttributes;
|
|
500
|
+
for (const k in attrs) {
|
|
501
|
+
if (k.toLowerCase() === k2) {
|
|
502
|
+
key = k;
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
481
506
|
attrs[key] = String(value);
|
|
507
|
+
// update this.attrs
|
|
482
508
|
if (this._attrs) {
|
|
483
|
-
this._attrs[
|
|
509
|
+
this._attrs[k2] = decode(attrs[key]);
|
|
484
510
|
}
|
|
485
511
|
// Update rawString
|
|
486
512
|
this.rawAttrs = Object.keys(attrs).map((name) => {
|
package/dist/esm/nodes/node.js
CHANGED
package/dist/main.js
CHANGED
|
@@ -6,6 +6,8 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
6
6
|
return extendStatics(d, b);
|
|
7
7
|
};
|
|
8
8
|
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
9
11
|
extendStatics(d, b);
|
|
10
12
|
function __() { this.constructor = d; }
|
|
11
13
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
@@ -14,12 +16,10 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
15
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
16
18
|
};
|
|
17
|
-
var
|
|
18
|
-
for (var
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
r[k] = a[j];
|
|
22
|
-
return r;
|
|
19
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
20
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
21
|
+
to[j] = from[i];
|
|
22
|
+
return to;
|
|
23
23
|
};
|
|
24
24
|
define("back", ["require", "exports"], function (require, exports) {
|
|
25
25
|
"use strict";
|
|
@@ -57,6 +57,16 @@ define("nodes/node", ["require", "exports"], function (require, exports) {
|
|
|
57
57
|
enumerable: false,
|
|
58
58
|
configurable: true
|
|
59
59
|
});
|
|
60
|
+
Object.defineProperty(Node.prototype, "textContent", {
|
|
61
|
+
get: function () {
|
|
62
|
+
return this.rawText;
|
|
63
|
+
},
|
|
64
|
+
set: function (val) {
|
|
65
|
+
this.rawText = val;
|
|
66
|
+
},
|
|
67
|
+
enumerable: false,
|
|
68
|
+
configurable: true
|
|
69
|
+
});
|
|
60
70
|
return Node;
|
|
61
71
|
}());
|
|
62
72
|
exports.default = Node;
|
|
@@ -450,6 +460,7 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
450
460
|
"use strict";
|
|
451
461
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
452
462
|
exports.base_parse = void 0;
|
|
463
|
+
he_1 = __importDefault(he_1);
|
|
453
464
|
node_3 = __importDefault(node_3);
|
|
454
465
|
type_3 = __importDefault(type_3);
|
|
455
466
|
text_1 = __importDefault(text_1);
|
|
@@ -457,6 +468,7 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
457
468
|
back_2 = __importDefault(back_2);
|
|
458
469
|
comment_1 = __importDefault(comment_1);
|
|
459
470
|
parse_1 = __importDefault(parse_1);
|
|
471
|
+
var decode = he_1.default.decode;
|
|
460
472
|
var kBlockElements = new Map();
|
|
461
473
|
kBlockElements.set('DIV', true);
|
|
462
474
|
kBlockElements.set('div', true);
|
|
@@ -581,13 +593,24 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
581
593
|
enumerable: false,
|
|
582
594
|
configurable: true
|
|
583
595
|
});
|
|
596
|
+
Object.defineProperty(HTMLElement.prototype, "textContent", {
|
|
597
|
+
get: function () {
|
|
598
|
+
return this.rawText;
|
|
599
|
+
},
|
|
600
|
+
set: function (val) {
|
|
601
|
+
var content = [new text_1.default(val)];
|
|
602
|
+
this.childNodes = content;
|
|
603
|
+
},
|
|
604
|
+
enumerable: false,
|
|
605
|
+
configurable: true
|
|
606
|
+
});
|
|
584
607
|
Object.defineProperty(HTMLElement.prototype, "text", {
|
|
585
608
|
/**
|
|
586
609
|
* Get unescaped text value of current node and its children.
|
|
587
610
|
* @return {string} text content
|
|
588
611
|
*/
|
|
589
612
|
get: function () {
|
|
590
|
-
return
|
|
613
|
+
return decode(this.rawText);
|
|
591
614
|
},
|
|
592
615
|
enumerable: false,
|
|
593
616
|
configurable: true
|
|
@@ -715,7 +738,7 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
715
738
|
function dfs(node) {
|
|
716
739
|
var idStr = node.id ? ("#" + node.id) : '';
|
|
717
740
|
var classStr = node.classNames.length ? ("." + node.classNames.join('.')) : '';
|
|
718
|
-
write(node.rawTagName + idStr + classStr);
|
|
741
|
+
write("" + node.rawTagName + idStr + classStr);
|
|
719
742
|
indention++;
|
|
720
743
|
node.childNodes.forEach(function (childNode) {
|
|
721
744
|
if (childNode.nodeType === type_3.default.ELEMENT_NODE) {
|
|
@@ -903,9 +926,10 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
903
926
|
enumerable: false,
|
|
904
927
|
configurable: true
|
|
905
928
|
});
|
|
906
|
-
Object.defineProperty(HTMLElement.prototype, "
|
|
929
|
+
Object.defineProperty(HTMLElement.prototype, "attrs", {
|
|
907
930
|
/**
|
|
908
931
|
* Get attributes
|
|
932
|
+
* @access private
|
|
909
933
|
* @return {Object} parsed and unescaped attributes
|
|
910
934
|
*/
|
|
911
935
|
get: function () {
|
|
@@ -916,13 +940,26 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
916
940
|
var attrs = this.rawAttributes;
|
|
917
941
|
for (var key in attrs) {
|
|
918
942
|
var val = attrs[key] || '';
|
|
919
|
-
this._attrs[key] =
|
|
943
|
+
this._attrs[key.toLowerCase()] = decode(val);
|
|
920
944
|
}
|
|
921
945
|
return this._attrs;
|
|
922
946
|
},
|
|
923
947
|
enumerable: false,
|
|
924
948
|
configurable: true
|
|
925
949
|
});
|
|
950
|
+
Object.defineProperty(HTMLElement.prototype, "attributes", {
|
|
951
|
+
get: function () {
|
|
952
|
+
var ret_attrs = {};
|
|
953
|
+
var attrs = this.rawAttributes;
|
|
954
|
+
for (var key in attrs) {
|
|
955
|
+
var val = attrs[key] || '';
|
|
956
|
+
ret_attrs[key] = decode(val);
|
|
957
|
+
}
|
|
958
|
+
return ret_attrs;
|
|
959
|
+
},
|
|
960
|
+
enumerable: false,
|
|
961
|
+
configurable: true
|
|
962
|
+
});
|
|
926
963
|
Object.defineProperty(HTMLElement.prototype, "rawAttributes", {
|
|
927
964
|
/**
|
|
928
965
|
* Get escaped (as-it) attributes
|
|
@@ -963,14 +1000,14 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
963
1000
|
}).join(' ');
|
|
964
1001
|
};
|
|
965
1002
|
HTMLElement.prototype.hasAttribute = function (key) {
|
|
966
|
-
return key in this.
|
|
1003
|
+
return key.toLowerCase() in this.attrs;
|
|
967
1004
|
};
|
|
968
1005
|
/**
|
|
969
1006
|
* Get an attribute
|
|
970
1007
|
* @return {string} value of the attribute
|
|
971
1008
|
*/
|
|
972
1009
|
HTMLElement.prototype.getAttribute = function (key) {
|
|
973
|
-
return this.
|
|
1010
|
+
return this.attrs[key.toLowerCase()];
|
|
974
1011
|
};
|
|
975
1012
|
/**
|
|
976
1013
|
* Set an attribute value to the HTMLElement
|
|
@@ -981,10 +1018,18 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
981
1018
|
if (arguments.length < 2) {
|
|
982
1019
|
throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
|
|
983
1020
|
}
|
|
1021
|
+
var k2 = key.toLowerCase();
|
|
984
1022
|
var attrs = this.rawAttributes;
|
|
1023
|
+
for (var k in attrs) {
|
|
1024
|
+
if (k.toLowerCase() === k2) {
|
|
1025
|
+
key = k;
|
|
1026
|
+
break;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
985
1029
|
attrs[key] = String(value);
|
|
1030
|
+
// update this.attrs
|
|
986
1031
|
if (this._attrs) {
|
|
987
|
-
this._attrs[
|
|
1032
|
+
this._attrs[k2] = decode(attrs[key]);
|
|
988
1033
|
}
|
|
989
1034
|
// Update rawString
|
|
990
1035
|
this.rawAttrs = Object.keys(attrs).map(function (name) {
|
|
@@ -1028,7 +1073,7 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
1028
1073
|
var idx = this.parentNode.childNodes.findIndex(function (child) {
|
|
1029
1074
|
return child === _this;
|
|
1030
1075
|
});
|
|
1031
|
-
(_a = this.parentNode.childNodes).splice.apply(_a,
|
|
1076
|
+
(_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes));
|
|
1032
1077
|
p.childNodes.forEach(function (n) {
|
|
1033
1078
|
if (n instanceof HTMLElement) {
|
|
1034
1079
|
n.parentNode = _this.parentNode;
|
|
@@ -1047,7 +1092,7 @@ define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "n
|
|
|
1047
1092
|
var idx = this.parentNode.childNodes.findIndex(function (child) {
|
|
1048
1093
|
return child === _this;
|
|
1049
1094
|
});
|
|
1050
|
-
(_c = this.parentNode.childNodes).splice.apply(_c,
|
|
1095
|
+
(_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes));
|
|
1051
1096
|
p.childNodes.forEach(function (n) {
|
|
1052
1097
|
if (n instanceof HTMLElement) {
|
|
1053
1098
|
n.parentNode = _this.parentNode;
|
package/dist/nodes/comment.d.ts
CHANGED
package/dist/nodes/comment.js
CHANGED
|
@@ -7,6 +7,8 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
package/dist/nodes/html.d.ts
CHANGED
|
@@ -62,6 +62,8 @@ export default class HTMLElement extends Node {
|
|
|
62
62
|
* @return {string} text content
|
|
63
63
|
*/
|
|
64
64
|
get rawText(): string;
|
|
65
|
+
get textContent(): string;
|
|
66
|
+
set textContent(val: string);
|
|
65
67
|
/**
|
|
66
68
|
* Get unescaped text value of current node and its children.
|
|
67
69
|
* @return {string} text content
|
|
@@ -124,9 +126,11 @@ export default class HTMLElement extends Node {
|
|
|
124
126
|
get lastChild(): Node;
|
|
125
127
|
/**
|
|
126
128
|
* Get attributes
|
|
129
|
+
* @access private
|
|
127
130
|
* @return {Object} parsed and unescaped attributes
|
|
128
131
|
*/
|
|
129
|
-
get
|
|
132
|
+
get attrs(): Attributes;
|
|
133
|
+
get attributes(): Record<string, string>;
|
|
130
134
|
/**
|
|
131
135
|
* Get escaped (as-it) attributes
|
|
132
136
|
* @return {Object} parsed attributes
|
package/dist/nodes/html.js
CHANGED
|
@@ -7,24 +7,24 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
15
|
};
|
|
14
16
|
})();
|
|
15
|
-
var
|
|
16
|
-
for (var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
r[k] = a[j];
|
|
20
|
-
return r;
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
18
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
19
|
+
to[j] = from[i];
|
|
20
|
+
return to;
|
|
21
21
|
};
|
|
22
22
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.base_parse = void 0;
|
|
27
|
-
var he_1 = require("he");
|
|
27
|
+
var he_1 = __importDefault(require("he"));
|
|
28
28
|
var node_1 = __importDefault(require("./node"));
|
|
29
29
|
var type_1 = __importDefault(require("./type"));
|
|
30
30
|
var text_1 = __importDefault(require("./text"));
|
|
@@ -32,6 +32,7 @@ var matcher_1 = __importDefault(require("../matcher"));
|
|
|
32
32
|
var back_1 = __importDefault(require("../back"));
|
|
33
33
|
var comment_1 = __importDefault(require("./comment"));
|
|
34
34
|
var parse_1 = __importDefault(require("../parse"));
|
|
35
|
+
var decode = he_1.default.decode;
|
|
35
36
|
var kBlockElements = new Map();
|
|
36
37
|
kBlockElements.set('DIV', true);
|
|
37
38
|
kBlockElements.set('div', true);
|
|
@@ -156,13 +157,24 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
156
157
|
enumerable: false,
|
|
157
158
|
configurable: true
|
|
158
159
|
});
|
|
160
|
+
Object.defineProperty(HTMLElement.prototype, "textContent", {
|
|
161
|
+
get: function () {
|
|
162
|
+
return this.rawText;
|
|
163
|
+
},
|
|
164
|
+
set: function (val) {
|
|
165
|
+
var content = [new text_1.default(val)];
|
|
166
|
+
this.childNodes = content;
|
|
167
|
+
},
|
|
168
|
+
enumerable: false,
|
|
169
|
+
configurable: true
|
|
170
|
+
});
|
|
159
171
|
Object.defineProperty(HTMLElement.prototype, "text", {
|
|
160
172
|
/**
|
|
161
173
|
* Get unescaped text value of current node and its children.
|
|
162
174
|
* @return {string} text content
|
|
163
175
|
*/
|
|
164
176
|
get: function () {
|
|
165
|
-
return
|
|
177
|
+
return decode(this.rawText);
|
|
166
178
|
},
|
|
167
179
|
enumerable: false,
|
|
168
180
|
configurable: true
|
|
@@ -290,7 +302,7 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
290
302
|
function dfs(node) {
|
|
291
303
|
var idStr = node.id ? ("#" + node.id) : '';
|
|
292
304
|
var classStr = node.classNames.length ? ("." + node.classNames.join('.')) : '';
|
|
293
|
-
write(node.rawTagName + idStr + classStr);
|
|
305
|
+
write("" + node.rawTagName + idStr + classStr);
|
|
294
306
|
indention++;
|
|
295
307
|
node.childNodes.forEach(function (childNode) {
|
|
296
308
|
if (childNode.nodeType === type_1.default.ELEMENT_NODE) {
|
|
@@ -478,9 +490,10 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
478
490
|
enumerable: false,
|
|
479
491
|
configurable: true
|
|
480
492
|
});
|
|
481
|
-
Object.defineProperty(HTMLElement.prototype, "
|
|
493
|
+
Object.defineProperty(HTMLElement.prototype, "attrs", {
|
|
482
494
|
/**
|
|
483
495
|
* Get attributes
|
|
496
|
+
* @access private
|
|
484
497
|
* @return {Object} parsed and unescaped attributes
|
|
485
498
|
*/
|
|
486
499
|
get: function () {
|
|
@@ -491,13 +504,26 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
491
504
|
var attrs = this.rawAttributes;
|
|
492
505
|
for (var key in attrs) {
|
|
493
506
|
var val = attrs[key] || '';
|
|
494
|
-
this._attrs[key] =
|
|
507
|
+
this._attrs[key.toLowerCase()] = decode(val);
|
|
495
508
|
}
|
|
496
509
|
return this._attrs;
|
|
497
510
|
},
|
|
498
511
|
enumerable: false,
|
|
499
512
|
configurable: true
|
|
500
513
|
});
|
|
514
|
+
Object.defineProperty(HTMLElement.prototype, "attributes", {
|
|
515
|
+
get: function () {
|
|
516
|
+
var ret_attrs = {};
|
|
517
|
+
var attrs = this.rawAttributes;
|
|
518
|
+
for (var key in attrs) {
|
|
519
|
+
var val = attrs[key] || '';
|
|
520
|
+
ret_attrs[key] = decode(val);
|
|
521
|
+
}
|
|
522
|
+
return ret_attrs;
|
|
523
|
+
},
|
|
524
|
+
enumerable: false,
|
|
525
|
+
configurable: true
|
|
526
|
+
});
|
|
501
527
|
Object.defineProperty(HTMLElement.prototype, "rawAttributes", {
|
|
502
528
|
/**
|
|
503
529
|
* Get escaped (as-it) attributes
|
|
@@ -538,14 +564,14 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
538
564
|
}).join(' ');
|
|
539
565
|
};
|
|
540
566
|
HTMLElement.prototype.hasAttribute = function (key) {
|
|
541
|
-
return key in this.
|
|
567
|
+
return key.toLowerCase() in this.attrs;
|
|
542
568
|
};
|
|
543
569
|
/**
|
|
544
570
|
* Get an attribute
|
|
545
571
|
* @return {string} value of the attribute
|
|
546
572
|
*/
|
|
547
573
|
HTMLElement.prototype.getAttribute = function (key) {
|
|
548
|
-
return this.
|
|
574
|
+
return this.attrs[key.toLowerCase()];
|
|
549
575
|
};
|
|
550
576
|
/**
|
|
551
577
|
* Set an attribute value to the HTMLElement
|
|
@@ -556,10 +582,18 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
556
582
|
if (arguments.length < 2) {
|
|
557
583
|
throw new Error('Failed to execute \'setAttribute\' on \'Element\'');
|
|
558
584
|
}
|
|
585
|
+
var k2 = key.toLowerCase();
|
|
559
586
|
var attrs = this.rawAttributes;
|
|
587
|
+
for (var k in attrs) {
|
|
588
|
+
if (k.toLowerCase() === k2) {
|
|
589
|
+
key = k;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
560
593
|
attrs[key] = String(value);
|
|
594
|
+
// update this.attrs
|
|
561
595
|
if (this._attrs) {
|
|
562
|
-
this._attrs[
|
|
596
|
+
this._attrs[k2] = decode(attrs[key]);
|
|
563
597
|
}
|
|
564
598
|
// Update rawString
|
|
565
599
|
this.rawAttrs = Object.keys(attrs).map(function (name) {
|
|
@@ -603,7 +637,7 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
603
637
|
var idx = this.parentNode.childNodes.findIndex(function (child) {
|
|
604
638
|
return child === _this;
|
|
605
639
|
});
|
|
606
|
-
(_a = this.parentNode.childNodes).splice.apply(_a,
|
|
640
|
+
(_a = this.parentNode.childNodes).splice.apply(_a, __spreadArray([idx + 1, 0], p.childNodes));
|
|
607
641
|
p.childNodes.forEach(function (n) {
|
|
608
642
|
if (n instanceof HTMLElement) {
|
|
609
643
|
n.parentNode = _this.parentNode;
|
|
@@ -622,7 +656,7 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
622
656
|
var idx = this.parentNode.childNodes.findIndex(function (child) {
|
|
623
657
|
return child === _this;
|
|
624
658
|
});
|
|
625
|
-
(_c = this.parentNode.childNodes).splice.apply(_c,
|
|
659
|
+
(_c = this.parentNode.childNodes).splice.apply(_c, __spreadArray([idx, 0], p.childNodes));
|
|
626
660
|
p.childNodes.forEach(function (n) {
|
|
627
661
|
if (n instanceof HTMLElement) {
|
|
628
662
|
n.parentNode = _this.parentNode;
|
package/dist/nodes/node.d.ts
CHANGED
package/dist/nodes/node.js
CHANGED
|
@@ -14,6 +14,16 @@ var Node = /** @class */ (function () {
|
|
|
14
14
|
enumerable: false,
|
|
15
15
|
configurable: true
|
|
16
16
|
});
|
|
17
|
+
Object.defineProperty(Node.prototype, "textContent", {
|
|
18
|
+
get: function () {
|
|
19
|
+
return this.rawText;
|
|
20
|
+
},
|
|
21
|
+
set: function (val) {
|
|
22
|
+
this.rawText = val;
|
|
23
|
+
},
|
|
24
|
+
enumerable: false,
|
|
25
|
+
configurable: true
|
|
26
|
+
});
|
|
17
27
|
return Node;
|
|
18
28
|
}());
|
|
19
29
|
exports.default = Node;
|
package/dist/nodes/text.js
CHANGED
|
@@ -7,6 +7,8 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
7
7
|
return extendStatics(d, b);
|
|
8
8
|
};
|
|
9
9
|
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
10
12
|
extendStatics(d, b);
|
|
11
13
|
function __() { this.constructor = d; }
|
|
12
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-html-parser",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.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
|
"module": "dist/esm/index.js",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"@typescript-eslint/eslint-plugin-tslint": "latest",
|
|
39
39
|
"@typescript-eslint/parser": "latest",
|
|
40
40
|
"blanket": "latest",
|
|
41
|
+
"cheerio": "^1.0.0-rc.5",
|
|
41
42
|
"del-cli": "latest",
|
|
42
43
|
"eslint": "latest",
|
|
43
44
|
"eslint-config-prettier": "latest",
|