/// <summary> /// Parse an XML valid node to a HtmlElement /// </summary> /// <param name="element">Element to be parsed</param> /// <returns>The parsed HtmlElement</returns> /// <exception cref="System.NotSupportedException">Thrown if an unsupported HTML tag is encountered</exception> private static HtmlElement ParseElement(XElement element) { // check if we support the given parent tag if (!SupportedTags.Contains(element.Name.LocalName.ToLowerInvariant())) { throw new NotSupportedException( string.Format("{0} tag is not supported. Only the following HTML tags are supported: {1}.", element.Name.LocalName.ToUpperInvariant(), string.Join(", ", SupportedTags.Select(f => f.ToUpperInvariant())))); } EHtmlTag tag = (EHtmlTag)Enum.Parse(typeof(EHtmlTag), element.Name.LocalName.ToTitleCase()); // get the attributes of the element as HTML attributes dictionary IDictionary <string, string> htmlAttributes = element.Attributes() .ToDictionary(f => f.Name.LocalName.ToLowerInvariant(), v => v.Value); HtmlElement html = new HtmlElement(tag, htmlAttributes); if (!element.HasElements) { // this means that the current element does not have any child elements // and that it is only a text element or an empty element html.InnerHtml = element.Value; return(html); } if (!element.Nodes().Any()) { return(html); } foreach (XNode node in element.Nodes()) { string text = node.ToString(); // if the current node is a text node, enclose it in a SPAN object // in order to preserve the order of elements in the finally parsed // HtmlElement object, otherwise try to parse the node as a full // fledged element and start recursion // TODO: check whether there are any INFINITE LOOP conditions which // will make the function throw OutOfMemoryException HtmlElement parsed = node.NodeType == XmlNodeType.Text ? new Span(text) : Parse(text); html.Prepend(parsed); } return(html); }
/// <summary> /// Constructor to specify extra HTML attributes as an anonymous type /// </summary> /// <param name="tag">An HTML tag to initialise this element with</param> /// <param name="htmlAttributes">Extra HTML attributes</param> public HtmlComponent(EHtmlTag tag, object htmlAttributes = null) { Tag = tag; Attributes = new Dictionary <string, string>(); CssClasses = new CssClassList(); AppendTags = new HtmlComponentList(); PrependTags = new HtmlComponentList(); if (htmlAttributes == null) { return; } bool isDict = (htmlAttributes as IDictionary <string, string>) != null; if (isDict) { Attributes = (IDictionary <string, string>)htmlAttributes; if (!Attributes.ContainsKey("class")) { return; } CssClasses.Add(Attributes["class"]); Attributes.Remove("class"); return; } NameValueCollection attribs = WebExtrasUtil.AnonymousObjectToHtmlAttributes(htmlAttributes); foreach (string key in attribs.Keys) { if (key.Equals("class", StringComparison.OrdinalIgnoreCase)) { CssClasses.Clear(); CssClasses.Add(attribs[key]); continue; } Attributes[key] = attribs[key]; } }
/// <summary> /// Constructor to specify extra HTML attributes as an anonymous type /// </summary> /// <param name="tag">An HTML tag to initialise this element with</param> /// <param name="htmlAttributes">Extra HTML attributes</param> public HtmlElement(EHtmlTag tag, object htmlAttributes) { Component = new HtmlComponent(tag, htmlAttributes); }
/// <summary> /// Default constructor /// </summary> /// <param name="tag">An HTML tag to initialise this element with</param> public HtmlElement(EHtmlTag tag) { Component = new HtmlComponent(tag); }