diff --git a/escher/src/layout_field.cpp b/escher/src/layout_field.cpp index 164bd5237..1b540c601 100644 --- a/escher/src/layout_field.cpp +++ b/escher/src/layout_field.cpp @@ -243,14 +243,15 @@ void LayoutField::insertLayoutAtCursor(LayoutRef layoutR, LayoutRef pointedLayou LayoutRef lastMergedLayoutChild = layoutWillBeMerged ? layoutR.childAtIndex(layoutR.numberOfChildren()-1) : LayoutRef(nullptr); m_contentView.cursor()->addLayoutAndMoveCursor(layoutR); if (!forceCursorRightOfLayout) { - if (pointedLayoutR.isDefined() && (!layoutWillBeMerged || pointedLayoutR != layoutR)) { + if (pointedLayoutR.isDefined() && pointedLayoutR.parent().isDefined() && (!layoutWillBeMerged || pointedLayoutR != layoutR)) { + // Make sure the layout was inserted (its parent is not nullptr) m_contentView.cursor()->setLayoutReference(pointedLayoutR); m_contentView.cursor()->setPosition(LayoutCursor::Position::Right); - } else if (!layoutWillBeMerged ) { //&& !layoutR.isAllocationFailure()) { + } else if (!layoutWillBeMerged && layoutR.parent().isDefined()) { //&& !layoutR.isAllocationFailure()) { m_contentView.cursor()->setLayoutReference(layoutR.layoutToPointWhenInserting()); m_contentView.cursor()->setPosition(LayoutCursor::Position::Right); } - } else if (!layoutWillBeMerged) { + } else if (!layoutWillBeMerged && layoutR.parent().isDefined()) { m_contentView.cursor()->setLayoutReference(layoutR); m_contentView.cursor()->setPosition(LayoutCursor::Position::Right); } diff --git a/poincare/src/layout_cursor.cpp b/poincare/src/layout_cursor.cpp index 33b1aba1d..8107810f8 100644 --- a/poincare/src/layout_cursor.cpp +++ b/poincare/src/layout_cursor.cpp @@ -107,11 +107,9 @@ void LayoutCursor::insertText(const char * text) { else { newChild = CharLayoutRef(text[i]); } - m_layoutRef.addSibling(this, newChild); - m_layoutRef = newChild; - m_position = Position::Right; + m_layoutRef.addSiblingAndMoveCursor(this, newChild); } - if (pointedChild.isDefined()) { + if (pointedChild.isDefined() && pointedChild.parent().isDefined()) { m_layoutRef = pointedChild; } }