From e5373c5cb04b8d32fda800ea09c62cf15bd82576 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Thu, 28 Aug 2025 11:10:39 +0800 Subject: [PATCH] Validate theme type name input in Add Theme Type dialog --- editor/scene/gui/theme_editor_plugin.cpp | 19 ++++--------------- scene/resources/theme.cpp | 24 ++++++++++++++++++++---- scene/resources/theme.h | 1 + 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/editor/scene/gui/theme_editor_plugin.cpp b/editor/scene/gui/theme_editor_plugin.cpp index 9a3f7f201a..d0a2d4a489 100644 --- a/editor/scene/gui/theme_editor_plugin.cpp +++ b/editor/scene/gui/theme_editor_plugin.cpp @@ -1334,11 +1334,7 @@ void ThemeItemEditorDialog::_edited_type_edited() { TreeItem *edited_item = edit_type_list->get_selected(); const String old_type_name = edited_item->get_metadata(0); - String new_type_name = edited_item->get_text(0).strip_edges(); - if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name. - new_type_name = new_type_name.validate_ascii_identifier(); - } - + const String &new_type_name = Theme::validate_type_name(edited_item->get_text(0)); if (old_type_name == new_type_name) { edited_item->set_text(0, old_type_name); return; @@ -1585,10 +1581,7 @@ void ThemeItemEditorDialog::_item_tree_button_pressed(Object *p_item, int p_colu } void ThemeItemEditorDialog::_add_theme_type() { - String new_type_name = edit_add_type_value->get_text().strip_edges(); - if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name. - new_type_name = new_type_name.validate_ascii_identifier(); - } + const String &new_type_name = Theme::validate_type_name(edit_add_type_value->get_text()); edit_add_type_value->clear(); EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); @@ -2254,7 +2247,7 @@ void ThemeTypeDialog::_add_type_options_cbk(int p_index) { } void ThemeTypeDialog::_add_type_dialog_entered(const String &p_value) { - _add_type_selected(p_value.strip_edges()); + _add_type_selected(Theme::validate_type_name(p_value)); } void ThemeTypeDialog::_add_type_dialog_activated(int p_index) { @@ -2890,11 +2883,7 @@ void ThemeTypeEditor::_rename_type_button_cbk() { } void ThemeTypeEditor::_theme_type_rename_dialog_confirmed() { - String new_type_name = theme_type_rename_line_edit->get_text().strip_edges(); - if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name. - new_type_name = new_type_name.validate_ascii_identifier(); - } - + const String &new_type_name = Theme::validate_type_name(theme_type_rename_line_edit->get_text()); if (edited_type == new_type_name) { return; } diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index fc9562c861..d2205614b5 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -176,8 +176,10 @@ void Theme::_get_property_list(List *p_list) const { // Static helpers. bool Theme::is_valid_type_name(const String &p_name) { - for (int i = 0; i < p_name.length(); i++) { - if (!is_ascii_identifier_char(p_name[i])) { + int len = p_name.length(); + const char32_t *str = p_name.ptr(); + for (int i = 0; i < len; i++) { + if (!is_ascii_identifier_char(str[i])) { return false; } } @@ -188,14 +190,28 @@ bool Theme::is_valid_item_name(const String &p_name) { if (p_name.is_empty()) { return false; } - for (int i = 0; i < p_name.length(); i++) { - if (!is_ascii_identifier_char(p_name[i])) { + int len = p_name.length(); + const char32_t *str = p_name.ptr(); + for (int i = 0; i < len; i++) { + if (!is_ascii_identifier_char(str[i])) { return false; } } return true; } +String Theme::validate_type_name(const String &p_name) { + String type_name = p_name.strip_edges(); + int len = type_name.length(); + char32_t *buffer = type_name.ptrw(); + for (int i = 0; i < len; i++) { + if (!is_ascii_identifier_char(buffer[i])) { + buffer[i] = '_'; + } + } + return type_name; +} + // Fallback values for theme item types, configurable per theme. void Theme::set_default_base_scale(float p_base_scale) { if (default_base_scale == p_base_scale) { diff --git a/scene/resources/theme.h b/scene/resources/theme.h index c5c5234d26..a5133183c4 100644 --- a/scene/resources/theme.h +++ b/scene/resources/theme.h @@ -116,6 +116,7 @@ protected: public: static bool is_valid_type_name(const String &p_name); static bool is_valid_item_name(const String &p_name); + static String validate_type_name(const String &p_name); void set_default_base_scale(float p_base_scale); float get_default_base_scale() const;