From 7daee68c0eebf8b1e787e7356eecff6f93709f37 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 20 Jul 2023 15:58:04 +0200 Subject: [PATCH] Teact: Fix `textContent` optimization --- src/components/test/TestSetContent.tsx | 24 ++++++++++++++++++++++++ src/lib/teact/teact-dom.ts | 9 ++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/components/test/TestSetContent.tsx diff --git a/src/components/test/TestSetContent.tsx b/src/components/test/TestSetContent.tsx new file mode 100644 index 000000000..61c2e71ae --- /dev/null +++ b/src/components/test/TestSetContent.tsx @@ -0,0 +1,24 @@ +import type { FC } from '../../lib/teact/teact'; +import React, { useState } from '../../lib/teact/teact'; + +// 1. Make sure "First line" is rendered even if followed by a component with single text. +// 2. Make sure it then can be normally removed (target is preserved). +// 3. Make sure "Last line" is also rendered. + +const Text: FC = () => { + return 'text component'; +}; + +export function App() { + const [withFirstLine, setWithFirstLine] = useState(true); + + return ( +
setWithFirstLine((current) => !current)}> + {withFirstLine && 'First line'} + + Last line +
+ ); +} + +export default App; diff --git a/src/lib/teact/teact-dom.ts b/src/lib/teact/teact-dom.ts index 6370f5efd..99dd0ca0b 100644 --- a/src/lib/teact/teact-dom.ts +++ b/src/lib/teact/teact-dom.ts @@ -129,9 +129,12 @@ function renderWithVirtual( mountChildren(parentEl, $new as VirtualElementComponent | VirtualElementFragment, { nextSibling, fragment }); } else { - const canSetTextContent = ( - !fragment && !nextSibling && $newAsReal.type === VirtualType.Text && $parent.children.length === 1 - ); + const canSetTextContent = !fragment + && !nextSibling + && $newAsReal.type === VirtualType.Text + && $parent.children.length === 1 + && !parentEl.firstChild; + if (canSetTextContent) { parentEl.textContent = $newAsReal.value; $newAsReal.target = parentEl.firstChild!;