All files / src/dom domnode.ts

97.06% Statements 66/68
94.44% Branches 34/36
95.83% Functions 23/24
97.06% Lines 66/68
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184    1x     1x 1x 1x 1x 1x 1x     1x                     3578x 3578x 3578x 3578x 3578x 3578x 3578x 3578x   3578x 1256x         1205x       2343x 2343x       2343x 2343x 2343x   2343x       408x       8701x       241x 241x       50x 50x       109x 109x 61x   48x                 20x       10x       2x       181x 129x                 5658x 5658x 3823x       1835x             3x     7x 2x   5x                 2x     5x 1x   4x                     5x 2x   3x 3x 3x 2x     1x     5x         2343x   2343x 149x     2343x 108x     2343x    
import { Location } from '../context';
import { Token } from '../lexer';
import { DOMTokenList } from './domtokenlist';
import { MetaTable, MetaElement } from '../meta';
 
export enum NodeClosed {
	Open = 0,            /* element wasn't closed */
	EndTag = 1,          /* element closed with end tag <p>...</p> */
	VoidOmitted = 2,     /* void element with omitted end tag <input> */
	VoidSelfClosed = 3,  /* self-closed void element <input/> */
	ImplicitClosed = 4,   /* element with optional end tag <li>foo<li>bar */
}
 
export class DOMNode {
	readonly tagName: string;
	readonly attr: { [key: string]: string; };
	readonly children: Array<DOMNode>;
	readonly location: Location;
	readonly meta: MetaElement;
	readonly parent: DOMNode
	readonly voidElement: boolean;
	closed: NodeClosed;
 
	constructor(tagName: string, parent?: DOMNode, closed: NodeClosed = NodeClosed.EndTag, meta?: MetaElement, location?: Location){
		this.children = [];
		this.tagName = tagName;
		this.parent = parent;
		this.attr = {};
		this.meta = meta;
		this.closed = closed;
		this.voidElement = this.meta ? this.meta.void : false;
		this.location = location;
 
		if (parent){
			parent.children.push(this);
		}
	}
 
	static rootNode() {
		return new DOMNode(undefined, undefined);
	}
 
	static fromTokens(startToken: Token, endToken: Token, parent: DOMNode, metaTable: MetaTable){
		const tagName = startToken.data[2];
		Iif (!tagName){
			throw new Error("tagName cannot be empty");
		}
 
		const meta = metaTable ? metaTable.getMetaFor(tagName) : null;
		const open = startToken.data[1] !== '/';
		const closed = isClosed(endToken, meta);
 
		return new DOMNode(tagName, open ? parent : undefined, closed, meta, startToken.location);
	}
 
	is(tagName: string): boolean {
		return (this.tagName && tagName === '*') || this.tagName === tagName;
	}
 
	isRootElement(): boolean {
		return typeof this.tagName === 'undefined';
	}
 
	setAttribute(key: string, value: any){
		key = key.toLowerCase();
		this.attr[key] = value;
	}
 
	hasAttribute(key: string){
		key = key.toLowerCase();
		return key in this.attr;
	}
 
	getAttribute(key: string){
		key = key.toLowerCase();
		if (key in this.attr){
			return this.attr[key];
		} else {
			return null;
		}
	}
 
	append(node: DOMNode){
		this.children.push(node);
	}
 
	get classList(){
		return new DOMTokenList(this.getAttribute('class'));
	}
 
	get id(){
		return this.getAttribute('id');
	}
 
	get siblings(){
		return this.parent.children;
	}
 
	getElementsByTagName(tagName: string): Array<DOMNode> {
		return this.children.reduce(function(matches, node){
			return matches.concat(node.is(tagName) ? [node] : [], node.getElementsByTagName(tagName));
		}, []);
	}
 
	/**
	 * Visit all nodes from this node and down. Depth first.
	 */
	visitDepthFirst(callback: (node: DOMNode) => void): void {
		function visit(node: DOMNode): void {
			node.children.forEach(visit);
			if (!node.isRootElement()){
				callback(node);
			}
		}
 
		visit(this);
	}
 
	/**
	 * Evaluates callbackk on all descendants, returning true if any are true.
	 */
	someChildren(callback: (node: DOMNode) => boolean){
		return this.children.some(visit);
 
		function visit(node: DOMNode): boolean {
			if (callback(node)){
				return true;
			} else {
				return node.children.some(visit);
			}
		}
	}
 
	/**
	 * Evaluates callbackk on all descendants, returning true if all are true.
	 */
	everyChildren(callback: (node: DOMNode) => boolean){
		return this.children.every(visit);
 
		function visit(node: DOMNode): boolean {
			if (!callback(node)){
				return false;
			}
			return node.children.every(visit);
		}
	}
 
	/**
	 * Visit all nodes from this node and down. Breadth first.
	 *
	 * The first node for which the callback evaluates to true is returned.
	 */
	find(callback: (node: DOMNode) => boolean): DOMNode {
		function visit(node: DOMNode): DOMNode {
			if (callback(node)){
				return node;
			}
			for (const child of node.children){
				const match = child.find(callback);
				if (match) {
					return match;
				}
			}
			return null;
		}
 
		return visit(this);
	}
}
 
function isClosed(endToken: Token, meta: MetaElement): NodeClosed {
	let closed = NodeClosed.Open;
 
	if (meta && meta.void){
		closed = NodeClosed.VoidOmitted;
	}
 
	if (endToken.data[0] === '/>'){
		closed = NodeClosed.VoidSelfClosed;
	}
 
	return closed;
}