source: trip-planner-front/node_modules/@angular/cdk/schematics/ng-update/html-parsing/elements.mjs@ b738035

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

initial commit

  • Property mode set to 100644
File size: 8.4 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.getStartOffsetOfAttribute = exports.findAttributeOnElementWithAttrs = exports.findAttributeOnElementWithTag = exports.findElementsWithAttribute = void 0;
11const parse5_1 = require("parse5");
12/**
13 * Parses a HTML fragment and traverses all AST nodes in order find elements that
14 * include the specified attribute.
15 */
16function findElementsWithAttribute(html, attributeName) {
17 const document = parse5_1.parseFragment(html, { sourceCodeLocationInfo: true });
18 const elements = [];
19 const visitNodes = (nodes) => {
20 nodes.forEach(n => {
21 var _a;
22 const node = n;
23 if (node.childNodes) {
24 visitNodes(node.childNodes);
25 }
26 if ((_a = node.attrs) === null || _a === void 0 ? void 0 : _a.some(attr => attr.name === attributeName.toLowerCase())) {
27 elements.push(node);
28 }
29 });
30 };
31 visitNodes(document.childNodes);
32 return elements;
33}
34exports.findElementsWithAttribute = findElementsWithAttribute;
35/**
36 * Finds elements with explicit tag names that also contain the specified attribute. Returns the
37 * attribute start offset based on the specified HTML.
38 */
39function findAttributeOnElementWithTag(html, name, tagNames) {
40 return findElementsWithAttribute(html, name)
41 .filter(element => tagNames.includes(element.tagName))
42 .map(element => getStartOffsetOfAttribute(element, name));
43}
44exports.findAttributeOnElementWithTag = findAttributeOnElementWithTag;
45/**
46 * Finds elements that contain the given attribute and contain at least one of the other
47 * specified attributes. Returns the primary attribute's start offset based on the specified HTML.
48 */
49function findAttributeOnElementWithAttrs(html, name, attrs) {
50 return findElementsWithAttribute(html, name)
51 .filter(element => attrs.some(attr => hasElementAttribute(element, attr)))
52 .map(element => getStartOffsetOfAttribute(element, name));
53}
54exports.findAttributeOnElementWithAttrs = findAttributeOnElementWithAttrs;
55/** Shorthand function that checks if the specified element contains the given attribute. */
56function hasElementAttribute(element, attributeName) {
57 return element.attrs && element.attrs.some(attr => attr.name === attributeName.toLowerCase());
58}
59/** Gets the start offset of the given attribute from a Parse5 element. */
60function getStartOffsetOfAttribute(element, attributeName) {
61 return element.sourceCodeLocation.attrs[attributeName.toLowerCase()].startOffset;
62}
63exports.getStartOffsetOfAttribute = getStartOffsetOfAttribute;
64//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZWxlbWVudHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL3NjaGVtYXRpY3MvbmctdXBkYXRlL2h0bWwtcGFyc2luZy9lbGVtZW50cy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUE7Ozs7OztHQU1HOzs7QUFFSCxtQ0FBeUQ7QUFFekQ7OztHQUdHO0FBQ0gsU0FBZ0IseUJBQXlCLENBQUMsSUFBWSxFQUFFLGFBQXFCO0lBQzNFLE1BQU0sUUFBUSxHQUFHLHNCQUFhLENBQUMsSUFBSSxFQUFFLEVBQUMsc0JBQXNCLEVBQUUsSUFBSSxFQUFDLENBQUMsQ0FBQztJQUNyRSxNQUFNLFFBQVEsR0FBYyxFQUFFLENBQUM7SUFFL0IsTUFBTSxVQUFVLEdBQUcsQ0FBQyxLQUFrQixFQUFFLEVBQUU7UUFDeEMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUMsRUFBRTs7WUFDaEIsTUFBTSxJQUFJLEdBQUcsQ0FBWSxDQUFDO1lBRTFCLElBQUksSUFBSSxDQUFDLFVBQVUsRUFBRTtnQkFDbkIsVUFBVSxDQUFDLElBQUksQ0FBQyxVQUFVLENBQUMsQ0FBQzthQUM3QjtZQUVELElBQUksTUFBQSxJQUFJLENBQUMsS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