11import { Parser , ParserOptions } from 'htmlparser2' ;
2- import { Directive , Node , NodeTag , Options , Attributes } from '../types/index.d' ;
3- import { LocationTracker } from './location-tracker' ;
2+ import { LocationTracker , SourceLocation } from './location-tracker' ;
3+
4+ export type Directive = {
5+ name : string | RegExp ;
6+ start : string ;
7+ end : string ;
8+ } ;
9+
10+ export type Options = {
11+ directives ?: Directive [ ] ;
12+ sourceLocations ?: boolean ;
13+ } & ParserOptions ;
14+
15+ export type Tag = string | boolean ;
16+ export type Attributes = Record < string , string | number | boolean > ;
17+ export type Content = NodeText | Array < Node | Node [ ] > ;
18+
19+ export type NodeText = string | number ;
20+ export type NodeTag = {
21+ tag ?: Tag ;
22+ attrs ?: Attributes ;
23+ content ?: Content ;
24+ location ?: SourceLocation ;
25+ } ;
26+
27+ export type Node = NodeText | NodeTag ;
428
529const defaultOptions : ParserOptions = {
630 lowerCaseTags : false ,
@@ -16,7 +40,7 @@ const defaultDirectives: Directive[] = [
1640 }
1741] ;
1842
19- const parser = ( html : string , options : Options = { } ) : Node [ ] => {
43+ export const parser = ( html : string , options : Options = { } ) : Node [ ] => {
2044 const locationTracker = new LocationTracker ( html ) ;
2145 const bufArray : Node [ ] = [ ] ;
2246 const results : Node [ ] = [ ] ;
@@ -53,7 +77,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
5377
5478 function onprocessinginstruction ( name : string , data : string ) {
5579 const directives = defaultDirectives . concat ( options . directives ?? [ ] ) ;
56- const last : Node = bufferArrayLast ( ) ;
80+ const last = bufferArrayLast ( ) ;
5781
5882 for ( const directive of directives ) {
5983 const directiveText = directive . start + data + directive . end ;
@@ -69,15 +93,17 @@ const parser = (html: string, options: Options = {}): Node[] => {
6993 last . content = [ ] ;
7094 }
7195
72- last . content . push ( directiveText ) ;
96+ if ( Array . isArray ( last . content ) ) {
97+ last . content . push ( directiveText ) ;
98+ }
7399 }
74100 }
75101 }
76102 }
77103
78104 function oncomment ( data : string ) {
79- const comment = `<!--${ data } -->` ;
80105 const last = bufferArrayLast ( ) ;
106+ const comment = `<!--${ data } -->` ;
81107
82108 if ( last === undefined ) {
83109 results . push ( comment ) ;
@@ -89,7 +115,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
89115 last . content = [ ] ;
90116 }
91117
92- last . content . push ( comment ) ;
118+ if ( Array . isArray ( last . content ) ) {
119+ last . content . push ( comment ) ;
120+ }
93121 }
94122 }
95123
@@ -131,7 +159,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
131159 last . content = [ ] ;
132160 }
133161
134- last . content . push ( buf ) ;
162+ if ( Array . isArray ( last . content ) ) {
163+ last . content . push ( buf ) ;
164+ }
135165 }
136166 }
137167 }
@@ -145,7 +175,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
145175 }
146176
147177 if ( typeof last === 'object' ) {
148- if ( last . content && last . content . length > 0 ) {
178+ if ( last . content && Array . isArray ( last . content ) && last . content . length > 0 ) {
149179 const lastContentNode = last . content [ last . content . length - 1 ] ;
150180 if ( typeof lastContentNode === 'string' && ! lastContentNode . startsWith ( '<!--' ) ) {
151181 last . content [ last . content . length - 1 ] = `${ lastContentNode } ${ text } ` ;
@@ -157,7 +187,9 @@ const parser = (html: string, options: Options = {}): Node[] => {
157187 last . content = [ ] ;
158188 }
159189
160- last . content . push ( text ) ;
190+ if ( Array . isArray ( last . content ) ) {
191+ last . content . push ( text ) ;
192+ }
161193 }
162194 }
163195
@@ -174,5 +206,3 @@ const parser = (html: string, options: Options = {}): Node[] => {
174206
175207 return results ;
176208} ;
177-
178- export default parser ;
0 commit comments