Skip to content

Commit

Permalink
Adding options for domParserOptions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rntgspr committed Dec 4, 2018
1 parent bfe2023 commit fc8b224
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a name="0.4.8"></a>
## [0.4.8](https://github.com/remarkablemark/html-react-parser/compare/v0.4.7...v0.4.8) (2018-12-04)



<a name="0.4.7"></a>
## [0.4.7](https://github.com/remarkablemark/html-react-parser/compare/v0.4.6...v0.4.7) (2018-09-14)

Expand Down
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@ 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.
*
* @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);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-react-parser",
"version": "0.4.7",
"version": "0.4.8",
"description": "An HTML to React parser.",
"author": "Mark <[email protected]>",
"main": "index.js",
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"img": "<img src=\"http://stat.ic/img.jpg\" alt=\"Image\"/>",
"void": "<link/><meta/><img/><br/><hr/><input/>",
"comment": "<!-- comment -->",
"doctype": "<!DOCTYPE html>"
"doctype": "<!DOCTYPE html>",
"special": "<SpecialTag>inside</SpecialTag>"
},
"svg": {
"simple": "<svg viewBox=\"0 0 512 512\" id=\"foo\">Inner</svg>",
Expand Down
14 changes: 14 additions & 0 deletions test/html-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<SpecialTag>inside</SpecialTag>';
const reactElement = Parser(html, {
domParserOptions: {
lowerCaseTags: false
}
});
const elementRender = render(reactElement);
assert.equal(elementRender, closedTag);
});
});
});
});

0 comments on commit fc8b224

Please sign in to comment.