Skip to content

Commit 52ad6bc

Browse files
committed
Merge pull request #4324 from drd/improve-warning-in-react-mount
Improve warning in react mount
2 parents f0caf89 + d2e8449 commit 52ad6bc

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/renderers/dom/client/ReactMount.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,29 @@ var ReactMount = {
880880
checksum
881881
);
882882

883-
var diffIndex = firstDifferenceIndex(markup, rootMarkup);
883+
var normalizedMarkup = markup;
884+
if (__DEV__) {
885+
// because rootMarkup is retrieved from the DOM, various normalizations
886+
// will have occurred which will not be present in `markup`. Here,
887+
// insert markup into a <div> or <iframe> depending on the container
888+
// type to perform the same normalizations before comparing.
889+
var normalizer;
890+
if (container.nodeType === ELEMENT_NODE_TYPE) {
891+
normalizer = document.createElement('div');
892+
normalizer.innerHTML = markup;
893+
normalizedMarkup = normalizer.innerHTML;
894+
} else {
895+
normalizer = document.createElement('iframe');
896+
document.body.appendChild(normalizer);
897+
normalizer.contentDocument.write(markup);
898+
normalizedMarkup = normalizer.contentDocument.documentElement.outerHTML;
899+
document.body.removeChild(normalizer);
900+
}
901+
}
902+
903+
var diffIndex = firstDifferenceIndex(normalizedMarkup, rootMarkup);
884904
var difference = ' (client) ' +
885-
markup.substring(diffIndex - 20, diffIndex + 20) +
905+
normalizedMarkup.substring(diffIndex - 20, diffIndex + 20) +
886906
'\n (server) ' + rootMarkup.substring(diffIndex - 20, diffIndex + 20);
887907

888908
invariant(

src/renderers/dom/client/__tests__/ReactMount-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ describe('ReactMount', function() {
156156
);
157157
});
158158

159+
it('should account for escaping on a checksum mismatch', function () {
160+
var div = document.createElement('div');
161+
var markup = React.renderToString(
162+
<div>This markup contains an nbsp entity: &nbsp; server text</div>);
163+
div.innerHTML = markup;
164+
165+
spyOn(console, 'error');
166+
React.render(
167+
<div>This markup contains an nbsp entity: &nbsp; client text</div>,
168+
div
169+
);
170+
expect(console.error.calls.length).toBe(1);
171+
expect(console.error.calls[0].args[0]).toContain(
172+
' (client) nbsp entity: &nbsp; client text</div>\n' +
173+
' (server) nbsp entity: &nbsp; server text</div>'
174+
);
175+
});
176+
159177
if (WebComponents !== undefined) {
160178
it('should allow mounting/unmounting to document fragment container',
161179
function() {

0 commit comments

Comments
 (0)