/// <summary> /// Creates new document creation options. Selects the source from the /// response by potentially using the encoding from the configuration. /// </summary> /// <param name="response">The response to hand over.</param> /// <param name="configuration">The configuration to use.</param> public CreateDocumentOptions(IResponse response, IConfiguration configuration) { var contentType = response.GetContentType(MimeTypeNames.Html); var encoding = configuration.DefaultEncoding(); var charset = contentType.GetParameter(AttributeNames.Charset); if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset)) encoding = TextEncoding.Resolve(charset); _source = new TextSource(response.Content, encoding); _contentType = contentType; _response = response; }
/// <summary> /// Opens a new document created from the response asynchronously in /// the given context. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="response">The response to examine.</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, IResponse response, CancellationToken cancel) { if (response == null) throw new ArgumentNullException("response"); if (context == null) context = BrowsingContext.New(); var source = new TextSource(response.Content, context.Configuration.DefaultEncoding()); var document = await context.LoadDocumentAsync(response, source, cancel).ConfigureAwait(false); context.NavigateTo(document); return document; }
/// <summary> /// Creates a new set of document options from the given response with /// the provided configuration. /// </summary> /// <param name="response">The response to pass on.</param> /// <param name="configuration">The configuration to use.</param> /// <param name="ancestor">The optional import ancestor.</param> public CreateDocumentOptions(IResponse response, IConfiguration configuration, IDocument ancestor = null) { var contentType = response.GetContentType(MimeTypeNames.Html); var charset = contentType.GetParameter(AttributeNames.Charset); var source = new TextSource(response.Content, configuration.DefaultEncoding()); if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset)) { source.CurrentEncoding = TextEncoding.Resolve(charset); } _source = source; _contentType = contentType; _response = response; _ancestor = ancestor; }
/// <summary> /// Opens a new document created from the response asynchronously in /// the given context. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="response">The response to examine.</param> /// <param name="cancel">The cancellation token.</param> /// <returns>The task that creates the document.</returns> public static Task<IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel) { if (response == null) throw new ArgumentNullException("response"); if (context == null) context = BrowsingContext.New(); var contentType = response.GetContentType(MimeTypes.Html); var encoding = context.Configuration.DefaultEncoding(); var charset = contentType.GetParameter(AttributeNames.Charset); if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset)) encoding = TextEncoding.Resolve(charset); var source = new TextSource(response.Content, encoding); return context.LoadDocumentAsync(response, contentType, source, cancel); }
void Release() { if (content != null && dispose) content.Dispose(); else if (source != null) source.Dispose(); dispose = false; source = null; content = null; }
/// <summary> /// Sets the response's content from the provided string. /// </summary> /// <param name="text">The text to use as content.</param> /// <returns>The current instance.</returns> public VirtualResponse Content(String text) { Release(); source = new TextSource(text); return this; }
/// <summary> /// Creates a new virtual response. /// </summary> public VirtualResponse() { address = Url.Create("http://localhost/"); status = HttpStatusCode.OK; headers = new Dictionary<String, String>(); content = MemoryStream.Null; source = null; dispose = false; }
static async Task<IDocument> LoadDocumentAsync(this IBrowsingContext context, IResponse response, TextSource source, CancellationToken cancel) { var contentType = response.Headers.GetOrDefault(HeaderNames.ContentType, MimeTypes.Html); if (contentType.IndexOf(';') > 0) { contentType = contentType.Substring(0, contentType.IndexOf(';')).Trim(); } if (contentType.Equals(MimeTypes.Xml, StringComparison.OrdinalIgnoreCase) || contentType.Equals(MimeTypes.ApplicationXml, StringComparison.OrdinalIgnoreCase)) { return await XmlDocument.LoadAsync(context, response, source, cancel).ConfigureAwait(false); } else if (contentType.Equals(MimeTypes.Svg, StringComparison.OrdinalIgnoreCase)) { return await SvgDocument.LoadAsync(context, response, source, cancel); } return await HtmlDocument.LoadAsync(context, response, source, cancel).ConfigureAwait(false); }
/// <summary> /// Creates new document creation options. /// </summary> /// <param name="response">The response to hand over.</param> /// <param name="contentType">The content mime-type.</param> /// <param name="source">The source of the document.</param> public CreateDocumentOptions(IResponse response, MimeType contentType, TextSource source) { _response = response; _contentType = contentType; _source = source; }
/// <summary> /// Creates new document creation options. /// </summary> /// <param name="response">The response to hand over.</param> /// <param name="source">The source of the document.</param> public CreateDocumentOptions(IResponse response, TextSource source) : this(response, response.GetContentType(MimeTypeNames.Html), source) { }
internal TextView(TextRange range, TextSource source) { _range = range; _source = source; }
static async Task<IDocument> LoadDocumentAsync(this IBrowsingContext context, IResponse response, MimeType contentType, TextSource source, CancellationToken cancel) { if (contentType.Represents(MimeTypes.Xml) || contentType.Represents(MimeTypes.ApplicationXml)) return await XmlDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false); else if (contentType.Represents(MimeTypes.Svg)) return await SvgDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false); return await HtmlDocument.LoadAsync(context, response, contentType, source, cancel).ConfigureAwait(false); }
void Release() { if (content != null && dispose) { content.Dispose(); } else if (source != null) { source.Dispose(); } dispose = false; source = null; content = null; }