Skip to content

Commit

Permalink
Fixes an issue where inferno-server renders empty style attribute if
Browse files Browse the repository at this point in the history
provided style prop is empty object
  • Loading branch information
Havunen committed Mar 2, 2024
1 parent 26af472 commit 192a650
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
style={{ 'background-color': null, 'border-bottom-color': null }}
/>
),
result: '<div style=""></div>',
result: '<div></div>',
},
{
description: 'Should style attribute if null',
Expand All @@ -449,6 +449,31 @@ describe('SSR Creation Queue Streams - (non-JSX)', () => {
template: () => <div style={{ opacity: 0.8 }} />,
result: '<div style="opacity:0.8;"></div>',
},
{
description: 'Should not render empty style attribute #1',
template: () => <div style={{ }} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #2',
template: () => <div style={null} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #3',
template: () => <div style={false} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #4',
template: () => <div style={0} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #5',
template: () => <div style={true} />,
result: '<div></div>',
},
{
description: 'Should render style opacity #2',
template: () => <div style="opacity:0.8;" />,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ describe('SSR Creation Streams - (non-JSX)', () => {
template: () => createElement('div', { style: { opacity: 0.8 } }),
result: '<div style="opacity:0.8;"></div>'
},
{
description: 'Should not render empty style attribute #1',
template: () => <div style={{ }} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #2',
template: () => <div style={null} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #3',
template: () => <div style={false} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #4',
template: () => <div style={0} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #5',
template: () => <div style={true} />,
result: '<div></div>',
},
{
description: 'Should render div className as number',
template: () => createElement('div', { className: 123 }),
Expand Down Expand Up @@ -250,7 +275,7 @@ describe('SSR Creation Streams - (non-JSX)', () => {
{
description: 'Should not render null styles',
template: () => <div style={{ 'background-color': null, 'border-bottom-color': null }} />,
result: '<div style=""></div>'
result: '<div></div>'
},
{
description: 'Should style attribute if null',
Expand Down
27 changes: 26 additions & 1 deletion packages/inferno-server/__tests__/creation.spec.server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('SSR Creation (JSX)', () => {
{
description: 'Should not render null styles',
template: () => <div style={{ 'background-color': null, 'border-bottom-color': null }} />,
result: '<div style=""></div>'
result: '<div></div>'
},
{
description: 'Should style attribute if null',
Expand All @@ -134,6 +134,31 @@ describe('SSR Creation (JSX)', () => {
template: () => <div style="opacity:0.8;" />,
result: '<div style="opacity:0.8;"></div>'
},
{
description: 'Should not render empty style attribute #1',
template: () => <div style={{ }} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #2',
template: () => <div style={null} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #3',
template: () => <div style={false} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #4',
template: () => <div style={0} />,
result: '<div></div>',
},
{
description: 'Should not render empty style attribute #5',
template: () => <div style={true} />,
result: '<div></div>',
},
{
description: 'Should render div className as number',
template: () => <div className={123} />,
Expand Down
12 changes: 11 additions & 1 deletion packages/inferno-server/src/prop-renderers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isString, isStringOrNumber } from 'inferno-shared';

export function renderStylesToString(styles: string | object): string {
function parseStyleAsString(styles: string | object): string {
if (isString(styles)) {
return styles;
} else {
Expand All @@ -15,3 +15,13 @@ export function renderStylesToString(styles: string | object): string {
return renderedString;
}
}

export function renderStyleAttribute(styles: string | object): string {
const stylesString = parseStyleAsString(styles);

if (stylesString) {
return ` style="${stylesString}"`;
}

return '';
}
6 changes: 2 additions & 4 deletions packages/inferno-server/src/renderToString.queuestream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'inferno-shared';
import { ChildFlags, VNodeFlags } from 'inferno-vnode-flags';
import { Readable } from 'stream';
import { renderStylesToString } from './prop-renderers';
import { renderStyleAttribute } from './prop-renderers';
import {
createDerivedState,
escapeText,
Expand Down Expand Up @@ -227,9 +227,7 @@ export class RenderQueueStream extends Readable {
break;
case 'style':
if (!isNullOrUndef(props.style)) {
renderedString += ` style="${renderStylesToString(
props.style,
)}"`;
renderedString += renderStyleAttribute(props.style);
}
break;
case 'children':
Expand Down
4 changes: 2 additions & 2 deletions packages/inferno-server/src/renderToString.stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'inferno-shared';
import { ChildFlags, VNodeFlags } from 'inferno-vnode-flags';
import { Readable } from 'stream';
import { renderStylesToString } from './prop-renderers';
import { renderStyleAttribute } from './prop-renderers';
import {
createDerivedState,
escapeText,
Expand Down Expand Up @@ -209,7 +209,7 @@ export class RenderStream extends Readable {
break;
case 'style':
if (!isNullOrUndef(props.style)) {
renderedString += ` style="${renderStylesToString(props.style)}"`;
renderedString += renderStyleAttribute(props.style);
}
break;
case 'children':
Expand Down
4 changes: 2 additions & 2 deletions packages/inferno-server/src/renderToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
throwError,
} from 'inferno-shared';
import { ChildFlags, VNodeFlags } from 'inferno-vnode-flags';
import { renderStylesToString } from './prop-renderers';
import { renderStyleAttribute } from './prop-renderers';
import {
createDerivedState,
escapeText,
Expand Down Expand Up @@ -124,7 +124,7 @@ function renderVNodeToString(vNode, parent, context): string {
break;
case 'style':
if (!isNullOrUndef(props.style)) {
renderedString += ` style="${renderStylesToString(props.style)}"`;
renderedString += renderStyleAttribute(props.style);
}
break;
case 'children':
Expand Down

0 comments on commit 192a650

Please sign in to comment.