Example #1
0
        internal bool RemoveCenteringNode()
        {
            MshtmlMarkupServices MarkupServices = new MshtmlMarkupServices(_element.document as IMarkupServicesRaw);
            IHTMLElement         element        = FindCenteringNode();

            // We couldnt find a parent, so nothing to remove
            if (element == null)
            {
                return(false);
            }


            MarkupPointer start  = MarkupServices.CreateMarkupPointer();
            MarkupPointer end    = MarkupServices.CreateMarkupPointer();
            MarkupPointer target = MarkupServices.CreateMarkupPointer();

            // Move the stuff inside the smart content container ouside of itself
            start.MoveAdjacentToElement(element, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin);
            end.MoveAdjacentToElement(element, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeEnd);
            target.MoveAdjacentToElement(element, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
            MarkupServices.Move(start, end, target);

            // remove the empty smart content container
            start.MoveAdjacentToElement(element, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
            end.MoveAdjacentToElement(element, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
            MarkupServices.Remove(start, end);

            return(true);
        }
Example #2
0
        private void UpdateImageLink(string href, IHTMLElement ImgElement, ILinkOptions defaultOptions)
        {
            MshtmlMarkupServices markupServices = new MshtmlMarkupServices((IMarkupServicesRaw)ImgElement.document);
            IHTMLElement         parentElement  = ImgElement.parentElement;

            if (!(parentElement is IHTMLAnchorElement))
            {
                parentElement = markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_A, null);
                MarkupRange range = markupServices.CreateMarkupRange();
                range.MoveToElement(ImgElement, true);
                markupServices.InsertElement(parentElement, range.Start, range.End);

                //set the default target attribute for the new element
                string             target            = defaultOptions.ShowInNewWindow ? "_blank" : null;
                IHTMLAnchorElement htmlAnchorElement = (parentElement as IHTMLAnchorElement);
                if (htmlAnchorElement.target != target) //don't set the target to null if its already null (avoids adding empty target attr)
                {
                    htmlAnchorElement.target = target;
                }

                ImageViewer viewer = DhtmlImageViewers.GetImageViewer(DhtmlImageViewer);
                if (viewer != null)
                {
                    if (defaultOptions.UseImageViewer)
                    {
                        viewer.Apply(htmlAnchorElement, defaultOptions.ImageViewerGroupName);
                    }
                }
            }
            parentElement.setAttribute("href", href, 0);
        }
Example #3
0
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            // Special case because MSHTML is not able remove underlines from <a> elements.
            if (!Underline)
            {
                // Find the parent <a> element (if one exists).
                IHTMLElement currentElement = markupRange.Start.CurrentScope;
                while (currentElement != null && !(currentElement is IHTMLAnchorElement))
                {
                    currentElement = currentElement.parentElement;
                }

                if (currentElement != null)
                {
                    // Explicitly remove the underline.
                    const string   noTextDecorationAttribute = "style=\"text-decoration: none\"";
                    ElementFactory factory = () => markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_FONT, noTextDecorationAttribute);
                    WrapInElement(factory, markupServices, markupRange);

                    return;
                }
            }

            if (!commands[IDM.UNDERLINE].Enabled)
            {
                return;
            }

            commands[IDM.UNDERLINE].Execute();

            Debug.Assert(commands[IDM.UNDERLINE].Latched == Underline, "UnderlineTextStyle did not yield the correct results.");
        }
Example #4
0
        private void Init(IHTMLDocument document, MshtmlMarkupServices markupServices, MarkupRange selectionRange, MarkupRangeFilter filter, DamageFunction damageFunction, bool expandRange)
        {
            // save references
            this.htmlDocument   = document;
            this.markupServices = markupServices;
            this.selectionRange = selectionRange;
            this.filter         = filter;
            this.damageFunction = damageFunction;

            // If the range is already the body, don't expand it or else it will be the whole document
            if (expandRange)
            {
                ExpandRangeToWordBoundaries(selectionRange);
            }

            // initialize pointer to beginning of selection range
            MarkupPointer wordStart = MarkupServices.CreateMarkupPointer(selectionRange.Start);
            MarkupPointer wordEnd   = MarkupServices.CreateMarkupPointer(selectionRange.Start);

            //create the range for holding the current word.
            //Be sure to set its gravity so that it stays around text that get replaced.
            currentWordRange = MarkupServices.CreateMarkupRange(wordStart, wordEnd);
            currentWordRange.Start.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Left;
            currentWordRange.End.Gravity   = _POINTER_GRAVITY.POINTER_GRAVITY_Right;

            currentVirtualPosition = currentWordRange.End.Clone();
        }
Example #5
0
        /// <summary>
        /// Wraps a MarkupRange in an element (or multiple elements if necessary to produce valid HTML).
        /// </summary>
        /// <param name="elementFactory">Creates elements to wrap the markupRange in as needed.</param>
        /// <param name="markupServices">The MarkupServices for the markupRange.</param>
        /// <param name="markupRange">The range to wrap.</param>
        protected void WrapInElement(ElementFactory elementFactory, MshtmlMarkupServices markupServices, MarkupRange markupRange)
        {
            Debug.Assert(markupRange.GetElements(ElementFilters.BLOCK_ELEMENTS, false).Length == 0,
                         "Did not expect MarkupRange to contain block elements");

            MarkupPointer startPointer = markupRange.Start.Clone();
            MarkupPointer endPointer   = markupRange.Start.Clone();
            MarkupContext context;

            while (endPointer.IsLeftOf(markupRange.End))
            {
                context = endPointer.Right(false);

                if (context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_EnterScope ||
                    context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope)
                {
                    if (!markupRange.InRange(context.Element))
                    {
                        // EnterScope example: <span>[markupRange.Start]Hello [endPointer]<i>Hello[markupRange.End]</i></span>
                        // ExitScope example: <span>[markupRange.Start]Hello [endPointer]</span><span>Hello[markupRange.End]</span>
                        InsertElement(elementFactory(), markupServices, startPointer, endPointer);
                        continue;
                    }
                }

                endPointer.Right(true);
            }

            InsertElement(elementFactory(), markupServices, startPointer, endPointer);
        }
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            const string   smallCapsAttribute = "style=\"font-variant: small-caps\"";
            ElementFactory factory            = () => markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_FONT, smallCapsAttribute);

            WrapInElement(factory, markupServices, markupRange);
        }
Example #7
0
        /// <summary>
        /// Makes sure that whole (not parts of) tables are included in the source of a paste.
        /// </summary>
        /// <param name="range">The original source range. The range may be modified.</param>
        /// <param name="markupServices">MarkupServices for the range.</param>
        private void ExpandToIncludeTables(MarkupRange range, MshtmlMarkupServices markupServices)
        {
            MarkupPointer pointer = markupServices.CreateMarkupPointer();

            IHTMLElement[] tableElements = range.GetElements(ElementFilters.TABLE_ELEMENTS, false);
            foreach (IHTMLElement element in tableElements)
            {
                IHTMLElement parentTable = element;
                while (parentTable != null && markupServices.GetElementTagId(parentTable) != _ELEMENT_TAG_ID.TAGID_TABLE)
                {
                    parentTable = parentTable.parentElement;
                }

                if (parentTable != null)
                {
                    pointer.MoveAdjacentToElement(parentTable, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
                    if (range.Start.IsRightOf(pointer))
                    {
                        range.Start.MoveToPointer(pointer);
                    }

                    pointer.MoveAdjacentToElement(parentTable, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
                    if (range.End.IsLeftOf(pointer))
                    {
                        range.End.MoveToPointer(pointer);
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Makes sure that whole (not parts of) lists are included in the source of a paste.
        /// </summary>
        /// <param name="range">The original source range. The range may be modified.</param>
        /// <param name="markupServices">MarkupServices for the range.</param>
        private void ExpandToIncludeLists(MarkupRange range, MshtmlMarkupServices markupServices)
        {
            MarkupPointer      pointer    = markupServices.CreateMarkupPointer();
            IHTMLElementFilter listFilter =
                ElementFilters.CreateCompoundElementFilter(ElementFilters.LIST_ELEMENTS, ElementFilters.LIST_ITEM_ELEMENTS);

            IHTMLElement[] listElements = range.GetElements(listFilter, false);
            foreach (IHTMLElement element in listElements)
            {
                IHTMLElement parentList = element;
                while (parentList != null && !ElementFilters.IsListElement(parentList))
                {
                    parentList = parentList.parentElement;
                }

                if (parentList != null)
                {
                    pointer.MoveAdjacentToElement(parentList, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
                    if (range.Start.IsRightOf(pointer))
                    {
                        range.Start.MoveToPointer(pointer);
                    }

                    pointer.MoveAdjacentToElement(parentList, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
                    if (range.End.IsLeftOf(pointer))
                    {
                        range.End.MoveToPointer(pointer);
                    }
                }
            }
        }
        private string GrowToAnchorParent(HTMLData htmlData)
        {
            if (htmlData.OnlyImageElement == null)
            {
                return(null);
            }

            string html;
            // Load up the html document from the clipboard to a document to examine the html about to be inserted
            MshtmlMarkupServices markupServices = new MshtmlMarkupServices(htmlData.HTMLDocument as IMarkupServicesRaw);
            MarkupRange          range          = markupServices.CreateMarkupRange(htmlData.OnlyImageElement, true);

            // look to see if this is a case where the inserted html is <a>|<img>|</a>
            MarkupContext markupContextStart = range.Start.Left(true);
            MarkupContext markupContextEnd   = range.End.Right(true);

            // if that is the cause, change the html about to be inserted to |<a><img></a>|
            if (markupContextStart.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope &&
                markupContextEnd.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope &&
                markupContextStart.Element.tagName == "A" &&
                markupContextEnd.Element.tagName == "A")
            {
                html = markupContextStart.Element.outerHTML;
            }
            else
            {
                html = htmlData.HTMLSelection;
            }

            return(html);
        }
Example #10
0
        internal SelectionPositionPreservationCookie(MshtmlMarkupServices markupServices, MarkupRange selection, MarkupRange bounds)
        {
            if (!selection.IsEmpty())
            {
                return;
            }

            initialMarkup = bounds.HtmlText;

            NormalizeBounds(ref bounds);

            MarkupPointer p = bounds.Start.Clone();

            movesRight = 0;
            while (p.IsLeftOf(selection.Start))
            {
                movesRight++;
                p.Right(true);
                if (p.IsRightOf(bounds.End))
                {
                    movesRight = int.MaxValue;
                    p.MoveToPointer(bounds.End);
                    break;
                }
            }

            charsLeft = 0;
            while (p.IsRightOf(selection.Start))
            {
                charsLeft++;
                p.MoveUnit(_MOVEUNIT_ACTION.MOVEUNIT_PREVCHAR);
            }
        }
Example #11
0
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            string         fontSizeAttribute = String.Format(CultureInfo.InvariantCulture, "style=\"font-size:{0:F1}pt\"", FontSizeInPoints);
            ElementFactory factory           = () => markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_FONT, fontSizeAttribute);

            WrapInElement(factory, markupServices, markupRange);
        }
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            const string   overlineAttribute = "style=\"text-decoration: overline\"";
            ElementFactory factory           = () => markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_FONT, overlineAttribute);

            WrapInElement(factory, markupServices, markupRange);
        }
Example #13
0
        public static String ToFormattedHtml(MshtmlMarkupServices markupServices, MarkupRange bounds)
        {
            StringBuilder sb        = new StringBuilder();
            HtmlWriter    xmlWriter = new HtmlWriter(sb);

            PrintHtml(xmlWriter, markupServices, bounds);
            return(sb.ToString());
        }
        public static IHTMLElement WrapRangeInElement(MshtmlMarkupServices services, MarkupRange range, _ELEMENT_TAG_ID tagId, string attributes)
        {
            IHTMLElement newElement = services.CreateElement(tagId, attributes);

            services.InsertElement(newElement, range.Start, range.End);

            return(newElement);
        }
Example #15
0
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            if (!commands[IDM.FORECOLOR].Enabled)
            {
                return;
            }

            commands[IDM.FORECOLOR].Execute(ColorHelper.ColorToString(FontColor));
        }
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            if (!commands[IDM.FONTNAME].Enabled)
            {
                return;
            }

            commands[IDM.FONTNAME].Execute(FontFamily);
        }
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            if (!commands[IDM.STRIKETHROUGH].Enabled)
            {
                return;
            }

            commands[IDM.STRIKETHROUGH].Execute();
        }
Example #18
0
        public override void Apply(MshtmlMarkupServices markupServices, MarkupRange markupRange, MshtmlCoreCommandSet commands)
        {
            if (!commands[IDM.BOLD].Enabled)
            {
                return;
            }

            BoldApplier boldApplier = new BoldApplier(markupServices, markupRange, commands[IDM.BOLD]);

            boldApplier.Execute();
        }
Example #19
0
        private static void PrintHtml(HtmlWriter writer, MshtmlMarkupServices MarkupServices, MarkupRange bounds)
        {
            //create a range to span a single position while walking the doc
            MarkupRange range = MarkupServices.CreateMarkupRange();

            range.Start.MoveToPointer(bounds.Start);
            range.End.MoveToPointer(bounds.Start);

            //create a context that can be reused while walking the document.
            MarkupContext context = new MarkupContext();

            //move the range.End to the right and print out each element along the way
            range.End.Right(true, context);
            while (context.Context != _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_None && range.Start.IsLeftOf(bounds.End))
            {
                string text = null;
                if (context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_Text)
                {
                    //if this is a text context, then get the text that is between the start and end points.
                    text = range.HtmlText;

                    //the range.HtmlText operation sometimes returns the outer tags for a text node,
                    //so we need to strip the tags.
                    //FIXME: if the Right/Left operations returned the available text value, this wouldn't be necessary.
                    if (text != null)
                    {
                        text = StripSurroundingTags(text);
                    }
                }
                else if (context.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope)
                {
                    string htmlText = range.HtmlText;
                    if (context.Element.innerHTML == null && htmlText != null && htmlText.IndexOf("&nbsp;") != -1)
                    {
                        //HACK: Under these conditions, there was a was an invisible NBSP char in the
                        //document that is not detectable by walking through the document with MarkupServices.
                        //So, we force the text of the element to be the &nbsp; char to ensure that the
                        //whitespace that was visible in the editor is visible in the final document.
                        text = "&nbsp;";
                    }
                }

                //print the context.
                printContext(writer, context, text, range);

                //move the start element to the spot where the end currently is so tht there is
                //only ever a single difference in position
                range.Start.MoveToPointer(range.End);

                //move the end to the next position
                range.End.Right(true, context);
            }
        }
Example #20
0
        protected ContentSelection(IHtmlEditorComponentContext editorComponentContext, IHTMLElement element, SmartContentState contentState)
        {
            _editorComponentContext = editorComponentContext;
            _markupServices         = editorComponentContext.MarkupServices;
            _element      = element;
            _contentState = contentState;

            _markupRange = _markupServices.CreateMarkupRange(_element, true);
            _markupRange.Start.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right;
            _markupRange.End.Gravity   = _POINTER_GRAVITY.POINTER_GRAVITY_Left;
            _markupRange.Start.Cling   = false;
            _markupRange.End.Cling     = false;
        }
Example #21
0
        /// <summary>
        /// Wraps the range from startPointer to endPointer with the given element (as long as the range is non-empty)
        /// and then moves the pointers past the inserted element.
        /// </summary>
        /// <param name="element">The element to wrap the range in.</param>
        /// <param name="markupServices">The MarkupServices for the start and end pointers.</param>
        /// <param name="startPointer">Pointer to place the beginning of the element.</param>
        /// <param name="endPointer">Pointer to place the end of the element.</param>
        private void InsertElement(IHTMLElement element, MshtmlMarkupServices markupServices,
                                   MarkupPointer startPointer, MarkupPointer endPointer)
        {
            Debug.Assert(startPointer.IsLeftOfOrEqualTo(endPointer), "Expected start to be left of or equal to end!");
            Debug.Assert(HTMLElementHelper.ElementsAreEqual(startPointer.CurrentScope, endPointer.CurrentScope),
                         "Expected start and end to be in the same scope, otherwise resulting HTML will be invalid!");

            if (startPointer.IsLeftOf(endPointer))
            {
                markupServices.InsertElement(element, startPointer, endPointer);
            }

            endPointer.Right(true);
            startPointer.MoveToPointer(endPointer);
        }
Example #22
0
        /// <summary>
        /// Searches through the provided document for a start and end comment marker and then returns the fragment as
        /// a MarkupRange.
        /// </summary>
        /// <param name="document">The document to search.</param>
        /// <param name="startMarker">The comment text that marks the start of the fragment
        /// (e.g. &lt;!--StartFragment--&gt; ).</param>
        /// <param name="endMarker">The comment text that marks the end of the fragment
        /// (e.g. &lt;!--EndFragment--&gt; ).</param>
        /// <returns>The fragment as a MarkupRange or null if no valid fragment was found.</returns>
        private MarkupRange FindMarkedFragment(IHTMLDocument2 document, string startMarker, string endMarker)
        {
            MarkupPointer        startFragment  = null;
            MarkupPointer        endFragment    = null;
            MshtmlMarkupServices markupServices = new MshtmlMarkupServices((IMarkupServicesRaw)document);

            // Look for the markers in the document.
            foreach (IHTMLElement element in document.all)
            {
                if (element is IHTMLCommentElement && ((IHTMLCommentElement)element).text == startMarker)
                {
                    startFragment = markupServices.CreateMarkupPointer(element, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
                }
                else if (element is IHTMLCommentElement && ((IHTMLCommentElement)element).text == endMarker)
                {
                    endFragment = markupServices.CreateMarkupPointer(element, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
                }
            }

            if (startFragment == null || endFragment == null || !startFragment.Positioned || !endFragment.Positioned ||
                startFragment.IsRightOf(endFragment))
            {
                Trace.WriteLine("Unable to find fragment or invalid fragment!");
                return(null);
            }

            // WinLive 251786: IE (and most other browsers) allow HTML like the following:
            //  <p>This is a paragraph[cursor]
            //  <p>This is a paragraph
            // However, when we use MarkupPointers to walk through this HTML, IE pretends there is a </p> at the end
            // of each of the above lines. This can cause issues when we copy part of this HTML somewhere else (e.g
            // everything after the [cursor]) and attempt to walk through both copies (e.g. during paste with keep
            // source formatting) at the same time. This holds true for some other elements, such as <li>s and <td>s.
            MarkupContext startContext = startFragment.Right(false);

            if (startFragment.IsLeftOf(endFragment) &&
                startContext.Context == _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_ExitScope &&
                startContext.Element != null &&
                ElementFilters.IsEndTagOptional(startContext.Element) &&
                !Regex.IsMatch(startContext.Element.outerHTML,
                               String.Format(CultureInfo.InvariantCulture, @"</{0}(\s[^>]*)?>\s*$", startContext.Element.tagName),
                               RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
            {
                startFragment.Right(true);
            }

            return(markupServices.CreateMarkupRange(startFragment, endFragment));
        }
 public SpellingHighlighter(ISpellingChecker spellingChecker, IHighlightRenderingServicesRaw highlightRenderingServices,
                            IDisplayServicesRaw displayServices, IMarkupServicesRaw markupServices, IHTMLDocument4 htmlDocument)
 {
     _spellingChecker            = spellingChecker;
     _highlightRenderingServices = highlightRenderingServices;
     _displayServices            = displayServices;
     _markupServicesRaw          = markupServices;
     _markupServices             = new MshtmlMarkupServices(_markupServicesRaw);
     _htmlDocument = htmlDocument;
     _tracker      = new HighlightSegmentTracker();
     //the timer to handle interleaving of spell checking
     _timer = new SpellingTimer(TIMER_INTERVAL);
     _timer.Start();
     _timer.Tick += new EventHandler(_timer_Tick);
     _workerQueue = new Queue();
 }
Example #24
0
        private IHTMLElement CreateNodeForCentering()
        {
            // Create markup services using the element's document that we are analyzing
            MshtmlMarkupServices MarkupServices = new MshtmlMarkupServices(_element.document as IMarkupServicesRaw);
            MarkupPointer        end            = MarkupServices.CreateMarkupPointer();
            MarkupPointer        start          = MarkupServices.CreateMarkupPointer();

            // Find the element that we will want to wrap.
            IHTMLElement elementToEncapsulate = _element;

            // If the elements parent is an A, we will also want to
            // wrap the A and not just the image inside
            if (_element.parentElement.tagName == "A")
            {
                elementToEncapsulate = _element.parentElement;
            }

            // Move the starting pointer to before the begining of the element we want to wrap
            start.MoveAdjacentToElement(elementToEncapsulate, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);

            // Find this elements parent
            IHTMLElement3 currentBlockScope = start.CurrentBlockScope() as IHTMLElement3;

            // If its parent is also the div that is around the post
            // we need to actually create a new div and just put it around the element

            // If it is splittable block, split it
            // e.g "<DIV>Blah<IMG/>Blah</DIV>" => "<DIV>Blah</DIV><DIV><IMG/></DIV><DIV>Blah</DIV>"
            if (!IsBodyElement(currentBlockScope))
            {
                // We are in a block that can be split so split it at the begining and end
                MarkupHelpers.SplitBlockForInsertionOrBreakout(MarkupServices, null, start);
                end.MoveAdjacentToElement(elementToEncapsulate, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
                MarkupHelpers.SplitBlockForInsertionOrBreakout(MarkupServices, null, end);

                // Position start back to the beginning of our element
                start.MoveAdjacentToElement(elementToEncapsulate, _ELEMENT_ADJACENCY.ELEM_ADJ_BeforeBegin);
            }

            // Now we can wrap it in an P tag (centering node)
            end.MoveAdjacentToElement(elementToEncapsulate, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterEnd);
            IHTMLElement centeringElement = MarkupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_P, string.Empty);

            MarkupServices.InsertElement(centeringElement, start, end);
            return(centeringElement);
        }
        public static void RemoveAttributes(MshtmlMarkupServices markupServices, MarkupRange selection, string[] attributesToRemove)
        {
            IHTMLElementFilter[] filters = new IHTMLElementFilter[attributesToRemove.Length];

            for (int i = 0; i < attributesToRemove.Length; i++)
            {
                filters[i] = ElementFilters.CreateElementAttributeFilter(attributesToRemove[i]);
            }

            IHTMLElement[] elements = selection.GetElements(ElementFilters.CreateCompoundElementFilter(filters), false);
            foreach (IHTMLElement element in elements)
            {
                foreach (string attribute in attributesToRemove)
                {
                    element.removeAttribute(attribute, 0);
                }
            }
        }
Example #26
0
        /// <summary>
        /// Initialize word range for the specified markup-range within the document
        /// </summary>
        public MshtmlWordRange(IHTMLDocument document, bool useDocumentSelectionRange, MarkupRangeFilter filter, DamageFunction damageFunction)
        {
            MshtmlMarkupServices markupServices = new MshtmlMarkupServices((IMarkupServicesRaw)document);
            IHTMLDocument2       document2      = (IHTMLDocument2)document;
            MarkupRange          markupRange;

            if (useDocumentSelectionRange)
            {
                markupRange = markupServices.CreateMarkupRange(document2.selection);
            }
            else
            {
                // TODO: Although this works fine, it would be better to only spellcheck inside editable regions.
                markupRange = markupServices.CreateMarkupRange(document2.body, false);
            }

            Init(document, markupServices, markupRange, filter, damageFunction, useDocumentSelectionRange);
        }
        /// <summary>
        /// Disambiguates a set of title regions to determine which should be editable based on proximity to the main post body element.
        /// </summary>
        /// <param name="bodyElement"></param>
        /// <param name="doc"></param>
        /// <param name="titleElements"></param>
        /// <returns>The title region in closest proximity to the post body element.</returns>
        protected static IHTMLElement GetPrimaryEditableTitleElement(IHTMLElement bodyElement, IHTMLDocument doc, IHTMLElement[] titleElements)
        {
            IHTMLDocument2 doc2         = (IHTMLDocument2)doc;
            IHTMLElement   titleElement = titleElements[0];

            if (titleElements.Length > 1)
            {
                try
                {
                    MshtmlMarkupServices markupServices = new MshtmlMarkupServices((IMarkupServicesRaw)doc2);
                    MarkupRange          bodyRange      = markupServices.CreateMarkupRange(bodyElement, true);
                    MarkupPointer        titlePointer   = null;
                    MarkupPointer        tempPointer    = markupServices.CreateMarkupPointer();
                    foreach (IHTMLElement title in titleElements)
                    {
                        tempPointer.MoveAdjacentToElement(title, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin);
                        if (titlePointer == null)
                        {
                            titlePointer = markupServices.CreateMarkupPointer(title, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin);
                        }
                        else
                        {
                            tempPointer.MoveAdjacentToElement(title, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin);
                            if (tempPointer.IsLeftOf(bodyRange.End) && tempPointer.IsRightOf(titlePointer))
                            {
                                //the temp pointer is closer to the body element, so assume it is more appropriate
                                //to use as the title.
                                titleElement = title;
                                titlePointer.MoveToPointer(tempPointer);
                            }
                        }
                    }
                }
                catch (COMException ex)
                {
                    Trace.WriteLine("Failed to differentiate between multiple nodes with title text, using the first node.  Exception: " + ex);
                }
                catch (InvalidCastException ex)
                {
                    Trace.WriteLine("Failed to differentiate between multiple nodes with title text, using the first node.  Exception: " + ex);
                }
            }
            return(titleElement);
        }
        public static void ClearBackgroundColor(MshtmlMarkupServices markupServices, MarkupRange selection)
        {
            HtmlStyleHelper htmlStyleHelper = new HtmlStyleHelper(markupServices);

            htmlStyleHelper.SplitInlineTags(selection.Start);
            htmlStyleHelper.SplitInlineTags(selection.End);

            IHTMLElement[] elements = selection.GetElements(ElementFilters.CreateTagIdFilter("font"), false);
            foreach (IHTMLElement element in elements)
            {
                element.style.backgroundColor = "";
            }

            // We may now be left with empty font tags, e.g. <font>blah</font>.
            // After switching between editors this becomes <font size="+0">blah</font>, which
            // causes blah to be rendered differently.
            // To avoid that we need to remove any empty-attribute font tags.
            selection.RemoveElementsByTagId(_ELEMENT_TAG_ID.TAGID_FONT, true);
        }
        public OleUndoUnit(MshtmlMarkupServices markupServices, MarkupRange selection)
        {
            _markupServices = markupServices;

            //serialize the current position of the markup pointers so that they can be restored if
            //the DOM gets rolled back into the same state via an undo/redo operation.
            if (selection != null && selection.Positioned)
            {
                IMarkupPointer2Raw pointer2StartRaw = selection.Start.PointerRaw as IMarkupPointer2Raw;
                IMarkupPointer2Raw pointer2EndRaw   = selection.End.PointerRaw as IMarkupPointer2Raw;
                pointer2StartRaw.GetMarkupPosition(out _startPosition);
                pointer2EndRaw.GetMarkupPosition(out _endPosition);
                pointer2StartRaw.GetContainer(out _markupContainer);
            }

            _description = "OleUndoUnit" + Guid.NewGuid().ToString();

            Undo = true;
        }
Example #30
0
        /// <summary>
        /// Creates a MarkupRange that contains the entire provided document.
        /// </summary>
        /// <param name="document">The document to select.</param>
        /// <returns>A MarkupRange that contains the entire document.</returns>
        private MarkupRange SelectAll(IHTMLDocument2 document)
        {
            MshtmlMarkupServices markupServices = new MshtmlMarkupServices((IMarkupServicesRaw)document);
            MarkupRange          entireDocument = markupServices.CreateMarkupRange(((IHTMLDocument3)document).documentElement, true);

            // Make sure the doctype and anything else outside the root element is selected too.
            MarkupContext context = entireDocument.Start.Left(true);

            while (context.Context != _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_None)
            {
                context = entireDocument.Start.Left(true);
            }

            context = entireDocument.End.Right(true);
            while (context.Context != _MARKUP_CONTEXT_TYPE.CONTEXT_TYPE_None)
            {
                context = entireDocument.End.Right(true);
            }

            return(entireDocument);
        }