Exemplo n.º 1
0
        public List <HtmlElement> querySelectorAll(string selector, bool first = false)
        {
            var hp = new HtmlParse(html);
            var ls = new List <HtmlElement>();

            // #cc .active a[href],body
            foreach (var tag in selector.Split(','))
            {
                qq(hp, " " + selector, hp.Length, first, ls);
            }
            return(ls);
        }
Exemplo n.º 2
0
        void qq(HtmlParse hp, string selector, int eof, bool first, List <HtmlElement> ls)
        {
            char[] flag = new char[] { ' ', '+', '>' };
            var    r    = selector.IndexOfAny(flag, 1);
            var    sel  = r == -1 ? selector.Substring(1) : selector.Substring(1, r - 1);

            if (selector[0] == '>')
            {
                //>aa dd+ee;
                var t = -1;
                while (true)
                {
                    var str = hp.ReadNode(sel, ref t);
                    hp.Position++;
                    if (hp.Position > eof)
                    {
                        break;
                    }
                    ;
                    t++;
                    if (t != 0)
                    {
                        continue;
                    }
                    if (r != -1)
                    {
                        qq(hp, selector.Substring(r), hp.Position + str.Length, first, ls);
                    }
                    else if (str != "")
                    {
                        ls.Add(new HtmlElement(str));
                    }
                    if (first && ls.Count != 0)
                    {
                        break;
                    }
                }
            }
            else if (selector[0] == ' ')
            {
                // li+ee;
                while (true)
                {
                    var str = hp.ReadNode(sel);
                    hp.Position++;
                    if (hp.Position > eof)
                    {
                        break;
                    }
                    ;
                    if (r != -1)
                    {
                        qq(hp, selector.Substring(r), hp.Position + str.Length, first, ls);
                    }
                    else if (str != "")
                    {
                        ls.Add(new HtmlElement(str));
                    }
                    if (first && ls.Count != 0)
                    {
                        break;
                    }
                }
            }
            else if (selector[0] == '+')
            {
                //
            }
            hp.Position = eof;
        }