Skip to content

Commit

Permalink
Allow MeasureFunc to be set to NULL no matter how many children a nod…
Browse files Browse the repository at this point in the history
…e has.

Summary: Within `UIView+CSSLayout`, we often nil out the measure function of a node because view hierarchy can often change. Unfortunately, this causes us to hit an assert which crashes the app. Instead, lets the measure func to be set to NULL, regardless of how many children a node might have.

Reviewed By: emilsjolander

Differential Revision: D4148727

fbshipit-source-id: 79a0f3ef1bf7b1dce9a14de96f870e35c042b78b
  • Loading branch information
d16r authored and Facebook Github Bot committed Nov 9, 2016
1 parent 502f3c7 commit e54af5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions CSSLayout/CSSLayout.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,13 @@ static void _CSSNodeMarkDirty(const CSSNodeRef node) {
}

void CSSNodeSetMeasureFunc(const CSSNodeRef node, CSSMeasureFunc measureFunc) {
CSS_ASSERT(CSSNodeChildCount(node) == 0, "Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc;
// You can always NULLify the measure function of a node.
if (measureFunc == NULL) {
node->measure = NULL;
} else {
CSS_ASSERT(CSSNodeChildCount(node) == 0, "Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc;
}
}

CSSMeasureFunc CSSNodeGetMeasureFunc(const CSSNodeRef node) {
Expand Down
14 changes: 13 additions & 1 deletion tests/CSSLayoutMeasureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ TEST(CSSLayoutTest, cannot_add_child_to_node_with_measure_func) {

const CSSNodeRef root_child0 = CSSNodeNew();
ASSERT_DEATH(CSSNodeInsertChild(root, root_child0, 0), "Cannot add child.*");
CSSNodeFreeRecursive(root);
}

TEST(CSSLayoutTest, cannot_add_measure_func_to_non_leaf_node) {
TEST(CSSLayoutTest, cannot_add_nonnull_measure_func_to_non_leaf_node) {
const CSSNodeRef root = CSSNodeNew();
const CSSNodeRef root_child0 = CSSNodeNew();
CSSNodeInsertChild(root, root_child0, 0);

ASSERT_DEATH(CSSNodeSetMeasureFunc(root, _measure), "Cannot set measure function.*");
CSSNodeFreeRecursive(root);
}

TEST(CSSLayoutTest, can_nullify_measure_func_on_any_node) {
const CSSNodeRef root = CSSNodeNew();
CSSNodeInsertChild(root, CSSNodeNew(), 0);

CSSNodeSetMeasureFunc(root, NULL);
ASSERT_TRUE(CSSNodeGetMeasureFunc(root) == NULL);
CSSNodeFreeRecursive(root);
}

#endif

0 comments on commit e54af5e

Please sign in to comment.