// Helper for EnsureInsertionPosition. // Generates minimally necessary content to ensure at least one insertion position. private static TextPointer CreateInsertionPositionInIncompleteContent(TextPointer position) { // Go inside the scoped element to its possible lowest level while (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart) { position = position.GetNextContextPosition(LogicalDirection.Forward); } while (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementEnd) { position = position.GetNextContextPosition(LogicalDirection.Backward); } DependencyObject parent = position.Parent; if (parent != null) { if (parent is Table) { // Creating implicit TableRowGroup TableRowGroup tableRowGroup = new TableRowGroup(); tableRowGroup.Reposition(position, position); position = tableRowGroup.ContentStart; parent = position.Parent; } if (parent is TableRowGroup) { // Creating implicit TableRow TableRow tableRow = new TableRow(); tableRow.Reposition(position, position); position = tableRow.ContentStart; parent = position.Parent; } if (parent is TableRow) { // Creating implicit TableCell TableCell tableCell = new TableCell(); tableCell.Reposition(position, position); position = tableCell.ContentStart; parent = position.Parent; } if (parent is List) { // Creating implicit ListItem ListItem listItem = new ListItem(); listItem.Reposition(position, position); position = listItem.ContentStart; parent = position.Parent; } if (parent is LineBreak || parent is InlineUIContainer) { position = ((Inline)parent).ElementStart; parent = position.Parent; } } if (parent == null) { // throw new InvalidOperationException(SR.Get(SRID.TextSchema_CannotInsertContentInThisPosition)); } TextPointer insertionPosition; if (TextSchema.IsValidChild(/*position:*/position, /*childType:*/typeof(Inline))) { insertionPosition = CreateImplicitRun(position); } else { Invariant.Assert(TextSchema.IsValidChild(/*position:*/position, /*childType:*/typeof(Block)), "Expecting valid parent-child relationship"); insertionPosition = CreateImplicitParagraph(position); } return insertionPosition; }