Beispiel #1
0
        public static IEnumerable <string> Query(this string data, string query, bool firstlevel = false)
        {
            var items = query.Split(' ');


            foreach (var item in HTMLParsing.QueryHtml(data, new CSSQuery(items[0]), firstlevel))
            {
                if (items.Length > 1)
                {
                    var c = ConcatString(items, 1);
                    if (string.IsNullOrWhiteSpace(c))
                    {
                        yield return(item);
                    }
                    else
                    {
                        foreach (var item2 in item.Query(c))
                        {
                            yield return(item2);
                        }
                    }
                }
                else
                {
                    yield return(item);
                }
            }
        }
Beispiel #2
0
        public static IEnumerable <string> Query(this IEnumerable <string> data, string query)
        {
            var qitems = query.Split(' ');

            foreach (var d in data)
            {
                foreach (var item in HTMLParsing.QueryHtml(d, new CSSQuery(qitems[0])))
                {
                    if (qitems.Length > 1)
                    {
                        var c = ConcatString(qitems, 1);
                        if (string.IsNullOrWhiteSpace(c))
                        {
                            yield return(item);
                        }
                        else
                        {
                            foreach (var item2 in item.Query(c))
                            {
                                yield return(item2);
                            }
                        }
                    }
                    else
                    {
                        yield return(item);
                    }
                }
            }
        }
Beispiel #3
0
 public static IEnumerable <string> GetAttribute(this IEnumerable <string> data, string name)
 {
     foreach (var item in data)
     {
         yield return(HTMLParsing.ParseAttributeValue(name, item, 0));
     }
 }
Beispiel #4
0
 public static string GetAttribute(this string data, string name)
 {
     if (string.IsNullOrWhiteSpace(data))
     {
         return("");
     }
     return(HTMLParsing.ParseAttributeValue(name, data, 0));
 }
Beispiel #5
0
        public static string RemoveTags(this string txt, string query)
        {
            var builder = new StringBuilder();
            var hq      = new CSSQuery(query);
            var index   = 0;

            while (txt != null && index < txt.Length)
            {
                var elementStart = 0;
                var e            = HTMLParsing.NextElement(builder, ref index, txt, out elementStart);

                if (index >= txt.Length)
                {
                    break;
                }
                if (e == hq.Element || string.IsNullOrEmpty(hq.Element))
                {
                    index = elementStart;
                    if (HTMLParsing.Validate(builder, ref index, txt, hq))
                    {
                        if (hq.Function != null)
                        {
                            var item = hq.Function.Post(builder, txt, ref index, e);

                            if (!string.IsNullOrEmpty(item))
                            {
                                txt   = txt.Remove(elementStart, index - elementStart);
                                index = elementStart;
                            }
                            index++;
                        }
                        else
                        {
                            var subIndex = HTMLParsing.FindEndofElement(builder, txt, elementStart, e);
                            if (subIndex >= 0)
                            {
                                txt   = txt.Remove(elementStart, subIndex);
                                index = elementStart;
                            }
                        }
                    }
                }
                index++;
            }
            return(txt);
        }
Beispiel #6
0
        public string Post(StringBuilder builder, string txt, ref int index, string element)
        {
            switch (Type)
            {
            case "eq":
            {
                int i = (int)Parameter;
                i--;
                Parameter = i;
                if (i <= 0)
                {
                    var html = HTMLParsing.ParseElement(builder, txt, ref index, element);
                    index = txt.Length;
                    return(html);
                }
                else
                {
                    return(null);
                }
            }
            break;

            case "first":
            {
                var html = HTMLParsing.ParseElement(builder, txt, ref index, element);
                index = txt.Length;
                return(html);
            }

            case "contains":
            {
                var end   = index + HTMLParsing.FindEndofElement(builder, txt, index, element);
                var start = HTMLParsing.FindIgnoreCase(txt, ">", index, end);
                if (HTMLParsing.FindIgnoreCase(txt, (string)Parameter, start, end) >= 0)
                {
                    var html = HTMLParsing.ParseElement(builder, txt, ref index, element);
                    index = end;
                    return(html);
                }
                else
                {
                    index = index + end;
                }
                return(null);
            }

            case "not":
            {
                var q1 = Parameter as HtmlQuery;

                var elementStart = index;
                if (!HTMLParsing.Validate(builder, ref index, txt, q1))
                {
                    index = elementStart;
                    return(HTMLParsing.ParseElement(builder, txt, ref index, element));
                }
            }
            break;
            }


            return(null);
        }