private void RemoveParentAnchorFromTitle(IHTMLElement titleElement, MshtmlMarkupServices markupServices)
        {
            try
            {
                // Look for a parent <A> anchor tag
                MarkupPointer startPointer = markupServices.CreateMarkupPointer(titleElement, _ELEMENT_ADJACENCY.ELEM_ADJ_AfterBegin);
                IHTMLElement parentAnchor = startPointer.GetParentElement(ElementFilters.ANCHOR_ELEMENTS);

                if (parentAnchor != null)
                {
                    // To make sure the title is readable, we will replace the anchor tag with a new span tag
                    // that has all of the anchor tag properties we believe need to be propogated.  This will
                    // keep the final render as close to the original as possible.

                    // Get the next parent block element to compare against the anchor tag
                    IHTMLElement parentBlock = startPointer.GetParentElement(ElementFilters.BLOCK_OR_TABLE_CELL_ELEMENTS);

                    // If no block element was found we will compare against the body
                    if (parentBlock == null)
                        parentBlock = startPointer.GetParentElement(ElementFilters.BODY_ELEMENT);

                    Debug.Assert(parentBlock != null, "parentBlock was unexpectedly null!");

                    IHTMLElement newAnchor = markupServices.CreateElement(_ELEMENT_TAG_ID.TAGID_SPAN, string.Empty);
                    IHTMLElement2 newAnchor2 = (IHTMLElement2)newAnchor;
                    IHTMLElement2 parentAnchor2 = (IHTMLElement2)parentAnchor;
                    IHTMLElement2 parentBlock2 = (IHTMLElement2)parentBlock;

                    // Only insert the size if it was redefined from the anchor's parent.  That way
                    // relative sizes don't layer on each other.
                    if (!(parentAnchor2.currentStyle.fontSize.Equals(parentBlock2.currentStyle.fontSize)))
                        newAnchor2.runtimeStyle.fontSize = parentAnchor2.currentStyle.fontSize;

                    // Copy all other attributes
                    CopyAnchorAttributes(parentAnchor2, newAnchor2);

                    markupServices.ReplaceElement(parentAnchor, newAnchor);
                }
            }
            catch (Exception ex)
            {
                // Any failure is ignorable, we will just continue with the parent anchor tag
                Debug.WriteLine("Failed to remove parent anchor tag from title, " + ex);
            }
        }
        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;
        }
        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);
        }
        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;
        }