From 24c0702818937fb274a50ebc293b499ce8489f82 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 3 Mar 2019 12:04:03 -0800 Subject: [PATCH] Fabric: Text measuring: Handling the case where the string is empty Summary: Surprisingly, we have some significant amount of text measuring requests where the string is empty. So, there is no need to go to platform specific layer to find that the size of those strings is zero. Reviewed By: mdvacca Differential Revision: D14297315 fbshipit-source-id: bf84cf27d5c0893262e8b27da8ff42fc77bcd6c5 --- .../fabric/attributedstring/AttributedString.cpp | 14 ++++++++++++++ .../fabric/attributedstring/AttributedString.h | 5 +++++ .../text/paragraph/ParagraphShadowNode.cpp | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/ReactCommon/fabric/attributedstring/AttributedString.cpp b/ReactCommon/fabric/attributedstring/AttributedString.cpp index 5a85cb28d7e..67d2e78258a 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.cpp +++ b/ReactCommon/fabric/attributedstring/AttributedString.cpp @@ -34,11 +34,21 @@ bool Fragment::operator!=(const Fragment &rhs) const { void AttributedString::appendFragment(const Fragment &fragment) { ensureUnsealed(); + + if (fragment.string.empty()) { + return; + } + fragments_.push_back(fragment); } void AttributedString::prependFragment(const Fragment &fragment) { ensureUnsealed(); + + if (fragment.string.empty()) { + return; + } + fragments_.insert(fragments_.begin(), fragment); } @@ -72,6 +82,10 @@ std::string AttributedString::getString() const { return string; } +bool AttributedString::isEmpty() const { + return fragments_.empty(); +} + bool AttributedString::operator==(const AttributedString &rhs) const { return fragments_ == rhs.fragments_; } diff --git a/ReactCommon/fabric/attributedstring/AttributedString.h b/ReactCommon/fabric/attributedstring/AttributedString.h index 2178e62f820..6fc1abc2533 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.h +++ b/ReactCommon/fabric/attributedstring/AttributedString.h @@ -69,6 +69,11 @@ class AttributedString : public Sealable, public DebugStringConvertible { */ std::string getString() const; + /* + * Returns `true` if the string is empty (has no any fragments). + */ + bool isEmpty() const; + bool operator==(const AttributedString &rhs) const; bool operator!=(const AttributedString &rhs) const; diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp index 13d80f4e9ac..028652ca5cb 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp @@ -59,6 +59,11 @@ void ParagraphShadowNode::updateLocalDataIfNeeded() { Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const { AttributedString attributedString = getAttributedString(); + + if (attributedString.isEmpty()) { + return {0, 0}; + } + const ParagraphAttributes paragraphAttributes = getProps()->paragraphAttributes;