1
- const { Parser } = require ( 'htmlparser2' ) ;
1
+ const { Parser} = require ( 'htmlparser2' ) ;
2
2
3
3
/**
4
4
* @see https://github.com/fb55/htmlparser2/wiki/Parser-options
@@ -14,141 +14,141 @@ const defaultDirectives = [{name: '!doctype', start: '<', end: '>'}];
14
14
* @return {PostHTMLTree }
15
15
*/
16
16
function postHTMLParser ( html , options ) {
17
- const bufArray = [ ] ;
18
- const results = [ ] ;
17
+ const bufArray = [ ] ;
18
+ const results = [ ] ;
19
19
20
- bufArray . last = function ( ) {
21
- return this [ this . length - 1 ] ;
22
- } ;
20
+ bufArray . last = function ( ) {
21
+ return this [ this . length - 1 ] ;
22
+ } ;
23
23
24
- function isDirective ( { name} , tag ) {
25
- if ( name instanceof RegExp ) {
26
- const regex = RegExp ( name . source , 'i' ) ;
24
+ function isDirective ( { name} , tag ) {
25
+ if ( name instanceof RegExp ) {
26
+ const regex = RegExp ( name . source , 'i' ) ;
27
27
28
- return regex . test ( tag ) ;
29
- }
30
-
31
- if ( tag !== name ) {
32
- return false ;
33
- }
28
+ return regex . test ( tag ) ;
29
+ }
34
30
35
- return true ;
31
+ if ( tag !== name ) {
32
+ return false ;
36
33
}
37
34
38
- function parserDirective ( name , data ) {
39
- const directives = [ ] . concat ( defaultDirectives , options . directives || [ ] ) ;
40
- const last = bufArray . last ( ) ;
35
+ return true ;
36
+ }
41
37
42
- for ( let i = 0 ; i < directives . length ; i ++ ) {
43
- const directive = directives [ i ] ;
44
- const directiveText = directive . start + data + directive . end ;
38
+ function parserDirective ( name , data ) {
39
+ const directives = [ ] . concat ( defaultDirectives , options . directives || [ ] ) ;
40
+ const last = bufArray . last ( ) ;
45
41
46
- name = name . toLowerCase ( ) ;
47
- if ( isDirective ( directive , name ) ) {
48
- if ( ! last ) {
49
- results . push ( directiveText ) ;
50
- return ;
51
- }
42
+ for ( let i = 0 ; i < directives . length ; i ++ ) {
43
+ const directive = directives [ i ] ;
44
+ const directiveText = directive . start + data + directive . end ;
52
45
53
- last . content || ( last . content = [ ] ) ;
54
- last . content . push ( directiveText ) ;
55
- }
46
+ name = name . toLowerCase ( ) ;
47
+ if ( isDirective ( directive , name ) ) {
48
+ if ( ! last ) {
49
+ results . push ( directiveText ) ;
50
+ return ;
56
51
}
57
- }
58
-
59
- function normalizeArributes ( attrs ) {
60
- const result = { } ;
61
- Object . keys ( attrs ) . forEach ( key => {
62
- const obj = { } ;
63
- obj [ key ] = attrs [ key ] . replace ( / & q u o t ; / g, '"' ) ;
64
- Object . assign ( result , obj ) ;
65
- } ) ;
66
52
67
- return result ;
53
+ last . content || ( last . content = [ ] ) ;
54
+ last . content . push ( directiveText ) ;
55
+ }
68
56
}
57
+ }
58
+
59
+ function normalizeArributes ( attrs ) {
60
+ const result = { } ;
61
+ Object . keys ( attrs ) . forEach ( key => {
62
+ const obj = { } ;
63
+ obj [ key ] = attrs [ key ] . replace ( / & q u o t ; / g, '"' ) ;
64
+ Object . assign ( result , obj ) ;
65
+ } ) ;
66
+
67
+ return result ;
68
+ }
69
+
70
+ const parser = new Parser ( {
71
+ onprocessinginstruction : parserDirective ,
72
+ oncomment ( data ) {
73
+ const comment = `<!--${ data } -->` ;
74
+ const last = bufArray . last ( ) ;
75
+
76
+ if ( ! last ) {
77
+ results . push ( comment ) ;
78
+ return ;
79
+ }
80
+
81
+ last . content || ( last . content = [ ] ) ;
82
+ last . content . push ( comment ) ;
83
+ } ,
84
+ onopentag ( tag , attrs ) {
85
+ const buf = { tag} ;
86
+
87
+ if ( Object . keys ( attrs ) . length ) {
88
+ buf . attrs = normalizeArributes ( attrs ) ;
89
+ }
90
+
91
+ bufArray . push ( buf ) ;
92
+ } ,
93
+ onclosetag ( ) {
94
+ const buf = bufArray . pop ( ) ;
95
+
96
+ if ( ! bufArray . length ) {
97
+ results . push ( buf ) ;
98
+ return ;
99
+ }
100
+
101
+ const last = bufArray . last ( ) ;
102
+ if ( ! Array . isArray ( last . content ) ) {
103
+ last . content = [ ] ;
104
+ }
105
+
106
+ last . content . push ( buf ) ;
107
+ } ,
108
+ ontext ( text ) {
109
+ const last = bufArray . last ( ) ;
110
+
111
+ if ( ! last ) {
112
+ results . push ( text ) ;
113
+ return ;
114
+ }
115
+
116
+ if ( last . content ?. length && typeof last . content [ last . content . length - 1 ] === 'string' ) {
117
+ last . content [ last . content . length - 1 ] = last . content [ last . content . length - 1 ] + text
118
+ return
119
+ }
120
+
121
+
122
+ last . content || ( last . content = [ ] ) ;
123
+ last . content . push ( text ) ;
124
+ }
125
+ } , options || defaultOptions ) ;
69
126
70
- const parser = new Parser ( {
71
- onprocessinginstruction : parserDirective ,
72
- oncomment ( data ) {
73
- const comment = `<!--${ data } -->` ;
74
- const last = bufArray . last ( ) ;
75
-
76
- if ( ! last ) {
77
- results . push ( comment ) ;
78
- return ;
79
- }
80
-
81
- last . content || ( last . content = [ ] ) ;
82
- last . content . push ( comment ) ;
83
- } ,
84
- onopentag ( tag , attrs ) {
85
- const buf = { tag } ;
86
-
87
- if ( Object . keys ( attrs ) . length ) {
88
- buf . attrs = normalizeArributes ( attrs ) ;
89
- }
90
-
91
- bufArray . push ( buf ) ;
92
- } ,
93
- onclosetag ( ) {
94
- const buf = bufArray . pop ( ) ;
95
-
96
- if ( ! bufArray . length ) {
97
- results . push ( buf ) ;
98
- return ;
99
- }
100
-
101
- const last = bufArray . last ( ) ;
102
- if ( ! Array . isArray ( last . content ) ) {
103
- last . content = [ ] ;
104
- }
105
-
106
- last . content . push ( buf ) ;
107
- } ,
108
- ontext ( text ) {
109
- const last = bufArray . last ( ) ;
110
-
111
- if ( ! last ) {
112
- results . push ( text ) ;
113
- return ;
114
- }
115
-
116
- if ( last . content ?. length && typeof last . content [ last . content . length - 1 ] === 'string' ) {
117
- last . content [ last . content . length - 1 ] = last . content [ last . content . length - 1 ] + text
118
- return
119
- }
120
-
121
-
122
- last . content || ( last . content = [ ] ) ;
123
- last . content . push ( text ) ;
124
- }
125
- } , options || defaultOptions ) ;
126
-
127
- parser . write ( html ) ;
128
- parser . end ( ) ;
127
+ parser . write ( html ) ;
128
+ parser . end ( ) ;
129
129
130
- return results ;
130
+ return results ;
131
131
}
132
132
133
133
function parserWrapper ( ...args ) {
134
- let option ;
135
-
136
- function parser ( html ) {
137
- const opt = Object . assign ( { } , defaultOptions , option ) ;
138
- return postHTMLParser ( html , opt ) ;
139
- }
140
-
141
- if (
142
- args . length === 1 &&
143
- Boolean ( args [ 0 ] ) &&
144
- args [ 0 ] . constructor . name === 'Object'
145
- ) {
146
- option = args [ 0 ] ;
147
- return parser ;
148
- }
149
-
150
- option = args [ 1 ] ;
151
- return parser ( args [ 0 ] ) ;
134
+ let option ;
135
+
136
+ function parser ( html ) {
137
+ const opt = Object . assign ( { } , defaultOptions , option ) ;
138
+ return postHTMLParser ( html , opt ) ;
139
+ }
140
+
141
+ if (
142
+ args . length === 1 &&
143
+ Boolean ( args [ 0 ] ) &&
144
+ args [ 0 ] . constructor . name === 'Object'
145
+ ) {
146
+ option = args [ 0 ] ;
147
+ return parser ;
148
+ }
149
+
150
+ option = args [ 1 ] ;
151
+ return parser ( args [ 0 ] ) ;
152
152
}
153
153
154
154
module . exports = parserWrapper ;
0 commit comments