AttributedString.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #include "AttributedString.h"
  8. #include <react/debug/DebugStringConvertibleItem.h>
  9. namespace facebook {
  10. namespace react {
  11. using Fragment = AttributedString::Fragment;
  12. using Fragments = AttributedString::Fragments;
  13. #pragma mark - Fragment
  14. std::string Fragment::AttachmentCharacter() {
  15. return "\uFFFC"; // Unicode `OBJECT REPLACEMENT CHARACTER`
  16. }
  17. bool Fragment::isAttachment() const {
  18. return string == AttachmentCharacter();
  19. }
  20. bool Fragment::operator==(const Fragment &rhs) const {
  21. return std::tie(
  22. string,
  23. textAttributes,
  24. parentShadowView.tag,
  25. parentShadowView.layoutMetrics) ==
  26. std::tie(
  27. rhs.string,
  28. rhs.textAttributes,
  29. rhs.parentShadowView.tag,
  30. rhs.parentShadowView.layoutMetrics);
  31. }
  32. bool Fragment::operator!=(const Fragment &rhs) const {
  33. return !(*this == rhs);
  34. }
  35. #pragma mark - AttributedString
  36. void AttributedString::appendFragment(const Fragment &fragment) {
  37. ensureUnsealed();
  38. if (fragment.string.empty()) {
  39. return;
  40. }
  41. fragments_.push_back(fragment);
  42. }
  43. void AttributedString::prependFragment(const Fragment &fragment) {
  44. ensureUnsealed();
  45. if (fragment.string.empty()) {
  46. return;
  47. }
  48. fragments_.insert(fragments_.begin(), fragment);
  49. }
  50. void AttributedString::appendAttributedString(
  51. const AttributedString &attributedString) {
  52. ensureUnsealed();
  53. fragments_.insert(
  54. fragments_.end(),
  55. attributedString.fragments_.begin(),
  56. attributedString.fragments_.end());
  57. }
  58. void AttributedString::prependAttributedString(
  59. const AttributedString &attributedString) {
  60. ensureUnsealed();
  61. fragments_.insert(
  62. fragments_.begin(),
  63. attributedString.fragments_.begin(),
  64. attributedString.fragments_.end());
  65. }
  66. Fragments const &AttributedString::getFragments() const {
  67. return fragments_;
  68. }
  69. Fragments &AttributedString::getFragments() {
  70. return fragments_;
  71. }
  72. std::string AttributedString::getString() const {
  73. auto string = std::string{};
  74. for (const auto &fragment : fragments_) {
  75. string += fragment.string;
  76. }
  77. return string;
  78. }
  79. bool AttributedString::isEmpty() const {
  80. return fragments_.empty();
  81. }
  82. bool AttributedString::compareTextAttributesWithoutFrame(
  83. const AttributedString &rhs) const {
  84. if (fragments_.size() != rhs.fragments_.size()) {
  85. return false;
  86. }
  87. for (unsigned i = 0; i < fragments_.size(); i++) {
  88. if (fragments_[i].textAttributes != rhs.fragments_[i].textAttributes ||
  89. fragments_[i].string != rhs.fragments_[i].string) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. bool AttributedString::operator==(const AttributedString &rhs) const {
  96. return fragments_ == rhs.fragments_;
  97. }
  98. bool AttributedString::operator!=(const AttributedString &rhs) const {
  99. return !(*this == rhs);
  100. }
  101. #pragma mark - DebugStringConvertible
  102. #if RN_DEBUG_STRING_CONVERTIBLE
  103. SharedDebugStringConvertibleList AttributedString::getDebugChildren() const {
  104. auto list = SharedDebugStringConvertibleList{};
  105. for (auto &&fragment : fragments_) {
  106. auto propsList =
  107. fragment.textAttributes.DebugStringConvertible::getDebugProps();
  108. list.push_back(std::make_shared<DebugStringConvertibleItem>(
  109. "Fragment",
  110. fragment.string,
  111. SharedDebugStringConvertibleList(),
  112. propsList));
  113. }
  114. return list;
  115. }
  116. #endif
  117. } // namespace react
  118. } // namespace facebook