Skip to content

Commit

Permalink
BREAKING: Default decodeEntities to true
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Sep 1, 2020
1 parent c79065b commit 8ac01e0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 117 deletions.
42 changes: 18 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,21 @@ A live demo of htmlparser2 is available [here](https://astexplorer.net/#/2AmVrGu

```javascript
const htmlparser2 = require("htmlparser2");
const parser = new htmlparser2.Parser(
{
onopentag(name, attribs) {
if (name === "script" && attribs.type === "text/javascript") {
console.log("JS! Hooray!");
}
},
ontext(text) {
console.log("-->", text);
},
onclosetag(tagname) {
if (tagname === "script") {
console.log("That's it?!");
}
},
const parser = new htmlparser2.Parser({
onopentag(name, attribs) {
if (name === "script" && attribs.type === "text/javascript") {
console.log("JS! Hooray!");
}
},
{ decodeEntities: true }
);
ontext(text) {
console.log("-->", text);
},
onclosetag(tagname) {
if (tagname === "script") {
console.log("That's it?!");
}
},
});
parser.write(
"Xyz <script type='text/javascript'>var foo = '<<bar>>';</ script>"
);
Expand All @@ -58,14 +55,11 @@ Use the `WritableStream` interface to process a streaming input:

```javascript
const WritableStream = require("htmlparser2/lib/WritableStream");
const parserStream = new WritableStream(
{
ontext(text) {
console.log("Streaming:", text);
},
const parserStream = new WritableStream({
ontext(text) {
console.log("Streaming:", text);
},
{ decodeEntities: true }
);
});

const htmlStream = fs.createReadStream("./my-file.html");
htmlStream.pipe(parserStream).on("finish", () => console.log("done"));
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export interface ParserOptions {
xmlMode?: boolean;

/**
* If set to true, entities within the document will be decoded. Defaults to `false`.
* Decode entities within the document. Defaults to `true`.
*/
decodeEntities?: boolean;

Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default class Tokenizer {
) {
this.cbs = cbs;
this.xmlMode = !!options?.xmlMode;
this.decodeEntities = !!options?.decodeEntities;
this.decodeEntities = options?.decodeEntities ?? true;
}

public reset() {
Expand Down
4 changes: 2 additions & 2 deletions src/__fixtures__/Stream/02-RSS.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
{
"event": "text",
"data": [
"How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href=\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\"&gt;Star City&lt;/a&gt;."
"How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\">Star City</a>."
]
},
{
Expand Down Expand Up @@ -403,7 +403,7 @@
{
"event": "text",
"data": [
"Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href=\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st."
"Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href=\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\">partial eclipse of the Sun</a> on Saturday, May 31st."
]
},
{
Expand Down
91 changes: 4 additions & 87 deletions src/__snapshots__/FeedHandler.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@ Object {
}
`;

exports[`Feeds Atom (1.0) 2`] = `
Object {
"author": "[email protected]",
"description": "A subtitle.",
"id": "urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6",
"items": Array [
Object {
"description": "Some content.",
"id": "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
"link": "http://example.org/2003/12/13/atom03",
"pubDate": 2003-12-13T18:30:02.000Z,
"title": "Atom-Powered Robots Run Amok",
},
Object {},
],
"link": "http://example.org/feed/",
"title": "Example Feed",
"type": "atom",
"updated": 2003-12-13T18:30:02.000Z,
}
`;

exports[`Feeds RDF test 1`] = `
Object {
"id": "",
Expand All @@ -65,82 +43,21 @@ Object {
}
`;

exports[`Feeds RDF test 2`] = `
Object {
"id": "",
"items": Array [
Object {
"description": "Great test content<br>A link: <a href=\\"http://github.com\\">Github</a>",
"link": "http://somefakesite/path/to/something.html",
"title": "Fast HTML Parsing",
},
Object {
"description": "The early bird gets the worm",
"link": "http://somefakesite/path/to/something-else.html",
"title": "This space intentionally left blank",
},
],
"link": "https://github.com/fb55/htmlparser2/",
"title": "A title to parse and remember",
"type": "rdf",
}
`;

exports[`Feeds RSS (2.0) 1`] = `
Object {
"author": "[email protected]",
"description": "Liftoff to Space Exploration.",
"id": "",
"items": Array [
Object {
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\"&gt;Star City&lt;/a&gt;.",
"id": "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",
"pubDate": 2003-06-03T09:39:21.000Z,
"title": "Star City",
},
Object {
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href=\\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\\"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572",
"pubDate": 2003-05-30T11:06:42.000Z,
},
Object {
"description": "Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/27.html#item571",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp",
"pubDate": 2003-05-27T08:37:32.000Z,
"title": "The Engine That Does More",
},
Object {
"description": "Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/20.html#item570",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp",
"pubDate": 2003-05-20T08:56:02.000Z,
"title": "Astronauts' Dirty Laundry",
},
],
"link": "http://liftoff.msfc.nasa.gov/",
"title": "Liftoff News",
"type": "rss",
"updated": 2003-06-10T09:41:01.000Z,
}
`;

exports[`Feeds RSS (2.0) 2`] = `
Object {
"author": "[email protected]",
"description": "Liftoff to Space Exploration.",
"id": "",
"items": Array [
Object {
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\"&gt;Star City&lt;/a&gt;.",
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\">Star City</a>.",
"id": "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",
"pubDate": 2003-06-03T09:39:21.000Z,
"title": "Star City",
},
Object {
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href=\\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\\"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.",
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href=\\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\\">partial eclipse of the Sun</a> on Saturday, May 31st.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572",
"pubDate": 2003-05-30T11:06:42.000Z,
},
Expand Down Expand Up @@ -173,14 +90,14 @@ Object {
"id": "",
"items": Array [
Object {
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\"&gt;Star City&lt;/a&gt;.",
"description": "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\\">Star City</a>.",
"id": "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573",
"link": "http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp",
"pubDate": 2003-06-03T09:39:21.000Z,
"title": "Star City",
},
Object {
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href=\\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\\"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.",
"description": "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href=\\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\\">partial eclipse of the Sun</a> on Saturday, May 31st.",
"id": "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572",
"pubDate": 2003-05-30T11:06:42.000Z,
},
Expand Down
4 changes: 2 additions & 2 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Index createDomStream 1`] = `
Array [
DataNode {
"data": "&amp;This is text",
"data": "&This is text",
"endIndex": null,
"next": DataNode {
"data": " and comments ",
Expand All @@ -25,7 +25,7 @@ Array [
"next": <tags />,
"parent": null,
"prev": DataNode {
"data": "&amp;This is text",
"data": "&This is text",
"endIndex": null,
"next": [Circular],
"parent": null,
Expand Down

0 comments on commit 8ac01e0

Please sign in to comment.