Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow auto-generated node names in PopupMenu::add_submenu_item #84668

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
7 changes: 6 additions & 1 deletion core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4699,11 +4699,16 @@ String String::property_name_encode() const {

static const char32_t invalid_node_name_characters[] = { '.', ':', '@', '/', '\"', UNIQUE_NODE_PREFIX[0], 0 };

String String::get_invalid_node_name_characters() {
String String::get_invalid_node_name_characters(bool p_allow_internal) {
// Do not use this function for critical validation.
String r;
const char32_t *c = invalid_node_name_characters;
while (*c) {
if (p_allow_internal && *c == '@') {
c++;
continue;
}

if (c != invalid_node_name_characters) {
r += " ";
}
Expand Down
2 changes: 1 addition & 1 deletion core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class String {
String property_name_encode() const;

// node functions
static String get_invalid_node_name_characters();
static String get_invalid_node_name_characters(bool p_allow_internal = false);
String validate_node_name() const;
String validate_identifier() const;
String validate_filename() const;
Expand Down
6 changes: 5 additions & 1 deletion scene/gui/popup_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,11 @@ void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, cons
}

void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
ERR_FAIL_COND_MSG(p_submenu.validate_node_name() != p_submenu, "Invalid node name for submenu, the following characters are not allowed:\n" + String::get_invalid_node_name_characters());
String submenu_name_safe = p_submenu.replace("@", "_"); // Allow special characters for auto-generated names.
if (submenu_name_safe.validate_node_name() != submenu_name_safe) {
ERR_FAIL_MSG(vformat("Invalid node name '%s' for a submenu, the following characters are not allowed:\n%s", p_submenu, String::get_invalid_node_name_characters(true)));
}

Item item;
item.text = p_label;
item.xl_text = atr(p_label);
Expand Down
Loading