private Parser(Reader <Token> reader, ISelectorGenerator generator) { Debug.Assert(reader != null); Debug.Assert(generator != null); _reader = reader; _generator = generator; }
public Selector <HtmlNode> After(ISelectorGenerator subgenerator) { return(nodes => nodes.SelectNonNull(parent => { var start = IndexOfChild(subgenerator, parent, 0); return start != null ? CreateNodesGroup(parent.OwnerDocument, parent.ChildNodes, start.Value + 1, parent.ChildNodes.Count - 1, true) : null; })); }
public Selector <HtmlNode> Has(ISelectorGenerator subgenerator) { var castedGenerator = (SelectorGenerator <HtmlNode>)subgenerator; var compiled = castedGenerator.Selector; return(nodes => nodes.Where(n => compiled(new[] { n }).Any())); }
private Parser(Reader <Token> reader, ISelectorGenerator generator, bool expectEoi) { Debug.Assert(reader != null); Debug.Assert(generator != null); _reader = reader; _generator = generator; _expectEoi = expectEoi; }
/// <summary> /// Initializes a new instance of <see cref="SelectorGeneratorTee"/> /// with the two other <see cref="ISelectorGenerator"/> objects /// it delegates to. /// </summary> public SelectorGeneratorTee(ISelectorGenerator primary, ISelectorGenerator secondary) { if (primary == null) throw new ArgumentNullException("primary"); if (secondary == null) throw new ArgumentNullException("secondary"); Primary = primary; Secondary = secondary; }
public Selector <HtmlNode> Before(ISelectorGenerator subgenerator) { return(nodes => nodes.SelectNonNull(parent => { var end = IndexOfChild(subgenerator, parent, 0); return end != null ? CreateNodesGroup(parent.OwnerDocument, parent.ChildNodes, 0, end.Value - 1, false) : null; })); }
public Selector <HtmlNode> Not(ISelectorGenerator subgenerator) { var castedGenerator = (SelectorGenerator <HtmlNode>)subgenerator; var compiled = castedGenerator.Selector; return(nodes => { var matches = compiled(nodes.Select(x => x.ParentNode)).ToList(); return nodes.Except(matches); }); }
/// <summary> /// Initializes a new instance of <see cref="SelectorGeneratorTee"/> /// with the two other <see cref="ISelectorGenerator"/> objects /// it delegates to. /// </summary> public SelectorGeneratorTee(ISelectorGenerator primary, ISelectorGenerator secondary) { if (primary == null) { throw new ArgumentNullException("primary"); } if (secondary == null) { throw new ArgumentNullException("secondary"); } Primary = primary; Secondary = secondary; }
public Selector <HtmlNode> Between(ISelectorGenerator startGenerator, ISelectorGenerator endGenerator) { return(nodes => nodes.SelectNonNull(parent => { var start = IndexOfChild(startGenerator, parent, 0); if (start == null) { return null; } var end = IndexOfChild(endGenerator, parent, start.Value); if (end == null) { return null; } return CreateNodesGroup(parent.OwnerDocument, parent.ChildNodes, start.Value + 1, end.Value - 1, null); })); }
private int?IndexOfChild(ISelectorGenerator subgenerator, HtmlNode parent, int startIndex) { var selector = GetSelector(subgenerator); var children = parent.ChildNodes; var limit = selector(new[] { parent }) .Select(x => new { Node = x, Position = children.IndexOf(x) }) .FirstOrDefault(x => { if (x.Position == -1) { throw new FormatException("The limit node must be a direct child of the context node."); } return(x.Position >= startIndex); }); return(limit != null ? limit.Position : (int?)null); }
public void SplitBefore(ISelectorGenerator subgenerator) { Add(Ops.SplitBefore(subgenerator)); }
public void Has(ISelectorGenerator subgenerator) { Add(Ops.Has(subgenerator)); }
public void After(ISelectorGenerator subgenerator) { throw new NotImplementedException(); }
private IEnumerable <HtmlNode> Split(ISelectorGenerator subgenerator, HtmlNode parent, bool keepBefore, bool keepAfter) { var selector = GetSelector(subgenerator); var children = parent.ChildNodes.ToArray(); var splitterPositions = new List <int>(); var splitterIndex = 0; foreach (var splitter in selector(new[] { parent })) { splitterIndex = Array.IndexOf(children, splitter, splitterIndex); if (splitterIndex == -1) { throw new FormatException("The node splitter must be a direct child of the context node."); } splitterPositions.Add(splitterIndex); } if (splitterPositions.Count == 0) { if (keepBefore && keepAfter) { yield return(parent); } yield break; } var doc = new HtmlDocument(); var keepSeparators = keepBefore != keepAfter; if (keepBefore) { yield return(CreateNodesGroup(doc, children, 0, splitterPositions[0] + (keepSeparators ? 0 : -1))); } for (int i = 1; i < splitterPositions.Count; i++) { var indexBegin = splitterPositions[i - 1] + 1; var indexEnd = splitterPositions[i] - 1; if (keepSeparators) { if (keepAfter) { indexBegin--; } else { indexEnd++; } } yield return(CreateNodesGroup(doc, children, indexBegin, indexEnd)); } if (keepAfter) { yield return(CreateNodesGroup(doc, children, splitterPositions[splitterPositions.Count - 1] + (keepSeparators ? 0 : 1), children.Length - 1)); } }
public void SplitAll(ISelectorGenerator subgenerator) { throw new NotImplementedException(); }
private Selector <HtmlNode> GetSelector(ISelectorGenerator subgenerator) { return(((SelectorGenerator <HtmlNode>)subgenerator).Selector); }
public void Before(ISelectorGenerator subgenerator) { Add(Ops.Before(subgenerator)); }
public void Between(ISelectorGenerator startGenerator, ISelectorGenerator endGenerator) { Add(Ops.Between(startGenerator, endGenerator)); }
public void SplitBetween(ISelectorGenerator subgenerator) { Add(Ops.SplitBetween(subgenerator)); }
public void Between(ISelectorGenerator startGenerator, ISelectorGenerator endGenerator) { throw new NotImplementedException(); }
public Selector <XmlNode> Has(ISelectorGenerator subgenerator) { throw new NotImplementedException(); }
public void Not(ISelectorGenerator generator) { throw new NotImplementedException(); }
/// <summary> /// Initializes a new instance of <see cref="SelectorGeneratorTee"/> /// with the two other <see cref="ISelectorGenerator"/> objects /// it delegates to. /// </summary> public SelectorGeneratorTee(ISelectorGenerator primary, ISelectorGenerator secondary) { Primary = primary ?? throw new ArgumentNullException(nameof(primary)); Secondary = secondary ?? throw new ArgumentNullException(nameof(secondary)); }
public void SplitAll(ISelectorGenerator subgenerator) { Add(Ops.SplitAll(subgenerator)); }
private Parser(Reader <Token> reader, ISelectorGenerator generator) : this(reader, generator, true) { }
public void After(ISelectorGenerator subgenerator) { Add(Ops.After(subgenerator)); }
public Selector <HtmlNode> SplitAll(ISelectorGenerator subgenerator) { return(nodes => nodes.SelectMany(x => Split(subgenerator, x, true, true))); }
public void Not(ISelectorGenerator subgenerator) { Add(Ops.Not(subgenerator)); }
public Selector <Control> Has(ISelectorGenerator subgenerator) { throw new NotImplementedException(); }
public Selector <HtmlNode> SplitBetween(ISelectorGenerator subgenerator) { return(nodes => nodes.SelectMany(x => Split(subgenerator, x, false, false))); }
private IEnumerable <HtmlNode> Split(ISelectorGenerator subgenerator, HtmlNode parent, bool keepBefore, bool keepAfter, bool?rightEndOpen) { Selector <HtmlNode> compiled = this.GetSelector(subgenerator); HtmlNode[] array = parent.ChildNodes.ToArray <HtmlNode>(); List <int> list = new List <int>(); int splitterIndex = 0; foreach (HtmlNode current in compiled(new HtmlNode[] { parent })) { #if SALTARELLE splitterIndex = array.IndexOf(current, splitterIndex); #else splitterIndex = Array.IndexOf <HtmlNode>(array, current, splitterIndex); #endif if (splitterIndex == -1) { throw new FormatException("The node splitter must be a direct child of the context node."); } list.Add(splitterIndex); } if (list.Count == 0) { if (keepBefore & keepAfter) { yield return(parent); } yield break; } HtmlDocument ownerDocument = parent.OwnerDocument; bool flag = keepBefore != keepAfter; if (keepBefore) { yield return(this.CreateNodesGroup(ownerDocument, array, 0, list[0] + (flag ? 0 : -1), rightEndOpen)); } int num4; for (int i = 1; i < list.Count; i = num4 + 1) { int num2 = list[i - 1] + 1; int num3 = list[i] - 1; if (flag) { if (keepAfter) { num2--; } else { num3++; } } yield return(this.CreateNodesGroup(ownerDocument, array, num2, num3, rightEndOpen)); num4 = i; } if (keepAfter) { yield return(this.CreateNodesGroup(ownerDocument, array, list[list.Count - 1] + (flag ? 0 : 1), array.Length - 1, rightEndOpen)); } yield break; }