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

Don't hyphenate custom CSS properties for ReactDOMServer #16167

Merged
merged 8 commits into from
Jul 26, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('CSSPropertyOperations', () => {
expect(html).toContain('"-ms-transition:none;-moz-transition:none"');
});

it('should not hyphenate custom CSS property', () => {
const styles = {
'--someColor': '#000000',
};
const div = <div style={styles} />;
const html = ReactDOMServer.renderToString(div);
expect(html).toContain('"--someColor:#000000"');
});

it('should set style attribute when styles exist', () => {
const styles = {
backgroundColor: '#000',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ describe('ReactDOMServerIntegration', () => {
expect(e.style.Foo).toBe('5');
});

itRenders('camel cased custom properties', async render => {
const e = await render(<div style={{'--someColor': '#000000'}} />);
expect(e.style.SomeColor).toBe('#000000');
});

itRenders('no undefined styles', async render => {
const e = await render(
<div style={{color: undefined, width: '30px'}} />,
Expand Down
5 changes: 4 additions & 1 deletion packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ function createMarkupForStyles(styles): string | null {
}
}
if (styleValue != null) {
serialized += delimiter + processStyleName(styleName) + ':';
serialized +=
delimiter +
(isCustomProperty ? styleName : processStyleName(styleName)) +
':';
serialized += dangerousStyleValue(
styleName,
styleValue,
Expand Down
5 changes: 4 additions & 1 deletion packages/react-dom/src/shared/CSSPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function createDangerousStringForStyles(styles) {
const styleValue = styles[styleName];
if (styleValue != null) {
const isCustomProperty = styleName.indexOf('--') === 0;
serialized += delimiter + hyphenateStyleName(styleName) + ':';
serialized +=
delimiter +
(isCustomProperty ? styleName : hyphenateStyleName(styleName)) +
':';
serialized += dangerousStyleValue(
styleName,
styleValue,
Expand Down