2018-05-07 17:43:08 -07:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-07 17:43:08 -07:00
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AttributedString.h"
|
|
|
|
|
2018-11-10 14:19:08 -08:00
|
|
|
#include <react/debug/DebugStringConvertibleItem.h>
|
2018-05-07 17:43:08 -07:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
using Fragment = AttributedString::Fragment;
|
|
|
|
using Fragments = AttributedString::Fragments;
|
|
|
|
|
2018-09-14 15:16:59 -07:00
|
|
|
#pragma mark - Fragment
|
|
|
|
|
|
|
|
bool Fragment::operator==(const Fragment &rhs) const {
|
2018-11-27 18:29:39 -08:00
|
|
|
return std::tie(string, textAttributes, shadowView, parentShadowView) ==
|
2018-10-05 11:01:08 -07:00
|
|
|
std::tie(
|
|
|
|
rhs.string,
|
|
|
|
rhs.textAttributes,
|
2018-11-27 18:29:39 -08:00
|
|
|
rhs.shadowView,
|
|
|
|
rhs.parentShadowView);
|
2018-09-14 15:16:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Fragment::operator!=(const Fragment &rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - AttributedString
|
|
|
|
|
2018-05-07 17:43:08 -07:00
|
|
|
void AttributedString::appendFragment(const Fragment &fragment) {
|
|
|
|
ensureUnsealed();
|
|
|
|
fragments_.push_back(fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AttributedString::prependFragment(const Fragment &fragment) {
|
|
|
|
ensureUnsealed();
|
|
|
|
fragments_.insert(fragments_.begin(), fragment);
|
|
|
|
}
|
|
|
|
|
2018-10-05 11:01:08 -07:00
|
|
|
void AttributedString::appendAttributedString(
|
|
|
|
const AttributedString &attributedString) {
|
2018-05-07 17:43:08 -07:00
|
|
|
ensureUnsealed();
|
2018-10-05 11:01:08 -07:00
|
|
|
fragments_.insert(
|
|
|
|
fragments_.end(),
|
|
|
|
attributedString.fragments_.begin(),
|
|
|
|
attributedString.fragments_.end());
|
2018-05-07 17:43:08 -07:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:01:08 -07:00
|
|
|
void AttributedString::prependAttributedString(
|
|
|
|
const AttributedString &attributedString) {
|
2018-05-07 17:43:08 -07:00
|
|
|
ensureUnsealed();
|
2018-10-05 11:01:08 -07:00
|
|
|
fragments_.insert(
|
|
|
|
fragments_.begin(),
|
|
|
|
attributedString.fragments_.begin(),
|
|
|
|
attributedString.fragments_.end());
|
2018-05-07 17:43:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<Fragment> &AttributedString::getFragments() const {
|
|
|
|
return fragments_;
|
|
|
|
}
|
|
|
|
|
2018-07-17 17:51:17 -07:00
|
|
|
std::string AttributedString::getString() const {
|
2018-10-05 11:01:08 -07:00
|
|
|
auto string = std::string{};
|
2018-07-17 17:51:17 -07:00
|
|
|
for (const auto &fragment : fragments_) {
|
|
|
|
string += fragment.string;
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2018-09-14 15:16:59 -07:00
|
|
|
bool AttributedString::operator==(const AttributedString &rhs) const {
|
2018-11-22 00:48:38 -08:00
|
|
|
return fragments_ == rhs.fragments_;
|
2018-09-14 15:16:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AttributedString::operator!=(const AttributedString &rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
2018-05-07 17:43:08 -07:00
|
|
|
#pragma mark - DebugStringConvertible
|
|
|
|
|
2018-09-28 10:24:38 -07:00
|
|
|
#if RN_DEBUG_STRING_CONVERTIBLE
|
2018-05-07 17:43:08 -07:00
|
|
|
SharedDebugStringConvertibleList AttributedString::getDebugChildren() const {
|
2018-10-05 11:01:08 -07:00
|
|
|
auto list = SharedDebugStringConvertibleList{};
|
2018-05-07 17:43:08 -07:00
|
|
|
|
|
|
|
for (auto &&fragment : fragments_) {
|
2018-10-05 11:01:08 -07:00
|
|
|
auto propsList =
|
|
|
|
fragment.textAttributes.DebugStringConvertible::getDebugProps();
|
2018-05-07 17:43:08 -07:00
|
|
|
|
2018-10-05 11:01:08 -07:00
|
|
|
list.push_back(std::make_shared<DebugStringConvertibleItem>(
|
2018-05-07 17:43:08 -07:00
|
|
|
"Fragment",
|
|
|
|
fragment.string,
|
|
|
|
SharedDebugStringConvertibleList(),
|
2018-10-05 11:01:08 -07:00
|
|
|
propsList));
|
2018-05-07 17:43:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2018-09-28 10:24:38 -07:00
|
|
|
#endif
|
2018-05-07 17:43:08 -07:00
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|