Ejemplo n.º 1
0
        private static void AddMenuItem(StringBuilder sb, TridionSiteMapNode node, string submenu, string cssClass)
        {
            var li = new TagBuilder("li");
            var a  = new TagBuilder("a");

            var span = new TagBuilder("span");

            span.Attributes.Add("class", cssClass);

            var span2 = new TagBuilder("span");

            span2.Attributes.Add("class", "arrow icon");

            span.InnerHtml = node.Title;

            if (cssClass != null)
            {
                a.InnerHtml = span + span2.ToString();
            }

            string href = null;

            if (node.Attributes["RedirectUrl"] != null)
            {
                href = node.Attributes["RedirectUrl"];
            }
            else
            {
                href = RootType.ToLower().Equals("structure") ? General.AdjustUrlToContext(node.Url) : General.AdjustUrlToContext(node.ResolvedUrl);
            }

            a.Attributes.Add("href", href);

            if (cssClass == null)
            {
                a.InnerHtml = node.Title;
            }

            if (submenu == null)
            {
                li.InnerHtml = a.ToString();
            }
            else
            {
                li.InnerHtml = a + submenu;
            }

            sb.AppendLine(li.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Extension method on String to resolve rich text.
        /// It will strip a full width table from the rtf so it can be placed separate in the view using Model.Field["key"].Value.FullWidthTable()
        /// Use as: Model.Field["key"].Value.ResolveRichText()
        /// </summary>
        /// <param name="value">the rich text string</param>
        /// <param name="displayFullWidthTable">display full width table, default value is false</param>
        /// <returns>MvcHtmlString (resolved rich text)</returns>
        public static MvcHtmlString ResolveRichText(this string value)
        {
            XmlDocument         doc   = new XmlDocument();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

            nsmgr.AddNamespace("xhtml", XhtmlNamespaceUri);
            nsmgr.AddNamespace("xlink", XlinkNamespaceUri);
            doc.LoadXml(string.Format("<xhtmlroot>{0}</xhtmlroot>", value));

            // remove full width table
            foreach (XmlNode table in doc.SelectNodes("//xhtml:table[@class='full-width']", nsmgr))
            {
                table.ParentNode.RemoveChild(table);
            }
            // resolve links which haven't been resolved
            foreach (XmlNode link in doc.SelectNodes("//xhtml:a[@xlink:href[starts-with(string(.),'tcm:')]][@xhtml:href='' or not(@xhtml:href)]", nsmgr))
            {
                string tcmuri  = link.Attributes["xlink:href"].Value;
                string linkUrl = LinkFactory.ResolveLink(tcmuri);

                if (!string.IsNullOrEmpty(linkUrl))
                {
                    linkUrl = General.AdjustUrlToContext(linkUrl);

                    // add href
                    XmlAttribute href = doc.CreateAttribute("xhtml:href");
                    href.Value = linkUrl;
                    link.Attributes.Append(href);

                    // remove all xlink attributes
                    foreach (XmlAttribute xlinkAttr in link.SelectNodes("//@xlink:*", nsmgr))
                    {
                        link.Attributes.Remove(xlinkAttr);
                    }
                }
                else
                {
                    // copy child nodes of link so we keep them
                    foreach (XmlNode child in link.ChildNodes)
                    {
                        link.ParentNode.InsertBefore(child.CloneNode(true), link);
                    }

                    // remove link node
                    link.ParentNode.RemoveChild(link);
                }
            }

            // remove any additional xlink attribute
            foreach (XmlNode node in doc.SelectNodes("//*[@xlink:*]", nsmgr))
            {
                foreach (XmlAttribute attr in node.SelectNodes("//@xlink:*", nsmgr))
                {
                    node.Attributes.Remove(attr);
                }
            }

            // add application context path to images
            foreach (XmlElement img in doc.SelectNodes("//*[@src]", nsmgr))
            {
                if (img.GetAttributeNode("src") != null)
                {
                    img.Attributes["src"].Value = General.AdjustUrlToContext(img.Attributes["src"].Value);
                }
            }

            // fix empty anchors by placing the id value as a text node and adding a style attribute with position:absolute and visibility:hidden so the value won't show up
            foreach (XmlElement anchor in doc.SelectNodes("//xhtml:a[not(node())]", nsmgr))
            {
                XmlAttribute style = doc.CreateAttribute("style");
                style.Value = "position:absolute;visibility:hidden;";
                anchor.Attributes.Append(style);
                anchor.InnerText = anchor.Attributes["id"] != null ? anchor.Attributes["id"].Value : "empty";
            }

            return(new MvcHtmlString(RemoveNamespaceReferences(doc.DocumentElement.InnerXml)));
        }