{"record":{"id":"eb34ec3f5d6cedae","repo":"dotnet/wpf","slug":"sr-textschema-cannotsplitelement-ancestor-type-name","errorCode":null,"errorMessage":"SR.TextSchema_CannotSplitElement (ancestor type name)","messagePattern":"SR\\.TextSchema_CannotSplitElement \\(ancestor type name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":1431,"sourceCode":"        {\n            _tree.EmptyDeadPositionList();\n            SyncToTreeGeneration();\n\n            if (this.TextContainer.Parent != null)\n            {\n                Type containerType = this.TextContainer.Parent.GetType();\n                if (!TextSchema.IsValidChildOfContainer(containerType, typeof(Paragraph)))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TextSchema_IllegalElement, \"Paragraph\", containerType));\n                }\n            }\n\n            Inline ancestor = this.GetNonMergeableInlineAncestor();\n\n            if (ancestor != null)\n            {\n                // Cannot split a hyperlink element!\n                throw new InvalidOperationException(SR.Format(SR.TextSchema_CannotSplitElement, ancestor.GetType().Name));\n            }\n\n            TextPointer position;\n\n            _tree.BeginChange();\n            try\n            {\n                position = TextRangeEdit.InsertParagraphBreak(this, /*moveIntoSecondParagraph:*/true);\n            }\n            finally\n            {\n                _tree.EndChange();\n            }\n\n            return position;\n        }\n\n        /// <summary>","sourceCodeStart":1413,"sourceCodeEnd":1449,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L1413-L1449","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check GetNonMergeableInlineAncestor() before inserting; if non-null, move the insertion point outside the element or skip the break.","Remove the Hyperlink (preserving its Run text as a plain Run) before inserting the paragraph break.","Intercept the Enter key in the editor and suppress or relocate the break when the caret is inside a Hyperlink."],"exampleFix":"// before\ncaret.InsertParagraphBreak();\n// after\nvar anc = caret.GetNonMergeableInlineAncestor();\nif (anc != null)\n{\n    var tp = new TextPointer(anc.ElementStart);\n    tp.InsertParagraphBreak();\n}\nelse\n    caret.InsertParagraphBreak();","handlingStrategy":"validation","validationCode":"if (pointer.GetNonMergeableInlineAncestor() == null)\n    pointer.InsertParagraphBreak();\nelse\n    /* relocate insertion point outside the Hyperlink first */;","typeGuard":null,"tryCatchPattern":"try { pointer.InsertParagraphBreak(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Hyperlink\")) { /* strip the link or move caret out */ }","preventionTips":["Always check GetNonMergeableInlineAncestor before breaking at a caret","Intercept Enter-key handling when the caret is inside a Hyperlink","Convert Hyperlink runs to plain Runs when restructuring text"],"tags":["wpf","hyperlink","text-schema"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}