Пример #1
0
 internal Document(IBrowsingContext context, TextSource source)
     : base(null, "#document", NodeType.Document)
 {
     Referrer            = String.Empty;
     ContentType         = MimeTypeNames.ApplicationXml;
     _attachedReferences = new List <WeakReference>();
     _async             = true;
     _designMode        = false;
     _firedUnload       = false;
     _salvageable       = true;
     _shown             = false;
     _context           = context;
     _source            = source;
     _ready             = DocumentReadyState.Loading;
     _sandbox           = Sandboxes.None;
     _quirksMode        = QuirksMode.Off;
     _loadingScripts    = new Queue <HtmlScriptElement>();
     _location          = new Location("about:blank");
     _location.Changed += LocationChanged;
     _view              = new Window(this);
     _loader            = context.GetService <IResourceLoader>();
     _loop              = context.GetService <IEventLoop>();
     _mutations         = new MutationHost(_loop);
     _statusCode        = HttpStatusCode.OK;
 }
        public BillundPizzaMenuParser()
        {
            this.client = new HttpClient();
            IBrowsingContext context = BrowsingContext.New(Configuration.Default);

            this.parser = context.GetService <IHtmlParser>();
        }
Пример #3
0
        /// <summary>
        /// Gets an instance of the given service.
        /// </summary>
        /// <typeparam name="T">The type of service to resolve.</typeparam>
        /// <returns>The instance of the service or null.</returns>
        public T GetService <T>() where T : class
        {
            var count = _services.Count;

            for (var i = 0; i < count; i++)
            {
                var service  = _services[i];
                var instance = service as T;

                if (instance == null)
                {
                    var creator = service as Func <IBrowsingContext, T>;

                    if (creator == null)
                    {
                        continue;
                    }

                    instance     = creator.Invoke(this);
                    _services[i] = instance;
                }

                return(instance);
            }

            return(_parent?.GetService <T>());
        }
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task <IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var loader = context.GetService <IDocumentLoader>();

            if (loader != null)
            {
                var download = loader.FetchAsync(request);
                cancel.Register(download.Cancel);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    if (response != null)
                    {
                        return(await context.OpenAsync(response, cancel).ConfigureAwait(false));
                    }
                }
            }

            return(await context.OpenNewAsync(request.Target.Href, cancel).ConfigureAwait(false));
        }
Пример #5
0
 public ScriptRequestProcessor(IBrowsingContext context, HtmlScriptElement script)
 {
     _context  = context;
     _document = script.Owner;
     _script   = script;
     _loader   = context.GetService <IResourceLoader>() !;
 }
Пример #6
0
 public RegardParser(ILogger <RegardParser> logger, IHostedService hostedService)
 {
     this.logger = logger;
     context     = BrowsingContext.New(Configuration.Default.WithDefaultLoader().WithDefaultCookies());
     parser      = context.GetService <IHtmlParser>();
     orderBot    = (OrderBot)hostedService;
 }
Пример #7
0
        public DiffingTestFixture()
        {
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
Пример #8
0
        public HtmlParser()
        {
            var config = Configuration.Default;

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>() !;
            _document   = _context.OpenNewAsync().Result;
        }
Пример #9
0
        public Int32 Insert(String ruleText, Int32 index)
        {
            var parser = _context.GetService <ICssParser>();
            var rule   = parser.ParseRule(this, ruleText);

            _rules.Insert(index, rule);
            rule.SetOwner(this);
            return(index);
        }
Пример #10
0
        /// <summary>
        /// Dom Parser by AngelSharp
        /// example:document.QuerySelectorAll("")
        /// </summary>
        /// <returns>IHtmlDocument</returns>
        public IHtmlDocument GetDocument(Encoding encode = null)
        {
            var config = Configuration.Default;
            IBrowsingContext context = BrowsingContext.New(config);
            var parser   = context.GetService <IHtmlParser>();
            var document = parser.ParseDocument(this.GetHtml(encode));

            return(document);
        }
        /// <summary>
        /// Loads the response as an HTML document.
        /// </summary>
        protected static Task <IDocument> LoadHtmlAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
        {
            var parser   = context.GetService <IHtmlParser>();
            var document = new HtmlDocument(context, options.Source);

            document.Setup(options.Response, options.ContentType, options.ImportAncestor);
            context.NavigateTo(document);
            return(parser.ParseDocumentAsync(document, cancellationToken));
        }
Пример #12
0
        /// <summary>
        /// Creates a <see cref="HtmlDiffer"/> with the provided strategy.
        /// </summary>
        /// <param name="diffingStrategy"></param>
        public HtmlDiffer(IDiffingStrategy diffingStrategy)
        {
            _diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
Пример #13
0
        private static Task <IDocument> LoadSvgAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancellationToken)
        {
            var parser   = context.GetService <IXmlParser>() ?? throw new InvalidOperationException("The IXmlParser service has been removed. Cannot continue.");
            var document = new SvgDocument(context, options.Source);

            document.Setup(options.Response, options.ContentType, options.ImportAncestor);
            context.NavigateTo(document);
            return(parser.ParseDocumentAsync(document, cancellationToken));
        }
Пример #14
0
        /// <summary>
        /// Creates a <see cref="HtmlDiffer"/> with the provided strategy.
        /// </summary>
        /// <param name="diffingStrategy"></param>
        public HtmlDiffer(IDiffingStrategy diffingStrategy)
        {
            _diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
            var config = Configuration.Default.WithCss();

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>() ?? throw new InvalidOperationException("No IHtmlParser registered in the default AngleSharp browsing context.");
            _document   = _context.OpenNewAsync().Result;
        }
Пример #15
0
        public override void SetHeader(string key, string value)
        {
            //var configuration = GetBasicConfiguration();

            var requester = client.GetService <DefaultHttpRequester>();

            requester.Headers[key] = value;

            //client = BrowsingContext.New(configuration.With(requester));
        }
Пример #16
0
        /// <summary>
        /// Creates an instance of the parser with a AngleSharp context
        /// without a <see cref="TestRenderer"/> registered.
        /// </summary>
        public TestHtmlParser()
        {
            var config = Configuration.Default
                         .WithCss()
                         .With(new HtmlComparer())
                         .With(this);

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
Пример #17
0
        async Task <Problem> DownloadProblem(IBrowsingContext context, Uri baseUri, int year, int day)
        {
            var problemStatement = await context.OpenAsync(baseUri + $"{year}/day/{day}");

            var input = await context.GetService <IDocumentLoader>().FetchAsync(
                new DocumentRequest(new Url(baseUri + $"{year}/day/{day}/input"))).Task;

            return(Problem.Parse(
                       year, day, baseUri + $"{year}/day/{day}", problemStatement,
                       new StreamReader(input.Content).ReadToEnd()
                       ));
        }
        /// <summary>
        /// Checks if the context is waiting for tasks from originator of type
        /// T to finish downloading.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <returns>Enumerable of awaitable tasks.</returns>
        public static IEnumerable <Task> GetDownloads <T>(this IBrowsingContext context)
            where T : INode
        {
            var loader = context.GetService <IResourceLoader>();

            if (loader == null)
            {
                return(Enumerable.Empty <Task>());
            }

            return(loader.GetDownloads().Where(m => m.Source is T).Select(m => m.Task));
        }
Пример #19
0
        /// <summary>
        /// Creates an instance of the parser with a AngleSharp context
        /// with the <paramref name="testRenderer"/> registered.
        /// </summary>
        public HtmlParser(ITestRenderer testRenderer, HtmlComparer htmlComparer)
        {
            var config = Configuration.Default
                         .WithCss()
                         .With(testRenderer)
                         .With(htmlComparer)
                         .With(this);

            _context    = BrowsingContext.New(config);
            _htmlParser = _context.GetService <IHtmlParser>();
            _document   = _context.OpenNewAsync().Result;
        }
        public Boolean Check(IRenderDevice device)
        {
            var factory = _context?.GetService <IDeclarationFactory>() ?? Factory.Declaration;
            var info    = factory?.Create(_name);

            if (info != null && !Object.Equals(info.Converter, ValueConverters.Any))
            {
                var normedValue = Normalize(_value);
                var result      = info.Converter.Convert(normedValue);
                return(result != null);
            }

            return(false);
        }
Пример #21
0
        /// <summary>
        /// Gets the style rule with the provided selector text.
        /// </summary>
        /// <param name="rules">The rules to look in.</param>
        /// <param name="selectorText">The selector text to look for.</param>
        /// <param name="context">The context for normalizing the CSS selector.</param>
        /// <returns>The style rule, if any.</returns>
        public static ICssStyleRule GetStyleRuleWith(this ICssRuleList rules, String selectorText, IBrowsingContext context = null)
        {
            var styleRules             = rules.OfType <ICssStyleRule>();
            var parser                 = context?.GetService <ICssSelectorParser>();
            var normalizedSelectorText = parser?.ParseSelector(selectorText)?.Text ?? selectorText;

            foreach (var rule in styleRules)
            {
                if (rule.SelectorText.Is(normalizedSelectorText))
                {
                    return(rule);
                }
            }

            return(null);
        }
Пример #22
0
        public LvdunSpider() : base(maxThread: 1)
        {
            this.http.RegResponsePipelines(this.BeforeResponse);

            this.http.RegRequestPipelines((client, request, meta) => {
                request.Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
                //request.Headers.Accept.TryParseAdd("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                //request.Headers.UserAgent.TryParseAdd("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
                return(request);
            });
            this.http.RegRequestPipelines(this.BeforeRequest);
            var config = Configuration.Default;
            IBrowsingContext context = BrowsingContext.New(config);

            parser = context.GetService <IHtmlParser>();
        }
        /// <summary>
        /// Loads a stylesheet resource via its URL.
        /// </summary>
        /// <param name="context">The context to use.</param>
        /// <param name="address">The address of the resource.</param>
        /// <param name="element">The hosting element.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The async task.</returns>
        public static async Task <IStyleSheet> OpenStyleSheetAsync(this IBrowsingContext context, Url address, IElement element, CancellationToken cancel)
        {
            var loader  = context.GetService <IResourceLoader>();
            var service = context.GetCssStyling();

            if (loader != null && service != null)
            {
                var request  = element.CreateRequestFor(address);
                var download = loader.FetchAsync(request);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    var options = new StyleOptions(element?.Owner ?? context.Active)
                    {
                        Element = element
                    };
                    return(await service.ParseStylesheetAsync(response, options, cancel).ConfigureAwait(false));
                }
            }

            return(null);
        }
        public void Update(String value)
        {
            if (IsReadOnly)
            {
                throw new DomException(DomError.NoModificationAllowed);
            }

            if (!_updating)
            {
                _declarations.Clear();

                if (!String.IsNullOrEmpty(value))
                {
                    var parser = _context.GetService <ICssParser>();
                    var decl   = parser.ParseDeclaration(value);

                    if (decl != null)
                    {
                        _declarations.AddRange(decl);
                    }
                }
            }
        }
Пример #25
0
    async Task <Problem> DownloadProblem(IBrowsingContext context, Uri baseUri, int year, int day)
    {
        var uri   = baseUri + $"{year}/day/{day}";
        var color = Console.ForegroundColor;

        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Updating " + uri);
        Console.ForegroundColor = color;

        var problemStatement = await context.OpenAsync(uri);

        var input = await context.GetService <IDocumentLoader>().FetchAsync(
            new DocumentRequest(new Url(baseUri + $"{year}/day/{day}/input"))).Task;

        if (input.StatusCode != HttpStatusCode.OK)
        {
            throw new AocCommuncationError("Could not fetch input", input.StatusCode, new StreamReader(input.Content).ReadToEnd());
        }

        return(Problem.Parse(
                   year, day, baseUri + $"{year}/day/{day}", problemStatement,
                   new StreamReader(input.Content).ReadToEnd()
                   ));
    }
        private async Task PrepareImageForMessage(Func <Task <string> > tokenProvider, IMessageDetails message, IBrowsingContext context)
        {
            var parser   = context.GetService <IHtmlParser>();
            var document = parser.ParseDocument(message.Body.Content);
            var images   = document.QuerySelectorAll("img[src]");
            var urls     = new List <string>();

            foreach (var img in images)
            {
                urls.Add(img.GetAttribute("src"));

                // fixing max-width for big images
                var style = (img.GetAttribute("style") ?? string.Empty) + "; max-width:100%!important;";
                img.SetAttribute("style", style);
            }

            foreach (var url in urls)
            {
                var base64 = await imagesService.GetBase64Image(tokenProvider, url, graphBaseEndpoint);

                message.Body.Content = document.ToHtml();
                message.Body.Content = message.Body.Content.Replace(url, base64);
            }
        }
 /// <summary>
 /// Gets the culture info associated with the current context.
 /// </summary>
 /// <param name="context">The current context.</param>
 /// <returns>The culture info assigned to the context.</returns>
 public static CultureInfo GetCulture(this IBrowsingContext context) => context.GetService <CultureInfo>() ?? CultureInfo.CurrentUICulture;
Пример #28
0
 public StyleSheetRequestProcessor(IBrowsingContext context, IHtmlLinkElement link)
     : base(context?.GetService <IResourceLoader>())
 {
     _context = context;
     _link    = link;
 }
Пример #29
0
 public DubbersHelper(IdibData idibData)
 {
     this.idibData   = idibData;
     browsingContext = BrowsingContext.New(Configuration.Default);
     htmlParser      = browsingContext.GetService <IHtmlParser>();
 }
Пример #30
0
 public FrameRequestProcessor(IBrowsingContext context, HtmlFrameElementBase element)
     : base(context?.GetService <IResourceLoader>())
 {
     _element = element;
 }