dotnet/wpf · error · InvalidOperationException

SR.TextSchema_CannotSplitElement (ancestor type name)

Error message

SR.TextSchema_CannotSplitElement (ancestor type name)

What it means

InsertParagraphBreak cannot split certain non-mergeable Inline elements. After GetNonMergeableInlineAncestor() returns a non-null ancestor (e.g. a Hyperlink, which is not splittable across a paragraph boundary), it throws InvalidOperationException(SR.TextSchema_CannotSplitElement) formatted with the ancestor's type name. Splitting would corrupt elements that carry their own semantics/properties.

Solutions

  1. Check GetNonMergeableInlineAncestor() before inserting; if non-null, move the insertion point outside the element or skip the break.
  2. Remove the Hyperlink (preserving its Run text as a plain Run) before inserting the paragraph break.
  3. Intercept the Enter key in the editor and suppress or relocate the break when the caret is inside a Hyperlink.

Example fix

// before
caret.InsertParagraphBreak();
// after
var anc = caret.GetNonMergeableInlineAncestor();
if (anc != null)
{
    var tp = new TextPointer(anc.ElementStart);
    tp.InsertParagraphBreak();
}
else
    caret.InsertParagraphBreak();
Defensive patterns

Strategy: validation

Validate before calling

if (pointer.GetNonMergeableInlineAncestor() == null)
    pointer.InsertParagraphBreak();
else
    /* relocate insertion point outside the Hyperlink first */;

Try / catch

try { pointer.InsertParagraphBreak(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Hyperlink")) { /* strip the link or move caret out */ }

Prevention

When it happens

Trigger: Calling InsertParagraphBreak at a caret position inside a Hyperlink (or another non-mergeable Inline such as an anchoring element that disallows splitting) — commonly when the user presses Enter in the middle of link text in a RichTextBox.

Common situations: Editing documents with hyperlinks: caret placed inside link text and the user hits Enter; automated text processing that inserts breaks without checking the surrounding inline ancestry; paste/IME logic inserting breaks inside links.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/eb34ec3f5d6cedae. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs:1431

        {
            _tree.EmptyDeadPositionList();
            SyncToTreeGeneration();

            if (this.TextContainer.Parent != null)
            {
                Type containerType = this.TextContainer.Parent.GetType();
                if (!TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph)))
                {
                    throw new InvalidOperationException(SR.Format(SR.TextSchema_IllegalElement, "Paragraph", containerType));
                }
            }

            Inline ancestor = this.GetNonMergeableInlineAncestor();

            if (ancestor != null)
            {
                // Cannot split a hyperlink element!
                throw new InvalidOperationException(SR.Format(SR.TextSchema_CannotSplitElement, ancestor.GetType().Name));
            }

            TextPointer position;

            _tree.BeginChange();
            try
            {
                position = TextRangeEdit.InsertParagraphBreak(this, /*moveIntoSecondParagraph:*/true);
            }
            finally
            {
                _tree.EndChange();
            }

            return position;
        }

        /// <summary>

View on GitHub (pinned to 81131a70a4)