Skip to content

Refactor shouldSetTextContent & Add tests (#11789) #11792

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMTextComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ describe('ReactDOMTextComponent', () => {
expect(el.textContent).toBe('');
});

it('can reconcile text from pre-rendered markup using dangerouslySetInnerHTML and an object with toString', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add some tests to ReactDOMServerIntegration* suite instead? They will test all possible combinations and verify those match between client-only/server-only/hydration scenarios.

const HelloObject = {toString: () => 'Hello'};
const el = document.createElement('div');
let reactEl = <p dangerouslySetInnerHTML={{__html: HelloObject}} />;
el.innerHTML = ReactDOMServer.renderToString(reactEl);

ReactDOM.hydrate(reactEl, el);
expect(el.textContent).toBe('Hello');
});

xit('can reconcile text arbitrarily split into multiple nodes', () => {
const el = document.createElement('div');
let inst = ReactDOM.render(
Expand Down
36 changes: 29 additions & 7 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,36 @@ const DOMRenderer = ReactFiberReconciler({
},

shouldSetTextContent(type: string, props: Props): boolean {
return (
type === 'textarea' ||
if (type === 'textarea') {
return true;
}

if (
typeof props.children === 'string' ||
typeof props.children === 'number' ||
(typeof props.dangerouslySetInnerHTML === 'object' &&
props.dangerouslySetInnerHTML !== null &&
typeof props.dangerouslySetInnerHTML.__html === 'string')
);
typeof props.children === 'number'
) {
return true;
}

if (
Copy link
Collaborator

Choose a reason for hiding this comment

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

Overall: we are reading props.dangerouslySetInnerHTML a lot of times here. I know old code did too, but this makes reading a bit difficult in all further conditions. Mind extracting it to a variable?

typeof props.dangerouslySetInnerHTML === 'object' &&
props.dangerouslySetInnerHTML !== null
) {
if (typeof props.dangerouslySetInnerHTML.__html === 'string') {
return true;
}

// Or allow any type of object through - this is to allow React to render
// the `toString` method of objects. (#11792)
if (
typeof props.dangerouslySetInnerHTML.__html === 'object' &&
props.dangerouslySetInnerHTML.__html !== 'null'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we comparing to a string literal here? Is this a bug? In that case it means we're missing a test that would have caught it.

) {
return true;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it strings and objects specifically that are allowed? What if __html is a number, for example?

What did React 15 do in this case? Can you point to similar logic there so we can compare the behavior?


return false;
},

shouldDeprioritizeSubtree(type: string, props: Props): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ describe('dangerouslySetInnerHTML', () => {
expect(circle.tagName).toBe('circle');
});
});

it('when rendering an object with a toString method', () => {
const container = document.createElement('div');
const HelloObject = {toString: () => 'Hello'};
const node = ReactDOM.render(
<div dangerouslySetInnerHTML={{__html: HelloObject}} />,
container,
);
expect(node.textContent).toBe('Hello');
});
});
8 changes: 4 additions & 4 deletions scripts/rollup/results.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
"filename": "react-dom.development.js",
"bundleType": "UMD_DEV",
"packageName": "react-dom",
"size": 626353,
"gzip": 144739
"size": 626271,
"gzip": 144757
},
{
"filename": "react-dom.production.min.js",
"bundleType": "UMD_PROD",
"packageName": "react-dom",
"size": 102821,
"gzip": 32649
"size": 102811,
"gzip": 32643
},
{
"filename": "react-dom.development.js",
Expand Down