From 934f36d049d28b112f0a237ad04e2c9ce8ccb459 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 24 Jan 2021 00:20:47 +0100 Subject: [PATCH 1/2] Fix HTML comments breaking hydration This PR resolves a regression when react-dom/server was used to render a Preact app. It was introduced in #2942 --- src/diff/index.js | 8 +++++++- test/browser/hydrate.test.js | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/diff/index.js b/src/diff/index.js index e4cf4c033f..b0bcd18810 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -318,7 +318,13 @@ function diffElementNodes( // // Note: This takes advantage of Text nodes having `.localName=undefined`, // which is loosely equal to Text VNodes' `.type=null`. Elements use string equality. - if (child != null && (dom == child || child.localName == nodeType)) { + if ( + child != null && + ((newVNode.type === null + ? child.nodeType === 3 + : child.localName === newVNode.type) || + dom == child) + ) { dom = child; excessDomChildren[i] = null; break; diff --git a/test/browser/hydrate.test.js b/test/browser/hydrate.test.js index a9ddd92a5f..5f6e537f95 100644 --- a/test/browser/hydrate.test.js +++ b/test/browser/hydrate.test.js @@ -445,4 +445,10 @@ describe('hydrate()', () => { '

hello baz

hello bar

' ); }); + + it('should skip comment nodes', () => { + scratch.innerHTML = '

hello foo

'; + hydrate(

hello {'foo'}

, scratch); + expect(scratch.innerHTML).to.equal('

hello foo

'); + }); }); From 5583b467d5b4bf725b11521b53d29158e5f4f20f Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 24 Jan 2021 00:49:35 +0100 Subject: [PATCH 2/2] golf -2B --- src/diff/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index b0bcd18810..da767fff04 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -320,9 +320,9 @@ function diffElementNodes( // which is loosely equal to Text VNodes' `.type=null`. Elements use string equality. if ( child != null && - ((newVNode.type === null + ((nodeType === null ? child.nodeType === 3 - : child.localName === newVNode.type) || + : child.localName === nodeType) || dom == child) ) { dom = child;