elements.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.findElementWithoutStructuralDirective = findElementWithoutStructuralDirective;
  8. exports.findElementWithTag = findElementWithTag;
  9. exports.findElementWithClassName = findElementWithClassName;
  10. const parse5_1 = require("parse5");
  11. const hasClassName = (node, className) => {
  12. var _a, _b;
  13. return (_b = (_a = node.attrs) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, attr => attr.name === 'class' && attr.value.indexOf(className) !== -1);
  14. };
  15. const compareCaseInsensitive = (a, b) => (a === null || a === void 0 ? void 0 : a.toLowerCase()) === (b === null || b === void 0 ? void 0 : b.toLowerCase());
  16. function findElementWithoutStructuralDirective(html, tagName, directiveName, attr) {
  17. const document = (0, parse5_1.parseFragment)(html, { sourceCodeLocationInfo: true });
  18. const elements = [];
  19. const visitNodes = (nodes) => {
  20. nodes.forEach(node => {
  21. if (node['childNodes'] && !(node['tagName'] === 'ng-template' && !!node.attrs.find(a => compareCaseInsensitive(a.name, directiveName)))) {
  22. visitNodes(node['childNodes']);
  23. }
  24. if (compareCaseInsensitive(node['tagName'], tagName)) {
  25. const element = node;
  26. const directive = `*${directiveName}`;
  27. if (!!element.attrs.find(a => compareCaseInsensitive(a.name, attr)) && !element.attrs.find(a => compareCaseInsensitive(a.name, directive))) {
  28. elements.push(element);
  29. }
  30. }
  31. });
  32. };
  33. visitNodes(document.childNodes);
  34. return elements
  35. .filter(e => { var _a; return (_a = e === null || e === void 0 ? void 0 : e.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.startTag; })
  36. .map(element => element.sourceCodeLocation.startTag.startOffset);
  37. }
  38. function findElementWithTag(html, tagName) {
  39. const document = (0, parse5_1.parseFragment)(html, { sourceCodeLocationInfo: true });
  40. const elements = [];
  41. const visitNodes = (nodes) => {
  42. nodes.forEach(node => {
  43. if (node['childNodes']) {
  44. visitNodes(node['childNodes']);
  45. }
  46. if (compareCaseInsensitive(node['tagName'], tagName)) {
  47. elements.push(node);
  48. }
  49. });
  50. };
  51. visitNodes(document.childNodes);
  52. return elements
  53. .filter(e => { var _a; return (_a = e === null || e === void 0 ? void 0 : e.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.startTag; })
  54. .map(element => element.sourceCodeLocation.startTag.startOffset);
  55. }
  56. function findElementWithClassName(html, className, tagName) {
  57. const document = (0, parse5_1.parseFragment)(html, { sourceCodeLocationInfo: true });
  58. const elements = [];
  59. const visitNodes = (nodes) => {
  60. nodes.forEach(node => {
  61. if (node['childNodes']) {
  62. visitNodes(node['childNodes']);
  63. }
  64. if (compareCaseInsensitive(node['tagName'], tagName) && hasClassName(node, className)) {
  65. elements.push(node);
  66. }
  67. });
  68. };
  69. visitNodes(document.childNodes);
  70. return elements
  71. .filter(e => { var _a; return (_a = e === null || e === void 0 ? void 0 : e.sourceCodeLocation) === null || _a === void 0 ? void 0 : _a.startTag; })
  72. .map(element => element.sourceCodeLocation.attrs.class.startOffset);
  73. }
  74. //# sourceMappingURL=elements.js.map