/////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        /// <param name="searchChildren"></param>
        public void FindByNameAttributeValue(CHtmlNodeCollection result, string name, string attributeName, string attributeValue, bool searchChildren)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(attributeName != null);

            attributeName = attributeName.Trim().ToLower();

            for (int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];

                if (node.NodeName.Equals(name) && node is IHtmlNodeHasAttribute)
                {
                    CHtmlAttribute attribute = ((IHtmlNodeHasAttribute)node).Attributes[attributeName];
                    if (attribute != null && attribute.Value == attributeValue)
                    {
                        result.Add(node);
                    }
                }

                if (searchChildren && node is CHtmlElement)
                {
                    ((CHtmlElement)node).Nodes.FindByNameAttributeValue(result, name, attributeName, attributeValue, searchChildren);
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="searchChildren"></param>
        public void FindByName(CHtmlNodeCollection result, string name, int depth)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(name != null);
            System.Diagnostics.Debug.Assert(depth >= 0);

            if (depth > 0)
            {
                name = name.Trim().ToLower();

                for (int index = 0, count = m_nodeList.Count; index < count; ++index)
                {
                    CHtmlNode node = m_nodeList[index];
                    if (node.NodeName.Equals(name))
                    {
                        result.Add(node);
                    }

                    if (depth > 1 && node is CHtmlElement)
                    {
                        ((CHtmlElement)node).Nodes.FindByName(result, name, depth - 1);
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        public void GetDescendent(CHtmlNodeCollection result)
        {
            System.Diagnostics.Debug.Assert(result != null);

            for (int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];
                result.Add(node);
                if (node is CHtmlElement)
                {
                    ((CHtmlElement)node).Nodes.GetDescendent(result);
                }
            }
        }
        private void FindLink(CHtmlNodeCollection parentNodes, ref CHtmlNodeCollection links)
        {
            if (parentNodes == null || parentNodes.Count == 0)
            {
                return;
            }

            CHtmlAttribute attr;

            foreach (CHtmlNode node in parentNodes)
            {
                if (node is CHtmlElement)
                {
                    if (node == null)
                    {
                        continue;
                    }

                    attr = null;
                    CHtmlElement element = node as CHtmlElement;
                    switch (element.Name.Trim().ToLower())
                    {
                    case "a":
                    case "link":
                    case "frame":
                        attr = element.Attributes["href"];
                        break;

                    case "script":
                        attr = element.Attributes["src"];
                        break;
                    }

                    if (attr != null)
                    {
                        links.Add(node);
                    }

                    if (element.Nodes.Count > 0)
                    {
                        FindLink(element.Nodes, ref links);
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="searchChildren"></param>
        public void FindByName(CHtmlNodeCollection result, string name, bool searchChildren)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(name != null);

            name = name.Trim().ToLower();

            for (int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];
                if (node.NodeName.Equals(name))
                {
                    result.Add(node);
                }

                if (searchChildren == true && node is CHtmlElement)
                {
                    ((CHtmlElement)node).Nodes.FindByName(result, name, searchChildren);
                }
            }
        }
Exemple #6
0
        ///////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// »ý¼ºÀÚ
        /// </summary>
        public CHtmlElement(CHtmlElement obj) : base(obj)
        {
            System.Diagnostics.Debug.Assert(obj != null);

            obj.AssertValid();

            m_name  = obj.m_name;
            m_nodes = new CHtmlNodeCollection(this);

            int count = obj.m_nodes.Count;

            m_nodes.Capacity = count;
            for (int index = 0; index < count; ++index)
            {
                m_nodes.Add((CHtmlNode)obj.m_nodes[index].Clone());
            }

            count = obj.m_attributes.Count;
            m_attributes.Capacity = count;
            for (int index = 0; index < count; ++index)
            {
                m_attributes.Add((CHtmlAttribute)obj.m_attributes[index].Clone());
            }
        }
        ///////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// ������
        /// </summary>
        public CHtmlElement(CHtmlElement obj)
            : base(obj)
        {
            System.Diagnostics.Debug.Assert(obj != null);

            obj.AssertValid();

            m_name = obj.m_name;
            m_nodes = new CHtmlNodeCollection(this);

            int count = obj.m_nodes.Count;
            m_nodes.Capacity = count;
            for(int index = 0; index < count; ++index)
                m_nodes.Add((CHtmlNode)obj.m_nodes[index].Clone());

            count = obj.m_attributes.Count;
            m_attributes.Capacity = count;
            for(int index = 0; index < count; ++index)
                m_attributes.Add((CHtmlAttribute)obj.m_attributes[index].Clone());
        }
        private void FindLink(CHtmlNodeCollection parentNodes, ref CHtmlNodeCollection links)
        {
            if (parentNodes == null || parentNodes.Count == 0) return;

            CHtmlAttribute attr;
            foreach(CHtmlNode node in parentNodes)
            {
                if(node is CHtmlElement)
                {
                    if(node == null) continue;

                    attr = null;
                    CHtmlElement element = node as CHtmlElement;
                    switch(element.Name.Trim().ToLower())
                    {
                        case "a":
                        case "link":
                        case "frame":
                            attr = element.Attributes["href"];
                            break;
                        case "script":
                            attr = element.Attributes["src"];
                            break;
                    }

                    if(attr != null)
                    {
                        links.Add(node);
                    }

                    if(element.Nodes.Count > 0)
                    {
                        FindLink(element.Nodes, ref links);
                    }
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        public void GetDescendent(CHtmlNodeCollection result)
        {
            System.Diagnostics.Debug.Assert(result != null);

            for(int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];
                result.Add(node);
                if(node is CHtmlElement)
                    ((CHtmlElement)node).Nodes.GetDescendent(result);
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="attributeName"></param>
        /// <param name="attributeValue"></param>
        /// <param name="searchChildren"></param>
        public void FindByNameAttributeValue(CHtmlNodeCollection result, string name, string attributeName, string attributeValue, bool searchChildren)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(attributeName != null);

            attributeName = attributeName.Trim().ToLower();

            for(int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];

                if(node.NodeName.Equals(name) && node is IHtmlNodeHasAttribute)
                {
                    CHtmlAttribute attribute = ((IHtmlNodeHasAttribute)node).Attributes[attributeName];
                    if(attribute != null && attribute.Value == attributeValue)
                        result.Add(node);
                }

                if(searchChildren && node is CHtmlElement)
                    ((CHtmlElement)node).Nodes.FindByNameAttributeValue(result, name, attributeName, attributeValue, searchChildren);
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="searchChildren"></param>
        public void FindByName(CHtmlNodeCollection result, string name, int depth)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(name != null);
            System.Diagnostics.Debug.Assert(depth >= 0);

            if(depth > 0)
            {
                name = name.Trim().ToLower();

                for(int index = 0, count = m_nodeList.Count; index < count; ++index)
                {
                    CHtmlNode node = m_nodeList[index];
                    if(node.NodeName.Equals(name))
                        result.Add(node);

                    if(depth > 1 && node is CHtmlElement)
                        ((CHtmlElement)node).Nodes.FindByName(result, name, depth - 1);
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 
        /// </summary>
        /// <param name="result"></param>
        /// <param name="name"></param>
        /// <param name="searchChildren"></param>
        public void FindByName(CHtmlNodeCollection result, string name, bool searchChildren)
        {
            System.Diagnostics.Debug.Assert(result != null);
            System.Diagnostics.Debug.Assert(name != null);

            name = name.Trim().ToLower();

            for(int index = 0, count = m_nodeList.Count; index < count; ++index)
            {
                CHtmlNode node = m_nodeList[index];
                if(node.NodeName.Equals(name))
                    result.Add(node);

                if(searchChildren == true && node is CHtmlElement)
                    ((CHtmlElement)node).Nodes.FindByName(result, name, searchChildren);
            }
        }