Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 10x 10x 10x 51x 51x 51x 51x 51x 51x 3x 3x 7x 7x 7x 51x 25x 51x 53x 53x 53x 122x 122x 85x 53x 51x 53x 122x 14x 108x 25x 25x 25x 25x 83x 2x 81x 7x 7x 74x 16x 16x 58x 36x 22x 39x 39x 39x 25x 25x 315x 315x 25x 89x 89x 64x 25x 3x 3x 3x 11x 7x 11x 3x | import { EventHandler } from "./interfaces";
//import ElementNode from "./nodes/ElementNode";
import MarkupParsingResult from "./MarkupParsingResult";
import markupToVirtualNode from "./markupToVirtualNode";
import { isBlankOrWhiteSpace } from "../utils/string";
/**
* Template tag to generate the virtual node from the string
*/
export default function html(strings: TemplateStringsArray, ...values: any): MarkupParsingResult {
const parts: MarkupParsingResult[] = [];
const eventHandlers: EventHandler[] = [];
const markup = processMarkup(strings, values, parts, eventHandlers);
const result = markupToVirtualNode(markup, 'html', { excludeTextWithWhiteSpacesOnly: true });
let node = result.node as Node;
//const vnode = result.vnode as ElementNode;
if (parts.length > 0) {
// const comments = node.childNodes !== undefined ?
// Array.from(node.childNodes)
// .filter(n => n.nodeType === Node.COMMENT_NODE) :
// [];
const comments = getAllComments(node.childNodes);
parts.forEach((part, i) => {
const partNode = part.node as Node;
// Add as a child of the vnode
//vnode.children.push(part.vnode as any);
// const comment = comments.length > i ?
// comments[i] :
// undefined;
// if (comment !== undefined) { // Prepend its DOM node to the next comment placeholder of the dom node
// node.insertBefore(partNode, comment);
// }
// else { // Add the part and the marker
// node.insertBefore(partNode, null);
// node.insertBefore(new Comment(), null);
// }
const comment = comments[i];
comment.parentNode.insertBefore(partNode, comment);
});
}
if (eventHandlers.length > 0) {
eventHandlers.forEach(eh => node.addEventListener(eh.name, eh.handler));
}
return result;
}
function processMarkup(strings: TemplateStringsArray, values: any, parts: any[], eventHandlers: EventHandler[]) {
const markupParts: string[] = [];
const length = values.length;
for (let i = 0; i < length; ++i) {
const markupPart = processMarkupPart(
strings !== undefined ? strings[i] : '',
values[i],
parts,
eventHandlers);
if (!isBlankOrWhiteSpace(markupPart)) {
markupParts.push(markupPart);
}
}
if (strings !== undefined) {
markupParts.push(strings[length]); // Add the last string
}
return markupParts.join('');
}
function processMarkupPart(leftSide: string, value: any, parts: any[], eventHandlers: EventHandler[]): string {
if (value === undefined ||
value === null ||
value === '') {
return removeRightMember(leftSide);
}
if (typeof value === 'function') {
const fcnName = getFunctionName(leftSide);
Eif (fcnName !== null) { // Add an event handler
eventHandlers.push({
name: fcnName.replace('on', ''),
handler: value
});
}
else {
throw Error('Not implemented');
}
return removeRightMember(leftSide);
}
if (Array.isArray(value) && isMarkupParsingResult(value[0])) { // Recurse for every item of the array
return processMarkup(undefined, value, parts, eventHandlers);
}
if (isMarkupParsingResult(value)) { // Handle virtual nodes
parts.push(value);
return `${leftSide}<!---->`; // Set the placeholder
}
if (typeof value === 'object') {
Eif (leftSide.endsWith('=')) { // It is an attribute
return `${leftSide}'${JSON.stringify(value)}'`;
}
else { // It is a text node
return JSON.stringify(value); // Show the raw data
}
}
// A primitive type
if (leftSide.endsWith('=')) { // It is an attribute
return `${leftSide}"${value}"`;
}
else { // It is a text node
return `${leftSide}${value}`;
}
}
/**
* Removes the rightmost member of a string
* Assumes that it always has the = operator
*/
function removeRightMember(str: string): string {
let lastSpace = str.lastIndexOf(' ');
Iif (lastSpace == -1) {
lastSpace = 0;
}
return str.substring(lastSpace, str.lastIndexOf(str));
}
function getFunctionName(leftSide: string): string | null {
const parts = leftSide.split(' ');
for (let i = 0; i < parts.length; ++i) {
const functionName = parts[i].trim().toLocaleLowerCase();
if (functionName[0] === 'o' && functionName[1] === 'n') {
return functionName.replace('=', '');
}
}
return null;
}
function isMarkupParsingResult(value: any) {
const type = typeof value;
if (type === 'string' ||
type === 'number') {
return false;
}
return (/*'vnode' in value &&*/
'node' in value);
}
function getAllComments(childNodes: NodeListOf<ChildNode>) : Comment[] {
let comments = [];
Iif (childNodes === undefined) {
return comments;
}
childNodes.forEach(childNode => {
if (childNode.nodeType === Node.COMMENT_NODE) {
comments.push(childNode);
}
Iif (childNode.childNodes.length > 0) {
comments = [...comments, ...getAllComments(childNode.childNodes)];
}
});
return comments;
}
|