{"record":{"id":"e800d9ed37983f40","repo":"dotnet/wpf","slug":"sr-format-sr-indifferentparagraphs-start-end","errorCode":null,"errorMessage":"SR.Format(SR.InDifferentParagraphs, \"start\", \"end\")","messagePattern":"SR\\.Format\\(SR\\.InDifferentParagraphs, \"start\", \"end\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Span.cs","lineNumber":109,"sourceCode":"            {\n                throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, \"start\", \"end\"));\n            }\n            if (start.CompareTo(end) > 0)\n            {\n                throw new ArgumentException(SR.Format(SR.BadTextPositionOrder, \"start\", \"end\"));\n            }\n\n            start.TextContainer.BeginChange();\n            try\n            {\n                start = TextRangeEditTables.EnsureInsertionPosition(start);\n                Invariant.Assert(start.Parent is Run);\n                end = TextRangeEditTables.EnsureInsertionPosition(end);\n                Invariant.Assert(end.Parent is Run);\n\n                if (start.Paragraph != end.Paragraph)\n                {\n                    throw new ArgumentException(SR.Format(SR.InDifferentParagraphs, \"start\", \"end\"));\n                }\n\n                // If start or end positions have a Hyperlink ancestor, we cannot split them.\n                Inline nonMergeableAncestor;\n                if ((nonMergeableAncestor = start.GetNonMergeableInlineAncestor()) != null)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TextSchema_CannotSplitElement, nonMergeableAncestor.GetType().Name));\n                }\n                if ((nonMergeableAncestor = end.GetNonMergeableInlineAncestor()) != null)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.TextSchema_CannotSplitElement, nonMergeableAncestor.GetType().Name));\n                }\n\n                TextElement commonAncestor = TextElement.GetCommonAncestor((TextElement)start.Parent, (TextElement)end.Parent);\n\n                while (start.Parent != commonAncestor)\n                {\n                    start = SplitElement(start);","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/Span.cs#L91-L127","documentation":"The Span(TextPointer start, TextPointer end) constructor throws ArgumentException with SR.InDifferentParagraphs when, after normalizing both positions to insertion positions inside Runs, the positions resolve to different Paragraph elements. A Paragraph-range Span (used for Paragraph construction/scoping) must lie entirely within one paragraph.","triggerScenarios":"new Span(start, end) where start and end are in the same document but in different Paragraph (or paragraph-like block) elements - e.g. start in paragraph 1 and end in paragraph 2 of a FlowDocument.","commonSituations":"Selecting across multiple paragraphs and passing the selection endpoints to create a paragraph-scoped Span; using document-wide Start/End pointers; processing a document line-by-line but pairing pointers from adjacent paragraphs.","solutions":["Only construct this Span when both positions are inside the same Paragraph (compare start.Paragraph and end.Paragraph).","Split multi-paragraph ranges into one Span per paragraph and process each separately.","Use TextRange (which supports cross-paragraph ranges) instead of Span when the range legitimately spans paragraphs.","Catch ArgumentException and fall back to a TextRange-based implementation."],"exampleFix":"// before\nvar span = new Span(selection.Start, selection.End); // may cross paragraphs\n// after\nif (selection.Start.Paragraph != selection.End.Paragraph)\n    return null; // or handle per paragraph\nvar span = new Span(selection.Start, selection.End);","handlingStrategy":"validation","validationCode":"if (start.Paragraph != end.Paragraph)\n    throw new ArgumentException(\"Span range must lie within a single paragraph.\");","typeGuard":"static bool WithinOneParagraph(TextPointer a, TextPointer b) => a.Paragraph != null && a.Paragraph == b.Paragraph;","tryCatchPattern":"try { span = new Span(start, end); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"paragraph\")) { return ProcessPerParagraph(start, end); }","preventionTips":["Check Paragraph equality before constructing paragraph-scoped Spans","Split multi-paragraph selections per paragraph","Use TextRange when cross-paragraph ranges are legitimate"],"tags":["wpf","documents","paragraph","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}