Example #1
0
        public static QueryFunction GetFunction(string s, string v)
        {
            switch (s)
            {
            case "first":
                return(new QueryFunction()
                {
                    Type = s
                });

            case "contains":
            {
                if (v != null)
                {
                    return new QueryFunction()
                           {
                               Type = s, Parameter = v
                           }
                }
                ;
            }
            break;

            case "eq":
            {
                int x = 0;
                if (int.TryParse(v, out x))
                {
                    return new QueryFunction()
                           {
                               Type = s, Parameter = x
                           }
                }
                ;
                ;
            }
            break;

            case "not":
            {
                if (v != null)
                {
                    var q1 = new CSSQuery(v);

                    return(new QueryFunction()
                        {
                            Type = s, Parameter = q1
                        });
                }
            }
            break;
            }
            return(null);
        }
Example #2
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);
        }
Example #3
0
        //todo - more selector support
        //todo - fix attribute parsing
        public static IEnumerable <string> QueryHtml(string txt, CSSQuery query, bool firstlevel = false)
        {
            var builder = new StringBuilder();

            var index = 0;

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

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

                            if (!string.IsNullOrEmpty(item))
                            {
                                yield return(item);
                            }
                        }
                        else
                        {
                            yield return(ParseElement(builder, txt, ref index, e, firstlevel));
                        }

                        //if (firstlevel)
                        //{
                        //  index =  FindEndofElement(builder, txt, index, query.Element);
                        //}
                        index++;
                    }
                }
            }
        }
Example #4
0
        public static bool Validate(StringBuilder builder, ref int index, string data, CSSQuery query)
        {
            var attributes = GetAttributes(builder, ref index, data);

            var allFound = true;

            foreach (var qv in query.Values)
            {
                allFound = false;
                foreach (var ea in attributes)
                {
                    if (qv.Name.Equals(ea.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        allFound = true;
                        if (!string.IsNullOrWhiteSpace(qv.Value))
                        {
                            allFound = false;

                            if (!string.IsNullOrWhiteSpace(ea.Value))
                            {
                                var id1 = ea.Value.IndexOf(qv.Value, StringComparison.InvariantCulture);
                                if (id1 >= 0)
                                {
                                    var ch3 = ea.Value[id1 - 1];
                                    if (ch3 == ' ' || ch3 == '\'' || ch3 == '"')
                                    {
                                        var lastPlace = id1 + qv.Value.Length;
                                        if (lastPlace >= ea.Value.Length)
                                        {
                                            allFound = true;
                                        }
                                        else
                                        {
                                            var c2 = ea.Value[lastPlace];
                                            if (c2 == ' ' || c2 == '\'' || c2 == '"')
                                            {
                                                allFound = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (!allFound)
                {
                    break;
                }
            }
            return(allFound);
        }