|
3 | 3 | * @since 2017-08-18 15:17:51 |
4 | 4 | */ |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Node kinds returned by the parser. |
| 8 | + */ |
6 | 9 | export enum SyntaxKind { |
| 10 | + /** |
| 11 | + * Plain text content. |
| 12 | + */ |
7 | 13 | Text = 'Text', |
| 14 | + /** |
| 15 | + * HTML tag node. |
| 16 | + */ |
8 | 17 | Tag = 'Tag', |
9 | 18 | } |
10 | 19 |
|
| 20 | +/** |
| 21 | + * Common source range shared by every AST node. |
| 22 | + */ |
11 | 23 | export interface IBaseNode { |
| 24 | + /** |
| 25 | + * Zero-based start offset in the source string. |
| 26 | + */ |
12 | 27 | start: number; |
| 28 | + /** |
| 29 | + * Zero-based end offset in the source string. |
| 30 | + */ |
13 | 31 | end: number; |
14 | 32 | } |
15 | 33 |
|
| 34 | +/** |
| 35 | + * Text node in the parsed AST. |
| 36 | + */ |
16 | 37 | export interface IText extends IBaseNode { |
| 38 | + /** |
| 39 | + * Discriminator for text nodes. |
| 40 | + */ |
17 | 41 | type: SyntaxKind.Text; |
| 42 | + /** |
| 43 | + * Original text content. |
| 44 | + */ |
18 | 45 | value: string; |
19 | 46 | } |
20 | 47 |
|
| 48 | +/** |
| 49 | + * Parsed attribute value, with quote information preserved. |
| 50 | + */ |
21 | 51 | export interface IAttributeValue extends IBaseNode { |
| 52 | + /** |
| 53 | + * Attribute value without surrounding quotes. |
| 54 | + */ |
22 | 55 | value: string; |
| 56 | + /** |
| 57 | + * Quote character used by the source value, or undefined for unquoted values. |
| 58 | + */ |
23 | 59 | quote: "'" | '"' | undefined; |
24 | 60 | } |
25 | 61 |
|
| 62 | +/** |
| 63 | + * Parsed tag attribute. |
| 64 | + */ |
26 | 65 | export interface IAttribute extends IBaseNode { |
| 66 | + /** |
| 67 | + * Attribute name token. |
| 68 | + */ |
27 | 69 | name: IText; |
| 70 | + /** |
| 71 | + * Attribute value, if the source provided one. |
| 72 | + */ |
28 | 73 | value: IAttributeValue | undefined; |
29 | 74 | } |
30 | 75 |
|
| 76 | +/** |
| 77 | + * Tag node in the parsed AST. |
| 78 | + */ |
31 | 79 | export interface ITag extends IBaseNode { |
| 80 | + /** |
| 81 | + * Discriminator for tag nodes. |
| 82 | + */ |
32 | 83 | type: SyntaxKind.Tag; |
33 | | - // original open tag, <Div id="id"> |
| 84 | + /** |
| 85 | + * Original opening tag text, such as <Div id="id">. |
| 86 | + */ |
34 | 87 | open: IText; |
35 | | - // lower case tag name, div |
| 88 | + /** |
| 89 | + * Lowercase tag name, such as div. |
| 90 | + */ |
36 | 91 | name: string; |
37 | | - // original case tag name, Div |
| 92 | + /** |
| 93 | + * Tag name with original casing, such as Div. |
| 94 | + */ |
38 | 95 | rawName: string; |
| 96 | + /** |
| 97 | + * Attributes in source order. |
| 98 | + */ |
39 | 99 | attributes: IAttribute[]; |
40 | | - // the attribute map, if `options.setAttributeMap` is `true` |
41 | | - // this will be a Record, key is the attribute name literal, |
42 | | - // value is the attribute self. |
| 100 | + /** |
| 101 | + * Attribute lookup map when ParseOptions.setAttributeMap is true. |
| 102 | + */ |
43 | 103 | attributeMap: Record<string, IAttribute> | undefined; |
44 | | - body: |
45 | | - | Array<ITag | IText> // with close tag |
46 | | - | undefined // self closed |
47 | | - | null; // EOF before open tag end |
48 | | - // original close tag, </DIV > |
49 | | - close: |
50 | | - | IText // with close tag |
51 | | - | undefined // self closed |
52 | | - | null; // EOF before end or without close tag |
| 104 | + /** |
| 105 | + * Child nodes for paired tags, undefined for self-closed tags, or null when the opening tag is incomplete. |
| 106 | + */ |
| 107 | + body: Array<ITag | IText> | undefined | null; |
| 108 | + /** |
| 109 | + * Original closing tag text, undefined for self-closed tags, or null when missing. |
| 110 | + */ |
| 111 | + close: IText | undefined | null; |
53 | 112 | } |
54 | 113 |
|
| 114 | +/** |
| 115 | + * Any node returned by parse. |
| 116 | + */ |
55 | 117 | export type INode = IText | ITag; |
0 commit comments