Fix RichTextLabel.remove_paragraph crash by popping current

(cherry picked from commit c0f8e0061d)
This commit is contained in:
Nicholas Foo 2023-11-01 22:25:01 +08:00 committed by Yuri Sizov
parent 776938f731
commit 4d3e25d09d
2 changed files with 8 additions and 5 deletions

View File

@ -3151,7 +3151,7 @@ void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline)
queue_redraw(); queue_redraw();
} }
void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_subitem_line) { void RichTextLabel::_remove_item(Item *p_item, const int p_line) {
int size = p_item->subitems.size(); int size = p_item->subitems.size();
if (size == 0) { if (size == 0) {
p_item->parent->subitems.erase(p_item); p_item->parent->subitems.erase(p_item);
@ -3160,7 +3160,7 @@ void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_sub
current_frame->lines.remove_at(p_line); current_frame->lines.remove_at(p_line);
if (p_line < (int)current_frame->lines.size() && current_frame->lines[p_line].from) { if (p_line < (int)current_frame->lines.size() && current_frame->lines[p_line].from) {
for (List<Item *>::Element *E = current_frame->lines[p_line].from->E; E; E = E->next()) { for (List<Item *>::Element *E = current_frame->lines[p_line].from->E; E; E = E->next()) {
if (E->get()->line > p_subitem_line) { if (E->get()->line > p_line) {
E->get()->line--; E->get()->line--;
} }
} }
@ -3169,7 +3169,7 @@ void RichTextLabel::_remove_item(Item *p_item, const int p_line, const int p_sub
} else { } else {
// First, remove all child items for the provided item. // First, remove all child items for the provided item.
while (p_item->subitems.size()) { while (p_item->subitems.size()) {
_remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line); _remove_item(p_item->subitems.front()->get(), p_line);
} }
// Then remove the provided item itself. // Then remove the provided item itself.
p_item->parent->subitems.erase(p_item); p_item->parent->subitems.erase(p_item);
@ -3377,7 +3377,10 @@ bool RichTextLabel::remove_paragraph(const int p_paragraph) {
for (int i = subitem_to_remove.size() - 1; i >= 0; i--) { for (int i = subitem_to_remove.size() - 1; i >= 0; i--) {
List<Item *>::Element *subitem = subitem_to_remove[i]; List<Item *>::Element *subitem = subitem_to_remove[i];
had_newline = had_newline || subitem->get()->type == ITEM_NEWLINE; had_newline = had_newline || subitem->get()->type == ITEM_NEWLINE;
_remove_item(subitem->get(), subitem->get()->line, p_paragraph); if (subitem->get() == current) {
pop();
}
_remove_item(subitem->get(), p_paragraph);
} }
if (!had_newline) { if (!had_newline) {

View File

@ -461,7 +461,7 @@ private:
_FORCE_INLINE_ float _update_scroll_exceeds(float p_total_height, float p_ctrl_height, float p_width, int p_idx, float p_old_scroll, float p_text_rect_height); _FORCE_INLINE_ float _update_scroll_exceeds(float p_total_height, float p_ctrl_height, float p_width, int p_idx, float p_old_scroll, float p_text_rect_height);
void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false); void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
void _remove_item(Item *p_item, const int p_line, const int p_subitem_line); void _remove_item(Item *p_item, const int p_line);
String language; String language;
TextDirection text_direction = TEXT_DIRECTION_AUTO; TextDirection text_direction = TEXT_DIRECTION_AUTO;