Skip to content

Commit

Permalink
Fixes various issues with fast text optimization (#10355)
Browse files Browse the repository at this point in the history
* Fixes various issues with fast text optimization

In #10354, a few scenarios are described where unexpected text changes
occur out of sync with the shadow tree from RN, and some scenarios even
crash the app.

This change ensures that we only use the fast text optimization when
there is exactly one raw text child in the shadow tree, and also
switches out of the fast text optimization when the number of children
increases to greater than 1, rather than only when a text value is
appended (i.e., we also have to switch out of fast text optimization
when for a child that might be prepended at index 0).

Fixes #10354

* Change files
  • Loading branch information
rozele committed Aug 15, 2022
1 parent 59ffbf5 commit 24bde0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fixes various issues with fast text optimization",
"packageName": "react-native-windows",
"email": "erozell@outlook.com",
"dependentChangeType": "patch"
}
9 changes: 6 additions & 3 deletions vnext/Microsoft.ReactNative/Views/TextViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ class TextShadowNode final : public ShadowNodeBase {
}

auto addInline = true;
if (index == 0) {
// Only convert to fast text when exactly one child is attached to the root Text node
if (index == 0 && m_children.size() == 1) {
auto run = childNode.GetView().try_as<winrt::Run>();
if (run != nullptr) {
m_firstChildNode = &child;
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.Text(run.Text());
addInline = false;
}
} else if (index == 1 && m_firstChildNode != nullptr) {
} else if (m_firstChildNode != nullptr) {
assert(m_children.size() == 2);
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.ClearValue(xaml::Controls::TextBlock::TextProperty());
Super::AddView(*m_firstChildNode, 0);
Expand All @@ -101,7 +103,8 @@ class TextShadowNode final : public ShadowNodeBase {
}

void RemoveChildAt(int64_t indexToRemove) override {
if (indexToRemove == 0 && m_firstChildNode) {
if (m_firstChildNode) {
assert(indexToRemove == 0);
auto textBlock = this->GetView().as<xaml::Controls::TextBlock>();
textBlock.ClearValue(xaml::Controls::TextBlock::TextProperty());
m_firstChildNode = nullptr;
Expand Down

0 comments on commit 24bde0e

Please sign in to comment.