Beispiel #1
0
        /// <summary>
        /// 分析 HTML 文本并创建文档
        /// </summary>
        /// <param name="html">HTML 文本</param>
        /// <param name="url">文档的 URL</param>
        /// <returns>分析好的 HTML 文档</returns>
        public virtual IHtmlDocument Parse(string html, Uri url)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            if (url != null && !url.IsAbsoluteUri)
            {
                throw new ArgumentException("必须是绝对URI", "url");
            }

            lock ( SyncRoot )
            {
                Initialize();

                Document = DomProvider.CreateDocument(url);

                if (!string.IsNullOrEmpty(html))
                {
                    ContainerStack.Push(Document);

                    ParseInternal(html);
                }

                if (Document.HtmlSpecification == null)//若始终没能找到 DTD 声明,则设置默认的规范
                {
                    DomProvider.SetHtmlSpecification(Document, null);
                }

                return(CompleteDocument(Document));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 根据 DTD 声明设置相应的 HTML 规范
        /// </summary>
        /// <param name="doctype">DTD 声明</param>
        /// <returns>所适用的 HTML 规范</returns>
        protected virtual void SetHtmlSpecification(HtmlDoctypeDeclaration doctype)
        {
            if (HtmlSpecification == null)
            {
                var declaration = doctype.IfNull(null, d => d.Declaration);

                SetHtmlSpecification(DomProvider.SetHtmlSpecification(Document, declaration));
            }
        }