Skip to content

Commit 8b6e572

Browse files
Make sure to normalize subtags when parsing BBCode
This PR makes it so that all subtags are normalized before usage. Normalization means removing any leading and/or trailing quotation marks from any given subtag. Fixes: #75501
1 parent 1db9de5 commit 8b6e572

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

scene/gui/rich_text_label.cpp

+49-9
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,12 @@ void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack
25792579
}
25802580
}
25812581

2582+
void RichTextLabel::_normalize_subtags(Vector<String> &subtags) {
2583+
for (String &subtag : subtags) {
2584+
subtag = subtag.unquote();
2585+
}
2586+
}
2587+
25822588
bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
25832589
Item *item = p_item;
25842590

@@ -3718,7 +3724,7 @@ void RichTextLabel::append_text(const String &p_bbcode) {
37183724
const String &expr = split_tag_block[i];
37193725
int value_pos = expr.find("=");
37203726
if (value_pos > -1) {
3721-
bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1);
3727+
bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote();
37223728
}
37233729
}
37243730
} else {
@@ -3827,6 +3833,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
38273833
tag_stack.push_front(tag);
38283834
} else if (tag.begins_with("table=")) {
38293835
Vector<String> subtag = tag.substr(6, tag.length()).split(",");
3836+
_normalize_subtags(subtag);
3837+
38303838
int columns = subtag[0].to_int();
38313839
if (columns < 1) {
38323840
columns = 1;
@@ -3886,9 +3894,12 @@ void RichTextLabel::append_text(const String &p_bbcode) {
38863894
tag_stack.push_front("cell");
38873895
} else if (tag.begins_with("cell ")) {
38883896
Vector<String> subtag = tag.substr(5, tag.length()).split(" ");
3897+
_normalize_subtags(subtag);
38893898

38903899
for (int i = 0; i < subtag.size(); i++) {
38913900
Vector<String> subtag_a = subtag[i].split("=");
3901+
_normalize_subtags(subtag_a);
3902+
38923903
if (subtag_a.size() == 2) {
38933904
if (subtag_a[0] == "expand") {
38943905
int ratio = subtag_a[1].to_int();
@@ -3903,12 +3914,16 @@ void RichTextLabel::append_text(const String &p_bbcode) {
39033914
const Color fallback_color = Color(0, 0, 0, 0);
39043915
for (int i = 0; i < subtag.size(); i++) {
39053916
Vector<String> subtag_a = subtag[i].split("=");
3917+
_normalize_subtags(subtag_a);
3918+
39063919
if (subtag_a.size() == 2) {
39073920
if (subtag_a[0] == "border") {
39083921
Color color = Color::from_string(subtag_a[1], fallback_color);
39093922
set_cell_border_color(color);
39103923
} else if (subtag_a[0] == "bg") {
39113924
Vector<String> subtag_b = subtag_a[1].split(",");
3925+
_normalize_subtags(subtag_b);
3926+
39123927
if (subtag_b.size() == 2) {
39133928
Color color1 = Color::from_string(subtag_b[0], fallback_color);
39143929
Color color2 = Color::from_string(subtag_b[1], fallback_color);
@@ -3920,6 +3935,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
39203935
}
39213936
} else if (subtag_a[0] == "padding") {
39223937
Vector<String> subtag_b = subtag_a[1].split(",");
3938+
_normalize_subtags(subtag_b);
3939+
39233940
if (subtag_b.size() == 4) {
39243941
set_cell_padding(Rect2(subtag_b[0].to_float(), subtag_b[1].to_float(), subtag_b[2].to_float(), subtag_b[3].to_float()));
39253942
}
@@ -4056,12 +4073,16 @@ void RichTextLabel::append_text(const String &p_bbcode) {
40564073
tag_stack.push_front("p");
40574074
} else if (tag.begins_with("p ")) {
40584075
Vector<String> subtag = tag.substr(2, tag.length()).split(" ");
4076+
_normalize_subtags(subtag);
4077+
40594078
HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
40604079
Control::TextDirection dir = Control::TEXT_DIRECTION_INHERITED;
40614080
String lang;
40624081
TextServer::StructuredTextParser st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
40634082
for (int i = 0; i < subtag.size(); i++) {
40644083
Vector<String> subtag_a = subtag[i].split("=");
4084+
_normalize_subtags(subtag_a);
4085+
40654086
if (subtag_a.size() == 2) {
40664087
if (subtag_a[0] == "align") {
40674088
if (subtag_a[1] == "l" || subtag_a[1] == "left") {
@@ -4110,24 +4131,26 @@ void RichTextLabel::append_text(const String &p_bbcode) {
41104131
if (end == -1) {
41114132
end = p_bbcode.length();
41124133
}
4113-
String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1);
4134+
String url = p_bbcode.substr(brk_end + 1, end - brk_end - 1).unquote();
41144135
push_meta(url);
41154136

41164137
pos = brk_end + 1;
41174138
tag_stack.push_front(tag);
41184139

41194140
} else if (tag.begins_with("url=")) {
4120-
String url = tag.substr(4, tag.length());
4141+
String url = tag.substr(4, tag.length()).unquote();
41214142
push_meta(url);
41224143
pos = brk_end + 1;
41234144
tag_stack.push_front("url");
41244145
} else if (tag.begins_with("hint=")) {
4125-
String description = tag.substr(5, tag.length());
4146+
String description = tag.substr(5, tag.length()).unquote();
41264147
push_hint(description);
41274148
pos = brk_end + 1;
41284149
tag_stack.push_front("hint");
41294150
} else if (tag.begins_with("dropcap")) {
41304151
Vector<String> subtag = tag.substr(5, tag.length()).split(" ");
4152+
_normalize_subtags(subtag);
4153+
41314154
int fs = theme_cache.normal_font_size * 3;
41324155
Ref<Font> f = theme_cache.normal_font;
41334156
Color color = theme_cache.default_color;
@@ -4137,6 +4160,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
41374160

41384161
for (int i = 0; i < subtag.size(); i++) {
41394162
Vector<String> subtag_a = subtag[i].split("=");
4163+
_normalize_subtags(subtag_a);
4164+
41404165
if (subtag_a.size() == 2) {
41414166
if (subtag_a[0] == "font" || subtag_a[0] == "f") {
41424167
String fnt = subtag_a[1];
@@ -4148,6 +4173,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
41484173
fs = subtag_a[1].to_int();
41494174
} else if (subtag_a[0] == "margins") {
41504175
Vector<String> subtag_b = subtag_a[1].split(",");
4176+
_normalize_subtags(subtag_b);
4177+
41514178
if (subtag_b.size() == 4) {
41524179
dropcap_margins.position.x = subtag_b[0].to_float();
41534180
dropcap_margins.position.y = subtag_b[1].to_float();
@@ -4178,6 +4205,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
41784205
int alignment = INLINE_ALIGNMENT_CENTER;
41794206
if (tag.begins_with("img=")) {
41804207
Vector<String> subtag = tag.substr(4, tag.length()).split(",");
4208+
_normalize_subtags(subtag);
4209+
41814210
if (subtag.size() > 1) {
41824211
if (subtag[0] == "top" || subtag[0] == "t") {
41834212
alignment = INLINE_ALIGNMENT_TOP_TO;
@@ -4261,14 +4290,14 @@ void RichTextLabel::append_text(const String &p_bbcode) {
42614290
pos = end;
42624291
tag_stack.push_front(bbcode_name);
42634292
} else if (tag.begins_with("color=")) {
4264-
String color_str = tag.substr(6, tag.length());
4293+
String color_str = tag.substr(6, tag.length()).unquote();
42654294
Color color = Color::from_string(color_str, theme_cache.default_color);
42664295
push_color(color);
42674296
pos = brk_end + 1;
42684297
tag_stack.push_front("color");
42694298

42704299
} else if (tag.begins_with("outline_color=")) {
4271-
String color_str = tag.substr(14, tag.length());
4300+
String color_str = tag.substr(14, tag.length()).unquote();
42724301
Color color = Color::from_string(color_str, theme_cache.default_color);
42734302
push_outline_color(color);
42744303
pos = brk_end + 1;
@@ -4284,6 +4313,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
42844313
int value_pos = tag.find("=");
42854314
String fnt_ftr = tag.substr(value_pos + 1);
42864315
Vector<String> subtag = fnt_ftr.split(",");
4316+
_normalize_subtags(subtag);
4317+
42874318
if (subtag.size() > 0) {
42884319
Ref<Font> font = theme_cache.normal_font;
42894320
DefaultFont def_font = NORMAL_FONT;
@@ -4298,6 +4329,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
42984329
Dictionary features;
42994330
for (int i = 0; i < subtag.size(); i++) {
43004331
Vector<String> subtag_a = subtag[i].split("=");
4332+
_normalize_subtags(subtag_a);
4333+
43014334
if (subtag_a.size() == 2) {
43024335
features[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
43034336
} else if (subtag_a.size() == 1) {
@@ -4321,7 +4354,7 @@ void RichTextLabel::append_text(const String &p_bbcode) {
43214354
tag_stack.push_front(tag.substr(0, value_pos));
43224355

43234356
} else if (tag.begins_with("font=")) {
4324-
String fnt = tag.substr(5, tag.length());
4357+
String fnt = tag.substr(5, tag.length()).unquote();
43254358

43264359
Ref<Font> fc = ResourceLoader::load(fnt, "Font");
43274360
if (fc.is_valid()) {
@@ -4333,6 +4366,7 @@ void RichTextLabel::append_text(const String &p_bbcode) {
43334366

43344367
} else if (tag.begins_with("font ")) {
43354368
Vector<String> subtag = tag.substr(2, tag.length()).split(" ");
4369+
_normalize_subtags(subtag);
43364370

43374371
Ref<Font> font = theme_cache.normal_font;
43384372
DefaultFont def_font = NORMAL_FONT;
@@ -4351,6 +4385,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
43514385
int fnt_size = -1;
43524386
for (int i = 1; i < subtag.size(); i++) {
43534387
Vector<String> subtag_a = subtag[i].split("=", true, 2);
4388+
_normalize_subtags(subtag_a);
4389+
43544390
if (subtag_a.size() == 2) {
43554391
if (subtag_a[0] == "name" || subtag_a[0] == "n") {
43564392
String fnt = subtag_a[1];
@@ -4388,6 +4424,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
43884424
Vector<String> variation_tags = subtag_a[1].split(",");
43894425
for (int j = 0; j < variation_tags.size(); j++) {
43904426
Vector<String> subtag_b = variation_tags[j].split("=");
4427+
_normalize_subtags(subtag_b);
4428+
43914429
if (subtag_b.size() == 2) {
43924430
variations[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
43934431
}
@@ -4400,6 +4438,8 @@ void RichTextLabel::append_text(const String &p_bbcode) {
44004438
Vector<String> feature_tags = subtag_a[1].split(",");
44014439
for (int j = 0; j < feature_tags.size(); j++) {
44024440
Vector<String> subtag_b = feature_tags[j].split("=");
4441+
_normalize_subtags(subtag_b);
4442+
44034443
if (subtag_b.size() == 2) {
44044444
features[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
44054445
} else if (subtag_b.size() == 1) {
@@ -4540,15 +4580,15 @@ void RichTextLabel::append_text(const String &p_bbcode) {
45404580
set_process_internal(true);
45414581

45424582
} else if (tag.begins_with("bgcolor=")) {
4543-
String color_str = tag.substr(8, tag.length());
4583+
String color_str = tag.substr(8, tag.length()).unquote();
45444584
Color color = Color::from_string(color_str, theme_cache.default_color);
45454585

45464586
push_bgcolor(color);
45474587
pos = brk_end + 1;
45484588
tag_stack.push_front("bgcolor");
45494589

45504590
} else if (tag.begins_with("fgcolor=")) {
4551-
String color_str = tag.substr(8, tag.length());
4591+
String color_str = tag.substr(8, tag.length()).unquote();
45524592
Color color = Color::from_string(color_str, theme_cache.default_color);
45534593

45544594
push_fgcolor(color);

scene/gui/rich_text_label.h

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ class RichTextLabel : public Control {
504504
Color _find_fgcolor(Item *p_item);
505505
bool _find_layout_subitem(Item *from, Item *to);
506506
void _fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack);
507+
void _normalize_subtags(Vector<String> &subtags);
507508

508509
void _update_fx(ItemFrame *p_frame, double p_delta_time);
509510
void _scroll_changed(double);

0 commit comments

Comments
 (0)