From c5276ef816e3739220c98ce2358076ace07b9c9b Mon Sep 17 00:00:00 2001 From: David Vacca Date: Sun, 16 Sep 2018 23:46:09 -0700 Subject: [PATCH] Introduce toDynamic conversion for AttributedString class Summary: This diff introduces a way to convert AttributedString object to dynamic objects Reviewed By: shergin Differential Revision: D9801438 fbshipit-source-id: b762f54917ae90bf53c7f9d07f63b876d1265ece --- .../fabric/attributedstring/conversions.h | 17 +++++++++- .../tests/AttributedStringTest.cpp | 34 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ReactCommon/fabric/attributedstring/conversions.h b/ReactCommon/fabric/attributedstring/conversions.h index d798a0a40..a9d5a985c 100644 --- a/ReactCommon/fabric/attributedstring/conversions.h +++ b/ReactCommon/fabric/attributedstring/conversions.h @@ -8,9 +8,10 @@ #pragma once #include +#include +#include #include #include -#include #include #include #include @@ -276,5 +277,19 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) { return _textAttributes; } +inline folly::dynamic toDynamic(const AttributedString &attributedString) { + auto value = folly::dynamic::object(); + auto fragments = folly::dynamic::array(); + for (auto fragment : attributedString.getFragments()) { + folly::dynamic dynamicFragment = folly::dynamic::object(); + dynamicFragment["string"] = fragment.string; + dynamicFragment["textAttributes"] = toDynamic(fragment.textAttributes); + fragments.push_back(dynamicFragment); + } + value("fragments", fragments); + value("string", attributedString.getString()); + return value; +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/attributedstring/tests/AttributedStringTest.cpp b/ReactCommon/fabric/attributedstring/tests/AttributedStringTest.cpp index 8369a7bcf..effec4e99 100644 --- a/ReactCommon/fabric/attributedstring/tests/AttributedStringTest.cpp +++ b/ReactCommon/fabric/attributedstring/tests/AttributedStringTest.cpp @@ -7,8 +7,40 @@ #include +#include +#include +#include +#include #include +#include +#include +#include +namespace facebook { +namespace react { TEST(AttributedStringTest, testSomething) { - // TODO + auto attString = new AttributedString(); + auto fragment = new AttributedString::Fragment(); + fragment->string = "test"; + + auto text = new TextAttributes(); + text->foregroundColor = {colorFromComponents({100/255.0, 153/255.0, 200/255.0, 1.0})}; + text->opacity = 0.5; + text->fontStyle = FontStyle::Italic; + text->fontWeight = FontWeight::Thin; + text->fontVariant = FontVariant::TabularNums; + fragment->textAttributes = *text; + + attString->prependFragment(*fragment); + + auto result = toDynamic(*attString); + assert(result["string"] == fragment->string); + auto textAttribute = result["fragments"][0]["textAttributes"]; + assert(textAttribute["foregroundColor"] == toDynamic(text->foregroundColor)); + assert(textAttribute["opacity"] == text->opacity); + assert(textAttribute["fontStyle"] == toString(*text->fontStyle)); + assert(textAttribute["fontWeight"] == toString(*text->fontWeight)); +} + +} }