-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathNode.ts
71 lines (58 loc) · 1.67 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Attribute from '../Attribute';
import Component from '../../Component';
import { INode } from '../interfaces';
import Text from '../Text';
export default class Node {
readonly start: number;
readonly end: number;
readonly component: Component;
readonly parent: INode;
readonly type: string;
prev?: INode;
next?: INode;
can_use_innerhtml: boolean;
var: string;
attributes: Attribute[];
constructor(component: Component, parent, _scope, info: any) {
this.start = info.start;
this.end = info.end;
this.type = info.type;
// this makes properties non-enumerable, which makes logging
// bearable. might have a performance cost. TODO remove in prod?
Object.defineProperties(this, {
component: {
value: component
},
parent: {
value: parent
}
});
}
cannot_use_innerhtml() {
if (this.can_use_innerhtml !== false) {
this.can_use_innerhtml = false;
if (this.parent) this.parent.cannot_use_innerhtml();
}
}
find_nearest(selector: RegExp) {
if (selector.test(this.type)) return this;
if (this.parent) return this.parent.find_nearest(selector);
}
get_static_attribute_value(name: string) {
const attribute = this.attributes && this.attributes.find(
(attr: Attribute) => attr.type === 'Attribute' && attr.name.toLowerCase() === name
);
if (!attribute) return null;
if (attribute.is_true) return true;
if (attribute.chunks.length === 0) return '';
if (attribute.chunks.length === 1 && attribute.chunks[0].type === 'Text') {
return (attribute.chunks[0] as Text).data;
}
return null;
}
has_ancestor(type: string) {
return this.parent ?
this.parent.type === type || this.parent.has_ancestor(type) :
false;
}
}