Skip to content

fix: escape tags inside of <title> (upgrade html-dom-parser to 1.0.0) #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`HTMLReactParser escapes tags inside of <title> 1`] = `
<title>
&lt;em&gt;text&lt;/em&gt;
</title>
`;

exports[`HTMLReactParser parses SVG 1`] = `
<svg
height="400"
Expand Down
1 change: 1 addition & 0 deletions test/data/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
void: '<link/><meta/><img/><br/><hr/><input/>',
comment: '<!-- comment -->',
doctype: '<!DOCTYPE html>',
title: '<title><em>text</em></title>',
customElement:
'<custom-element class="myClass" custom-attribute="value" style="-o-transition: all .5s; line-height: 1;"></custom-element>'
};
4 changes: 4 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ describe('HTMLReactParser', () => {
const reactElement = parse('<i>' + encodedEntities + '</i>');
expect(reactElement.props.children).toBe(decodedEntities);
});

it('escapes tags inside of <title>', () => {
expect(parse(html.title)).toMatchSnapshot();
});
});

describe('replace option', () => {
Expand Down