Merge pull request #110044 from timothyqiu/theme-type-name-validation

Validate theme type name input in Add Theme Type dialog
This commit is contained in:
Thaddeus Crews
2025-08-29 12:00:44 -05:00
3 changed files with 25 additions and 19 deletions

View File

@@ -1334,11 +1334,7 @@ void ThemeItemEditorDialog::_edited_type_edited() {
TreeItem *edited_item = edit_type_list->get_selected(); TreeItem *edited_item = edit_type_list->get_selected();
const String old_type_name = edited_item->get_metadata(0); const String old_type_name = edited_item->get_metadata(0);
String new_type_name = edited_item->get_text(0).strip_edges(); const String &new_type_name = Theme::validate_type_name(edited_item->get_text(0));
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();
}
if (old_type_name == new_type_name) { if (old_type_name == new_type_name) {
edited_item->set_text(0, old_type_name); edited_item->set_text(0, old_type_name);
return; return;
@@ -1585,10 +1581,7 @@ void ThemeItemEditorDialog::_item_tree_button_pressed(Object *p_item, int p_colu
} }
void ThemeItemEditorDialog::_add_theme_type() { void ThemeItemEditorDialog::_add_theme_type() {
String new_type_name = edit_add_type_value->get_text().strip_edges(); const String &new_type_name = Theme::validate_type_name(edit_add_type_value->get_text());
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();
}
edit_add_type_value->clear(); edit_add_type_value->clear();
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); 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) { 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) { 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() { void ThemeTypeEditor::_theme_type_rename_dialog_confirmed() {
String new_type_name = theme_type_rename_line_edit->get_text().strip_edges(); const String &new_type_name = Theme::validate_type_name(theme_type_rename_line_edit->get_text());
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();
}
if (edited_type == new_type_name) { if (edited_type == new_type_name) {
return; return;
} }

View File

@@ -176,8 +176,10 @@ void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
// Static helpers. // Static helpers.
bool Theme::is_valid_type_name(const String &p_name) { bool Theme::is_valid_type_name(const String &p_name) {
for (int i = 0; i < p_name.length(); i++) { int len = p_name.length();
if (!is_ascii_identifier_char(p_name[i])) { const char32_t *str = p_name.ptr();
for (int i = 0; i < len; i++) {
if (!is_ascii_identifier_char(str[i])) {
return false; return false;
} }
} }
@@ -188,14 +190,28 @@ bool Theme::is_valid_item_name(const String &p_name) {
if (p_name.is_empty()) { if (p_name.is_empty()) {
return false; return false;
} }
for (int i = 0; i < p_name.length(); i++) { int len = p_name.length();
if (!is_ascii_identifier_char(p_name[i])) { const char32_t *str = p_name.ptr();
for (int i = 0; i < len; i++) {
if (!is_ascii_identifier_char(str[i])) {
return false; return false;
} }
} }
return true; 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. // Fallback values for theme item types, configurable per theme.
void Theme::set_default_base_scale(float p_base_scale) { void Theme::set_default_base_scale(float p_base_scale) {
if (default_base_scale == p_base_scale) { if (default_base_scale == p_base_scale) {

View File

@@ -116,6 +116,7 @@ protected:
public: public:
static bool is_valid_type_name(const String &p_name); static bool is_valid_type_name(const String &p_name);
static bool is_valid_item_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); void set_default_base_scale(float p_base_scale);
float get_default_base_scale() const; float get_default_base_scale() const;