1
1
// Packages
2
- import saxes from 'saxes' ;
2
+ import { SaxesParser , SaxesTag } from 'saxes' ;
3
3
4
4
// Ours
5
5
import { nsLookup } from './namespaces' ;
@@ -25,23 +25,24 @@ class RSS implements Parser {
25
25
private _stack : XMLNode [ ] ;
26
26
private _buffer : XMLNode [ ] ;
27
27
28
- private _parser : saxes . SaxesParser ;
28
+ private _parser : SaxesParser ;
29
29
30
30
/**
31
31
* Creates an instance of RSS.
32
32
*/
33
33
constructor ( ) {
34
34
// XML Parser
35
- this . _parser = new saxes . SaxesParser ( {
35
+ this . _parser = new SaxesParser ( {
36
36
xmlns : true ,
37
37
position : false
38
38
} ) ;
39
- this . _parser . onopentag = this . onopentag . bind ( this ) ;
40
- this . _parser . onclosetag = this . onclosetag . bind ( this ) ;
41
- this . _parser . ontext = this . ontext . bind ( this ) ;
42
- this . _parser . oncdata = this . ontext . bind ( this ) ;
43
- this . _parser . onerror = this . onerror . bind ( this ) ;
44
- this . _parser . onend = this . onend . bind ( this ) ;
39
+
40
+ this . _parser . on ( 'opentag' , this . onopentag ) ;
41
+ this . _parser . on ( 'closetag' , this . onclosetag ) ;
42
+ this . _parser . on ( 'text' , this . ontext ) ;
43
+ this . _parser . on ( 'cdata' , this . ontext ) ;
44
+ this . _parser . on ( 'error' , this . onerror ) ;
45
+ this . _parser . on ( 'end' , this . onend ) ;
45
46
46
47
/**
47
48
* Holds all open tags
@@ -95,7 +96,7 @@ class RSS implements Parser {
95
96
this . _parser . close ( ) ;
96
97
}
97
98
98
- onopentag ( tag : saxes . SaxesTag ) {
99
+ onopentag = ( tag : SaxesTag ) => {
99
100
const node : XMLNode = {
100
101
$name : tag . name ,
101
102
$prefix : tag . prefix ,
@@ -152,9 +153,9 @@ class RSS implements Parser {
152
153
153
154
this . _stack . unshift ( node ) ;
154
155
}
155
- }
156
+ } ;
156
157
157
- onclosetag ( tag : saxes . SaxesTag ) {
158
+ onclosetag = ( tag : SaxesTag ) => {
158
159
// NOTE: We only rely on the internal stack to ensure correct output
159
160
// in some cases. That being said, it's up to the consumer to decide
160
161
// what happens in case of XML error.
@@ -199,23 +200,23 @@ class RSS implements Parser {
199
200
}
200
201
}
201
202
}
202
- }
203
+ } ;
203
204
204
- ontext ( text : string ) {
205
+ ontext = ( text : string ) => {
205
206
text = text . trim ( ) ;
206
207
if ( text && this . _stack . length > 0 ) {
207
208
this . _stack [ 0 ] . value += text ;
208
209
}
209
- }
210
+ } ;
210
211
211
- onerror ( err : Error ) {
212
+ onerror = ( err : Error ) => {
212
213
throw err ;
213
- }
214
+ } ;
214
215
215
- onend ( ) {
216
+ onend = ( ) => {
216
217
// We are done here
217
218
this . _done = true ;
218
- }
219
+ } ;
219
220
220
221
/**
221
222
* Checks if a given node is <rss> or <feed> tag
@@ -242,7 +243,7 @@ class RSS implements Parser {
242
243
/**
243
244
* Parse tag attributes
244
245
*/
245
- attributes ( tag : saxes . SaxesTag ) {
246
+ attributes ( tag : SaxesTag ) {
246
247
const attrs = new Map < string , string > ( ) ;
247
248
248
249
return Object . entries ( tag . attributes ) . reduce (
@@ -288,7 +289,7 @@ class RSS implements Parser {
288
289
/**
289
290
* Check if a node and an XML tag are equal
290
291
*/
291
- equals ( node : XMLNode , tag : saxes . SaxesTag ) {
292
+ equals ( node : XMLNode , tag : SaxesTag ) {
292
293
return node . $name === tag . name ;
293
294
}
294
295
0 commit comments