From 8ed068a86fcb63dfa983c4f7387a17bdf707613e Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 6 Oct 2022 09:38:34 +0200 Subject: [PATCH 1/3] Fix duplicate ids when Fragments are involved --- hooks/src/index.js | 5 +-- hooks/test/browser/useId.test.js | 54 +++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/hooks/src/index.js b/hooks/src/index.js index b120d0add5..9f5d098139 100644 --- a/hooks/src/index.js +++ b/hooks/src/index.js @@ -1,4 +1,4 @@ -import { Fragment, options } from 'preact'; +import { options } from 'preact'; /** @type {number} */ let currentIndex; @@ -30,7 +30,8 @@ options._diff = vnode => { if ( typeof vnode.type === 'function' && !vnode._mask && - vnode.type !== Fragment + // Ignore root Fragment node + vnode._parent !== null ) { vnode._mask = (vnode._parent && vnode._parent._mask ? vnode._parent._mask : '') + diff --git a/hooks/test/browser/useId.test.js b/hooks/test/browser/useId.test.js index d19567b81e..175e366873 100644 --- a/hooks/test/browser/useId.test.js +++ b/hooks/test/browser/useId.test.js @@ -272,6 +272,58 @@ describe('useId', () => { scratch.innerHTML = rtsOutput; hydrate(, scratch); - expect(rtsOutput === scratch.innerHTML).to.equal(true); + expect(rtsOutput).to.equal(scratch.innerHTML); + }); + + it('should be unique across Fragments', () => { + const ids = []; + function Foo() { + const id = useId(); + ids.push(id); + return

{id}

; + } + + function App() { + return ( +
+ + + + +
+ ); + } + + render(, scratch); + + expect(ids[0]).not.to.equal(ids[1]); + }); + + it('should match implicite Fragments with RTS', () => { + function Foo() { + const id = useId(); + return

{id}

; + } + + function Bar(props) { + return props.children; + } + + function App() { + return ( + + + + + + + ); + } + + const rtsOutput = rts(); + + scratch.innerHTML = rtsOutput; + hydrate(, scratch); + expect(rtsOutput).to.equal(scratch.innerHTML); }); }); From 5e31c9abc57e97e21e8957ca1d848aff433469b1 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 6 Oct 2022 20:43:06 +0200 Subject: [PATCH 2/3] Add useId top level Fragment test case --- hooks/test/browser/useId.test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/hooks/test/browser/useId.test.js b/hooks/test/browser/useId.test.js index 175e366873..232dffcf0a 100644 --- a/hooks/test/browser/useId.test.js +++ b/hooks/test/browser/useId.test.js @@ -326,4 +326,30 @@ describe('useId', () => { hydrate(, scratch); expect(rtsOutput).to.equal(scratch.innerHTML); }); + + it('should skip component top level Fragment child', () => { + const Wrapper = ({ children }) => { + return {children}; + }; + + function Foo() { + const id = useId(); + return

{id}

; + } + + function App() { + const id = useId(); + return ( +
+

{id}

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

P481

P476951

'); + }); }); From a08f182cdac0e5de2a4d9e3b63d75d7b33e89019 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 6 Oct 2022 21:20:50 +0200 Subject: [PATCH 3/3] Update preact-render-to-string --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2866f5f478..f8dae7b24d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -52,7 +52,7 @@ "mocha": "^8.2.1", "npm-merge-driver-install": "^1.1.1", "npm-run-all": "^4.0.0", - "preact-render-to-string": "^5.2.4", + "preact-render-to-string": "^5.2.5", "prettier": "^1.18.2", "prop-types": "^15.7.2", "sade": "^1.7.4", @@ -12519,9 +12519,9 @@ } }, "node_modules/preact-render-to-string": { - "version": "5.2.4", - "resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.4.tgz", - "integrity": "sha512-iIPHb3BXUQ3Za6KNhkjN/waq11Oh+QWWtAgN3id3LrL+cszH3DYh8TxJPNQ6Aogsbu4JsqdJLBZltwPFpG6N6w==", + "version": "5.2.5", + "resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.5.tgz", + "integrity": "sha512-rEBn42C3Wh+AjPxXUbDkb6xw0cTJQgxdYlp6ytUR1uBZF647Wn6ykkopMeQlRl7ggX+qnYYjZ4Hs1abZENl7ww==", "dev": true, "dependencies": { "pretty-format": "^3.8.0" @@ -24848,9 +24848,9 @@ "peer": true }, "preact-render-to-string": { - "version": "5.2.4", - "resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.4.tgz", - "integrity": "sha512-iIPHb3BXUQ3Za6KNhkjN/waq11Oh+QWWtAgN3id3LrL+cszH3DYh8TxJPNQ6Aogsbu4JsqdJLBZltwPFpG6N6w==", + "version": "5.2.5", + "resolved": "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.5.tgz", + "integrity": "sha512-rEBn42C3Wh+AjPxXUbDkb6xw0cTJQgxdYlp6ytUR1uBZF647Wn6ykkopMeQlRl7ggX+qnYYjZ4Hs1abZENl7ww==", "dev": true, "requires": { "pretty-format": "^3.8.0" diff --git a/package.json b/package.json index b70c292cdf..b7a71517f2 100644 --- a/package.json +++ b/package.json @@ -294,7 +294,7 @@ "mocha": "^8.2.1", "npm-merge-driver-install": "^1.1.1", "npm-run-all": "^4.0.0", - "preact-render-to-string": "^5.2.4", + "preact-render-to-string": "^5.2.5", "prettier": "^1.18.2", "prop-types": "^15.7.2", "sade": "^1.7.4",