Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc8b224

Browse files
committedDec 4, 2018
Adding options for domParserOptions
Adding options for `domParserOptions` using Object.assign before send it directly to the parser itself; Setting up a default value for `domParserOptions`; Consider a possible refactoring, I am using github interface to update it. update: Now on keyboard, reafactoring made, Thanks to @remarkablemark about the iE hint! Tests added.
1 parent bfe2023 commit fc8b224

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed
 

‎CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
<a name="0.4.8"></a>
6+
## [0.4.8](https://github.com/remarkablemark/html-react-parser/compare/v0.4.7...v0.4.8) (2018-12-04)
7+
8+
9+
510
<a name="0.4.7"></a>
611
## [0.4.7](https://github.com/remarkablemark/html-react-parser/compare/v0.4.6...v0.4.7) (2018-09-14)
712

‎index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,41 @@ var domToReact = require('./lib/dom-to-react');
22
var htmlToDOM = require('html-dom-parser');
33

44
// decode HTML entities by default for `htmlparser2`
5-
var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false };
5+
var defaultDomParserOptions = {
6+
decodeEntities: true,
7+
lowerCaseAttributeNames: false
8+
};
69

710
/**
811
* Convert HTML string to React elements.
912
*
1013
* @param {String} html - The HTML string.
1114
* @param {Object} [options] - The additional options.
1215
* @param {Function} [options.replace] - The replace method.
16+
* @param {Object} [options.domParserOptions] - Additional domParserOptions options.
1317
* @return {ReactElement|Array}
1418
*/
1519
function HTMLReactParser(html, options) {
1620
if (typeof html !== 'string') {
1721
throw new TypeError('First argument must be a string');
1822
}
23+
24+
if (options && typeof options !== 'object') {
25+
throw new TypeError('Second argument must be an object');
26+
}
27+
28+
// adding new options to domParserOptions
29+
// var domParserOptions = Object.assign(defaultDomParserOptions, (options && options.domParserOptions) || {});
30+
var domParserOptions = defaultDomParserOptions;
31+
if (options && options.domParserOptions instanceof Object) {
32+
domParserOptions = options.domParserOptions;
33+
for (var key in defaultDomParserOptions) {
34+
if (domParserOptions[key] === undefined) {
35+
domParserOptions[key] = defaultDomParserOptions[key];
36+
}
37+
}
38+
}
39+
1940
return domToReact(htmlToDOM(html, domParserOptions), options);
2041
}
2142

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-react-parser",
3-
"version": "0.4.7",
3+
"version": "0.4.8",
44
"description": "An HTML to React parser.",
55
"author": "Mark <mark@remarkablemark.org>",
66
"main": "index.js",

‎test/helpers/data.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"img": "<img src=\"http://stat.ic/img.jpg\" alt=\"Image\"/>",
1212
"void": "<link/><meta/><img/><br/><hr/><input/>",
1313
"comment": "<!-- comment -->",
14-
"doctype": "<!DOCTYPE html>"
14+
"doctype": "<!DOCTYPE html>",
15+
"special": "<SpecialTag>inside</SpecialTag>"
1516
},
1617
"svg": {
1718
"simple": "<svg viewBox=\"0 0 512 512\" id=\"foo\">Inner</svg>",

‎test/html-to-react.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,19 @@ describe('html-to-react', () => {
109109
);
110110
});
111111
});
112+
113+
describe('domParserOptions', () => {
114+
it('lowerCaseTags passed to htmlToDOM, should drop a React warning', () => {
115+
const html = data.html.special;
116+
const closedTag = '<SpecialTag>inside</SpecialTag>';
117+
const reactElement = Parser(html, {
118+
domParserOptions: {
119+
lowerCaseTags: false
120+
}
121+
});
122+
const elementRender = render(reactElement);
123+
assert.equal(elementRender, closedTag);
124+
});
125+
});
112126
});
113127
});

0 commit comments

Comments
 (0)
Failed to load comments.