{"record":{"id":"eb390ff2aa5578cf","repo":"dotnet/wpf","slug":"sr-textschema-illegalhyperlinkchild-childtype","errorCode":null,"errorMessage":"SR.TextSchema_IllegalHyperlinkChild (childType)","messagePattern":"SR\\.TextSchema_IllegalHyperlinkChild \\(childType\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs","lineNumber":134,"sourceCode":"            return isValidChild;\n        }\n\n        internal static bool IsValidChild(TextElement parent, Type childType)\n        {\n            return ValidateChild(parent, childType, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);\n        }\n\n        internal static bool ValidateChild(TextElement parent, Type childType, bool throwIfIllegalChild, bool throwIfIllegalHyperlinkDescendent)\n        {\n            // Disallow nested hyperlink elements.\n            if (TextSchema.HasHyperlinkAncestor(parent))\n            {\n                if (typeof(Hyperlink).IsAssignableFrom(childType) ||\n                    typeof(AnchoredBlock).IsAssignableFrom(childType))\n                {\n                    if (throwIfIllegalHyperlinkDescendent)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.TextSchema_IllegalHyperlinkChild, childType));\n                    }\n                    return false;\n                }\n            }\n\n            bool isValidChild = IsValidChild(parent.GetType(), childType);\n\n            if (!isValidChild && throwIfIllegalChild)\n            {\n                throw new InvalidOperationException(SR.Format(SR.TextSchema_ChildTypeIsInvalid, parent.GetType().Name, childType.Name));\n            }\n\n            return isValidChild;\n        }\n\n        internal static bool IsValidChild(TextPointer position, Type childType)\n        {\n            return ValidateChild(position, childType, false /* throwIfIllegalChild */, false /* throwIfIllegalHyperlinkDescendent */);","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextSchema.cs#L116-L152","documentation":"ValidateChild rejects child types that may not appear inside a Hyperlink descendant. Hyperlink (an Inline) may not contain AnchoredBlock elements or nested Hyperlinks; when such a childType is requested inside hyperlink content and throwIfIllegalHyperlinkDescendent is true, this exception is thrown. This keeps hyperlink content inline-only.","triggerScenarios":"Attempting to insert a Hyperlink or an AnchoredBlock (Paragraph, Table, List, Section, Floater, Figure) as a child inside a Hyperlink's content, e.g. hyper.Inlines.Add(new Paragraph(...)) or nested Hyperlink creation, during construction or paste/edit operations.","commonSituations":"Nesting hyperlinks in rich text editors; pasting formatted HTML with block-level content inside an anchor; code-generated FlowDocuments that place lists/tables inside link text.","solutions":["Place only inline, non-hyperlink content (Run, Span, Italic, Bold, LineBreak) inside a Hyperlink","Check TextSchema.IsValidChild(hyperlink, childType) before adding; if false, split content outside the link","Move AnchoredBlock content (Paragraph/Table/List) outside the Hyperlink, linking only the inline run(s)","Catch InvalidOperationException mentioning TextSchema_IllegalHyperlinkChild and sanitize pasted content to strip nested links/blocks"],"exampleFix":"// before\nHyperlink link = new Hyperlink();\nlink.Inlines.Add(new Paragraph(new Run(\"click\"))); // invalid: AnchoredBlock in Hyperlink\n// after\nHyperlink link = new Hyperlink(new Run(\"click\"));\nlink.NavigateUri = new Uri(\"https://example.com\");","handlingStrategy":"validation","validationCode":"static bool CanAddToHyperlink(Type t) => !(typeof(Hyperlink).IsAssignableFrom(t) || typeof(AnchoredBlock).IsAssignableFrom(t)) && TextSchema.IsValidChild(typeof(Hyperlink), t);","typeGuard":"static bool IsInlineNonBlock(TextElement e) => e is Inline && !(e is Hyperlink) && !(e is AnchoredBlock);","tryCatchPattern":"try { hyperlink.Inlines.Add(inline); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"IllegalHyperlinkChild\")) { /* move element outside the link */ }","preventionTips":["Only put Run/Span-style inline content inside Hyperlink","Never nest Hyperlink or add Paragraph/Table/List inside a link","Check IsValidChild before insertion"],"tags":["wpf","hyperlink","textschema"],"backgroundTag":"schema-validation-failed","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"}