File size: 2,538 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * @template {string} Type
 * @template {import('../interfaces.js').INode} [Parent=import('../interfaces.js').INode]
 */
export default class Node {
	/**
	 * @readonly
	 * @type {number}
	 */
	start;

	/**
	 * @readonly
	 * @type {number}
	 */
	end;

	/**
	 * @readonly
	 * @type {import('../../Component.js').default}
	 */
	component;

	/**
	 * @readonly
	 * @type {Parent}
	 */
	parent;

	/**
	 * @readonly
	 * @type {Type}
	 */
	type;

	/** @type {import('../interfaces.js').INode} */
	prev;

	/** @type {import('../interfaces.js').INode} */
	next;

	/** @type {boolean} */
	can_use_innerhtml;

	/** @type {boolean} */
	is_static_content;

	/** @type {string} */
	var;

	/** @type {import('../Attribute.js').default[]} */
	attributes = [];

	/**
	 * @param {import('../../Component.js').default} component
	 * @param {Node} parent
	 * @param {any} _scope
	 * @param {import('../../../interfaces.js').TemplateNode} info
	 */
	constructor(component, parent, _scope, info) {
		this.start = info.start;
		this.end = info.end;
		this.type = /** @type {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
			}
		});
		this.can_use_innerhtml = true;
		this.is_static_content = true;
	}
	cannot_use_innerhtml() {
		if (this.can_use_innerhtml !== false) {
			this.can_use_innerhtml = false;
			if (this.parent) this.parent.cannot_use_innerhtml();
		}
	}
	not_static_content() {
		this.is_static_content = false;
		if (this.parent) this.parent.not_static_content();
	}

	/** @param {RegExp} selector */
	find_nearest(selector) {
		if (selector.test(this.type)) return this;
		if (this.parent) return this.parent.find_nearest(selector);
	}

	/** @param {string} name */
	get_static_attribute_value(name) {
		const attribute = this.attributes.find(
			/** @param {import('../Attribute.js').default} attr */
			(attr) => 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 /** @type {import('../Text.js').default} */ (attribute.chunks[0]).data;
		}
		return null;
	}

	/** @param {string} type */
	has_ancestor(type) {
		return this.parent ? this.parent.type === type || this.parent.has_ancestor(type) : false;
	}
}