Skip to content

Commit 226cc15

Browse files
Update replace method with additional key parameter
The `replace` method now has 2 parameters: 1. domNode - The object describing the DOM node. 2. key - The array index. The `key` parameter was added because if the element had siblings, then the replaced React element should have a unique "key" prop or else React will display a warning: > Warning: Each child in an array or iterator should have a > unique "key" prop. See https://fb.me/react-warning-keys for > more information. Updated the mock data for complex html and used the `key` parameter to fix a test.
1 parent db39cf6 commit 226cc15

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

lib/dom-to-react.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function domToReact(nodes, options) {
2828

2929
// replace with custom React element (if applicable)
3030
if (isReplacePresent) {
31-
replacement = options.replace(node);
31+
replacement = options.replace(node, i); // i = key
3232

3333
if (React.isValidElement(replacement)) {
3434
result.push(replacement);
@@ -69,7 +69,7 @@ function domToReact(nodes, options) {
6969
continue;
7070
}
7171

72-
// specify a `key` prop if returning an array
72+
// specify a "key" prop if element has siblings
7373
// https://fb.me/react-warning-keys
7474
if (len > 1) {
7575
props.key = i;

test/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"multiple": "<p>foo</p><p>bar</p>",
55
"nested": "<div><p>foo <em>bar</em></p></div>",
66
"attributes": "<hr id=\"foo\" class=\"bar baz\" style=\"background: #fff; text-align: center;\" data-foo=\"bar\" />",
7-
"complex": "<html><head><title>Title</title></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px;\">Heading</h1><p>Paragraph</p><div class=\"class1 class2\">Some <em>text</em>.</div><script>alert();</script></body></html>",
7+
"complex": "<html><head><meta charset=\"utf-8\"/><title>Title</title><link rel=\"stylesheet\" href=\"style.css\"/></head><body><header id=\"header\">Header</header><h1 style=\"color:#000;font-size:42px;\">Heading</h1><hr/><p>Paragraph</p><img src=\"image.jpg\"/><div class=\"class1 class2\">Some <em>text</em>.</div><script>alert();</script></body></html>",
88
"textarea": "<textarea>foo</textarea>",
99
"script": "<script>alert(1 < 2);</script>",
1010
"img": "<img src=\"http://stat.ic/img.jpg\" alt=\"Image\"/>",

test/html-to-react.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ describe('html-to-react', function() {
7777
it('overrides the element if replace is valid', function() {
7878
var html = data.html.complex;
7979
var reactElement = Parser(html, {
80-
replace: function(node) {
80+
replace: function(node, key) {
8181
if (node.name === 'title') {
82-
return React.createElement('meta', { charSet: 'utf-8' });
82+
return React.createElement('title', { key: key }, 'Replaced Title');
8383
}
8484
}
8585
});
8686
assert.equal(
8787
helpers.render(reactElement),
88-
html.replace('<title>Title</title>', '<meta charset="utf-8"/>')
88+
html.replace('<title>Title</title>', '<title>Replaced Title</title>')
8989
);
9090
});
9191

0 commit comments

Comments
 (0)