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 | 26x 26x 1685x 1039x 646x 530x 1039x 1039x 1039x 530x 530x 515x 515x 515x 474x 474x | import {HtmlCompilerContext, HtmlCompilerModule} from '../htmlCompiler';
import {TagNode, TextNode} from '../../..';
/**
* Compile module that detects and evaluates embedded JS expressions in attributes and text nodes
*/
export class ExpressionModule implements HtmlCompilerModule {
async enterNode(htmlContext: HtmlCompilerContext): Promise<void> {
if (TextNode.isTextNode(htmlContext.node)) {
await processTextNode(htmlContext, htmlContext.node);
} else if (TagNode.isTagNode(htmlContext.node)) {
await processTagNode(htmlContext, htmlContext.node);
}
}
}
async function processTextNode(htmlContext: HtmlCompilerContext, node: TextNode): Promise<void> {
// create eval context from the current scope
const evalContext = htmlContext.createEvalContext();
// compile text
const textValue = await htmlContext.pipelineContext.pipeline.compileExpression(node.text, evalContext);
// save back to node
node.text = (textValue !== null && textValue !== undefined) ? String(textValue) : '';
}
async function processTagNode(htmlContext: HtmlCompilerContext, node: TagNode): Promise<void> {
// create eval context from the current scope
const evalContext = htmlContext.createEvalContext();
// loop through each attribute and compile it
for (const entry of Array.from(node.getAttributes().entries())) {
const key = entry[0];
const value = entry[1];
// only process strings, anything else must already be compiled
if (typeof value === 'string') {
// compile the value, preserving the raw output and not converting to a string
const result = htmlContext.pipelineContext.pipeline.compileExpression(value, evalContext);
// Set attribute
node.setRawAttribute(key, await result);
}
}
} |