private void ResetSelection() { try { // Keep it within the postBody div! _mshtmlEditor.DocumentEvents.SelectionChanged -= new EventHandler(DocumentEvents_SelectionChanged); IHTMLElement postBody = PostBodyElement; if (postBody != null) { MarkupRange range = MarkupServices.CreateMarkupRange(postBody, false); range.Collapse(true); range.ToTextRange().select(); } } finally { _mshtmlEditor.DocumentEvents.SelectionChanged += new EventHandler(DocumentEvents_SelectionChanged); } _defaultSelection = new HtmlEditorSelection(_mshtmlEditor, HTMLDocument); ((IHtmlEditorComponentContext)this).Selection = _defaultSelection; }
private void DocumentEvents_SelectionChanged(object sender, EventArgs e) { using (ApplicationPerformance.LogEvent("SelectionChanged")) { BeginSelectionChangedCaching(); try { if (_mshtmlEditor.MshtmlControl.DocumentIsComplete) { #if SELECTION_DEBUG if (SelectionDebugDialog == null) { SelectionDebugDialog = new SelectionDebugDialog(); SelectionDebugDialog.Show(); } SelectionDebugDialog.Add(SelectedMarkupRange); #endif // update selection type if (_defaultSelection == null) { _defaultSelection = new HtmlEditorSelection(MshtmlEditor, HTMLDocument); } ((IHtmlEditorComponentContext)this).Selection = _defaultSelection; if (_suspendSelectionValidationDepth == 0) { //If the selection is contiguous, let the editor decide if its a valid selection //and clear it if it is not. //Resetting the selection may cause another selection change event, which can incorrectly return //a non-empty selection, so to avoid recursive stack overflows, we suspend validation for subsequent //selection changes. (bug 405391) _suspendSelectionValidationDepth++; try { if (!IsValidContiguousSelection()) { if (HasContiguousSelection) { if (ShouldEmptySelection()) { EmptySelection(); } else { // WinLive 196005: In some circumstances we do not want to empty an invalid selection. if (_selectionIsInvalid == false) { _selectionIsInvalid = true; OnCommandStateChanged(); } } return; } else { if (TryMoveToValidSelection(((IHtmlEditorComponentContext)this).Selection.SelectedMarkupRange)) { return; } } } else { if (_selectionIsInvalid == true) { _selectionIsInvalid = false; OnCommandStateChanged(); } } } finally { //unsuspend selection validation now that any subsequent selection changes have passed. _suspendSelectionValidationDepth--; } } else { // Are we still seeing the problems that made this _suspendSelectionValidationDepth stuff necessary? // Rather than masking product instability, we should figure out the root cause. // I don't feel good about just removing this immediately, however. // Who knows what code has now been written that depends on this code being in place. // Let's try to determine if we're still hitting the stack overflow scenario. // In time, if we have not seen this assert, then we may want to remove the _suspendSelectionValidationDepth code. Debug.WriteLine("Avoided a recursive stack overflow. Figure out why it was happening in the first place!"); } } } finally { EndSelectionChangedCaching(); if (ApplicationPerformance.ContainsEvent("Backspace")) ApplicationPerformance.EndEvent("Backspace"); } } }