Esempio n. 1
0
        private void CssNthChild(bool backwards, bool ofType)
        {
            string argS = tq.ChompTo(")").Trim().ToLower();
            Match  mAB  = NTH_AB.Match(argS);
            Match  mB   = NTH_B.Match(argS);
            int    a;
            int    b;

            if ("odd".Equals(argS))
            {
                a = 2;
                b = 1;
            }
            else if ("even".Equals(argS))
            {
                a = 2;
                b = 0;
            }
            else if (mAB.Success && mAB.Length == argS.Length) /*matches*/
            {
                a = !string.IsNullOrEmpty(mAB.Groups[3].Value) ? Convert.ToInt32(Regex.Replace(mAB.Groups[1].Value, "^\\+", string.Empty)) : 1;
                b = !string.IsNullOrEmpty(mAB.Groups[4].Value) ? Convert.ToInt32(Regex.Replace(mAB.Groups[4].Value, "^\\+", string.Empty)) : 0;
            }
            else if (mB.Success && mB.Length == argS.Length) /*matches*/
            {
                a = 0;
                b = Convert.ToInt32(Regex.Replace(mB.Groups[0].Value, "^\\+", string.Empty));
            }
            else
            {
                throw new Selector.SelectorParseException("Could not parse nth-index '%s': unexpected format", argS);
            }
            if (ofType)
            {
                if (backwards)
                {
                    evals.Add(new Evaluator.IsNthLastOfType(a, b));
                }
                else
                {
                    evals.Add(new Evaluator.IsNthOfType(a, b));
                }
            }
            else
            {
                if (backwards)
                {
                    evals.Add(new Evaluator.IsNthLastChild(a, b));
                }
                else
                {
                    evals.Add(new Evaluator.IsNthChild(a, b));
                }
            }
        }
Esempio n. 2
0
        public void ProcessResponseHeaders(WebHeaderCollection resHeaders)
        {
            foreach (string name in resHeaders.AllKeys)
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    continue;                     // http/1.1 line
                }

                var value = resHeaders[name];                 //.Split(';');

                if (name.Equals("Set-Cookie", StringComparison.OrdinalIgnoreCase))
                {
                    var values = resHeaders["Set-Cookie"].Split(';', ',');
                    foreach (string v in values)
                    {
                        if (string.IsNullOrWhiteSpace(v))
                        {
                            continue;
                        }

                        var cd         = new TokenQueue(v);
                        var cookieName = cd.ChompTo("=").Trim();
                        var cookieVal  = cd.ConsumeTo(";").Trim();

                        if (cookieVal == null)
                        {
                            cookieVal = string.Empty;
                        }

                        // ignores path, date, domain, secure et al. req'd?
                        if (StringUtil.In(cookieName.ToLowerInvariant(), "domain", "path", "expires", "max-age", "secure", "httponly"))
                        {
                            // This is added for NSoup, since we do headers a bit differently around here.
                            continue;
                        }

                        // name not blank, value not null
                        if (!string.IsNullOrEmpty(cookieName))
                        {
                            Cookie(cookieName, cookieVal);
                        }
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        Header(name, /*values[0]*/ value);
                    }
                }
            }
        }
Esempio n. 3
0
        public void ProcessResponseHeaders(WebHeaderCollection resHeaders)
        {
            foreach (string name in resHeaders.Keys)
            {
                if (name == null)
                {
                    continue; // http/1.1 line
                }

                string value = resHeaders[name]; //.Split(';');

                if (name.Equals("Set-Cookie", StringComparison.InvariantCultureIgnoreCase))
                {
                    string[] values = resHeaders["Set-Cookie"].Split(';', ',');
                    foreach (string v in values)
                    {
                        if (v == null)
                        {
                            continue;
                        }

                        TokenQueue cd         = new TokenQueue(v);
                        string     cookieName = cd.ChompTo("=").Trim();
                        string     cookieVal  = cd.ConsumeTo(";").Trim();

                        if (cookieVal == null)
                        {
                            cookieVal = string.Empty;
                        }

                        // ignores path, date, domain, secure et al. req'd?
                        if (StringUtil.In(cookieName.ToLowerInvariant(), "domain", "path", "expires", "max-age", "secure", "httponly"))
                        {
                            // This is added for Texxtoor.BaseLibrary.Core.HtmlAgility.ToXml, since we do headers a bit differently around here.
                            continue;
                        }

                        // name not blank, value not null
                        if (!string.IsNullOrEmpty(cookieName))
                        {
                            Cookie(cookieName, cookieVal);
                        }
                    }
                }
                else
                { // only take the first instance of each header
                    if (/*values.Length > 0*/ !string.IsNullOrEmpty(value))
                    {
                        Header(name, /*values[0]*/ value);
                    }
                }
            }
        }
Esempio n. 4
0
        private int ConsumeIndex()
        {
            string indexS = _tq.ChompTo(")").Trim();

            int index;
            if (!int.TryParse(indexS, out index))
            {
                throw new Exception("Index must be numeric");
            }

            return index;
        }
Esempio n. 5
0
        private void CssNthChild(bool backwards, bool ofType)
        {
            String argS = tq.ChompTo(")").Trim().ToLowerInvariant();
            Match  mAB  = iText.IO.Util.StringUtil.Match(NTH_AB, argS);
            Match  mB   = iText.IO.Util.StringUtil.Match(NTH_B, argS);
            int    a;
            int    b;

            if ("odd".Equals(argS))
            {
                a = 2;
                b = 1;
            }
            else
            {
                if ("even".Equals(argS))
                {
                    a = 2;
                    b = 0;
                }
                else
                {
                    if (mAB.Success)
                    {
                        a = iText.IO.Util.StringUtil.Group(mAB, 3) != null?Convert.ToInt32(iText.IO.Util.StringUtil.Group(mAB, 1
                                                                                                                          ).ReplaceFirst("^\\+", ""), System.Globalization.CultureInfo.InvariantCulture) : 1;

                        b = iText.IO.Util.StringUtil.Group(mAB, 4) != null?Convert.ToInt32(iText.IO.Util.StringUtil.Group(mAB, 4
                                                                                                                          ).ReplaceFirst("^\\+", ""), System.Globalization.CultureInfo.InvariantCulture) : 0;
                    }
                    else
                    {
                        if (mB.Success)
                        {
                            a = 0;
                            b = Convert.ToInt32(iText.IO.Util.StringUtil.Group(mB).ReplaceFirst("^\\+", ""), System.Globalization.CultureInfo.InvariantCulture
                                                );
                        }
                        else
                        {
                            throw new Selector.SelectorParseException("Could not parse nth-index " + PortUtil.EscapedSingleBracket + "{0}"
                                                                      + PortUtil.EscapedSingleBracket + ": unexpected format", argS);
                        }
                    }
                }
            }
            if (ofType)
            {
                if (backwards)
                {
                    evals.Add(new Evaluator.IsNthLastOfType(a, b));
                }
                else
                {
                    evals.Add(new Evaluator.IsNthOfType(a, b));
                }
            }
            else
            {
                if (backwards)
                {
                    evals.Add(new Evaluator.IsNthLastChild(a, b));
                }
                else
                {
                    evals.Add(new Evaluator.IsNthChild(a, b));
                }
            }
        }