/// <exclude />
        public static Control AsAspNetControl(this XhtmlDocument xhtmlDocument, IXElementToControlMapper controlMapper)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                HtmlGenericControl htmlControl = new HtmlGenericControl("html");
                CopyAttributes(xhtmlDocument.Root, htmlControl);

                HtmlHead headControl = xhtmlDocument.BuildHtmlHeadControl(controlMapper);

                Control bodyControl = xhtmlDocument.Body.AsAspNetControl(controlMapper);

                htmlControl.Controls.Add(headControl);
                htmlControl.Controls.Add(bodyControl);

                PlaceHolder pageHolder = new PlaceHolder();
                if (xhtmlDocument.DocumentType != null)
                {
                    string docType = xhtmlDocument.DocumentType.ToString();
                    if (docType.Contains("[]"))
                    {
                        docType = docType.Remove(docType.IndexOf("[]"), 2);
                    }

                    pageHolder.Controls.Add(new LiteralControl(docType));
                }
                pageHolder.Controls.Add(htmlControl);

                return pageHolder;
            }
        }
        /// <exclude />
        public static Control AsAspNetControl(this XhtmlDocument xhtmlDocument, IXElementToControlMapper controlMapper)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                var htmlControl = new HtmlGenericControl("html");
                CopyAttributes(xhtmlDocument.Root, htmlControl);

                HtmlHead headControl = xhtmlDocument.BuildHtmlHeadControl(controlMapper);

                Control bodyControl = xhtmlDocument.Body.AsAspNetControl(controlMapper);

                htmlControl.Controls.Add(headControl);
                htmlControl.Controls.Add(bodyControl);

                PlaceHolder pageHolder = new PlaceHolder();
                if (xhtmlDocument.DocumentType != null)
                {
                    string docType = xhtmlDocument.DocumentType.ToString();
                    var    offset  = docType.IndexOf("[]", StringComparison.Ordinal);
                    if (offset >= 0)
                    {
                        docType = docType.Remove(offset, 2);
                    }

                    pageHolder.Controls.Add(new LiteralControl(docType));
                }
                pageHolder.Controls.Add(htmlControl);

                return(pageHolder);
            }
        }
        /// <exclude />
        public static Control AsAspNetControl(this XNode xnode, IXElementToControlMapper controlMapper)
        {
            if (xnode is XElement) return ((XElement)xnode).AsAspNetControl(controlMapper);
            if (xnode is XDocument) return ((XDocument)xnode).Root.AsAspNetControl(controlMapper);

            if (xnode is XText) return new LiteralControl(((XText)xnode).Value);
            if (xnode is XComment) return new LiteralControl(string.Format("<!--{0}-->", ((XComment)xnode).Value));

            throw new NotImplementedException(string.Format("Type '{0}' not handled", xnode.GetType().Name));
        }
        /// <exclude />
        public static Control AsAspNetControl(this XElement element, IXElementToControlMapper controlMapper)
        {
            Control control;

            if (controlMapper.TryGetControlFromXElement(element, out control) == false)
            {
                if (IsHtmlControlElement(element) || element.Attribute("id") != null)
                {
                    control = new HtmlGenericControl(element.Name.LocalName);
                    control.ClientIDMode = ClientIDMode.Static;
                    CopyAttributes(element, (HtmlControl)control);
                    ExportChildNodes(element.Nodes(), control, controlMapper);
                }
                else
                {
                    XElement copy = CopyWithoutNamespace(element, Namespaces.Xhtml);
                    control = new LiteralControl(copy.ToString());
                }
            }

            return(control);
        }
        /// <exclude />
        public static Control AsAspNetControl(this XNode xnode, IXElementToControlMapper controlMapper)
        {
            if (xnode is XElement)
            {
                return(((XElement)xnode).AsAspNetControl(controlMapper));
            }
            if (xnode is XDocument)
            {
                return(((XDocument)xnode).Root.AsAspNetControl(controlMapper));
            }

            if (xnode is XText)
            {
                return(new LiteralControl(((XText)xnode).Value));
            }
            if (xnode is XComment)
            {
                return(new LiteralControl(string.Format("<!--{0}-->", ((XComment)xnode).Value)));
            }

            throw new NotImplementedException(string.Format("Type '{0}' not handled", xnode.GetType().Name));
        }
        /// <exclude />
        public static Control AsAspNetControl(this XNode xnode, IXElementToControlMapper controlMapper)
        {
            if (xnode is XElement element)
            {
                return(element.AsAspNetControl(controlMapper));
            }
            if (xnode is XDocument document)
            {
                return(document.Root.AsAspNetControl(controlMapper));
            }

            if (xnode is XText text)
            {
                return(new LiteralControl(text.Value));
            }
            if (xnode is XComment comment)
            {
                return(new LiteralControl($"<!--{comment.Value}-->"));
            }

            throw new NotImplementedException($"Type '{xnode.GetType().Name}' not handled");
        }
        private static HtmlHead BuildHtmlHeadControl(this XhtmlDocument xhtmlDocument, IXElementToControlMapper controlMapper)
        {
            HtmlHead headControl = new HtmlHead();

            xhtmlDocument.MergeToHeadControl(headControl, controlMapper);

            return headControl;
        }
        private static HtmlHead BuildHtmlHeadControl(this XhtmlDocument xhtmlDocument, IXElementToControlMapper controlMapper)
        {
            var headControl = new HtmlHead();

            xhtmlDocument.MergeToHeadControl(headControl, controlMapper);

            return(headControl);
        }
        internal static void MergeToHeadControl(this XhtmlDocument xhtmlDocument, HtmlHead headControl, IXElementToControlMapper controlMapper)
        {
            XElement headSource = xhtmlDocument.Head;

            if (headSource == null) return;

            CopyAttributes(headSource, headControl);

            XElement titleElement = headSource.Elements(Namespaces.Xhtml + "title").LastOrDefault();
            if (titleElement != null)
            {
                HtmlTitle existingControl = headControl.Controls.OfType<HtmlTitle>().FirstOrDefault();

                if (existingControl != null)
                {
                    headControl.Controls.Remove(existingControl);
                }

                // NOTE: we aren't using headControl.Title property since it adds "<title>" tag as the last one
                headControl.Controls.AddAt(0, new HtmlTitle { Text = HttpUtility.HtmlEncode(titleElement.Value) });
            }

            var metaTags = headSource.Elements().Where(f => f.Name == Namespaces.Xhtml + "meta");
            int metaTagPosition = Math.Min(1, headControl.Controls.Count);
            foreach (var metaTag in metaTags)
            {
                HtmlMeta metaControl = new HtmlMeta();
                foreach (var attribute in metaTag.Attributes())
                {
                    metaControl.Attributes.Add(attribute.Name.LocalName, attribute.Value);
                }
                headControl.Controls.AddAt(metaTagPosition++, metaControl);
            }

            ExportChildNodes(headSource.Nodes().Where(f => ((f is XElement) == false || ((XElement)f).Name != Namespaces.Xhtml + "title" && ((XElement)f).Name != Namespaces.Xhtml + "meta")), headControl, controlMapper);

            headControl.RemoveDuplicates();
        }
Esempio n. 10
0
        /// <exclude />
        public static Control Render(XDocument document, FunctionContextContainer contextContainer, IXElementToControlMapper mapper, IPage page)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                ExecuteEmbeddedFunctions(document.Root, contextContainer);

                ResolvePageFields(document, page);

                NormalizeAspNetForms(document);

                if (document.Root.Name != Namespaces.Xhtml + "html")
                {
                    return(new LiteralControl(document.ToString()));
                }

                XhtmlDocument xhtmlDocument = new XhtmlDocument(document);
                NormalizeXhtmlDocument(xhtmlDocument);

                ResolveRelativePaths(xhtmlDocument);

                PrioritizeHeadNodex(xhtmlDocument);

                AppendC1MetaTags(page, xhtmlDocument);

                LocalizationParser.Parse(xhtmlDocument);

                return(xhtmlDocument.AsAspNetControl(mapper));
            }
        }
Esempio n. 11
0
        private void MergeHeadSection(XhtmlDocument xhtmlDocument, HtmlHead headControl, IXElementToControlMapper controlMapper)
        {
            xhtmlDocument.MergeToHeadControl(headControl, controlMapper);

            // handling custom master page head control locally - removing it if a generic meta description tag was in the document
            if (headControl.Controls.OfType<HtmlMeta>().Any(f=>f.Name=="description"))
            {
                var existingDescriptionMetaTag = headControl.Controls.OfType<DescriptionMetaTag>().FirstOrDefault();
                if (existingDescriptionMetaTag != null)
                {
                    headControl.Controls.Remove(existingDescriptionMetaTag);
                }
            }
        }
        private static void ExportChildNodes(IEnumerable<XNode> nodes, Control containerControl, IXElementToControlMapper controlMapper)
        {
            foreach (var childNode in nodes)
            {
                if (childNode is XElement)
                {
                    containerControl.Controls.Add(((XElement)childNode).AsAspNetControl(controlMapper));
                    continue;
                }

                if (childNode is XCData)
                {
                    if (!childNode.Ancestors().Any(f => f.Name.LocalName == "script"))
                    {
                        XCData cdata = (XCData)childNode;
                        LiteralControl literal = new LiteralControl(cdata.Value);
                        containerControl.Controls.Add(literal);
                        continue;
                    }
                }

                if (childNode is XText)
                {
                    LiteralControl literal = new LiteralControl(childNode.ToString());
                    containerControl.Controls.Add(literal);
                    continue;
                }

                if (childNode is XComment)
                {
                    containerControl.Controls.Add(new LiteralControl(childNode.ToString() + "\n"));
                    continue;
                }

                throw new NotImplementedException(string.Format("Unhandled XNode type '{0}'", childNode.GetType()));
            }
        }
Esempio n. 13
0
        /// <exclude />
        public static Control Render(XDocument document, FunctionContextContainer contextContainer, IXElementToControlMapper mapper, IPage page)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                ProcessPageDocument(document, contextContainer, page);

                if (document.Root.Name != RenderingElementNames.Html)
                {
                    return(new LiteralControl(document.ToString()));
                }

                var xhtmlDocument = new XhtmlDocument(document);

                ProcessXhtmlDocument(xhtmlDocument, page);

                using (Profiler.Measure("Converting XHTML document into an ASP.NET control"))
                {
                    return(xhtmlDocument.AsAspNetControl(mapper));
                }
            }
        }
Esempio n. 14
0
 private static void AddNodesAsControls(IEnumerable <XNode> nodes, Control parent, IXElementToControlMapper mapper)
 {
     foreach (var node in nodes)
     {
         var c = node.AsAspNetControl(mapper);
         parent.Controls.Add(c);
     }
 }
        /// <exclude />
        public static Control AsAspNetControl(this XElement element, IXElementToControlMapper controlMapper)
        {
            Control control;

            if (controlMapper.TryGetControlFromXElement(element, out control) == false)
            {
                if (IsHtmlControlElement(element) || element.Attribute("id") != null)
                {
                    control = new HtmlGenericControl(element.Name.LocalName);
                    control.ClientIDMode = ClientIDMode.Static;
                    CopyAttributes(element, (HtmlControl)control);
                    ExportChildNodes(element.Nodes(), control, controlMapper);
                }
                else
                {
                    XElement copy = CopyWithoutNamespace(element, Namespaces.Xhtml);
                    control = new LiteralControl(copy.ToString());
                }
            }

            return control;
        }
        private static void ExportChildNodes(IEnumerable <XNode> nodes, Control containerControl, IXElementToControlMapper controlMapper)
        {
            foreach (var childNode in nodes)
            {
                if (childNode is XElement element)
                {
                    containerControl.Controls.Add(element.AsAspNetControl(controlMapper));
                    continue;
                }

                if (childNode is XCData cdata)
                {
                    if (!childNode.Ancestors().Any(f => f.Name.LocalName == "script"))
                    {
                        var literal = new LiteralControl(cdata.Value);
                        containerControl.Controls.Add(literal);
                        continue;
                    }
                }

                if (childNode is XText)
                {
                    var literal = new LiteralControl(childNode.ToString());
                    containerControl.Controls.Add(literal);
                    continue;
                }

                if (childNode is XComment)
                {
                    containerControl.Controls.Add(new LiteralControl(childNode + "\n"));
                    continue;
                }

                throw new NotImplementedException($"Unhandled XNode type '{childNode.GetType()}'");
            }
        }
        internal static void MergeToHeadControl(this XhtmlDocument xhtmlDocument, HtmlHead headControl, IXElementToControlMapper controlMapper)
        {
            XElement headSource = xhtmlDocument.Head;

            if (headSource == null)
            {
                return;
            }

            CopyAttributes(headSource, headControl);

            XElement titleElement = headSource.Elements(XName_Title).LastOrDefault();

            if (titleElement != null)
            {
                HtmlTitle existingControl = headControl.Controls.OfType <HtmlTitle>().FirstOrDefault();

                if (existingControl != null)
                {
                    headControl.Controls.Remove(existingControl);
                }

                // NOTE: we aren't using headControl.Title property since it adds "<title>" tag as the last one
                headControl.Controls.AddAt(0, new HtmlTitle {
                    Text = HttpUtility.HtmlEncode(titleElement.Value)
                });
            }

            var metaTags        = headSource.Elements().Where(f => f.Name == XName_Meta);
            int metaTagPosition = Math.Min(1, headControl.Controls.Count);

            foreach (var metaTag in metaTags)
            {
                var metaControl = new HtmlMeta();
                foreach (var attribute in metaTag.Attributes())
                {
                    if (attribute.Name.LocalName == "id")
                    {
                        metaControl.ID = attribute.Value;
                    }
                    else
                    {
                        metaControl.Attributes.Add(attribute.Name.LocalName, attribute.Value);
                    }
                }
                headControl.Controls.AddAt(metaTagPosition++, metaControl);
            }

            ExportChildNodes(headSource.Nodes().Where(f =>
                                                      !(f is XElement element) || (element.Name != XName_Title && element.Name != XName_Meta)),
                             headControl, controlMapper);

            headControl.RemoveDuplicates();
        }
Esempio n. 18
0
        private void MergeHeadSection(XhtmlDocument xhtmlDocument, HtmlHead headControl, IXElementToControlMapper controlMapper)
        {
            xhtmlDocument.MergeToHeadControl(headControl, controlMapper);

            // handling custom master page head control locally - removing it if a generic meta description tag was in the document
            if (headControl.Controls.OfType <HtmlMeta>().Any(f => f.Name == "description"))
            {
                var existingDescriptionMetaTag = headControl.Controls.OfType <DescriptionMetaTag>().FirstOrDefault();
                if (existingDescriptionMetaTag != null)
                {
                    headControl.Controls.Remove(existingDescriptionMetaTag);
                }
            }
        }
Esempio n. 19
0
 private static void AddNodesAsControls(IEnumerable<XNode> nodes, Control parent, IXElementToControlMapper mapper)
 {
     foreach (var node in nodes)
     {
         var c = node.AsAspNetControl(mapper);
         parent.Controls.Add(c);
     }
 }
Esempio n. 20
0
        /// <exclude />
        public static Control Render(XDocument document, FunctionContextContainer contextContainer, IXElementToControlMapper mapper, IPage page)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                using (Profiler.Measure("Executing embedded functions"))
                {
                    ExecuteEmbeddedFunctions(document.Root, contextContainer);
                }

                using (Profiler.Measure("Resolving page fields"))
                {
                    ResolvePageFields(document, page);
                }

                using (Profiler.Measure("Normalizing ASP.NET forms"))
                {
                    NormalizeAspNetForms(document);
                }

                if (document.Root.Name != RenderingElementNames.Html)
                {
                    return(new LiteralControl(document.ToString()));
                }

                var xhtmlDocument = new XhtmlDocument(document);

                ProcessXhtmlDocument(xhtmlDocument, page);

                using (Profiler.Measure("Converting XHTML document into an ASP.NET control"))
                {
                    return(xhtmlDocument.AsAspNetControl(mapper));
                }
            }
        }
Esempio n. 21
0
        /// <exclude />
        public static Control Render(XDocument document, FunctionContextContainer contextContainer, IXElementToControlMapper mapper, IPage page)
        {
            using (TimerProfilerFacade.CreateTimerProfiler())
            {
                bool disableCaching = false;

                using (Profiler.Measure("Executing embedded functions"))
                {
                    ExecuteFunctionsRec(document.Root, contextContainer, func =>
                    {
                        if (!disableCaching && !FunctionAllowsCaching(func))
                        {
                            disableCaching = true;
                        }

                        return(true);
                    });
                }

                if (disableCaching)
                {
                    using (Profiler.Measure("PageRenderer: Disabling HTTP caching as at least one of the functions is not cacheable"))
                    {
                        HttpContext.Current?.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    }
                }

                using (Profiler.Measure("Resolving page fields"))
                {
                    ResolvePageFields(document, page);
                }

                using (Profiler.Measure("Normalizing ASP.NET forms"))
                {
                    NormalizeAspNetForms(document);
                }

                if (document.Root.Name != RenderingElementNames.Html)
                {
                    return(new LiteralControl(document.ToString()));
                }

                var xhtmlDocument = new XhtmlDocument(document);

                ProcessXhtmlDocument(xhtmlDocument, page);

                using (Profiler.Measure("Converting XHTML document into an ASP.NET control"))
                {
                    return(xhtmlDocument.AsAspNetControl(mapper));
                }
            }
        }