From 07e10551f5febfa70075df22255cb2eb1dec5f54 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 27 Dec 2020 17:47:03 -0500 Subject: [PATCH 1/4] fix: escape tags inside of `` (html-dom-parser@1.0.0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #202 html-dom-parser 0.5.0 → 1.0.0 * https://github.com/remarkablemark/html-dom-parser/releases/tag/v1.0.0 * https://github.com/fb55/htmlparser2/releases/tag/v5.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 78608284..21a3a3ea 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "dom" ], "dependencies": { - "html-dom-parser": "0.5.0", + "html-dom-parser": "1.0.0", "react-property": "1.0.1", "style-to-js": "1.1.0" }, From 943e2d30a104c1084fbc27a09b33dd7a1e4f352f Mon Sep 17 00:00:00 2001 From: Mark <mark@remarkablemark.org> Date: Sun, 27 Dec 2020 17:52:10 -0500 Subject: [PATCH 2/4] test: add snapshot to verify tags inside of `<title>` are escaped --- test/__snapshots__/index.test.js.snap | 6 ++++++ test/data/html.js | 1 + test/index.test.js | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 658946d3..772788cd 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -1,5 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`HTMLReactParser escapes tags inside of <title> 1`] = ` +<title> + <em>text</em> + +`; + exports[`HTMLReactParser parses SVG 1`] = `

', comment: '', doctype: '', + title: '<em>text</em>', customElement: '' }; diff --git a/test/index.test.js b/test/index.test.js index 59f0ba5f..4ceb844c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -75,6 +75,10 @@ describe('HTMLReactParser', () => { const reactElement = parse('' + encodedEntities + ''); expect(reactElement.props.children).toBe(decodedEntities); }); + + it('escapes tags inside of ', () => { + expect(parse(html.title)).toMatchSnapshot(); + }); }); describe('replace option', () => { From 1dc24f11e03ea27b02ff45e1cde96ffdf2478ca9 Mon Sep 17 00:00:00 2001 From: Mark <mark@remarkablemark.org> Date: Sun, 27 Dec 2020 17:53:27 -0500 Subject: [PATCH 3/4] refactor(index): prune unnecessary option for `htmlparser2` `htmlparser2` defaults `decodeEntities` to true in v5.0.0: https://github.com/fb55/htmlparser2/releases/tag/v5.0.0 --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index c66fbbdf..3929d5a5 100644 --- a/index.js +++ b/index.js @@ -2,8 +2,7 @@ var domToReact = require('./lib/dom-to-react'); var attributesToProps = require('./lib/attributes-to-props'); var htmlToDOM = require('html-dom-parser'); -// decode HTML entities by default for `htmlparser2` -var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false }; +var domParserOptions = { lowerCaseAttributeNames: false }; /** * Converts HTML string to React elements. From 886e53d3dfd792289197e5e69ba92e422fffe54e Mon Sep 17 00:00:00 2001 From: Mark <mark@remarkablemark.org> Date: Sun, 27 Dec 2020 17:57:51 -0500 Subject: [PATCH 4/4] docs(readme): update htmlparser2 section and remove `decodeEntities` --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c9fe49cd..29276a0f 100644 --- a/README.md +++ b/README.md @@ -314,23 +314,21 @@ parse('<br>', { ### htmlparser2 -The default [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-options) are: +Along with the default [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode), the parser also sets: -```js +```json { - decodeEntities: true, - lowerCaseAttributeNames: false + "lowerCaseAttributeNames": false } ``` Since [v0.12.0](https://github.com/remarkablemark/html-react-parser/tree/v0.12.0), the htmlparser2 options can be overridden. -The following example enables [`decodeEntities`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-decodeentities) and [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode): +The following example enables [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode) but disables [`lowerCaseAttributeNames`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-lowercaseattributenames): ```js parse('<p /><p />', { htmlparser2: { - decodeEntities: true, xmlMode: true } });