Ejemplo n.º 1
0
        public HtmlCollection Clone(bool deep = true)
        {
            var collection = new HtmlCollection();

            foreach (var element in this)
            {
                collection.Add(element.Clone(deep) as IElement);
            }
            return(collection);
        }
Ejemplo n.º 2
0
        public HtmlCollection QuerySelectorAll(string selector)
        {
            if (string.IsNullOrWhiteSpace(selector))
            {
                return(this);
            }
            var collection = new HtmlCollection();

            collection.AddRange(_querySelectorAll(selector));
            return(collection);
        }
Ejemplo n.º 3
0
        public async Task <HtmlCollection> DocumentElement()
        {
            IDocument container;

            if (!string.IsNullOrWhiteSpace(Raw))
            {
                var parser = new HtmlParser();
                container = parser.Parse(Raw);
            }
            else if (!string.IsNullOrWhiteSpace(Url))
            {
                container = await BrowsingContext.New(Configuration.Default.WithDefaultLoader(requesters: new[] { _requester })).OpenAsync(Url);
            }
            else
            {
                throw new Exception("no source");
            }

            if (Actions != null)
            {
                foreach (var action in Actions)
                {
                    var subjects = container.QuerySelectorAll(action.At);
                    if (action.Type == "insert")
                    {
                        var sources = await action.Source.DocumentElement();

                        subjects.ForEach(subject => sources.ForEach(source => subject.AppendChild(source.Clone(deep: true))));
                    }
                    else if (action.Type == "replace")
                    {
                        var sources = await action.Source.DocumentElement();

                        subjects.ForEach(subject => subject.Replace(sources.Clone().ToArray()));
                    }
                    else if (action.Type == "remove")
                    {
                        subjects.ForEach(subject => subject.Remove());
                    }
                    else if (action.Type == "insert-after")
                    {
                        var sources = await action.Source.DocumentElement();

                        subjects.ForEach(subject => subject.Insert(AdjacentPosition.AfterEnd, sources.ToHtml()));
                    }
                    else if (action.Type == "insert-before")
                    {
                        var sources = await action.Source.DocumentElement();

                        subjects.ForEach(subject => subject.Insert(AdjacentPosition.BeforeBegin, sources.ToHtml()));
                    }
                    else if (action.Type == "wrap")
                    {
                        var sources = await action.Source.DocumentElement();

                        if (sources.Count > 1)
                        {
                            throw new Exception("Wrap can only support a single element in the source");
                        }
                        subjects.ForEach(subject => {
                            var contextSource = sources.First().Clone();
                            subject.Replace(contextSource);
                            contextSource.AppendChild(subject);
                        });
                    }
                }
            }

            var elements = new HtmlCollection();

            if (!string.IsNullOrWhiteSpace(Raw))
            {
                if (Raw.Contains("<html>"))
                {
                    elements.Add(container.DocumentElement);
                }
                else
                {
                    if (container.Body.Children.Any())
                    {
                        elements.AddRange(container.Body.Children);
                    }
                    else if (container.Head.Children.Any())
                    {
                        elements.AddRange(container.Head.Children);
                    }
                    else
                    {
                        throw new Exception("Could not find created element");
                    }
                }

                if (!string.IsNullOrWhiteSpace(Select))
                {
                    elements = elements.QuerySelectorAll(Select);
                }
            }
            else if (!string.IsNullOrWhiteSpace(Url))
            {
                if (!string.IsNullOrWhiteSpace(Select))
                {
                    elements.AddRange(container.QuerySelectorAll(Select));
                }
                else
                {
                    elements.Add(container.DocumentElement);
                }
            }
            else
            {
                throw new Exception("no source");
            }


            return(elements);
        }