/// <summary> /// Builds a list of nodes according with 8.4 Parsing HTML fragments. /// </summary> /// <param name="sourceCode">The string to use as source code.</param> /// <param name="context">[Optional] The context element to use.</param> /// <param name="configuration">[Optional] Custom options to use for the document generation.</param> /// <returns>A list of parsed nodes.</returns> public static INodeList HtmlFragment(String sourceCode, IElement context = null, IConfiguration configuration = null) { if (sourceCode == null) { throw new ArgumentException("sourceCode"); } if (configuration == null) { configuration = new Configuration(); } var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None); var stream = new TextSource(sourceCode); var doc = new Document(browsingContext, stream); var node = context as Element; var parser = Construct(doc, configuration); if (node == null) { return(parser.Parse().ChildNodes); } var owner = node.Owner; if (owner != null && owner.QuirksMode != QuirksMode.Off) { doc.QuirksMode = owner.QuirksMode; } return(parser.SwitchToFragment(node).Parse().DocumentElement.ChildNodes); }
/// <summary> /// Builds a new HTML Document by asynchronously requesting the given URL. /// </summary> /// <param name="url">The URL which points to the address containing the source code.</param> /// <param name="cancel">The cancellation token for cancelling the asynchronous request.</param> /// <param name="configuration">[Optional] Custom options to use for the document generation.</param> /// <returns>The task that constructs the HTML document.</returns> public static async Task <IDocument> HtmlAsync(Uri url, CancellationToken cancel, IConfiguration configuration = null) { if (url == null) { throw new ArgumentNullException("url"); } if (configuration == null) { configuration = AngleSharp.Configuration.Default; } var requester = configuration.GetRequester(url.Scheme) ?? new DefaultRequester(); using (var response = await requester.LoadAsync(new Url(url), cancel).ConfigureAwait(false)) { var stream = new TextSource(response.Content, configuration.DefaultEncoding()); var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None); var doc = new Document(browsingContext, stream) { DocumentUri = url.OriginalString }; return(await Construct(doc, configuration).ParseAsync(cancel).ConfigureAwait(false)); } }
/// <summary> /// Builds a new HTML Document with the given (network) stream. /// </summary> /// <param name="content">The stream of chars to use as source code.</param> /// <param name="configuration">[Optional] Custom options to use for the document generation.</param> /// <param name="url">[Optional] The base URL of the document.</param> /// <returns>The constructed HTML document.</returns> public static IDocument Html(Stream content, IConfiguration configuration = null, String url = null) { if (content == null) { throw new ArgumentNullException("content"); } if (configuration == null) { configuration = AngleSharp.Configuration.Default; } var stream = new TextSource(content, configuration.DefaultEncoding()); var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None); var doc = new Document(browsingContext, stream) { DocumentUri = url }; return(Construct(doc, configuration).Parse()); }
/// <summary> /// Builds a new HTML Document asynchronously with the given (network) stream. /// </summary> /// <param name="content">The stream of chars to use as source code.</param> /// <param name="cancel">The cancellation token for cancelling the asynchronous request.</param> /// <param name="configuration">[Optional] Custom options to use for the document generation.</param> /// <param name="url">[Optional] The base URL of the document.</param> /// <returns>The task to construct the HTML document.</returns> public static async Task <IDocument> HtmlAsync(Stream content, CancellationToken cancel, IConfiguration configuration = null, String url = null) { if (content == null) { throw new ArgumentException("content"); } if (configuration == null) { configuration = AngleSharp.Configuration.Default; } var stream = new TextSource(content, configuration.DefaultEncoding()); var browsingContext = new SimpleBrowsingContext(configuration, Sandboxes.None); var doc = new Document(browsingContext, stream) { DocumentUri = url }; var parser = Construct(doc, configuration); return(await parser.ParseAsync(cancel).ConfigureAwait(false)); }