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 | 5x 5x 5x 24x 24x 24x 24x 24x 24x 24x 1x 23x 24x 24x 24x 36x 36x 36x 24x 24x 24x 36x 36x 1x 1x 1x 1x 35x 35x 35x 16x 11x 5x 19x 12x 7x 1x 1x 1x 1x 1x 2x 2x 1x 44x 44x 25x 19x | 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;
Iif (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);
Eif (!isBlankOrWhiteSpace(markupPart)) {
markupParts.push(markupPart);
}
}
Eif (strings !== undefined) {
markupParts.push(strings[length]); // Add the last string
}
return markupParts.join('');
}
function processMarkupPart(leftSide: string, value: any, parts: any[], eventHandlers: EventHandler[]): string {
Iif (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);
}
Iif (Array.isArray(value) && isMarkupParsingResult(value[0])) { // Recurse for every item of the array
return processMarkup(undefined, value, parts, eventHandlers);
}
Iif (isMarkupParsingResult(value)) { // Handle virtual nodes
parts.push(value);
return `${leftSide}<!---->`; // Set the placeholder
}
if (typeof value === 'object') {
if (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 = [];
if (childNodes === undefined) {
return comments;
}
childNodes.forEach(childNode => {
if (childNode.nodeType === Node.COMMENT_NODE) {
comments.push(childNode);
}
if (childNode.childNodes.length > 0) {
comments = [...comments, ...getAllComments(childNode.childNodes)];
}
});
return comments;
}
|