|
|
|
|
@ -1,29 +1,63 @@
|
|
|
|
|
import Attribute from '../Attribute';
|
|
|
|
|
import Component from '../../Component';
|
|
|
|
|
import { INode } from '../interfaces';
|
|
|
|
|
import Text from '../Text';
|
|
|
|
|
import { TemplateNode } from '../../../interfaces';
|
|
|
|
|
|
|
|
|
|
export default class Node {
|
|
|
|
|
readonly start: number;
|
|
|
|
|
readonly end: number;
|
|
|
|
|
readonly component: Component;
|
|
|
|
|
readonly parent: INode;
|
|
|
|
|
readonly type: string;
|
|
|
|
|
|
|
|
|
|
prev?: INode;
|
|
|
|
|
next?: INode;
|
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @type {number}
|
|
|
|
|
*/
|
|
|
|
|
start;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @type {number}
|
|
|
|
|
*/
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @type {import('../../Component.js').default}
|
|
|
|
|
*/
|
|
|
|
|
component;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @type {import('../interfaces.js').INode}
|
|
|
|
|
*/
|
|
|
|
|
parent;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @type {string}
|
|
|
|
|
*/
|
|
|
|
|
type;
|
|
|
|
|
|
|
|
|
|
/** @type {import('../interfaces.js').INode} */
|
|
|
|
|
prev;
|
|
|
|
|
|
|
|
|
|
can_use_innerhtml: boolean;
|
|
|
|
|
is_static_content: boolean;
|
|
|
|
|
var: string;
|
|
|
|
|
attributes: Attribute[];
|
|
|
|
|
/** @type {import('../interfaces.js').INode} */
|
|
|
|
|
next;
|
|
|
|
|
|
|
|
|
|
constructor(component: Component, parent: Node, _scope, info: TemplateNode) {
|
|
|
|
|
/** @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 undefined
|
|
|
|
|
*/
|
|
|
|
|
constructor(component, parent, _scope, info) {
|
|
|
|
|
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, {
|
|
|
|
|
@ -34,48 +68,55 @@ export default class Node {
|
|
|
|
|
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();
|
|
|
|
|
if (this.parent)
|
|
|
|
|
this.parent.cannot_use_innerhtml();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
not_static_content() {
|
|
|
|
|
this.is_static_content = false;
|
|
|
|
|
if (this.parent) this.parent.not_static_content();
|
|
|
|
|
if (this.parent)
|
|
|
|
|
this.parent.not_static_content();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
find_nearest(selector: RegExp) {
|
|
|
|
|
if (selector.test(this.type)) return this;
|
|
|
|
|
if (this.parent) return this.parent.find_nearest(selector);
|
|
|
|
|
/** @param {RegExp} selector */
|
|
|
|
|
find_nearest(selector) {
|
|
|
|
|
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 &&
|
|
|
|
|
/** @param {string} name */
|
|
|
|
|
get_static_attribute_value(name) {
|
|
|
|
|
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 '';
|
|
|
|
|
|
|
|
|
|
/** @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 (attribute.chunks[0] as Text).data;
|
|
|
|
|
return ( /** @type {import('../Text.js').default} */(attribute.chunks[0])).data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has_ancestor(type: string) {
|
|
|
|
|
/** @param {string} type */
|
|
|
|
|
has_ancestor(type) {
|
|
|
|
|
return this.parent ? this.parent.type === type || this.parent.has_ancestor(type) : false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|