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 | 26x 26x 1625x 29x 1596x 9x 29x 13x 13x 16x 29x 13x 13x 4x 4x 4x 9x 9x 9x 11x 11x 9x 9x 9x 5x 4x 4x 5x 5x 5x 4x 4x 7x 7x 5x 4x 4x 3x 3x 3x 3x 4x 4x 4x 11x 11x 11x 8x 11x 11x 11x | import {HtmlCompilerModule, HtmlCompilerContext} from '../htmlCompiler';
import { MIfNode, MElseIfNode, MElseNode, MForNode, ConditionalNode, DocumentNode, MForOfNode, MForInNode, MScopeNode } from '../../..';
/**
* Process dom logic: m-if, m-for, etc.
*/
export class DomLogicModule implements HtmlCompilerModule {
enterNode(htmlContext: HtmlCompilerContext): void {
if (MIfNode.isMIfNode(htmlContext.node) || MElseIfNode.isMElseIfNode(htmlContext.node) || MElseNode.isMElseNode(htmlContext.node)) {
// process conditional nodes
compileConditional(htmlContext.node, htmlContext);
} else if (MForNode.isMForNode(htmlContext.node)) {
// process m-for nodes
compileMFor(htmlContext.node, htmlContext);
}
}
}
function compileConditional(conditional: ConditionalNode, htmlContext: HtmlCompilerContext): void {
// if conditional is true, then delete rest of conditional train and promote children
if (conditional.isTruthy) {
removeFollowingConditionals(conditional);
// delete self, but keep children because this is true
conditional.removeSelf(true);
} else {
// if not true, then delete
conditional.removeSelf(false);
}
// mark as deleted
htmlContext.setDeleted();
}
function removeFollowingConditionals(trueConditional: ConditionalNode): void {
let currentConditional: ConditionalNode | null = trueConditional.nextConditional;
while (currentConditional != null) {
// save next in case we remove this one
const nextConditional: ConditionalNode | null = currentConditional.nextConditional;
// remove conditional
currentConditional.removeSelf();
// move on to text
currentConditional = nextConditional;
}
}
function compileMFor(mFor: MForNode, htmlContext: HtmlCompilerContext): void {
// extract contents
const forContents: DocumentNode = mFor.createDomFromChildren();
// generate iteration data for the loop. This will be in order.
const iterations: MForIteration[] = evaluateMFor(mFor);
// append iterations
// This has to go in reverse, since we are effectively inserting at the head of a linked list each time
for (let i = iterations.length - 1; i >= 0; i--) {
const iteration = iterations[i];
compileIteration(mFor, iteration, forContents);
}
// remove m-for after processing
mFor.removeSelf();
htmlContext.setDeleted();
}
function evaluateMFor(mFor: MForNode): MForIteration[] {
if (MForOfNode.isMForOfNode(mFor)) {
return evaluateForOf(mFor.expression);
} else Eif (MForInNode.isMForInNode(mFor)) {
return evaluateForIn(mFor.expression);
} else {
throw new Error('m-for node is neither a for...of nor a for...in loop');
}
}
function evaluateForOf(ofValue: unknown): MForIteration[] {
const iterations: MForIteration[] = [];
// get the compiled of expression as an array
const arrayValue = ofValue;
// make sure that it actually is an array
if (arrayValue !== undefined && Array.isArray(arrayValue)) {
let index = 0;
for (const value of arrayValue as unknown[]) {
iterations.push({
value: value,
index: index
});
index++;
}
}
return iterations;
}
function evaluateForIn(inValue: unknown): MForIteration[] {
const iterations: MForIteration[] = [];
// make sure that it actually is an object
if (inValue !== undefined && typeof inValue === 'object') {
const inObj = inValue as Record<string, unknown>;
const inValues = Object.keys(inObj);
let index = 0;
for (const value of inValues) {
iterations.push({
value: value,
index: index
});
index++;
}
}
return iterations;
}
function compileIteration(mFor: MForNode, iteration: MForIteration, forContents: DocumentNode): void {
// create scope
const mScope = new MScopeNode();
// bind value var
mScope.setRawAttribute(mFor.varName, iteration.value);
// bind index var, if included
if (mFor.indexName !== undefined) {
mScope.setRawAttribute(mFor.indexName, iteration.index);
}
// append copy of children
const forContentsClone = forContents.clone(true);
mScope.appendChildren(forContentsClone.childNodes);
// append to m-for node
mFor.appendSibling(mScope);
}
interface MForIteration {
value: unknown;
index: number;
}
|