public static IHTMLTxtRange AquireTxtRange(MarkupRange range)
        {
            try
            {
                if (cache != null)
                {
                    int documentId = GetDocumentKey(range.MarkupServices.MarkupServicesRaw);

                    Queue         queue       = null;
                    IHTMLTxtRange returnRange = null;

                    lock (cache.SyncRoot)
                    {
                        queue = (Queue)cache[documentId];

                        if (queue == null)
                        {
                            queue = Queue.Synchronized(new Queue());
                            cache.Add(documentId, queue);
                        }

                        if (queue.Count > 0)
                        {
                            lock (queue.SyncRoot)
                            {
                                returnRange = (IHTMLTxtRange)queue.Dequeue();
                            }
                        }
                        else
                        {
                            returnRange = range.ToTextRange();
                        }
                    }

                    return(returnRange);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failure in IHTMLTxtRangePool: " + ex);
                cache = null;
            }

            try
            {
                return(range.ToTextRange());
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failure in IHTMLTxtRangePool: " + ex);
                return(null);
            }
        }
        private void ApplyBlockStyle(_ELEMENT_TAG_ID styleTagId, MarkupRange selection, MarkupRange maximumBounds, MarkupRange postOpSelection)
        {
            Debug.Assert(selection != maximumBounds, "selection and maximumBounds must be distinct objects");
            SelectionPositionPreservationCookie selectionPreservationCookie = null;

            //update the range cling and gravity so it will stick with the re-arranged block content
            selection.Start.PushCling(false);
            selection.Start.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Left);
            selection.End.PushCling(false);
            selection.End.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Right);

            try
            {
                if (selection.IsEmpty())
                {
                    //nothing is selected, so expand the selection to cover the entire parent block element
                    IHTMLElementFilter stopFilter =
                        ElementFilters.CreateCompoundElementFilter(ElementFilters.BLOCK_ELEMENTS,
                                                                   new IHTMLElementFilter(IsSplitStopElement));
                    MovePointerLeftUntilRegionBreak(selection.Start, stopFilter, maximumBounds.Start);
                    MovePointerRightUntilRegionBreak(selection.End, stopFilter, maximumBounds.End);
                }

                using (IUndoUnit undo = _editor.CreateSelectionUndoUnit(selection))
                {
                    selectionPreservationCookie = SelectionPositionPreservationHelper.Save(_markupServices, postOpSelection, selection);
                    if (selection.IsEmptyOfContent())
                    {
                        ApplyBlockFormatToEmptySelection(selection, styleTagId, maximumBounds);
                    }
                    else
                    {
                        ApplyBlockFormatToContentSelection(selection, styleTagId, maximumBounds);
                    }
                    undo.Commit();
                }
            }
            finally
            {
                selection.Start.PopCling();
                selection.Start.PopGravity();
                selection.End.PopCling();
                selection.End.PopGravity();
            }

            if (!SelectionPositionPreservationHelper.Restore(selectionPreservationCookie, selection, selection.Clone()))
                selection.ToTextRange().select();
        }
        public void InsertLink(string url, string linkText, string title, string rel, bool newWindow, MarkupRange range)
        {
            string html = HtmlGenerationService.GenerateHtmlFromLink(url, linkText, title, rel, newWindow);
            if (range == null)
            {
                range = SelectedMarkupRange;
            }
            IHTMLTxtRange txtRange = range.ToTextRange();
            if (txtRange.text != null)
            {
                int length = txtRange.text.TrimEnd(null).Length;
                txtRange.moveEnd("CHARACTER", length - txtRange.text.Length);
                if (txtRange.text != null)
                {
                    length = txtRange.text.Length;
                    int startLength = txtRange.text.TrimStart(null).Length;
                    txtRange.moveStart("CHARACTER", length - startLength);
                }
                range.MoveToTextRange(txtRange);
            }

            //put the cursor at the end of the link, but outside of it
            range.Start.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Left);
            range.End.PushGravity(_POINTER_GRAVITY.POINTER_GRAVITY_Right);

            try
            {
                InsertHtml(range.Start, range.End, html);
                range.Start.MoveToPointer(range.End);
                range.ToTextRange().select();
            }
            finally
            {
                range.Start.PopGravity();
                range.End.PopGravity();
            }

        }
        /// <summary>
        /// Synchronizes the selection with a TextRange and notifies selection listeners that the selection has changed.
        /// </summary>
        private void UpdateSelection(MarkupRange selection)
        {
            if (selection != null)
                selection.ToTextRange().select();

            //fire the selection changed event since the IHTMLTxtRange object doesn't
            (MshtmlEditor.MshtmlControl.DocumentEvents as HTMLDocumentEvents2).onselectionchange(null);
        }
        public static bool AdjustMarkupRange(MarkupRange range, int offset, int length)
        {
            IHTMLTxtRange stagingTextRange = range.ToTextRange();

            return(AdjustMarkupRange(ref stagingTextRange, range, offset, length));
        }
        internal bool Restore(MarkupRange selection, MarkupRange bounds)
        {
            if (initialMarkup == null)
                return false;

            NormalizeBounds(ref bounds);
            /*
                        if (initialMarkup != bounds.HtmlText)
                        {
                            Trace.Fail("Unexpected markup");
                            Trace.WriteLine(initialMarkup);
                            Trace.WriteLine(bounds.HtmlText);
                            return false;
                        }
            */

            selection.Start.MoveToPointer(bounds.Start);

            if (movesRight == int.MaxValue)
            {
                selection.Start.MoveToPointer(bounds.End);
            }
            else
            {
                for (int i = 0; i < movesRight; i++)
                {
                    selection.Start.Right(true);
                }
            }

            for (int i = 0; i < charsLeft; i++)
            {
                selection.Start.MoveUnit(_MOVEUNIT_ACTION.MOVEUNIT_PREVCHAR);
            }

            selection.Collapse(true);
            selection.ToTextRange().select();

            Debug.Assert(bounds.InRange(selection, true), "Selection was out of bounds");

            return true;
        }