-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Ignore CR/LF differences when warning about markup mismatch #11119
Conversation
Deploy preview ready! Built with commit 72e67ff |
I haven’t thought about attributes though. I guess they also need it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all you want to do is ignore the warning your logic should be able to be self-contained completely in warnForTextDifference
and fully behind a __DEV__
flag so we don't need to ship this logic nor use it in production.
I.e. we shouldn't need to change the behavior of the isDifferent
flag if we don't expect to patch it up.
if (normalizedClientText === serverText) { | ||
return false; | ||
} | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return value is meant to indicate that it should patch it up. So this says that we will patch up the DOM if it differs, which is not what your comment says.
It's always going to "pass" because we normalize HTML anyway. Except that it won't pass because we intentionally don't patch up dangerouslyInnerHTML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. I love the test cases especially!
var normalizeMarkupForTextOrAttribute = function(markup: mixed): string { | ||
const markupString = typeof markup === 'string' | ||
? markup | ||
: '' + (markup: any); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super proud but I couldn't get Flow to understand I'm just trying to cast to string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String(markup)
They took some head scratching 😛 |
Thanks for the fix, that was quick! |
Nvm, failed my reading comprehension of the source code. |
I believe the PR normalises both now. |
Fixes #11103.
According to the spec, HTML parser turns
\r\n
or lone\r
into\n
.Since we compare JS app string (which might have
\r
) with the representation parsed from HTML (which can't), we have a false positive text mismatch warning.I am solving this by adding a second check. It's only activated when we have a mismatch, so it only penalizes users with mismatching markup, or users with
\r
in content.I opted not to patch up the markup when the only difference is\r
normalization. This is because we never did that before, and nobody complained. So it's probably unobservable to the user anyway (even though it's observable from the DOM—you can't have\r
by settinginnerHTML
or parsing, but you can if youcreateTextNode
s with it directly).An alternative could be to always normalize\r
when creating text nodes (and patch up markup if we encounter it). This would also make the behavior consistent betweeninnerHTML
andcreateTextNode
path on the client side.It seems like we can always do the latter if we want to, but the former is an unobtrusive fix that can be applied now.Updated it to just ignore the warning, but still patch up. Production logic doesn't change now.
Later updated to normalize both server and client values before emitting the warning. I started stripping both null and "replacement" character (browser sometimes turns null into it) since that's the only other corner case I know.