From ed909b619c8145958a7d17596b2aef34b0f3d19b Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Fri, 22 Nov 2019 20:05:04 -0800 Subject: [PATCH] Fabric: Removing AttributedString::Fragment::shadowNode Summary: Before this change `AttributedString::Fragment` had two ShadowView objects (`shadowView` and `parentShadowView`). This diff unifies those two things into one. That allows us to save some CPU and memory and makes things a bit simpler. Besides that, now the length of NSAttributedString and AttributedString is now always the same (it's one Unicode character for an attachment for both). Changelog: [Internal] Fabric-specific internal change. Reviewed By: JoshuaGross Differential Revision: D18607658 fbshipit-source-id: 502ae244e98a52694adc0d646650f8ea0d7922ae --- .../fabric/attributedstring/AttributedString.cpp | 16 ++++++++++------ .../fabric/attributedstring/AttributedString.h | 10 ++++++++-- .../text/basetext/BaseTextShadowNode.cpp | 3 ++- .../platform/ios/RCTAttributedTextUtils.mm | 5 ++--- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ReactCommon/fabric/attributedstring/AttributedString.cpp b/ReactCommon/fabric/attributedstring/AttributedString.cpp index 1902c7a9af3..974d214ea69 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.cpp +++ b/ReactCommon/fabric/attributedstring/AttributedString.cpp @@ -17,13 +17,17 @@ using Fragments = AttributedString::Fragments; #pragma mark - Fragment +std::string Fragment::AttachmentCharacter() { + return "\uFFFC"; // Unicode `OBJECT REPLACEMENT CHARACTER` +} + +bool Fragment::isAttachment() const { + return string == AttachmentCharacter(); +} + bool Fragment::operator==(const Fragment &rhs) const { - return std::tie(string, textAttributes, shadowView, parentShadowView) == - std::tie( - rhs.string, - rhs.textAttributes, - rhs.shadowView, - rhs.parentShadowView); + return std::tie(string, textAttributes, parentShadowView) == + std::tie(rhs.string, rhs.textAttributes, rhs.parentShadowView); } bool Fragment::operator!=(const Fragment &rhs) const { diff --git a/ReactCommon/fabric/attributedstring/AttributedString.h b/ReactCommon/fabric/attributedstring/AttributedString.h index 08e4bd11567..7a952e4bfcf 100644 --- a/ReactCommon/fabric/attributedstring/AttributedString.h +++ b/ReactCommon/fabric/attributedstring/AttributedString.h @@ -35,11 +35,18 @@ class AttributedString : public Sealable, public DebugStringConvertible { public: class Fragment { public: + static std::string AttachmentCharacter(); + std::string string; TextAttributes textAttributes; - ShadowView shadowView; ShadowView parentShadowView; + /* + * Returns true is the Fragment represents an attachment. + * Equivalent to `string == AttachmentCharacter()`. + */ + bool isAttachment() const; + bool operator==(const Fragment &rhs) const; bool operator!=(const Fragment &rhs) const; }; @@ -99,7 +106,6 @@ struct hash { 0, fragment.string, fragment.textAttributes, - fragment.shadowView, fragment.parentShadowView); } }; diff --git a/ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp b/ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp index 927445be33e..d873d6025a6 100644 --- a/ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp +++ b/ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp @@ -54,7 +54,8 @@ AttributedString BaseTextShadowNode::getAttributedString( // Any other kind of ShadowNode auto fragment = AttributedString::Fragment{}; - fragment.shadowView = ShadowView(*childNode); + fragment.string = AttributedString::Fragment::AttachmentCharacter(); + fragment.parentShadowView = ShadowView(*childNode); fragment.textAttributes = textAttributes; attributedString.appendFragment(fragment); } diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm index 73b1c0e5133..b5ffa1ab382 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm @@ -224,9 +224,8 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt for (auto fragment : attributedString.getFragments()) { NSAttributedString *nsAttributedStringFragment; - auto layoutMetrics = fragment.shadowView.layoutMetrics; - - if (layoutMetrics != EmptyLayoutMetrics) { + if (fragment.isAttachment()) { + auto layoutMetrics = fragment.parentShadowView.layoutMetrics; CGRect bounds = {.origin = {.x = layoutMetrics.frame.origin.x, .y = layoutMetrics.frame.origin.y}, .size = {.width = layoutMetrics.frame.size.width, .height = layoutMetrics.frame.size.height}};