Skip to content

Commit

Permalink
Refactor and move YGNodeToString implementation to different file
Browse files Browse the repository at this point in the history
Reviewed By: emilsjolander

Differential Revision: D6397372

fbshipit-source-id: 79e701efe7f19db6dac1aea6328ebf0ac84a7ac3
  • Loading branch information
priteshrnandgaonkar authored and facebook-github-bot committed Nov 23, 2017
1 parent e7bd0f0 commit 0984f29
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 409 deletions.
12 changes: 12 additions & 0 deletions React/React.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@
3DFE0D1B1DF8575800459392 /* YGMacros.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 130A77041DF767AF001F9587 /* YGMacros.h */; };
3DFE0D1C1DF8575800459392 /* Yoga.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 130A77081DF767AF001F9587 /* Yoga.h */; };
3EDCA8A51D3591E700450C31 /* RCTErrorInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */; };
5376C5E41FC6DDBC0083513D /* YGNodePrint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5376C5E11FC6DDB20083513D /* YGNodePrint.cpp */; };
5376C5E51FC6DDBD0083513D /* YGNodePrint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5376C5E11FC6DDB20083513D /* YGNodePrint.cpp */; };
5376C5E61FC6DDC10083513D /* YGNodePrint.h in Headers */ = {isa = PBXBuildFile; fileRef = 5376C5E01FC6DDB20083513D /* YGNodePrint.h */; };
5376C5E71FC6DDC20083513D /* YGNodePrint.h in Headers */ = {isa = PBXBuildFile; fileRef = 5376C5E01FC6DDB20083513D /* YGNodePrint.h */; };
53D123971FBF1DF5001B8A10 /* libyoga.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D3C059A1DE3340900C268FA /* libyoga.a */; };
53D1239A1FBF1EF2001B8A10 /* YGEnums.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53CBF1861FB4FE80002CBB31 /* YGEnums.cpp */; };
53D1239B1FBF1EF4001B8A10 /* YGEnums.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53CBF1861FB4FE80002CBB31 /* YGEnums.cpp */; };
Expand Down Expand Up @@ -2051,6 +2055,8 @@
3EDCA8A21D3591E700450C31 /* RCTErrorCustomizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTErrorCustomizer.h; sourceTree = "<group>"; };
3EDCA8A31D3591E700450C31 /* RCTErrorInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTErrorInfo.h; sourceTree = "<group>"; };
3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTErrorInfo.m; sourceTree = "<group>"; };
5376C5E01FC6DDB20083513D /* YGNodePrint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGNodePrint.h; sourceTree = "<group>"; };
5376C5E11FC6DDB20083513D /* YGNodePrint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGNodePrint.cpp; sourceTree = "<group>"; };
53CBF1851FB4FE80002CBB31 /* Yoga-internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Yoga-internal.h"; sourceTree = "<group>"; };
53CBF1861FB4FE80002CBB31 /* YGEnums.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGEnums.cpp; sourceTree = "<group>"; };
53CBF1871FB4FE80002CBB31 /* Yoga.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Yoga.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2215,6 +2221,8 @@
130A77021DF767AF001F9587 /* yoga */ = {
isa = PBXGroup;
children = (
5376C5E11FC6DDB20083513D /* YGNodePrint.cpp */,
5376C5E01FC6DDB20083513D /* YGNodePrint.h */,
53CBF1861FB4FE80002CBB31 /* YGEnums.cpp */,
53CBF1881FB4FE80002CBB31 /* YGNodeList.cpp */,
53CBF1851FB4FE80002CBB31 /* Yoga-internal.h */,
Expand Down Expand Up @@ -3100,6 +3108,7 @@
3DFE0D161DF8574D00459392 /* YGEnums.h in Headers */,
3DFE0D171DF8574D00459392 /* YGMacros.h in Headers */,
3DFE0D181DF8574D00459392 /* YGNodeList.h in Headers */,
5376C5E71FC6DDC20083513D /* YGNodePrint.h in Headers */,
3DFE0D191DF8574D00459392 /* Yoga.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -3177,6 +3186,7 @@
133957881DF76D3500EC27BE /* YGEnums.h in Headers */,
1339578B1DF76D3500EC27BE /* Yoga.h in Headers */,
1339578A1DF76D3500EC27BE /* YGNodeList.h in Headers */,
5376C5E61FC6DDC10083513D /* YGNodePrint.h in Headers */,
133957891DF76D3500EC27BE /* YGMacros.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -4053,6 +4063,7 @@
53D123A01FBF1EFF001B8A10 /* Yoga.cpp in Sources */,
53D1239A1FBF1EF2001B8A10 /* YGEnums.cpp in Sources */,
53D1239C1FBF1EF7001B8A10 /* YGNodeList.cpp in Sources */,
5376C5E41FC6DDBC0083513D /* YGNodePrint.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -4063,6 +4074,7 @@
53D123A11FBF1EFF001B8A10 /* Yoga.cpp in Sources */,
53D1239B1FBF1EF4001B8A10 /* YGEnums.cpp in Sources */,
53D1239D1FBF1EF7001B8A10 /* YGNodeList.cpp in Sources */,
5376C5E51FC6DDBD0083513D /* YGNodePrint.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
12 changes: 12 additions & 0 deletions React/ReactLegacy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@
3DFE0D1B1DF8575800459392 /* YGMacros.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 130A77041DF767AF001F9587 /* YGMacros.h */; };
3DFE0D1C1DF8575800459392 /* Yoga.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 130A77081DF767AF001F9587 /* Yoga.h */; };
3EDCA8A51D3591E700450C31 /* RCTErrorInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */; };
53330EE71FC6EE74008D7FA9 /* YGNodePrint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53330EE41FC6EE70008D7FA9 /* YGNodePrint.cpp */; };
53330EE81FC6EE75008D7FA9 /* YGNodePrint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53330EE41FC6EE70008D7FA9 /* YGNodePrint.cpp */; };
53330EEA1FC6EE7F008D7FA9 /* YGNodePrint.h in Headers */ = {isa = PBXBuildFile; fileRef = 53330EE31FC6EE70008D7FA9 /* YGNodePrint.h */; };
53330EEB1FC6EE7F008D7FA9 /* YGNodePrint.h in Headers */ = {isa = PBXBuildFile; fileRef = 53330EE31FC6EE70008D7FA9 /* YGNodePrint.h */; };
53CBF1BF1FB50263002CBB31 /* Yoga-internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 53CBF1BB1FB50262002CBB31 /* Yoga-internal.h */; };
53CBF1C01FB50263002CBB31 /* YGEnums.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53CBF1BC1FB50263002CBB31 /* YGEnums.cpp */; };
53CBF1C11FB50263002CBB31 /* Yoga.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53CBF1BD1FB50263002CBB31 /* Yoga.cpp */; };
Expand Down Expand Up @@ -1347,6 +1351,8 @@
3EDCA8A21D3591E700450C31 /* RCTErrorCustomizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTErrorCustomizer.h; sourceTree = "<group>"; };
3EDCA8A31D3591E700450C31 /* RCTErrorInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTErrorInfo.h; sourceTree = "<group>"; };
3EDCA8A41D3591E700450C31 /* RCTErrorInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTErrorInfo.m; sourceTree = "<group>"; };
53330EE31FC6EE70008D7FA9 /* YGNodePrint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YGNodePrint.h; sourceTree = "<group>"; };
53330EE41FC6EE70008D7FA9 /* YGNodePrint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGNodePrint.cpp; sourceTree = "<group>"; };
53CBF1BB1FB50262002CBB31 /* Yoga-internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Yoga-internal.h"; sourceTree = "<group>"; };
53CBF1BC1FB50263002CBB31 /* YGEnums.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGEnums.cpp; sourceTree = "<group>"; };
53CBF1BD1FB50263002CBB31 /* Yoga.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Yoga.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1464,6 +1470,8 @@
130A77021DF767AF001F9587 /* yoga */ = {
isa = PBXGroup;
children = (
53330EE41FC6EE70008D7FA9 /* YGNodePrint.cpp */,
53330EE31FC6EE70008D7FA9 /* YGNodePrint.h */,
53CBF1BC1FB50263002CBB31 /* YGEnums.cpp */,
53CBF1BE1FB50263002CBB31 /* YGNodeList.cpp */,
53CBF1BB1FB50262002CBB31 /* Yoga-internal.h */,
Expand Down Expand Up @@ -2048,6 +2056,7 @@
3DFE0D171DF8574D00459392 /* YGMacros.h in Headers */,
3DFE0D181DF8574D00459392 /* YGNodeList.h in Headers */,
3DFE0D191DF8574D00459392 /* Yoga.h in Headers */,
53330EEB1FC6EE7F008D7FA9 /* YGNodePrint.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2077,6 +2086,7 @@
1339578B1DF76D3500EC27BE /* Yoga.h in Headers */,
1339578A1DF76D3500EC27BE /* YGNodeList.h in Headers */,
133957891DF76D3500EC27BE /* YGMacros.h in Headers */,
53330EEA1FC6EE7F008D7FA9 /* YGNodePrint.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -2615,13 +2625,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
53330EE71FC6EE74008D7FA9 /* YGNodePrint.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
3D3C06181DE3340C00C268FA /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
53330EE81FC6EE75008D7FA9 /* YGNodePrint.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
3 changes: 2 additions & 1 deletion ReactCommon/yoga/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ LOCAL_MODULE := yogacore
LOCAL_SRC_FILES := \
yoga/Yoga.cpp \
yoga/YGEnums.cpp \
yoga/YGNodeList.cpp
yoga/YGNodeList.cpp \
yoga/YGNodePrint.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
Expand Down
220 changes: 220 additions & 0 deletions ReactCommon/yoga/yoga/YGNodePrint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

#include "YGNodePrint.h"
#include <stdarg.h>
#include "YGEnums.h"
#include "Yoga-internal.h"

namespace facebook {
namespace yoga {
typedef std::string string;

static void indent(string* base, uint32_t level) {
for (uint32_t i = 0; i < level; ++i) {
base->append(" ");
}
}

static bool areFourValuesEqual(const YGValue four[4]) {
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
YGValueEqual(four[0], four[3]);
}

static void appendFormatedString(string* str, const char* fmt, ...) {
char buffer[1024];
va_list args;
va_start(args, fmt);
va_list argsCopy;
va_copy(argsCopy, args);
va_end(args);
vsnprintf(buffer, 1024, fmt, argsCopy);
va_end(argsCopy);
string result = string(buffer);
str->append(result);
}

static void
appendFloatIfNotUndefined(string* base, const string key, const float num) {
if (!YGFloatIsUndefined(num)) {
appendFormatedString(base, "%s: %g; ", key.c_str(), num);
}
}

static void appendNumberIfNotUndefined(
string* base,
const string key,
const YGValue* const number) {
if (number->unit != YGUnitUndefined) {
if (number->unit == YGUnitAuto) {
base->append(key + ": auto; ");
} else {
string unit = number->unit == YGUnitPoint ? "px" : "%%";
appendFormatedString(
base, "%s: %g%s; ", key.c_str(), number->value, unit.c_str());
}
}
}

static void appendNumberIfNotAuto(
string* base,
const string key,
const YGValue* const number) {
if (number->unit != YGUnitAuto) {
appendNumberIfNotUndefined(base, key, number);
}
}

static void appendNumberIfNotZero(
string* base,
const string str,
const YGValue* const number) {
if (!YGFloatsEqual(number->value, 0)) {
appendNumberIfNotUndefined(base, str, number);
}
}

static void appendEdges(string* base, const string key, const YGValue* edges) {
if (areFourValuesEqual(edges)) {
appendNumberIfNotZero(base, key, &edges[YGEdgeLeft]);
} else {
for (int edge = YGEdgeLeft; edge != YGEdgeAll; ++edge) {
string str = key + "-" + YGEdgeToString(static_cast<YGEdge>(edge));
appendNumberIfNotZero(base, str, &edges[edge]);
}
}
}

static void appendEdgeIfNotUndefined(
string* base,
const string str,
const YGValue* edges,
const YGEdge edge) {
appendNumberIfNotUndefined(
base, str, YGComputedEdgeValue(edges, edge, &YGValueUndefined));
}

void YGNodeToString(
std::string* str,
YGNodeRef node,
YGPrintOptions options,
uint32_t level) {
indent(str, level);
appendFormatedString(str, "<div ");
if (node->print != nullptr) {
node->print(node);
}

if (options & YGPrintOptionsLayout) {
appendFormatedString(str, "layout=\"");
appendFormatedString(
str, "width: %g; ", node->layout.dimensions[YGDimensionWidth]);
appendFormatedString(
str, "height: %g; ", node->layout.dimensions[YGDimensionHeight]);
appendFormatedString(str, "top: %g; ", node->layout.position[YGEdgeTop]);
appendFormatedString(str, "left: %g;", node->layout.position[YGEdgeLeft]);
appendFormatedString(str, "\" ");
}

if (options & YGPrintOptionsStyle) {
appendFormatedString(str, "style=\"");
if (node->style.flexDirection != gYGNodeDefaults.style.flexDirection) {
appendFormatedString(
str,
"flex-direction: %s; ",
YGFlexDirectionToString(node->style.flexDirection));
}
if (node->style.justifyContent != gYGNodeDefaults.style.justifyContent) {
appendFormatedString(
str,
"justify-content: %s; ",
YGJustifyToString(node->style.justifyContent));
}
if (node->style.alignItems != gYGNodeDefaults.style.alignItems) {
appendFormatedString(
str, "align-items: %s; ", YGAlignToString(node->style.alignItems));
}
if (node->style.alignContent != gYGNodeDefaults.style.alignContent) {
appendFormatedString(
str,
"align-content: %s; ",
YGAlignToString(node->style.alignContent));
}
if (node->style.alignSelf != gYGNodeDefaults.style.alignSelf) {
appendFormatedString(
str, "align-self: %s; ", YGAlignToString(node->style.alignSelf));
}
appendFloatIfNotUndefined(str, "flex-grow", node->style.flexGrow);
appendFloatIfNotUndefined(str, "flex-shrink", node->style.flexShrink);
appendNumberIfNotAuto(str, "flex-basis", &node->style.flexBasis);
appendFloatIfNotUndefined(str, "flex", node->style.flex);

if (node->style.flexWrap != gYGNodeDefaults.style.flexWrap) {
appendFormatedString(
str, "flexWrap: %s; ", YGWrapToString(node->style.flexWrap));
}

if (node->style.overflow != gYGNodeDefaults.style.overflow) {
appendFormatedString(
str, "overflow: %s; ", YGOverflowToString(node->style.overflow));
}

if (node->style.display != gYGNodeDefaults.style.display) {
appendFormatedString(
str, "display: %s; ", YGDisplayToString(node->style.display));
}
appendEdges(str, "margin", node->style.margin);
appendEdges(str, "padding", node->style.padding);
appendEdges(str, "border", node->style.border);

appendNumberIfNotAuto(
str, "width", &node->style.dimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "height", &node->style.dimensions[YGDimensionHeight]);
appendNumberIfNotAuto(
str, "max-width", &node->style.maxDimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "max-height", &node->style.maxDimensions[YGDimensionHeight]);
appendNumberIfNotAuto(
str, "min-width", &node->style.minDimensions[YGDimensionWidth]);
appendNumberIfNotAuto(
str, "min-height", &node->style.minDimensions[YGDimensionHeight]);

if (node->style.positionType != gYGNodeDefaults.style.positionType) {
appendFormatedString(
str,
"position: %s; ",
YGPositionTypeToString(node->style.positionType));
}

appendEdgeIfNotUndefined(str, "left", node->style.position, YGEdgeLeft);
appendEdgeIfNotUndefined(str, "right", node->style.position, YGEdgeRight);
appendEdgeIfNotUndefined(str, "top", node->style.position, YGEdgeTop);
appendEdgeIfNotUndefined(str, "bottom", node->style.position, YGEdgeBottom);
appendFormatedString(str, "\" ");

if (node->measure != nullptr) {
appendFormatedString(str, "has-custom-measure=\"true\"");
}
}
appendFormatedString(str, ">");

const uint32_t childCount = YGNodeListCount(node->children);
if (options & YGPrintOptionsChildren && childCount > 0) {
for (uint32_t i = 0; i < childCount; i++) {
appendFormatedString(str, "\n");
YGNodeToString(str, YGNodeGetChild(node, i), options, level + 1);
}
appendFormatedString(str, "\n");
indent(str, level);
}
appendFormatedString(str, "</div>");
}
} // namespace yoga
} // namespace facebook
24 changes: 24 additions & 0 deletions ReactCommon/yoga/yoga/YGNodePrint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include <string>

#include "Yoga.h"

namespace facebook {
namespace yoga {

void YGNodeToString(
std::string* str,
YGNodeRef node,
YGPrintOptions options,
uint32_t level);

} // namespace yoga
} // namespace facebook
Loading

0 comments on commit 0984f29

Please sign in to comment.