示例#1
0
 /// <summary>
 /// If this element is a table, and there's nothing else before or after it and it's aligned left or right
 /// then removes the alignment.
 /// </summary>
 /// <param name="range">The original source range.</param>
 private void RemoveAlignmentIfSingleTable(MarkupRange range)
 {
     // WinLive 273280: Alignment on a table acts like a float, which can throw off the layout of the rest of
     // the document. If there is nothing before or after the table, then we can safely remove the alignment.
     IHTMLElement[] topLevelElements = range.GetTopLevelElements(e => !(e is IHTMLCommentElement));
     if (topLevelElements.Length == 1 &&
         topLevelElements[0] is IHTMLTable &&
         (String.Compare(topLevelElements[0].getAttribute("align", 2) as string, "left", StringComparison.OrdinalIgnoreCase) == 0 ||
          String.Compare(topLevelElements[0].getAttribute("align", 2) as string, "right", StringComparison.OrdinalIgnoreCase) == 0))
     {
         topLevelElements[0].removeAttribute("align", 0);
     }
 }
        public static void InsertContentIntoElement(string content, ISmartContent sContent, IContentSourceSidebarContext contentSourceContext, IHTMLElement element)
        {
            MshtmlMarkupServices MarkupServices = new MshtmlMarkupServices((IMarkupServicesRaw)element.document);

            //Note: undo/redo disabled for smart content since undo causes the HTML to get out of sync
            //with the inserter's settings state, so undo changes will be blown away the next time the
            //the inserter's HTML is regenerated.  Also note that making this insertion without wrapping it
            //in an undo clears the undo/redo stack, which is what we want for beta.
            //string undoId = Guid.NewGuid().ToString();

            MarkupRange htmlRange = MarkupServices.CreateMarkupRange(element, false);

            htmlRange.Start.PushCling(true);
            htmlRange.End.PushCling(true);
            MarkupServices.Remove(htmlRange.Start, htmlRange.End);
            htmlRange.Start.PopCling();
            htmlRange.End.PopCling();

            element.style.padding = ToPaddingString(sContent.Layout);

            if (sContent.Layout.Alignment == Alignment.None ||
                sContent.Layout.Alignment == Alignment.Right ||
                sContent.Layout.Alignment == Alignment.Left)
            {
                element.style.display     = "inline";
                element.style.marginLeft  = "0px";
                element.style.marginRight = "0px";
                element.style.styleFloat  = sContent.Layout.Alignment.ToString().ToLower(CultureInfo.InvariantCulture);
            }
            else if (sContent.Layout.Alignment == Alignment.Center)
            {
                element.style.styleFloat  = Alignment.None.ToString().ToLower(CultureInfo.InvariantCulture);
                element.style.display     = "block";
                element.style.marginLeft  = "auto";
                element.style.marginRight = "auto";
            }

            // Clear out any width on the overall smart content block, if the element is centered, we will add the width back in later
            // after we calcuate it from the childern, the current width value is stale.
            element.style.width = "";

            //Note: we use MarkupServices to insert the content so that IE doesn't try to fix up URLs.
            //Element.insertAdjacentHTML() is a no-no because it rewrites relaive URLs to include
            //the fullpath from the local filesytem.

            //MarkupServices.ParseString() doesn't attempt to fix up URLs, so its safe to use.
            //We will now stage the new content into a MarkupContainer, and then move it into
            //the working document.
            MarkupPointer sc1 = MarkupServices.CreateMarkupPointer();
            MarkupPointer sc2 = MarkupServices.CreateMarkupPointer();

            //Create a temporary document from the html and set the start/end pointers to the
            //start and end of the document.
            MarkupServices.ParseString(content, sc1, sc2);
            IHTMLDocument2 doc          = sc1.GetDocument();
            MarkupRange    stagingRange = MarkupServices.CreateMarkupRange(sc1, sc2);

            stagingRange.MoveToElement(doc.body, false);

            //IE7 hack: fixes bug 305512.  Note that this will destroy the inner content of the element,
            //so make sure it is called before the refreshed content is inserted.
            BeforeInsertInvalidateHackForIE7(element);

            //move the content from the staging area into the actual insertion point.
            MarkupServices.Move(stagingRange.Start, stagingRange.End, htmlRange.End);

            if (sContent.Layout.Alignment == Alignment.Center)
            {
                MarkupContext mc    = htmlRange.End.Right(false);
                MarkupRange   range = MarkupServices.CreateMarkupRange(mc.Element, false);

                IHTMLElement[] childern = range.GetTopLevelElements(MarkupRange.FilterNone);

                int maxWidth = 0;
                foreach (IHTMLElement child in childern)
                {
                    maxWidth = Math.Max(maxWidth, child.offsetWidth);
                }

                if (maxWidth != 0)
                {
                    mc.Element.style.width = maxWidth;
                }
            }

            // Let the context provider know the smart content was edited.
            string contentSourceId, contentId;

            ContentSourceManager.ParseContainingElementId(element.id, out contentSourceId, out contentId);
            contentSourceContext.OnSmartContentEdited(contentId);
        }