elements.js 2.7 KB

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