Esempio n. 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));
            }
        }
Esempio n. 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));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 在容器指定位置添加一个注释节点
        /// </summary>
        /// <param name="container">要添加注释的容器</param>
        /// <param name="index">要添加注释的位置</param>
        /// <param name="name">添加注释的内容</param>
        /// <returns>添加好的注释节点</returns>
        public IHtmlComment AddComment(IHtmlContainer container, int index, string comment)
        {
            lock ( _sync )
            {
                unchecked { _version++; }

                var commentNode = DomProvider.EnsureDomContainer(container).InsertNode(index, new DomComment(comment));

                OnDomChanged(this, new HtmlDomChangedEventArgs(commentNode, container, HtmlDomChangedAction.Add));

                return(commentNode);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 在容器指定位置添加一个元素
        /// </summary>
        /// <param name="container">要添加元素的容器</param>
        /// <param name="index">要添加元素的位置</param>
        /// <param name="name">添加元素的名称</param>
        /// <returns>添加好的元素</returns>
        public IHtmlElement AddElement(IHtmlContainer container, int index, string name)
        {
            lock ( _sync )
            {
                unchecked { _version++; }

                var element = DomProvider.EnsureDomContainer(container).InsertNode(index, new DomElement(name, null));

                OnDomChanged(this, new HtmlDomChangedEventArgs(element, container, HtmlDomChangedAction.Add));

                return(element);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 在容器指定位置添加一个特殊节点
        /// </summary>
        /// <param name="container">要添加特殊节点的容器</param>
        /// <param name="index">要添加特殊节点的位置</param>
        /// <param name="name">添加特殊节点的 HTML</param>
        /// <returns>添加好的特殊节点</returns>
        public IHtmlSpecial AddSpecial(IHtmlContainer container, int index, string html)
        {
            lock ( _sync )
            {
                unchecked { _version++; }

                var specialNode = DomProvider.EnsureDomContainer(container).InsertNode(index, new DomSpecial(html));

                //UNDONE 未确定special node具体是什么

                OnDomChanged(this, new HtmlDomChangedEventArgs(specialNode, container, HtmlDomChangedAction.Add));

                return(specialNode);
            }
        }
Esempio n. 6
0
 /// <summary>
 /// 完成最终文档解析构建
 /// </summary>
 /// <param name="document">已经完成的文档结构</param>
 /// <returns>最终完成的文档</returns>
 protected virtual IHtmlDocument CompleteDocument(IHtmlDocument document)
 {
     return(DomProvider.CompleteDocument(document));
 }
Esempio n. 7
0
 /// <summary>
 /// 创建特殊标签节点并加入当前容器
 /// </summary>
 /// <param name="html">特殊标签的 HTML 内容</param>
 /// <returns>创建的特殊标签节点</returns>
 protected virtual IHtmlSpecial CreateSpecial(string html)
 {
     return(DomProvider.AddSpecial(CurrentContainer, html));
 }
Esempio n. 8
0
 /// <summary>
 /// 创建注释节点并加入当前容器
 /// </summary>
 /// <param name="comment">注释内容</param>
 /// <returns>创建的注释节点</returns>
 protected virtual IHtmlComment CreateCommet(string comment)
 {
     return(DomProvider.AddComment(CurrentContainer, comment));
 }
Esempio n. 9
0
 /// <summary>
 /// 创建元素并加入当前容器
 /// </summary>
 /// <param name="tagName">元素名</param>
 /// <param name="attributes">元素属性集合</param>
 /// <returns>创建好的元素</returns>
 protected virtual IHtmlElement CreateElement(string tagName, Dictionary <string, string> attributes)
 {
     return(DomProvider.AddElement(CurrentContainer, tagName, attributes));
 }
Esempio n. 10
0
 /// <summary>
 /// 创建文本节点添加到当前容器
 /// </summary>
 /// <param name="text">HTML 文本</param>
 /// <returns></returns>
 protected virtual IHtmlTextNode CreateTextNode(string text)
 {
     return(DomProvider.AddTextNode(CurrentContainer, text));
 }