internal async static Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken) { var scripting = context.Configuration.IsScripting(); var parserOptions = new HtmlParserOptions { IsScripting = scripting }; var document = new HtmlDocument(context, options.Source); var parser = new HtmlDomBuilder(document); document.Setup(options); context.NavigateTo(document); context.Fire(new HtmlParseEvent(document, completed: false)); await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false); context.Fire(new HtmlParseEvent(document, completed: true)); return document; }
/// <summary> /// Creates a new document fragment with the given nodelist as /// children. /// </summary> /// <param name="context">The context for the fragment mode.</param> /// <param name="html">The HTML source code to use.</param> internal DocumentFragment(Element context, String html) : this(context.Owner) { var source = new TextSource(html); var document = new HtmlDocument(Owner.Context, source); var parser = new HtmlDomBuilder(document); var options = new HtmlParserOptions { IsEmbedded = false, IsScripting = Owner.Options.IsScripting() }; var root = parser.ParseFragment(options, context).DocumentElement; while (root.HasChildNodes) { var child = root.FirstChild; root.RemoveChild(child); this.PreInsert(child, null); } }
/// <summary> /// Loads the document in the provided context from the given response. /// </summary> /// <param name="context">The browsing context.</param> /// <param name="response">The response to consider.</param> /// <param name="source">The source to use.</param> /// <param name="cancelToken">Token for cancellation.</param> /// <returns>The task that builds the document.</returns> internal async static Task<HtmlDocument> LoadAsync(IBrowsingContext context, IResponse response, TextSource source, CancellationToken cancelToken) { var document = new HtmlDocument(context, source); var evt = new HtmlParseStartEvent(document); var config = context.Configuration; var events = config.Events; var parser = new HtmlDomBuilder(document); document.ContentType = response.Headers.GetOrDefault(HeaderNames.ContentType, MimeTypes.Html); document.Referrer = response.Headers.GetOrDefault(HeaderNames.Referer, String.Empty); document.DocumentUri = response.Address.Href; document.Cookie = response.Headers.GetOrDefault(HeaderNames.SetCookie, String.Empty); document.ReadyState = DocumentReadyState.Loading; if (events != null) events.Publish(evt); var options = new HtmlParserOptions { IsScripting = config.IsScripting() }; await parser.ParseAsync(options, cancelToken).ConfigureAwait(false); evt.FireEnd(); return document; }
public static INodeList ToHtmlFragment(this String sourceCode, IElement context = null, IConfiguration configuration = null) { var ctx = BrowsingContext.New(configuration); var source = new TextSource(sourceCode); var document = new HtmlDocument(ctx, source); var parser = new HtmlDomBuilder(sourceCode, configuration); var element = context as Element; if (element != null) { var options = new HtmlParserOptions { IsEmbedded = false, IsScripting = configuration.IsScripting() }; return parser.ParseFragment(options, element).DocumentElement.ChildNodes; } else { return parser.Parse(default(HtmlParserOptions)).ChildNodes; } }
/// <summary> /// Parses the string and returns the result. /// </summary> public IHtmlDocument Parse(String source) { var document = CreateDocument(source); var parser = new HtmlDomBuilder(document); return parser.Parse(_options); }
/// <summary> /// Parses the stream asynchronously with option to cancel. /// </summary> public async Task<IHtmlDocument> ParseAsync(Stream source, CancellationToken cancel) { var document = CreateDocument(source); var parser = new HtmlDomBuilder(document); return await parser.ParseAsync(_options, cancel).ConfigureAwait(false); }
/// <summary> /// Loads the document in the provided context from the given response. /// </summary> /// <param name="context">The browsing context.</param> /// <param name="options">The creation options to consider.</param> /// <param name="cancelToken">Token for cancellation.</param> /// <returns>The task that builds the document.</returns> internal async static Task<IDocument> LoadAsync(IBrowsingContext context, CreateDocumentOptions options, CancellationToken cancelToken) { var document = new HtmlDocument(context, options.Source); var evt = new HtmlParseStartEvent(document); var config = context.Configuration; var events = config.Events; var parser = new HtmlDomBuilder(document); var parserOptions = new HtmlParserOptions { IsScripting = config.IsScripting() }; document.Setup(options); context.NavigateTo(document); if (events != null) { events.Publish(evt); } await parser.ParseAsync(parserOptions, cancelToken).ConfigureAwait(false); evt.FireEnd(); return document; }
/// <summary> /// Parses the string and returns the result. /// </summary> public INodeList ParseFragment(String source, IElement context) { var document = CreateDocument(source); var parser = new HtmlDomBuilder(document); if (context != null) { var element = context as Element; if (element == null) { var configuration = document.Options; var factory = configuration.GetFactory<IElementFactory<HtmlElement>>(); element = factory.Create(document, context.LocalName, context.Prefix); } return parser.ParseFragment(_options, element).DocumentElement.ChildNodes; } return parser.Parse(_options).ChildNodes; }
/// <summary> /// Parses the string and returns the result. /// </summary> public INodeList ParseFragment(String source, IElement context) { var document = CreateDocument(source); var parser = new HtmlDomBuilder(document); if (context == null) return parser.Parse(_options).ChildNodes; var element = context as Element ?? Factory.HtmlElements.Create(document, context.LocalName, context.Prefix); return parser.ParseFragment(_options, element).DocumentElement.ChildNodes; }