Skip to content

Commit 825ef23

Browse files
committed
Merge pull request godotengine#110044 from timothyqiu/theme-type-name-validation
Validate theme type name input in Add Theme Type dialog
2 parents b0da1c7 + e5373c5 commit 825ef23

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

editor/scene/gui/theme_editor_plugin.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,11 +1334,7 @@ void ThemeItemEditorDialog::_edited_type_edited() {
13341334
TreeItem *edited_item = edit_type_list->get_selected();
13351335
const String old_type_name = edited_item->get_metadata(0);
13361336

1337-
String new_type_name = edited_item->get_text(0).strip_edges();
1338-
if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name.
1339-
new_type_name = new_type_name.validate_ascii_identifier();
1340-
}
1341-
1337+
const String &new_type_name = Theme::validate_type_name(edited_item->get_text(0));
13421338
if (old_type_name == new_type_name) {
13431339
edited_item->set_text(0, old_type_name);
13441340
return;
@@ -1585,10 +1581,7 @@ void ThemeItemEditorDialog::_item_tree_button_pressed(Object *p_item, int p_colu
15851581
}
15861582

15871583
void ThemeItemEditorDialog::_add_theme_type() {
1588-
String new_type_name = edit_add_type_value->get_text().strip_edges();
1589-
if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name.
1590-
new_type_name = new_type_name.validate_ascii_identifier();
1591-
}
1584+
const String &new_type_name = Theme::validate_type_name(edit_add_type_value->get_text());
15921585
edit_add_type_value->clear();
15931586

15941587
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
@@ -2254,7 +2247,7 @@ void ThemeTypeDialog::_add_type_options_cbk(int p_index) {
22542247
}
22552248

22562249
void ThemeTypeDialog::_add_type_dialog_entered(const String &p_value) {
2257-
_add_type_selected(p_value.strip_edges());
2250+
_add_type_selected(Theme::validate_type_name(p_value));
22582251
}
22592252

22602253
void ThemeTypeDialog::_add_type_dialog_activated(int p_index) {
@@ -2890,11 +2883,7 @@ void ThemeTypeEditor::_rename_type_button_cbk() {
28902883
}
28912884

28922885
void ThemeTypeEditor::_theme_type_rename_dialog_confirmed() {
2893-
String new_type_name = theme_type_rename_line_edit->get_text().strip_edges();
2894-
if (!new_type_name.is_empty()) { // The type name can be empty, unlike the item name.
2895-
new_type_name = new_type_name.validate_ascii_identifier();
2896-
}
2897-
2886+
const String &new_type_name = Theme::validate_type_name(theme_type_rename_line_edit->get_text());
28982887
if (edited_type == new_type_name) {
28992888
return;
29002889
}

scene/resources/theme.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
176176

177177
// Static helpers.
178178
bool Theme::is_valid_type_name(const String &p_name) {
179-
for (int i = 0; i < p_name.length(); i++) {
180-
if (!is_ascii_identifier_char(p_name[i])) {
179+
int len = p_name.length();
180+
const char32_t *str = p_name.ptr();
181+
for (int i = 0; i < len; i++) {
182+
if (!is_ascii_identifier_char(str[i])) {
181183
return false;
182184
}
183185
}
@@ -188,14 +190,28 @@ bool Theme::is_valid_item_name(const String &p_name) {
188190
if (p_name.is_empty()) {
189191
return false;
190192
}
191-
for (int i = 0; i < p_name.length(); i++) {
192-
if (!is_ascii_identifier_char(p_name[i])) {
193+
int len = p_name.length();
194+
const char32_t *str = p_name.ptr();
195+
for (int i = 0; i < len; i++) {
196+
if (!is_ascii_identifier_char(str[i])) {
193197
return false;
194198
}
195199
}
196200
return true;
197201
}
198202

203+
String Theme::validate_type_name(const String &p_name) {
204+
String type_name = p_name.strip_edges();
205+
int len = type_name.length();
206+
char32_t *buffer = type_name.ptrw();
207+
for (int i = 0; i < len; i++) {
208+
if (!is_ascii_identifier_char(buffer[i])) {
209+
buffer[i] = '_';
210+
}
211+
}
212+
return type_name;
213+
}
214+
199215
// Fallback values for theme item types, configurable per theme.
200216
void Theme::set_default_base_scale(float p_base_scale) {
201217
if (default_base_scale == p_base_scale) {

scene/resources/theme.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Theme : public Resource {
116116
public:
117117
static bool is_valid_type_name(const String &p_name);
118118
static bool is_valid_item_name(const String &p_name);
119+
static String validate_type_name(const String &p_name);
119120

120121
void set_default_base_scale(float p_base_scale);
121122
float get_default_base_scale() const;

0 commit comments

Comments
 (0)