Skip to content

Commit 24b04e5

Browse files
authored
sync #20007 to v4 (#20210)
* sync #20007 to v4 * Fixing a bug in Node::enumerateChildren (#20045) This patch fixes an issue that if both // (recursive enumeration) and .. (starting from parent node) are specified, Node::enumerateChildren does not honor the latter and starts searching from current node rather than parent node.
1 parent 8a64e6f commit 24b04e5

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

cocos/2d/CCNode.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,22 +809,27 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *
809809

810810
// Remove '//', '/..' if exist
811811
std::string newName = name.substr(subStrStartPos, subStrlength);
812-
812+
813+
const Node* target = this;
814+
813815
if (searchFromParent)
814816
{
815-
newName.insert(0, "[[:alnum:]]+/");
817+
if (nullptr == _parent)
818+
{
819+
return;
820+
}
821+
target = _parent;
816822
}
817823

818-
819824
if (searchRecursively)
820825
{
821826
// name is '//xxx'
822-
doEnumerateRecursive(this, newName, callback);
827+
target->doEnumerateRecursive(target, newName, callback);
823828
}
824829
else
825830
{
826831
// name is xxx
827-
doEnumerate(newName, callback);
832+
target->doEnumerate(newName, callback);
828833
}
829834
}
830835

cocos/2d/CCNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ class CC_DLL Node : public Ref
794794
* @param name The name to search for, supports c++11 regular expression.
795795
* Search syntax options:
796796
* `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively.
797-
* `..`: The search should move up to the node's parent. Can only be placed at the end of string.
797+
* `/..`: The search should move up to the node's parent. Can only be placed at the end of string.
798798
* `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children.
799799
*
800800
* @code

tests/cpp-tests/Classes/NodeTest/NodeTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,14 +1395,14 @@ void NodeNameTest::test(float dt)
13951395
// search from parent
13961396
// name is xxx/..
13971397
i = 0;
1398-
parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
1398+
parent->getChildByName("node1")->enumerateChildren("node[[:digit:]]+/node/..", [&i](Node* node) -> bool {
13991399
++i;
14001400
return true;
14011401
});
14021402
CCAssert(i == 1, "");
14031403

14041404
i = 0;
1405-
parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
1405+
parent->getChildByName("node1")->enumerateChildren("node[[:digit:]]+/node/..", [&i](Node* node) -> bool {
14061406
++i;
14071407
return false;
14081408
});
@@ -1441,11 +1441,11 @@ void NodeNameTest::test(float dt)
14411441
CCAssert(i == 1, "");
14421442

14431443
i = 0;
1444-
parent->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
1444+
parent->getChildByName("node1")->enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
14451445
++i;
14461446
return false;
14471447
});
1448-
CCAssert(i == 100, "");
1448+
CCAssert(i == 110, "");
14491449

14501450
// utils::findChildren()
14511451

0 commit comments

Comments
 (0)