示例#1
0
        /// <summary>
        /// Associate an HTML tag with an HtmlElementCreator delegate used to
        /// construct a corresponding derived HtmlElement instance
        /// </summary>
        /// <param name="tag">HTML tag</param>
        /// <param name="creator">
        /// HtmlElementCreator used to create an HtmlElement for this type of tag
        /// </param>
        public static void Register(string tag, HtmlElementCreator creator)
        {
            // Ensure parameters
            if (tag == null)
            {
                throw new ArgumentNullException("tag", "tag cannot be null!");
            }
            if (string.IsNullOrEmpty(tag))
            {
                throw new ArgumentException("tag cannot be empty!", "tag");
            }
            if (creator == null)
            {
                throw new ArgumentNullException("creator", "creator cannot be null!");
            }

            // Add the tag to the element factory
            _elementFactory[tag] = creator;
        }
示例#2
0
        /// <summary>
        /// Create a new HtmlElement
        /// </summary>
        /// <param name="tag">HTML tag of the element</param>
        /// <param name="attributes">Attributes defined on the element</param>
        /// <param name="parent">Parent HtmlElement</param>
        /// <param name="page">Page containing the element</param>
        /// <returns>HtmlElement</returns>
        internal static HtmlElement Create(string tag, IDictionary <string, string> attributes, HtmlElement parent, HtmlPage page)
        {
            HtmlElement element = null;

            // Try to use an HtmlElementCreator
            HtmlElementCreator creator = null;

            if (_elementFactory.TryGetValue(tag, out creator))
            {
                // Ignore any exceptions raised during creation
                try { element = creator(tag, attributes, parent, page); }
                catch { }
            }

            // If we don't have a creator for the element or it failed to create,
            // create a default HtmlElement
            if (element == null)
            {
                element = new HtmlElement(tag, attributes, parent, page);
            }

            return(element);
        }