All files / lib/pipeline/module standardHtmlFormatter.ts

99.03% Statements 102/103
96.67% Branches 58/60
100% Functions 15/15
99.01% Lines 100/101

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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 37426x                         26x                     105x         78x   69x 57x       69x 69x       69x           30x 9x       21x       57x 4x         69x 6x         203x   203x   491x 69x 69x       422x     422x     422x     422x   422x   219x     134x       219x 219x   170x     49x 49x 49x       203x             422x 2x     420x     420x     420x 292x 65x   227x       128x     128x   4x 8x       4x     4x       128x 128x 128x 180x       128x           146x         420x   69x     69x     69x 66x       69x 66x     69x   351x         422x   422x 422x   225x     225x     225x         225x     225x       422x       422x   356x     66x     66x   66x         422x   422x 581x     422x               26x       26x         26x         26x                                                                             26x                         26x                       26x                                 26x 105x   75x   30x                   26x 13x   4x   2x   2x   4x   1x    
import {
    DocumentNode,
    HtmlFormatter,
    Node,
    NodeType,
    NodeWithChildren,
    TextNode
} from '../..';
 
/**
 * Standard HTML formatter.
 * Implements minification, basic pretty formatting, and bypass modes.
 */
export class StandardHtmlFormatter implements HtmlFormatter {
    readonly options: FormatterOptions;
 
    /**
     * Create a new StandardHtmlFormatter.
     * Configuration options can be passed through the {@link options} parameter.
     * If a partial options object is provided, then the missing options will be inherited from {@link NoneFormatterPreset}.
     * 
     * @param options Configuration options for the formatter
     */
    constructor(options?: Partial<FormatterOptions>) {
        this.options = createFormatterOptions(options);
    }
 
    formatDom(dom: DocumentNode): void {
        // bypass if formatting is disabled
        if (this.options.formatMode !== FormatterMode.NONE) {
            // strip comments, if enabled
            if (this.options.stripComments) {
                this.stripComments(dom);
            }
 
            // strip CDATA, if enabled
            Eif (this.options.stripCDATA) {
                this.stripCDATA(dom);
            }
 
            // format whitespace
            this.formatWhitespace(dom);
        }
    }
 
    formatHtml(html: string): string {
        // bypass if no formatting enabled
        if (this.options.formatMode === FormatterMode.NONE) {
            return html;
        }
 
        // trim to clean up any leading or trailing whitespace
        return html.trim();
    }
 
    protected stripComments(root: NodeWithChildren): void {
        for (const commentNode of root.findChildNodesByNodeType(NodeType.Comment)) {
            commentNode.removeSelf();
        }
    }
 
    protected stripCDATA(root: NodeWithChildren): void {
        for (const cdataNode of root.findChildNodesByNodeType(NodeType.CDATA)) {
            cdataNode.removeSelf();
        }
    }
 
    protected formatWhitespace(startNode: Node, depth = 0): void {
        let currentNode: Node | null = startNode;
 
        while (currentNode != null) {
            // skip to first child of document
            if (DocumentNode.isDocumentNode(currentNode)) {
                currentNode = currentNode.firstChild;
                continue;
            }
 
            // get content node (if there is one)
            const contentNode: Node | null = StandardHtmlFormatter.getContentNode(currentNode);
 
            // get preceding text node, or insert it if possible
            const textNode: TextNode = StandardHtmlFormatter.getOrInsertTextNode(currentNode);
            
            // absorb adjacent text nodes
            StandardHtmlFormatter.absorbAdjacentTextNodes(textNode);
    
            // process text node
            this.formatTextNode(textNode, depth);
 
            if (contentNode != null) {
                // process children of content node
                if (NodeWithChildren.isNodeWithChildren(contentNode) && contentNode.firstChild != null) {
 
                    // no need to loop through children, child will process its own neighbors
                    this.formatWhitespace(contentNode.firstChild, depth + 1);
                }
 
                // pick next node
                const nextNode = contentNode.nextSibling;
                if (nextNode != null) {
                    // continue to next sibling, if found
                    currentNode = nextNode;
                } else {
                    // If this is the last node at this level, insert a final text node and iterate one more time to support closing indentation
                    const newLastNode = new TextNode();
                    contentNode.appendSibling(newLastNode);
                    currentNode = newLastNode;
                }
            } else {
                // if this is the last node and it IS a text node, then we are done
                currentNode = null;
            }
        }
    }
 
    private formatTextNode(textNode: TextNode, depth: number): void {
        // if this text node is whitespace-sensitive, then skip formatting
        if (textNode.isWhitespaceSensitive) {
            return;
        }
 
        const isMinimizeMode = this.options.formatMode === FormatterMode.MINIMIZED;
 
        // extract actual text from node
        const textContent: string | null = StandardHtmlFormatter.extractTextContent(textNode, isMinimizeMode);
 
        // if this is inline text (or we are in ugly mode), then dont format
        if (isMinimizeMode || StandardHtmlFormatter.isInlineText(textNode)) {
            if (textContent != null) {
                textNode.text = textContent;
            } else {
                textNode.removeSelf();
            }
        } else {
            // start text with newline to close previous line
            const text = [ this.options.endOfLine ];
    
            // append text content, if any
            if (textContent != null) {
                // append content index
                for (let i = 0; i < depth; i++) {
                    text.push(this.options.indentString);
                }
    
                // append text content
                text.push(textContent);
    
                // close out content line
                text.push(this.options.endOfLine);
            }
 
            // append closing indent if there is a following node
            const hasNeighbor: boolean = textNode.nextSibling != null;
            const closingIndentDepth: number = hasNeighbor ? depth : depth - 1;
            for (let i = 0; i < closingIndentDepth; i++) {
                text.push(this.options.indentString);
            }
    
            // set node content
            textNode.text = text.join('');
        }
    }
 
    private static isInlineText(textNode: TextNode): boolean {
        // inline text is a text node with no siblings, and no more than one line of non-whitespace content
        return textNode.prevSibling == null && textNode.nextSibling == null && /^\s*[^\r\n]+\s*$/g.test(textNode.text);
    }
 
    private static extractTextContent(textNode: TextNode, isMinimizeMode: boolean): string | null {
        // check if node has text and text is non-empty
        if (textNode.hasContent) {
            // use trimmed text content only if we are not in minimize mode
            const textContent = isMinimizeMode ? textNode.text : textNode.textContent;
 
            // compact whitespace in text
            let compactText = textContent.replace(/\s{2,}|\t|\r|\n/gm, ' ');
 
            // if this is after an opening tag, then trim the front
            if (textNode.prevSibling == null) {
                compactText = compactText.replace(/^\s+/g, '');
            }
 
            // if this is before a closing tag, then trim the end
            if (textNode.nextSibling == null) {
                compactText = compactText.replace(/\s+$/g, '');
            }
 
            return compactText;
        } else {
            return null;
        }
    }
 
    private static absorbAdjacentTextNodes(textNode: TextNode): void {
        const newTextParts = [ textNode.text ];
 
        let currentNode: Node | null = textNode.nextSibling;
        while (currentNode != null && TextNode.isTextNode(currentNode)) {
            // save next node, since we might remove this one
            const nextNode = currentNode.nextSibling;
 
            // steal its text
            newTextParts.push(currentNode.text);
 
            // if next node is white-space sensitive, then the merged text is too
            Iif (currentNode.isWhitespaceSensitive) {
                textNode.isWhitespaceSensitive = true;
            }
 
            // remove stolen node
            currentNode.removeSelf();
 
            // increment to next node
            currentNode = nextNode;
        }
 
        // combine string parts and assign to node
        textNode.text = newTextParts.join('');
    }
 
    private static getOrInsertTextNode(startNode: Node): TextNode {
        if (TextNode.isTextNode(startNode)) {
            // start node is text node
            return startNode;
        } else {
            // create new text node
            const textNode: TextNode = new TextNode();
            
            // insert before startNode
            startNode.prependSibling(textNode);
 
            return textNode;
        }
    }
 
    private static getContentNode(startNode: Node): Node | null {
        let currNode: Node | null = startNode;
 
        while (currNode != null && TextNode.isTextNode(currNode)) {
            currNode = currNode.nextSibling;
        }
 
        return currNode;
    }
}
 
/**
 * Formatting styles for the standard HtmlFormatter.
 * These do not have to be respected by subclasses
 */
export enum FormatterMode {
    /**
     * Pretty mode - will format HTML for human readability
     */
    PRETTY = 'pretty',
 
    /**
     * Minimized / "ugly" mode - compacts HTML to single line
     */
    MINIMIZED = 'minimized',
 
    /**
     * None / disabled mode - do not modify the HTML
     */
    NONE = 'none'
}
 
/**
 * Configuration options to modify the behavior of {@link StandardHtmlFormatter}
 */
export interface FormatterOptions {
    /**
     * General formatter mode
     */
    readonly formatMode: FormatterMode;
 
    /**
     * String to use as a line terminator.
     */
    readonly endOfLine: string;
 
    /**
     * String to use as indentation.
     * This string will be repeated once for each level of indentation.
     */
    readonly indentString: string;
 
    /**
     * If true, XML comments will be removed
     */
    readonly stripComments: boolean;
 
    /**
     * If true, CDATA sections will be removed
     */
    readonly stripCDATA: boolean;
}
 
 
/**
 * Formatter preset that disables all processing.
 * Input DOM and HTML will be ignore and unchanged.
 */
export const NoneFormatterPreset: FormatterOptions = {
    formatMode: FormatterMode.NONE,
    indentString: '',
    endOfLine: '',
    stripComments: false,
    stripCDATA: false
};
 
/**
 * Formatter preset that attempts to "prettify" the generated HTML.
 * This mode applies formatting and indentation, and strips CDATA sections.
 * HTML comments are left in place.
 */
export const PrettyFormatterPreset: FormatterOptions = {
    formatMode: FormatterMode.PRETTY,
    indentString: '    ',
    endOfLine: '\n',
    stripComments: false,
    stripCDATA: true
};
 
/**
 * Formatter preset that minimizes the generated HTML.
 * This mode compacts whitespace, strips comments and CDATA, and packs all markup onto a single line.
 */
export const MinimizedFormatterPreset: FormatterOptions = {
    formatMode: FormatterMode.MINIMIZED,
    indentString: '',
    endOfLine: '',
    stripComments: true,
    stripCDATA: true
};
 
/**
 * Creates a complete {@link FormatterOptions} object from a partial object and a default object.
 * If {@link defaultOptions} is not specified, {@link NoneFormatterPreset} will be used.
 * If {@link options} is not specified, then {@link defaultOptions} will be used directly.
 *
 * @param defaultOptions {@link FormatterOptions} object to pull default values from
 * @param options Partial {@link FormatterOptions} object to override {@link defaultOptions}
 * @returns returns a complete {@link FormatterOptions} object created from {@link defaultOptions} and {@link options}
 */
export function createFormatterOptions(options?: Partial<FormatterOptions>, defaultOptions = NoneFormatterPreset): FormatterOptions {
    if (options !== undefined) {
        // inherit from default options and assign any provided properties
        return Object.assign(Object.create(defaultOptions), options) as FormatterOptions;
    } else {
        return defaultOptions;
    }
}
 
/**
 * Creates an HtmlFormatter, optionally using a standard profile name
 * @internal
 * @param formatterName Name of the formatter profile to use
 * @returns an HtmlFormatter instance configured from formatterName
 */
export function createStandardHtmlFormatter(formatterName?: string): HtmlFormatter {
    switch (formatterName) {
        case FormatterMode.PRETTY:
            return new StandardHtmlFormatter(PrettyFormatterPreset);
        case FormatterMode.MINIMIZED:
            return new StandardHtmlFormatter(MinimizedFormatterPreset);
        case FormatterMode.NONE:
            return new StandardHtmlFormatter(NoneFormatterPreset);
        case undefined:
            return new StandardHtmlFormatter();
        default:
            throw new Error(`Unknown HTML formatter: ${ formatterName }`);
    }
}