Skip to content

Commit 31e9b42

Browse files
committed
refactor: upgrade to saxes v5
1 parent 198c81f commit 31e9b42

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test": "tsdx test"
2626
},
2727
"dependencies": {
28-
"saxes": "^3.1.11"
28+
"saxes": "^5.0.1"
2929
},
3030
"devDependencies": {
3131
"@types/globby": "^9.1.0",

src/rss/index.ts

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Packages
2-
import saxes from 'saxes';
2+
import { SaxesParser, SaxesTag } from 'saxes';
33

44
// Ours
55
import { nsLookup } from './namespaces';
@@ -25,23 +25,24 @@ class RSS implements Parser {
2525
private _stack: XMLNode[];
2626
private _buffer: XMLNode[];
2727

28-
private _parser: saxes.SaxesParser;
28+
private _parser: SaxesParser;
2929

3030
/**
3131
* Creates an instance of RSS.
3232
*/
3333
constructor() {
3434
// XML Parser
35-
this._parser = new saxes.SaxesParser({
35+
this._parser = new SaxesParser({
3636
xmlns: true,
3737
position: false
3838
});
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);
4546

4647
/**
4748
* Holds all open tags
@@ -95,7 +96,7 @@ class RSS implements Parser {
9596
this._parser.close();
9697
}
9798

98-
onopentag(tag: saxes.SaxesTag) {
99+
onopentag = (tag: SaxesTag) => {
99100
const node: XMLNode = {
100101
$name: tag.name,
101102
$prefix: tag.prefix,
@@ -152,9 +153,9 @@ class RSS implements Parser {
152153

153154
this._stack.unshift(node);
154155
}
155-
}
156+
};
156157

157-
onclosetag(tag: saxes.SaxesTag) {
158+
onclosetag = (tag: SaxesTag) => {
158159
// NOTE: We only rely on the internal stack to ensure correct output
159160
// in some cases. That being said, it's up to the consumer to decide
160161
// what happens in case of XML error.
@@ -199,23 +200,23 @@ class RSS implements Parser {
199200
}
200201
}
201202
}
202-
}
203+
};
203204

204-
ontext(text: string) {
205+
ontext = (text: string) => {
205206
text = text.trim();
206207
if (text && this._stack.length > 0) {
207208
this._stack[0].value += text;
208209
}
209-
}
210+
};
210211

211-
onerror(err: Error) {
212+
onerror = (err: Error) => {
212213
throw err;
213-
}
214+
};
214215

215-
onend() {
216+
onend = () => {
216217
// We are done here
217218
this._done = true;
218-
}
219+
};
219220

220221
/**
221222
* Checks if a given node is <rss> or <feed> tag
@@ -242,7 +243,7 @@ class RSS implements Parser {
242243
/**
243244
* Parse tag attributes
244245
*/
245-
attributes(tag: saxes.SaxesTag) {
246+
attributes(tag: SaxesTag) {
246247
const attrs = new Map<string, string>();
247248

248249
return Object.entries(tag.attributes).reduce(
@@ -288,7 +289,7 @@ class RSS implements Parser {
288289
/**
289290
* Check if a node and an XML tag are equal
290291
*/
291-
equals(node: XMLNode, tag: saxes.SaxesTag) {
292+
equals(node: XMLNode, tag: SaxesTag) {
292293
return node.$name === tag.name;
293294
}
294295

0 commit comments

Comments
 (0)