Example #1
0
        /// <summary>
        ///   Add given text as a component
        /// </summary>
        /// <param name="text">Text to add</param>
        public void Add(string text)
        {
            HtmlComponent c = new HtmlComponent(WebExtrasSettings.DefaultTagForTextEncapsulation)
            {
                InnerHtml = text
            };

            Add(c);
        }
Example #2
0
        /// <summary>
        ///   Constructor
        /// </summary>
        /// <param name="options">List of options</param>
        /// <param name="htmlAttributes">[Optional] Extra HTML attributes</param>
        public HtmlSelect(string[] options, object htmlAttributes = null)
            : base(EHtmlTag.Select, htmlAttributes)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options", "Select list options cannot be null");
            }

            Array.ForEach(options, f =>
            {
                var option = new HtmlComponent(EHtmlTag.Option, new { value = f })
                {
                    InnerHtml = f
                };
                AppendTags.Add(option);
            });
        }
Example #3
0
        /// <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 HtmlComponent 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);

            HtmlComponent html = new HtmlComponent(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();

                // TODO: check whether there are any INFINITE LOOP conditions which
                // will make the function throw OutOfMemoryException
                HtmlComponent parsed = node.NodeType == XmlNodeType.Text ? new NullWrapperComponent(text) : Parse(text);

                html.PrependTags.Add(parsed);
            }

            return(html);
        }
Example #4
0
        /// <summary>
        ///   Constructor
        /// </summary>
        /// <param name="options">List of options</param>
        /// <param name="htmlAttributes">[Optional] Extra HTML attributes</param>
        public HtmlSelect(IEnumerable <HtmlSelectListOption> options, object htmlAttributes = null)
            : base(EHtmlTag.Select, htmlAttributes)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options", "Select list options cannot be null");
            }

            Array.ForEach(options.ToArray(), f =>
            {
                var option = new HtmlComponent(EHtmlTag.Option, new { value = f.Value })
                {
                    InnerHtml = f.Text
                };

                if (f.Selected)
                {
                    option.Attributes["selected"] = "true";
                }

                AppendTags.Add(option);
            });
        }