Skip to content

Commit 470a958

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Introduce toDynamic conversion for ParagraphLocalData class
Summary: This diff introduces a way to convert ParagraphLocalData object to dynamic objects Reviewed By: shergin Differential Revision: D9801892 fbshipit-source-id: e50217042a216ea67f28178bb80b136cbb8fb195
1 parent c5276ef commit 470a958

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

ReactCommon/fabric/attributedstring/tests/AttributedStringTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#include <fabric/graphics/conversions.h>
1414
#include <gtest/gtest.h>
1515
#include <assert.h>
16-
#include <string>
17-
#include <iostream>
16+
1817
namespace facebook {
1918
namespace react {
2019

ReactCommon/fabric/components/text/BUCK

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fb_xplat_cxx_test(
8181
"-Wall",
8282
],
8383
contacts = ["oncall+react_native@xmail.facebook.com"],
84-
platforms = APPLE,
84+
platforms = (ANDROID, APPLE),
8585
deps = [
8686
"xplat//folly:molly",
8787
"xplat//third-party/gmock:gtest",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#include <fabric/attributedstring/conversions.h>
3+
#include <fabric/components/text/ParagraphLocalData.h>
4+
#include <folly/dynamic.h>
5+
6+
namespace facebook {
7+
namespace react {
8+
9+
inline folly::dynamic toDynamic(const ParagraphLocalData &paragraphLocalData) {
10+
folly::dynamic newLocalData = folly::dynamic::object();
11+
newLocalData["attributedString"] = toDynamic(paragraphLocalData.getAttributedString());
12+
return newLocalData;
13+
}
14+
15+
}
16+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
8+
#include <memory>
9+
10+
#include <fabric/attributedstring/AttributedString.h>
11+
#include <fabric/attributedstring/TextAttributes.h>
12+
#include <fabric/attributedstring/primitives.h>
13+
#include <fabric/components/text/conversions.h>
14+
#include <fabric/components/text/ParagraphLocalData.h>
15+
#include <gtest/gtest.h>
16+
#include <assert.h>
17+
18+
namespace facebook {
19+
namespace react {
20+
21+
TEST(ParagraphLocalDataTest, testSomething) {
22+
auto attString = AttributedString();
23+
auto fragment = AttributedString::Fragment();
24+
fragment.string = "test";
25+
26+
auto text = TextAttributes();
27+
text.foregroundColor = {colorFromComponents({100/255.0, 153/255.0, 253/255.0, 1.0})};
28+
text.opacity = 0.5;
29+
text.fontStyle = FontStyle::Italic;
30+
text.fontWeight = FontWeight::Thin;
31+
text.fontVariant = FontVariant::TabularNums;
32+
fragment.textAttributes = text;
33+
attString.prependFragment(fragment);
34+
35+
auto paragraphLocalData = ParagraphLocalData();
36+
paragraphLocalData.setAttributedString(attString);
37+
38+
auto result = toDynamic(paragraphLocalData)["attributedString"];
39+
40+
assert(result["string"] == fragment.string);
41+
auto textAttribute = result["fragments"][0]["textAttributes"];
42+
assert(textAttribute["foregroundColor"] == toDynamic(text.foregroundColor));
43+
assert(textAttribute["opacity"] == text.opacity);
44+
assert(textAttribute["fontStyle"] == toString(*text.fontStyle));
45+
assert(textAttribute["fontWeight"] == toString(*text.fontWeight));
46+
}
47+
48+
}
49+
}

0 commit comments

Comments
 (0)