|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import {DefaultTreeDocument, DefaultTreeElement, parseFragment} from 'parse5'; |
| 9 | +import {ChildNode, Element, parseFragment} from 'parse5'; |
10 | 10 |
|
11 | 11 | /** |
12 | 12 | * Parses a HTML fragment and traverses all AST nodes in order find elements that |
13 | 13 | * include the specified attribute. |
14 | 14 | */ |
15 | 15 | export function findElementsWithAttribute(html: string, attributeName: string) { |
16 | | - const document = parseFragment(html, {sourceCodeLocationInfo: true}) as DefaultTreeDocument; |
17 | | - const elements: DefaultTreeElement[] = []; |
| 16 | + const document = parseFragment(html, {sourceCodeLocationInfo: true}); |
| 17 | + const elements: Element[] = []; |
18 | 18 |
|
19 | | - const visitNodes = nodes => { |
20 | | - nodes.forEach(node => { |
| 19 | + const visitNodes = (nodes: ChildNode[]) => { |
| 20 | + nodes.forEach((node: Element) => { |
21 | 21 | if (node.childNodes) { |
22 | 22 | visitNodes(node.childNodes); |
23 | 23 | } |
24 | 24 |
|
25 | | - if (node.attrs && node.attrs.some(attr => attr.name === attributeName.toLowerCase())) { |
| 25 | + if (node.attrs?.some(attr => attr.name === attributeName.toLowerCase())) { |
26 | 26 | elements.push(node); |
27 | 27 | } |
28 | 28 | }); |
@@ -54,7 +54,7 @@ export function findAttributeOnElementWithAttrs(html: string, name: string, attr |
54 | 54 | } |
55 | 55 |
|
56 | 56 | /** Shorthand function that checks if the specified element contains the given attribute. */ |
57 | | -function hasElementAttribute(element: DefaultTreeElement, attributeName: string): boolean { |
| 57 | +function hasElementAttribute(element: Element, attributeName: string): boolean { |
58 | 58 | return element.attrs && element.attrs.some(attr => attr.name === attributeName.toLowerCase()); |
59 | 59 | } |
60 | 60 |
|
|
0 commit comments