"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.addBodyClass = exports.getHtmlHeadTagElement = exports.appendHtmlElementToHead = void 0; const schematics_1 = require("@angular-devkit/schematics"); const parse5_element_1 = require("./parse5-element"); const parse5_1 = require("parse5"); /** Appends the given element HTML fragment to the `` element of the specified HTML file. */ function appendHtmlElementToHead(host, htmlFilePath, elementHtml) { const htmlFileBuffer = host.read(htmlFilePath); if (!htmlFileBuffer) { throw new schematics_1.SchematicsException(`Could not read file for path: ${htmlFilePath}`); } const htmlContent = htmlFileBuffer.toString(); if (htmlContent.includes(elementHtml)) { return; } const headTag = getHtmlHeadTagElement(htmlContent); if (!headTag) { throw Error(`Could not find '' element in HTML file: ${htmlFileBuffer}`); } // We always have access to the source code location here because the `getHeadTagElement` // function explicitly has the `sourceCodeLocationInfo` option enabled. const endTagOffset = headTag.sourceCodeLocation.endTag.startOffset; const indentationOffset = (0, parse5_element_1.getChildElementIndentation)(headTag); const insertion = `${' '.repeat(indentationOffset)}${elementHtml}`; const recordedChange = host.beginUpdate(htmlFilePath).insertRight(endTagOffset, `${insertion}\n`); host.commitUpdate(recordedChange); } exports.appendHtmlElementToHead = appendHtmlElementToHead; /** Parses the given HTML file and returns the head element if available. */ function getHtmlHeadTagElement(htmlContent) { return getElementByTagName('head', htmlContent); } exports.getHtmlHeadTagElement = getHtmlHeadTagElement; /** Adds a class to the body of the document. */ function addBodyClass(host, htmlFilePath, className) { const htmlFileBuffer = host.read(htmlFilePath); if (!htmlFileBuffer) { throw new schematics_1.SchematicsException(`Could not read file for path: ${htmlFilePath}`); } const htmlContent = htmlFileBuffer.toString(); const body = getElementByTagName('body', htmlContent); if (!body) { throw Error(`Could not find element in HTML file: ${htmlFileBuffer}`); } const classAttribute = body.attrs.find(attribute => attribute.name === 'class'); if (classAttribute) { const hasClass = classAttribute.value .split(' ') .map(part => part.trim()) .includes(className); if (!hasClass) { // We have source code location info enabled, and we pre-checked that the element // has attributes, specifically the `class` attribute. const classAttributeLocation = body.sourceCodeLocation.attrs.class; const recordedChange = host .beginUpdate(htmlFilePath) .insertRight(classAttributeLocation.endOffset - 1, ` ${className}`); host.commitUpdate(recordedChange); } } else { const recordedChange = host .beginUpdate(htmlFilePath) .insertRight(body.sourceCodeLocation.startTag.endOffset - 1, ` class="${className}"`); host.commitUpdate(recordedChange); } } exports.addBodyClass = addBodyClass; /** Finds an element by its tag name. */ function getElementByTagName(tagName, htmlContent) { const document = (0, parse5_1.parse)(htmlContent, { sourceCodeLocationInfo: true }); const nodeQueue = [...document.childNodes]; while (nodeQueue.length) { const node = nodeQueue.shift(); if (node.nodeName.toLowerCase() === tagName) { return node; } else if (node.childNodes) { nodeQueue.push(...node.childNodes); } } return null; } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHRtbC1tYW5pcHVsYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3NjaGVtYXRpY3MvdXRpbHMvaHRtbC1tYW5pcHVsYXRpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBRUgsMkRBQXFFO0FBQ3JFLHFEQUFxRTtBQUNyRSxtQ0FBMEM7QUFFMUMsa0dBQWtHO0FBQ2xHLFNBQWdCLHVCQUF1QixDQUFDLElBQVUsRUFBRSxZQUFvQixFQUFFLFdBQW1CO0lBQzNGLE1BQU0sY0FBYyxHQUFHLElBQUksQ0FBQyxJQUFJLENBQUMsWUFBWSxDQUFDLENBQUM7SUFFL0MsSUFBSSxDQUFDLGNBQWMsRUFBRTtRQUNuQixNQUFNLElBQUksZ0NBQW1CLENBQUMsaUNBQWlDLFlBQVksRUFBRSxDQUFDLENBQUM7S0FDaEY7SUFFRCxNQUFNLFdBQV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