Skip to content

Commit 47a85cf

Browse files
windyGexdevelopit
authored andcommitted
Only execute appendChild when vnode length greater than origin node. (preactjs#717)
1 parent b80bec3 commit 47a85cf

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/vdom/diff.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function innerDiffNode(dom, vchildren, context, mountAll, isHydrating) {
222222
child = idiff(child, vchild, context, mountAll);
223223

224224
if (child && child!==dom) {
225-
if (i>=len) {
225+
if (i>len) {
226226
dom.appendChild(child);
227227
}
228228
else if (child!==originalChildren[i]) {

test/browser/render.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,45 @@ describe('render()', () => {
582582
let html = scratch.firstElementChild.firstElementChild.outerHTML;
583583
expect(sortAttributes(html)).to.equal(sortAttributes('<input type="range" min="0" max="100" list="steplist">'));
584584
});
585+
586+
it('should not execute append operation when child is at last', (done) => {
587+
let input;
588+
class TodoList extends Component {
589+
constructor(props) {
590+
super(props);
591+
this.state = { todos: [], text: '' };
592+
this.setText = this.setText.bind(this);
593+
this.addTodo = this.addTodo.bind(this);
594+
}
595+
setText(e) {
596+
this.setState({ text: e.target.value });
597+
}
598+
addTodo() {
599+
let { todos, text } = this.state;
600+
todos = todos.concat({ text });
601+
this.setState({ todos, text: '' });
602+
}
603+
render() {
604+
const {todos, text} = this.state;
605+
return (
606+
<div onKeyDown={ this.addTodo }>
607+
{ todos.map( todo => (<div>{todo.text}</div> )) }
608+
<input value={text} onInput={this.setText} ref={(i) => input = i} />
609+
</div>
610+
);
611+
}
612+
}
613+
const root = render(<TodoList />, scratch);
614+
input.focus();
615+
input.value = 1;
616+
root._component.setText({
617+
target: input
618+
});
619+
root._component.addTodo();
620+
expect(document.activeElement).to.equal(input);
621+
setTimeout(() =>{
622+
expect(/1/.test(scratch.innerHTML)).to.equal(true);
623+
done();
624+
}, 10);
625+
});
585626
});

0 commit comments

Comments
 (0)