1
1
import { Parser , ParserOptions } from 'htmlparser2' ;
2
- import { LocationTracker } from './location-tracker' ;
2
+ import { LocationTracker , SourceLocation } from './location-tracker' ;
3
3
4
4
export type Directive = {
5
5
name : string | RegExp ;
@@ -14,8 +14,7 @@ export type Options = {
14
14
15
15
export type Tag = string | boolean ;
16
16
export type Attributes = Record < string , string | number | boolean > ;
17
- // export type Content = NodeText | Node[] | Node[][];
18
- export type Content = NodeText | ( Node | Node [ ] ) [ ] ;
17
+ export type Content = NodeText | Array < Node | Node [ ] > ;
19
18
20
19
export type NodeText = string | number ;
21
20
export type NodeTag = {
@@ -27,16 +26,6 @@ export type NodeTag = {
27
26
28
27
export type Node = NodeText | NodeTag ;
29
28
30
- export type SourceLocation = {
31
- start : Position ;
32
- end : Position ;
33
- } ;
34
-
35
- export type Position = {
36
- line : number ;
37
- column : number ;
38
- } ;
39
-
40
29
const defaultOptions : ParserOptions = {
41
30
lowerCaseTags : false ,
42
31
lowerCaseAttributeNames : false ,
@@ -51,7 +40,7 @@ const defaultDirectives: Directive[] = [
51
40
}
52
41
] ;
53
42
54
- const parser = ( html : string , options : Options = { } ) : Node [ ] => {
43
+ export const parser = ( html : string , options : Options = { } ) : Node [ ] => {
55
44
const locationTracker = new LocationTracker ( html ) ;
56
45
const bufArray : Node [ ] = [ ] ;
57
46
const results : Node [ ] = [ ] ;
@@ -60,25 +49,6 @@ const parser = (html: string, options: Options = {}): Node[] => {
60
49
return bufArray [ bufArray . length - 1 ] ;
61
50
}
62
51
63
- function resolveContent ( text : NodeText ) : void {
64
- const last = bufferArrayLast ( ) ;
65
-
66
- if ( last === undefined ) {
67
- results . push ( text ) ;
68
- return ;
69
- }
70
-
71
- if ( typeof last === 'object' ) {
72
- if ( last . content === undefined ) {
73
- last . content = [ ] ;
74
- }
75
-
76
- if ( Array . isArray ( last . content ) ) {
77
- last . content . push ( text ) ;
78
- }
79
- }
80
- }
81
-
82
52
function isDirective ( directive : Directive , tag : string ) : boolean {
83
53
if ( directive . name instanceof RegExp ) {
84
54
const regex = new RegExp ( directive . name . source , 'i' ) ;
@@ -107,20 +77,48 @@ const parser = (html: string, options: Options = {}): Node[] => {
107
77
108
78
function onprocessinginstruction ( name : string , data : string ) {
109
79
const directives = defaultDirectives . concat ( options . directives ?? [ ] ) ;
80
+ const last = bufferArrayLast ( ) ;
110
81
111
82
for ( const directive of directives ) {
112
83
const directiveText = directive . start + data + directive . end ;
113
84
114
85
if ( isDirective ( directive , name . toLowerCase ( ) ) ) {
115
- resolveContent ( directiveText ) ;
86
+ if ( last === undefined ) {
87
+ results . push ( directiveText ) ;
88
+ return ;
89
+ }
90
+
91
+ if ( typeof last === 'object' ) {
92
+ if ( last . content === undefined ) {
93
+ last . content = [ ] ;
94
+ }
95
+
96
+ if ( Array . isArray ( last . content ) ) {
97
+ last . content . push ( directiveText ) ;
98
+ }
99
+ }
116
100
}
117
101
}
118
102
}
119
103
120
104
function oncomment ( data : string ) {
105
+ const last = bufferArrayLast ( ) ;
121
106
const comment = `<!--${ data } -->` ;
122
107
123
- resolveContent ( comment ) ;
108
+ if ( last === undefined ) {
109
+ results . push ( comment ) ;
110
+ return ;
111
+ }
112
+
113
+ if ( typeof last === 'object' ) {
114
+ if ( last . content === undefined ) {
115
+ last . content = [ ] ;
116
+ }
117
+
118
+ if ( Array . isArray ( last . content ) ) {
119
+ last . content . push ( comment ) ;
120
+ }
121
+ }
124
122
}
125
123
126
124
function onopentag ( tag : string , attrs : Attributes ) {
@@ -188,6 +186,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
188
186
if ( last . content === undefined ) {
189
187
last . content = [ ] ;
190
188
}
189
+
191
190
if ( Array . isArray ( last . content ) ) {
192
191
last . content . push ( text ) ;
193
192
}
@@ -207,5 +206,3 @@ const parser = (html: string, options: Options = {}): Node[] => {
207
206
208
207
return results ;
209
208
} ;
210
-
211
- export default parser ;
0 commit comments