Skip to content

Commit

Permalink
Ignore HTML comments during hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored and JoviDeCroock committed Mar 12, 2023
1 parent 31e65f4 commit 11addc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ function placeChild(
oldDom = newDom.nextSibling;
}

// Skip over comment nodes. Will be removed right after calling diffChildren
// in diff so that the vnode tree matches the DOM tree again.
while (oldDom !== null && oldDom.nodeType === 8) {
oldDom = oldDom.nextSibling;
}

return oldDom;
}

Expand Down
9 changes: 9 additions & 0 deletions test/browser/hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('hydrate()', () => {
scratch
);
expect(scratch.innerHTML).to.equal('<p><i>0</i><b>1</b></p>');
expect(getLog()).to.deep.equal(['Comment.remove()']);
});

it('should reuse existing DOM when given components', () => {
Expand Down Expand Up @@ -462,5 +463,13 @@ describe('hydrate()', () => {
scratch.innerHTML = '<p>hello <!-- c -->foo</p>';
hydrate(<p>hello {'foo'}</p>, scratch);
expect(scratch.innerHTML).to.equal('<p>hello foo</p>');
expect(getLog()).to.deep.equal(['Comment.remove()']);
});

it('should skip over multiple comment nodes', () => {
scratch.innerHTML = '<p>hello <!-- a --><!-- b -->foo</p>';
hydrate(<p>hello {'foo'}</p>, scratch);
expect(scratch.innerHTML).to.equal('<p>hello foo</p>');
expect(getLog()).to.deep.equal(['Comment.remove()', 'Comment.remove()']);
});
});

0 comments on commit 11addc8

Please sign in to comment.