private void ByAttribute() { TokenQueue cq = new TokenQueue(_tq.ChompBalanced('[', ']')); // content queue string key = cq.ConsumeToAny("=", "!=", "^=", "$=", "*=", "~="); // eq, not, start, end, contain, match, (no val) if (string.IsNullOrEmpty(key)) { throw new Exception("key is empty."); } cq.ConsumeWhitespace(); if (cq.IsEmpty) { if (key.StartsWith("^")) { _evals.Add(new Evaluator.AttributeStarting(key.Substring(1))); } else { _evals.Add(new Evaluator.Attribute(key)); } } else { if (cq.MatchChomp("=")) { _evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder())); } else if (cq.MatchChomp("!=")) { _evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder())); } else if (cq.MatchChomp("^=")) { _evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder())); } else if (cq.MatchChomp("$=")) { _evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder())); } else if (cq.MatchChomp("*=")) { _evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder())); } else if (cq.MatchChomp("~=")) { _evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder()))); } else { throw new Selector.SelectorParseException("Could not parse attribute query '{0}': unexpected token at '{1}'", _query, cq.Remainder()); } } }
private void ByAttribute() { TokenQueue cq = new TokenQueue(tq.ChompBalanced('[', ']')); // content queue string key = cq.ConsumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val) Validate.NotEmpty(key); cq.ConsumeWhitespace(); if (cq.IsEmpty()) { if (key.StartsWith("^", StringComparison.Ordinal)) { evals.Add(new Evaluator.AttributeStarting(key.Substring(1))); /*substring*/ } else { evals.Add(new Evaluator.Attribute(key)); } } else { if (cq.MatchChomp("=")) { evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder())); } else if (cq.MatchChomp("!=")) { evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder())); } else if (cq.MatchChomp("^=")) { evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder())); } else if (cq.MatchChomp("$=")) { evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder())); } else if (cq.MatchChomp("*=")) { evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder())); } else if (cq.MatchChomp("~=")) { evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder(), RegexOptions.Compiled))); } else { throw new Selector.SelectorParseException("Could not parse attribute query '{0}': unexpected token at '{1}'", query, cq.Remainder()); } } }
public void addFirst() { TokenQueue tq = new TokenQueue("One Two"); tq.ConsumeWord(); tq.AddFirst("Three"); Assert.AreEqual("Three Two", tq.Remainder()); }
public void chompBalanced() { TokenQueue tq = new TokenQueue(":contains(one (two) three) four"); string pre = tq.ConsumeTo("("); string guts = tq.ChompBalanced('(', ')'); string remainder = tq.Remainder(); Assert.AreEqual(":contains", pre); Assert.AreEqual("one (two) three", guts); Assert.AreEqual(" four", remainder); }
private void ByAttribute() { TokenQueue cq = new TokenQueue(tq.ChompBalanced('[', ']')); // content queue string key = cq.ConsumeToAny("=", "!=", "^=", "$=", "*=", "~="); // eq, not, start, end, contain, match, (no val) if (key.Length == 0) { throw Failure.EmptyString("key"); } cq.ConsumeWhitespace(); if (cq.IsEmpty) { if (key.StartsWith("^", StringComparison.Ordinal)) { evals.Add(new Evaluator.AttributeStarting(key.Substring(1))); } else { evals.Add(new Evaluator.Attribute(key)); } } else { if (cq.MatchChomp("=")) { evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder())); } else if (cq.MatchChomp("!=")) { evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder())); } else if (cq.MatchChomp("^=")) { evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder())); } else if (cq.MatchChomp("$=")) { evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder())); } else if (cq.MatchChomp("*=")) { evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder())); } else if (cq.MatchChomp("~=")) { evals.Add(new Evaluator.AttributeWithValueMatching(key, new Regex(cq.Remainder()))); } else { throw DomFailure.CannotParseAttributeQuery(query, cq.Remainder()); } } }
private void FindElements() { if (_tq.MatchChomp("#")) { ById(); } else if (_tq.MatchChomp(".")) { ByClass(); } else if (_tq.MatchesWord()) { ByTag(); } else if (_tq.Matches("[")) { ByAttribute(); } else if (_tq.MatchChomp("*")) { AllElements(); } else if (_tq.MatchChomp(":lt(")) { IndexLessThan(); } else if (_tq.MatchChomp(":gt(")) { IndexGreaterThan(); } else if (_tq.MatchChomp(":eq(")) { IndexEquals(); } else if (_tq.Matches(":has(")) { Has(); } else if (_tq.Matches(":contains(")) { Contains(false); } else if (_tq.Matches(":containsOwn(")) { Contains(true); } else if (_tq.Matches(":matches(")) { Matches(false); } else if (_tq.Matches(":matchesOwn(")) { Matches(true); } else if (_tq.Matches(":not(")) { Not(); } else // unhandled { throw new Selector.SelectorParseException("Could not parse query '{0}': unexpected token at '{1}'", _query, _tq.Remainder()); } }
private void ByAttribute() { TokenQueue cq = new TokenQueue(tq.ChompBalanced('[', ']')); // content queue String key = cq.ConsumeToAny(AttributeEvals); // eq, not, start, end, contain, match, (no val) Validate.NotEmpty(key); cq.ConsumeWhitespace(); if (cq.IsEmpty()) { if (key.StartsWith("^")) { evals.Add(new Evaluator.AttributeStarting(key.Substring(1))); } else { evals.Add(new Evaluator.Attribute(key)); } } else { if (cq.MatchChomp("=")) { evals.Add(new Evaluator.AttributeWithValue(key, cq.Remainder())); } else { if (cq.MatchChomp("!=")) { evals.Add(new Evaluator.AttributeWithValueNot(key, cq.Remainder())); } else { if (cq.MatchChomp("^=")) { evals.Add(new Evaluator.AttributeWithValueStarting(key, cq.Remainder())); } else { if (cq.MatchChomp("$=")) { evals.Add(new Evaluator.AttributeWithValueEnding(key, cq.Remainder())); } else { if (cq.MatchChomp("*=")) { evals.Add(new Evaluator.AttributeWithValueContaining(key, cq.Remainder())); } else { if (cq.MatchChomp("~=")) { evals.Add(new Evaluator.AttributeWithValueMatching(key, iText.IO.Util.StringUtil.RegexCompile(cq.Remainder ()))); } else { throw new Selector.SelectorParseException("Could not parse attribute query " + PortUtil.EscapedSingleBracket + "{0}" + PortUtil.EscapedSingleBracket + ": unexpected token at " + PortUtil.EscapedSingleBracket + "{1}" + PortUtil.EscapedSingleBracket, query, cq.Remainder()); } } } } } } } }
private void FindElements() { if (tq.MatchChomp("#")) { ById(); } else { if (tq.MatchChomp(".")) { ByClass(); } else { if (tq.MatchesWord()) { ByTag(); } else { if (tq.Matches("[")) { ByAttribute(); } else { if (tq.MatchChomp("*")) { AllElements(); } else { if (tq.MatchChomp(":lt(")) { IndexLessThan(); } else { if (tq.MatchChomp(":gt(")) { IndexGreaterThan(); } else { if (tq.MatchChomp(":eq(")) { IndexEquals(); } else { if (tq.Matches(":has(")) { Has(); } else { if (tq.Matches(":contains(")) { Contains(false); } else { if (tq.Matches(":containsOwn(")) { Contains(true); } else { if (tq.Matches(":matches(")) { Matches(false); } else { if (tq.Matches(":matchesOwn(")) { Matches(true); } else { if (tq.Matches(":not(")) { Not(); } else { if (tq.MatchChomp(":nth-child(")) { CssNthChild(false, false); } else { if (tq.MatchChomp(":nth-last-child(")) { CssNthChild(true, false); } else { if (tq.MatchChomp(":nth-of-type(")) { CssNthChild(false, true); } else { if (tq.MatchChomp(":nth-last-of-type(")) { CssNthChild(true, true); } else { if (tq.MatchChomp(":first-child")) { evals.Add(new Evaluator.IsFirstChild()); } else { if (tq.MatchChomp(":last-child")) { evals.Add(new Evaluator.IsLastChild()); } else { if (tq.MatchChomp(":first-of-type")) { evals.Add(new Evaluator.IsFirstOfType()); } else { if (tq.MatchChomp(":last-of-type")) { evals.Add(new Evaluator.IsLastOfType()); } else { if (tq.MatchChomp(":only-child")) { evals.Add(new Evaluator.IsOnlyChild()); } else { if (tq.MatchChomp(":only-of-type")) { evals.Add(new Evaluator.IsOnlyOfType()); } else { if (tq.MatchChomp(":empty")) { evals.Add(new Evaluator.IsEmpty()); } else { if (tq.MatchChomp(":root")) { evals.Add(new Evaluator.IsRoot()); } else { // unhandled throw new Selector.SelectorParseException("Could not parse query " + PortUtil.EscapedSingleBracket + "{0}" + PortUtil.EscapedSingleBracket + ": unexpected token at " + PortUtil.EscapedSingleBracket + "{1}" + PortUtil.EscapedSingleBracket, query, tq.Remainder()); } } } } } } } } } } } } } } } } } } } } } } } } } } }
private void FindElements() { if (tq.MatchChomp("#")) { ById(); return; } if (tq.MatchChomp(".")) { ByClass(); return; } if (tq.MatchesWord()) { ByTag(); return; } if (tq.Matches("[")) { ByAttribute(); return; } if (tq.MatchChomp("*")) { AllElements(); return; } if (tq.Matches(":not(")) { Not(); return; } if (tq.Matches(":has(")) { Has(); return; } foreach (var exp in _options.Expressions) { if (!tq.Matches(exp.MatchToken)) { continue; } if (exp.HasArguments) { tq.Consume(exp.Token); string cq = tq.ChompBalanced('(', ')'); var eval = exp.CreateEvaluator(cq); evals.Add(eval); return; } else { throw new NotImplementedException(); } } // unhandled throw DomFailure.CouldNotParseQuery(query, tq.Remainder()); }