Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions cocos/2d/CCNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,22 +809,27 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *

// Remove '//', '/..' if exist
std::string newName = name.substr(subStrStartPos, subStrlength);


const Node* target = this;

if (searchFromParent)
{
newName.insert(0, "[[:alnum:]]+/");
if (nullptr == _parent)
{
return;
}
target = _parent;
}


if (searchRecursively)
{
// name is '//xxx'
doEnumerateRecursive(this, newName, callback);
target->doEnumerateRecursive(target, newName, callback);
}
else
{
// name is xxx
doEnumerate(newName, callback);
target->doEnumerate(newName, callback);
}
}

Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/CCNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ class CC_DLL Node : public Ref
* @param name The name to search for, supports c++11 regular expression.
* Search syntax options:
* `//`: Can only be placed at the begin of the search string. This indicates that it will search recursively.
* `..`: The search should move up to the node's parent. Can only be placed at the end of string.
* `/..`: The search should move up to the node's parent. Can only be placed at the end of string.
* `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children.
*
* @code
Expand Down
8 changes: 4 additions & 4 deletions tests/cpp-tests/Classes/NodeTest/NodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,14 +1395,14 @@ void NodeNameTest::test(float dt)
// search from parent
// name is xxx/..
i = 0;
parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
parent->getChildByName("node1")->enumerateChildren("node[[:digit:]]+/node/..", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");

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

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

// utils::findChildren()

Expand Down