Skip to content

Remove key parameter from replace and use React.cloneElement instead #18

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 3 commits into from
Aug 30, 2016
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
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"indent": [
"error",
4,
{ SwitchCase: 1 }
{ "SwitchCase": 1 }
],
"linebreak-style": [
"error",
Expand All @@ -26,8 +26,11 @@
"error",
"always"
],
"no-unused-vars": [
"error",
{ "vars": "all", "args": "none" }
],
"strict": 2,
"no-unused-vars": 2,
"no-cond-assign": 2,
"camelcase": 1
}
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,16 @@ ReactDOM.render(

### Options

#### replace(domNode, key)
#### replace(domNode)

The `replace` method allows you to swap an element with your own React element.

The method has 2 parameters:
The method has 1 parameter:
1. `domNode`: An object which shares the same schema as the output from [htmlparser2.parseDOM](https://github.com/fb55/domhandler#example).
2. `key`: A number to keep track of the element. You should set it as the ["key" prop](https://fb.me/react-warning-keys) in case your element has siblings.

```js
Parser('<p id="replace">text</p>', {
replace: function(domNode, key) {
replace: function(domNode) {
console.log(domNode);
// { type: 'tag',
// name: 'p',
Expand All @@ -88,8 +87,6 @@ Parser('<p id="replace">text</p>', {
// prev: null,
// parent: null }

console.log(key); // 0

return;
// element is not replaced because
// a valid React element is not returned
Expand All @@ -106,14 +103,13 @@ var React = require('react');
var html = '<div><p id="main">replace me</p></div>';

var reactElement = Parser(html, {
replace: function(domNode, key) {
replace: function(domNode) {
if (domNode.attribs && domNode.attribs.id === 'main') {
return React.createElement('span', {
key: key,
style: { fontSize: '42px' } },
'replaced!');
// equivalent jsx syntax:
// return <span key={key} style={{ fontSize: '42px' }}>replaced!</span>;
// return <span style={{ fontSize: '42px' }}>replaced!</span>;
}
}
});
Expand Down
7 changes: 6 additions & 1 deletion lib/dom-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ function domToReact(nodes, options) {

// replace with custom React element (if applicable)
if (isReplacePresent) {
replacement = options.replace(node, i); // i = key
replacement = options.replace(node);

if (React.isValidElement(replacement)) {
// specify a "key" prop if element has siblings
// https://fb.me/react-warning-keys
if (len > 1) {
replacement = React.cloneElement(replacement, { key: i });
}
result.push(replacement);
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions test/html-to-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ describe('html-to-react', function() {
it('overrides the element if replace is valid', function() {
var html = data.html.complex;
var reactElement = Parser(html, {
replace: function(node, key) {
replace: function(node) {
if (node.name === 'title') {
return React.createElement('title', { key: key }, 'Replaced Title');
return React.createElement('title', {}, 'Replaced Title');
}
}
});
Expand Down