Ejemplo n.º 1
0
 private Parser(Reader <Token> reader, ISelectorGenerator generator)
 {
     Debug.Assert(reader != null);
     Debug.Assert(generator != null);
     _reader    = reader;
     _generator = generator;
 }
Ejemplo n.º 2
0
 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;
     }));
 }
Ejemplo n.º 3
0
        public Selector <HtmlNode> Has(ISelectorGenerator subgenerator)
        {
            var castedGenerator = (SelectorGenerator <HtmlNode>)subgenerator;

            var compiled = castedGenerator.Selector;

            return(nodes => nodes.Where(n => compiled(new[] { n }).Any()));
        }
Ejemplo n.º 4
0
 private Parser(Reader <Token> reader, ISelectorGenerator generator, bool expectEoi)
 {
     Debug.Assert(reader != null);
     Debug.Assert(generator != null);
     _reader    = reader;
     _generator = generator;
     _expectEoi = expectEoi;
 }
Ejemplo n.º 5
0
        /// <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;
        }
Ejemplo n.º 6
0
 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;
     }));
 }
Ejemplo n.º 7
0
        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);
            });
        }
Ejemplo n.º 8
0
        /// <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;
        }
Ejemplo n.º 9
0
        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);
            }));
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
 public void SplitBefore(ISelectorGenerator subgenerator)
 {
     Add(Ops.SplitBefore(subgenerator));
 }
Ejemplo n.º 12
0
 public void Has(ISelectorGenerator subgenerator)
 {
     Add(Ops.Has(subgenerator));
 }
Ejemplo n.º 13
0
 public void After(ISelectorGenerator subgenerator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 14
0
        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));
            }
        }
Ejemplo n.º 15
0
 public void SplitAll(ISelectorGenerator subgenerator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 16
0
 private Selector <HtmlNode> GetSelector(ISelectorGenerator subgenerator)
 {
     return(((SelectorGenerator <HtmlNode>)subgenerator).Selector);
 }
Ejemplo n.º 17
0
 public void Before(ISelectorGenerator subgenerator)
 {
     Add(Ops.Before(subgenerator));
 }
Ejemplo n.º 18
0
 public void Between(ISelectorGenerator startGenerator, ISelectorGenerator endGenerator)
 {
     Add(Ops.Between(startGenerator, endGenerator));
 }
Ejemplo n.º 19
0
 public void SplitBetween(ISelectorGenerator subgenerator)
 {
     Add(Ops.SplitBetween(subgenerator));
 }
Ejemplo n.º 20
0
 public void Between(ISelectorGenerator startGenerator, ISelectorGenerator endGenerator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 public Selector <XmlNode> Has(ISelectorGenerator subgenerator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 22
0
 public void Not(ISelectorGenerator generator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
 /// <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));
 }
Ejemplo n.º 24
0
 public void SplitAll(ISelectorGenerator subgenerator)
 {
     Add(Ops.SplitAll(subgenerator));
 }
Ejemplo n.º 25
0
 private Parser(Reader <Token> reader, ISelectorGenerator generator)
     : this(reader, generator, true)
 {
 }
Ejemplo n.º 26
0
 public void After(ISelectorGenerator subgenerator)
 {
     Add(Ops.After(subgenerator));
 }
Ejemplo n.º 27
0
 public Selector <HtmlNode> SplitAll(ISelectorGenerator subgenerator)
 {
     return(nodes => nodes.SelectMany(x => Split(subgenerator, x, true, true)));
 }
Ejemplo n.º 28
0
 public void Not(ISelectorGenerator subgenerator)
 {
     Add(Ops.Not(subgenerator));
 }
Ejemplo n.º 29
0
 public Selector <Control> Has(ISelectorGenerator subgenerator)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 30
0
 public Selector <HtmlNode> SplitBetween(ISelectorGenerator subgenerator)
 {
     return(nodes => nodes.SelectMany(x => Split(subgenerator, x, false, false)));
 }
Ejemplo n.º 31
0
        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;
        }