diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e9f34e5..37842335 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ 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. + +## [0.4.8](https://github.com/remarkablemark/html-react-parser/compare/v0.4.7...v0.4.8) (2018-12-04) + + + ## [0.4.7](https://github.com/remarkablemark/html-react-parser/compare/v0.4.6...v0.4.7) (2018-09-14) diff --git a/index.js b/index.js index 6fcb8ece..be87c66e 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,10 @@ var domToReact = require('./lib/dom-to-react'); var htmlToDOM = require('html-dom-parser'); // decode HTML entities by default for `htmlparser2` -var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false }; +var defaultDomParserOptions = { + decodeEntities: true, + lowerCaseAttributeNames: false +}; /** * Convert HTML string to React elements. @@ -10,12 +13,30 @@ var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false }; * @param {String} html - The HTML string. * @param {Object} [options] - The additional options. * @param {Function} [options.replace] - The replace method. + * @param {Object} [options.domParserOptions] - Additional domParserOptions options. * @return {ReactElement|Array} */ function HTMLReactParser(html, options) { if (typeof html !== 'string') { throw new TypeError('First argument must be a string'); } + + if (options && typeof options !== 'object') { + throw new TypeError('Second argument must be an object'); + } + + // adding new options to domParserOptions + // var domParserOptions = Object.assign(defaultDomParserOptions, (options && options.domParserOptions) || {}); + var domParserOptions = defaultDomParserOptions; + if (options && options.domParserOptions instanceof Object) { + domParserOptions = options.domParserOptions; + for (var key in defaultDomParserOptions) { + if (domParserOptions[key] === undefined) { + domParserOptions[key] = defaultDomParserOptions[key]; + } + } + } + return domToReact(htmlToDOM(html, domParserOptions), options); } diff --git a/package.json b/package.json index 9b501915..1648e2dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-react-parser", - "version": "0.4.7", + "version": "0.4.8", "description": "An HTML to React parser.", "author": "Mark ", "main": "index.js", diff --git a/test/helpers/data.json b/test/helpers/data.json index 73a42d7e..c6efd2b0 100644 --- a/test/helpers/data.json +++ b/test/helpers/data.json @@ -11,7 +11,8 @@ "img": "\"Image\"/", "void": "

", "comment": "", - "doctype": "" + "doctype": "", + "special": "inside" }, "svg": { "simple": "Inner", diff --git a/test/html-to-react.js b/test/html-to-react.js index 1d3430cd..54cd6516 100644 --- a/test/html-to-react.js +++ b/test/html-to-react.js @@ -109,5 +109,19 @@ describe('html-to-react', () => { ); }); }); + + describe('domParserOptions', () => { + it('lowerCaseTags passed to htmlToDOM, should drop a React warning', () => { + const html = data.html.special; + const closedTag = 'inside'; + const reactElement = Parser(html, { + domParserOptions: { + lowerCaseTags: false + } + }); + const elementRender = render(reactElement); + assert.equal(elementRender, closedTag); + }); + }); }); });