forked from ashi009/node-fast-html-parser
-
Notifications
You must be signed in to change notification settings - Fork 111
/
node.ts
51 lines (50 loc) · 1.16 KB
/
node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { decode, encode } from 'he';
import NodeType from './type';
import HTMLElement from './html';
/**
* Node Class as base class for TextNode and HTMLElement.
*/
export default abstract class Node {
abstract rawTagName: string;
abstract nodeType: NodeType;
public childNodes = [] as Node[];
public range: readonly [number, number];
abstract text: string;
abstract rawText: string;
// abstract get rawText(): string;
abstract toString(): string;
abstract clone(): Node;
public constructor(
public parentNode = null as HTMLElement | null,
range?: [number, number]
) {
Object.defineProperty(this, 'range', {
enumerable: false,
writable: true,
configurable: true,
value: range ?? [-1, -1]
});
}
/**
* Remove current node
*/
public remove() {
if (this.parentNode) {
const children = this.parentNode.childNodes;
this.parentNode.childNodes = children.filter((child) => {
return this !== child;
});
this.parentNode = null;
}
return this;
}
public get innerText() {
return this.rawText;
}
public get textContent() {
return decode(this.rawText);
}
public set textContent(val: string) {
this.rawText = encode(val);
}
}