Files
react-native/ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp
Ramanpreet Nara 4a4e083e2a Refactor BaseTextShadowNode::getAttributedString to recurse on SharedShadowNode
Summary: Previously, `BaseTextShadowNode::getAttributedString` used to recurse on a list of `SharedShadowNode`s (i.e: the children). In the `RawText` base case of this recursion, we'll need to record the parent of the current `RawText` (so that we can dispatch the `onPress` event to it). Therefore, we need to start recursing using the `SharedShadowNode` itself, and not its children.

Reviewed By: shergin

Differential Revision: D9696908

fbshipit-source-id: dbf3f9c21a7ae4de421d0355c4e5900b3947dc2a
2018-09-10 11:41:30 -07:00

57 lines
1.8 KiB
C++

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "BaseTextShadowNode.h"
#include <fabric/components/text/RawTextShadowNode.h>
#include <fabric/components/text/RawTextProps.h>
#include <fabric/components/text/TextShadowNode.h>
#include <fabric/components/text/TextProps.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
namespace facebook {
namespace react {
AttributedString BaseTextShadowNode::getAttributedString(
const TextAttributes &textAttributes,
const SharedShadowNode &parentNode
) const {
AttributedString attributedString;
for (const auto &childNode : parentNode->getChildren()) {
// RawShadowNode
auto rawTextShadowNode = std::dynamic_pointer_cast<const RawTextShadowNode>(childNode);
if (rawTextShadowNode) {
AttributedString::Fragment fragment;
fragment.string = rawTextShadowNode->getProps()->text;
fragment.textAttributes = textAttributes;
attributedString.appendFragment(fragment);
continue;
}
// TextShadowNode
auto textShadowNode = std::dynamic_pointer_cast<const TextShadowNode>(childNode);
if (textShadowNode) {
TextAttributes localTextAttributes = textAttributes;
localTextAttributes.apply(textShadowNode->getProps()->textAttributes);
attributedString.appendAttributedString(textShadowNode->getAttributedString(localTextAttributes, textShadowNode));
continue;
}
// Any other kind of ShadowNode
AttributedString::Fragment fragment;
fragment.shadowNode = childNode;
fragment.textAttributes = textAttributes;
attributedString.appendFragment(fragment);
}
return attributedString;
}
} // namespace react
} // namespace facebook