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

Single text node hot path #4523

Closed
wants to merge 2 commits 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: 9 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ function diffElementNodes(
}
}

// If the new vnode didn't have dangerouslySetInnerHTML, diff its children
if (newHtml) {
// Avoid re-applying the same '__html' if it did not changed between re-render
if (
Expand All @@ -499,6 +498,15 @@ function diffElementNodes(
}

newVNode._children = [];
} else if (
typeof newChildren === 'string' &&
typeof oldProps.children === 'string'
) {
if (newChildren !== oldProps.children) {
oldVNode._children[0]._dom.data = newChildren;
newVNode._children = oldVNode._children;
oldVNode._children = null;
}
} else {
if (oldHtml) dom.innerHTML = '';

Expand Down
6 changes: 4 additions & 2 deletions test/browser/fragments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1392,8 +1392,10 @@ describe('Fragment', () => {
'rendering from false to true'
);
expectDomLogToBe([
'<ol>450123.appendChild(<li>4)',
'<ol>501234.appendChild(<li>5)'
'<ol>450123.insertBefore(<li>0, <li>4)',
'<ol>045123.insertBefore(<li>1, <li>4)',
'<ol>014523.insertBefore(<li>2, <li>4)',
'<ol>012453.insertBefore(<li>3, <li>4)'
]);
});

Expand Down
2 changes: 1 addition & 1 deletion test/browser/keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe('keys', () => {
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd', 'swap back');
expect(getLog()).to.deep.equal(
['<ol>acbd.insertBefore(<li>c, <li>d)'],
['<ol>acbd.insertBefore(<li>b, <li>c)'],
'swap back'
);
});
Expand Down
20 changes: 20 additions & 0 deletions test/browser/lifecycles/componentWillUnmount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,25 @@ describe('Lifecycle methods', () => {
render(<Foo />, scratch);
render(null, scratch);
});

it('should only remove dom after componentWillUnmount was called with single text child', () => {
class Foo extends Component {
componentWillUnmount() {
expect(document.getElementById('foo')).to.not.equal(null);
}

render() {
return <div id="foo" />;
}
}

render(
<div>
<Foo />
</div>,
scratch
);
render(<div>Text</div>, scratch);
});
});
});
12 changes: 12 additions & 0 deletions test/browser/placeholders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,16 @@ describe('null placeholders', () => {
expect(getLog()).to.deep.equal(['<div>Test3.remove()']);
expect(ref).to.have.been.calledOnce;
});

it('should properly mount and unmount text nodes with placeholders', () => {
function App({ show = false }) {
return <div>{show && 'Hello'}</div>;
}

render(<App show />, scratch);
expect(scratch.innerHTML).to.equal('<div>Hello</div>');

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div></div>');
});
});
65 changes: 60 additions & 5 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupRerender } from 'preact/test-utils';
import { createElement, render, Component, options } from 'preact';
import { createElement, render, Component, options, Fragment } from 'preact';
import {
setupScratch,
teardown,
Expand Down Expand Up @@ -305,6 +305,28 @@ describe('render()', () => {
expect(scratch.innerHTML).to.equal('Testing, huh! How is it going?');
});

it('should render strings delimited by fragments text content', () => {
render(
<Fragment>
<Fragment>Foo</Fragment>
Bar
<Fragment>Baz</Fragment>
</Fragment>,
scratch
);
expect(scratch.innerHTML).to.equal('FooBarBaz');

render(
<Fragment>
<Fragment>Baz</Fragment>
Bar
<Fragment>Foo</Fragment>
</Fragment>,
scratch
);
expect(scratch.innerHTML).to.equal('BazBarFoo');
});

it('should render arrays of mixed elements', () => {
render(getMixedArray(), scratch);
expect(scratch.innerHTML).to.equal(mixedArrayHTML);
Expand Down Expand Up @@ -1676,10 +1698,10 @@ describe('render()', () => {
expect(getLog()).to.deep.equal([
'<div>.appendChild(#text)',
'<div>1352640.insertBefore(<div>11, <div>1)',
'<div>111352640.insertBefore(<div>1, <div>5)',
'<div>113152640.insertBefore(<div>6, <div>0)',
'<div>113152460.insertBefore(<div>2, <div>0)',
'<div>113154620.insertBefore(<div>5, <div>0)',
'<div>111352640.insertBefore(<div>3, <div>1)',
'<div>113152640.insertBefore(<div>4, <div>5)',
'<div>113145260.insertBefore(<div>6, <div>5)',
'<div>113146520.insertBefore(<div>2, <div>5)',
'<div>.appendChild(#text)',
'<div>113146250.appendChild(<div>9)',
'<div>.appendChild(#text)',
Expand Down Expand Up @@ -1813,4 +1835,37 @@ describe('render()', () => {
);
}
});

it('should correctly transition from multiple children to single text node and back', () => {
class Child extends Component {
componentDidMount() {}
componentWillUnmount() {}
render() {
return 'Child';
}
}

const proto = Child.prototype;
sinon.spy(Child.prototype, 'componentDidMount');
sinon.spy(Child.prototype, 'componentWillUnmount');

function App({ textChild = false }) {
return <div>{textChild ? 'Hello' : [<Child />, <span>b</span>]}</div>;
}

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div>Child<span>b</span></div>');
expect(proto.componentDidMount).to.have.been.calledOnce;
expect(proto.componentWillUnmount).to.have.not.been.called;

render(<App textChild />, scratch);
expect(scratch.innerHTML).to.equal('<div>Hello</div>');
expect(proto.componentDidMount).to.have.been.calledOnce;
expect(proto.componentWillUnmount).to.have.been.calledOnce;

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div>Child<span>b</span></div>');
expect(proto.componentDidMount).to.have.been.calledTwice;
expect(proto.componentWillUnmount).to.have.been.calledOnce;
});
});