public static TextPointer RemoveHyperlink(TextPointer start)
{
var backspacePosition = start.GetNextInsertionPosition(LogicalDirection.Backward);
Hyperlink hyperlink;
if (backspacePosition == null || !IsHyperlinkBoundaryCrossed(start, backspacePosition, out hyperlink))
{
return null;
}
// Remember caretPosition with forward gravity. This is necessary since we are going to delete
// the hyperlink element preceeding caretPosition and after deletion current caretPosition
// (with backward gravity) will follow content preceeding the hyperlink.
// We want to remember content following the hyperlink to set new caret position at.
var newCaretPosition = start.GetPositionAtOffset(0, LogicalDirection.Forward);
// Deleting the hyperlink is done using logic below.
// 1. Copy its children Inline to a temporary array.
var hyperlinkChildren = hyperlink.Inlines;
var inlines = new Inline[hyperlinkChildren.Count];
hyperlinkChildren.CopyTo(inlines, 0);
// 2. Remove each child from parent hyperlink element and insert it after the hyperlink.
for (int i = inlines.Length - 1; i >= 0; i--)
{
hyperlinkChildren.Remove(inlines[i]);
hyperlink.SiblingInlines.InsertAfter(hyperlink, inlines[i]);
}
// 3. Apply hyperlink's local formatting properties to inlines (which are now outside hyperlink scope).
var localProperties = hyperlink.GetLocalValueEnumerator();
var inlineRange = new TextRange(inlines[0].ContentStart, inlines[inlines.Length - 1].ContentEnd);
while (localProperties.MoveNext())
{
var property = localProperties.Current;
var dp = property.Property;
object value = property.Value;
if (!dp.ReadOnly &&
dp != Inline.TextDecorationsProperty && // Ignore hyperlink defaults.
dp != TextElement.ForegroundProperty &&
dp != BaseUriHelper.BaseUriProperty &&
!IsHyperlinkProperty(dp))
{
inlineRange.ApplyPropertyValue(dp, value);
}
}
// 4. Delete the (empty) hyperlink element.
hyperlink.SiblingInlines.Remove(hyperlink);
return newCaretPosition;
}