All files / lib/pipeline/module/compiler whitespaceModule.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 2/2
100% Lines 14/14

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        26x               26x           26x     1531x     6x 6x     6x                         9x         16x     15x     15x 12x       15x 3x        
import {
    HtmlCompilerContext,
    HtmlCompilerModule
} from '../htmlCompiler';
import {
    MWhitespaceNode,
    MWhitespaceNodeMode,
    Node,
    NodeWithChildren,
    NodeWithText
} from '../../..';
 
const NODE_TAG_IS_WHITESPACE_PROCESSED = 'WhitespaceModule.IsWhitespaceProcessed';
 
/**
 * Compiler module that process whitespace-sensitivity.
 * This module handles <m-whitespace> by setting isWhitespaceSensitive on all child nodes
 */
export class WhitespaceModule implements HtmlCompilerModule {
    exitNode(htmlContext: HtmlCompilerContext): void {
        // if this is an m-whitespace node, then compile
        if (MWhitespaceNode.isMWhitespaceNode(htmlContext.node)) {
 
            // apply whitespace settings to all children
            const isWhitespaceSensitive = (htmlContext.node.mode === MWhitespaceNodeMode.SENSITIVE);
            applyWhitespaceSettings(isWhitespaceSensitive, htmlContext.node.childNodes);
 
            // remove node and promote children
            htmlContext.node.removeSelf(true);
        }
    }
}
 
/**
 * Applies whitespace sensitivity settings to one or more nodes.
 * This function will also tag each processed node as having been processed for whitespace, and will skip nodes that have already been tagged.
 * @param isWhitespaceSensitive If the nodes should be set to
 * @param nodes Nodes to processes, can safely be empty.
 */
function applyWhitespaceSettings(isWhitespaceSensitive: boolean, nodes: Node[]): void {
    // process all nodes
    for (const node of nodes) {
 
        // Skip nodes that have already been processed for whitespace.
        // This is necessary to support nesting and overriding of <m-whitespace>.
        // Otherwise a parent m-whitespace would overwrite the lower-level settings.
        if (!node.nodeTags.has(NODE_TAG_IS_WHITESPACE_PROCESSED)) {
 
            // Mark as processed
            node.nodeTags.add(NODE_TAG_IS_WHITESPACE_PROCESSED);
 
            // if node is a text node, then set whitespace sensitivity
            if (NodeWithText.isNodeWithText(node)) {
                node.isWhitespaceSensitive = isWhitespaceSensitive;
            }
 
            // process child nodes, if any
            if (NodeWithChildren.isNodeWithChildren(node)) {
                applyWhitespaceSettings(isWhitespaceSensitive, node.childNodes);
            }
        }
    }
}