source: trip-planner-front/node_modules/@angular/cdk/schematics/utils/html-manipulation.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 12.1 KB
Line 
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.addBodyClass = exports.getHtmlHeadTagElement = exports.appendHtmlElementToHead = void 0;
11const schematics_1 = require("@angular-devkit/schematics");
12const parse5_element_1 = require("./parse5-element");
13const parse5_1 = require("parse5");
14/** Appends the given element HTML fragment to the `<head>` element of the specified HTML file. */
15function appendHtmlElementToHead(host, htmlFilePath, elementHtml) {
16 const htmlFileBuffer = host.read(htmlFilePath);
17 if (!htmlFileBuffer) {
18 throw new schematics_1.SchematicsException(`Could not read file for path: ${htmlFilePath}`);
19 }
20 const htmlContent = htmlFileBuffer.toString();
21 if (htmlContent.includes(elementHtml)) {
22 return;
23 }
24 const headTag = getHtmlHeadTagElement(htmlContent);
25 if (!headTag) {
26 throw Error(`Could not find '<head>' element in HTML file: ${htmlFileBuffer}`);
27 }
28 // We always have access to the source code location here because the `getHeadTagElement`
29 // function explicitly has the `sourceCodeLocationInfo` option enabled.
30 const endTagOffset = headTag.sourceCodeLocation.endTag.startOffset;
31 const indentationOffset = parse5_element_1.getChildElementIndentation(headTag);
32 const insertion = `${' '.repeat(indentationOffset)}${elementHtml}`;
33 const recordedChange = host
34 .beginUpdate(htmlFilePath)
35 .insertRight(endTagOffset, `${insertion}\n`);
36 host.commitUpdate(recordedChange);
37}
38exports.appendHtmlElementToHead = appendHtmlElementToHead;
39/** Parses the given HTML file and returns the head element if available. */
40function getHtmlHeadTagElement(htmlContent) {
41 return getElementByTagName('head', htmlContent);
42}
43exports.getHtmlHeadTagElement = getHtmlHeadTagElement;
44/** Adds a class to the body of the document. */
45function addBodyClass(host, htmlFilePath, className) {
46 const htmlFileBuffer = host.read(htmlFilePath);
47 if (!htmlFileBuffer) {
48 throw new schematics_1.SchematicsException(`Could not read file for path: ${htmlFilePath}`);
49 }
50 const htmlContent = htmlFileBuffer.toString();
51 const body = getElementByTagName('body', htmlContent);
52 if (!body) {
53 throw Error(`Could not find <body> element in HTML file: ${htmlFileBuffer}`);
54 }
55 const classAttribute = body.attrs.find(attribute => attribute.name === 'class');
56 if (classAttribute) {
57 const hasClass = classAttribute.value.split(' ').map(part => part.trim()).includes(className);
58 if (!hasClass) {
59 const classAttributeLocation = body.sourceCodeLocation.attrs.class;
60 const recordedChange = host
61 .beginUpdate(htmlFilePath)
62 .insertRight(classAttributeLocation.endOffset - 1, ` ${className}`);
63 host.commitUpdate(recordedChange);
64 }
65 }
66 else {
67 const recordedChange = host
68 .beginUpdate(htmlFilePath)
69 .insertRight(body.sourceCodeLocation.startTag.endOffset - 1, ` class="${className}"`);
70 host.commitUpdate(recordedChange);
71 }
72}
73exports.addBodyClass = addBodyClass;
74/** Finds an element by its tag name. */
75function getElementByTagName(tagName, htmlContent) {
76 const document = parse5_1.parse(htmlContent, { sourceCodeLocationInfo: true });
77 const nodeQueue = [...document.childNodes];
78 while (nodeQueue.length) {
79 const node = nodeQueue.shift();
80 if (node.nodeName.toLowerCase() === tagName) {
81 return node;
82 }
83 else if (node.childNodes) {
84 nodeQueue.push(...node.childNodes);
85 }
86 }
87 return null;
88}
89//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHRtbC1tYW5pcHVsYXRpb24uanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3NjaGVtYXRpY3MvdXRpbHMvaHRtbC1tYW5pcHVsYXRpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7R0FNRzs7O0FBRUgsMkRBQXFFO0FBQ3JFL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