Skip to content
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

Protect void elements from translations which try to set their children #155

Merged
merged 1 commit into from
Feb 19, 2018

Conversation

stasm
Copy link
Contributor

@stasm stasm commented Feb 9, 2018

A broken translation may accidentally set a value which <Localized> will normally insert as a child of its wrapped component. For void elements we need to actively forbid this because React throws when trying to set children of void elements, breaking the entire app.

@stasm stasm force-pushed the localized-protect-void branch from 828b1b0 to 6ce6ca3 Compare February 9, 2018 16:26
@stasm
Copy link
Contributor Author

stasm commented Feb 9, 2018

@hkasemir Would you mind reviewing this?

Copy link
Contributor

@hkasemir hkasemir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a thought about if we should consider reporting the improperly formatted message, but otherwise, looks good to me!

// "void element tags must neither have `children` nor use
// `dangerouslySetInnerHTML`" error.
if (elem.type in VOID_ELEMENTS) {
return cloneElement(elem, localizedProps);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance we'd want to log a warning to the console to indicate an invalid message format? Or do we prefer silently handling the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to tackle the problem of error reporting in a separate PR.

);

assert.ok(wrapper.contains(
<input title="TITLE" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

'source': true,
'track': true,
'wbr': true
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also a quick search of void elements produced a larger list than this:
https://github.com/wooorm/html-void-elements/

any reason we're using this smaller list?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though this does look suspiciously exactly like what is in the React code: https://github.com/facebook/react/blob/master/packages/react-dom/src/shared/omittedCloseTags.js

with the addition of menuitem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice find in React's source. I'll use their file instead of the void-elements package because what we really care about here is not a complete list of void elements but rather the list that React uses to throw errors.

@stasm stasm force-pushed the localized-protect-void branch 2 times, most recently from 08d6fbc to 2874a99 Compare February 19, 2018 15:57
A broken translation may accidentally set a value which <Localized> will
normally insert as a child of its wrapped component. For void elements we need
to actively forbid this because React throws when trying to set children of
void elements, breaking the entire app.
@stasm stasm force-pushed the localized-protect-void branch from 2874a99 to 67cd61f Compare February 19, 2018 16:53
@stasm stasm merged commit 6f1d6a8 into projectfluent:master Feb 19, 2018
@stasm stasm deleted the localized-protect-void branch February 19, 2018 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants