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

Fix infinite loop because of props mutation #1577

Merged
merged 2 commits into from
Apr 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
4 changes: 3 additions & 1 deletion src/create-element.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import options from './options';
import { assign } from './util';

/**
* Create an virtual node (used for JSX)
Expand All @@ -9,7 +10,8 @@ import options from './options';
* @returns {import('./internal').VNode}
*/
export function createElement(type, props, children) {
if (props==null) props = {};
props = assign({}, props);

if (arguments.length>3) {
children = [children];
for (let i=3; i<arguments.length; i++) {
Expand Down
24 changes: 24 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setupRerender } from 'preact/test-utils';
import { createElement as h, render, Component } from '../../src/index';
import { setupScratch, teardown, getMixedArray, mixedArrayHTML, sortCss, serializeHtml, supportsPassiveEvents, supportsDataList } from '../_util/helpers';
import { clearLog, getLog, logCall } from '../_util/logCall';
import options from '../../src/options';

/** @jsx h */

Expand Down Expand Up @@ -975,6 +976,29 @@ describe('render()', () => {
expect(scratch.textContent).to.equal('01');
});

it('should not cause infinite loop with referentially equal props', () => {
let i = 0;
let prevDiff = options.diff;
options.diff = () => {
if (++i > 10) {
options.diff = prevDiff;
throw new Error('Infinite loop');
}
};

function App({ children, ...rest }) {
return (
<div {...rest}>
<div {...rest}>{children}</div>
</div>
);
}

render(<App>10</App>, scratch);
expect(scratch.textContent).to.equal('10');
options.diff = prevDiff;
});

describe('replaceNode parameter', () => {

function appendChildToScratch(id) {
Expand Down
4 changes: 2 additions & 2 deletions test/shared/createContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('createContext', () => {

const providerComponent = <Provider {...contextValue}>{children}</Provider>;
//expect(providerComponent).to.have.property('tag', 'Provider');
expect(providerComponent).to.have.property('props', contextValue);
expect(providerComponent).with.nested.deep.property('props.children', children);
expect(providerComponent.props.value).to.equal(contextValue.value);
expect(providerComponent.props.children).to.equal(children);
});
});
2 changes: 1 addition & 1 deletion test/shared/createElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('createElement(jsx)', () => {

it('should set VNode#props property', () => {
const props = {};
expect(h('div', props)).to.have.property('props', props);
expect(h('div', props).props).to.deep.equal(props);
});

it('should set VNode#text property', () => {
Expand Down