/// <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="encoding">The optional default encoding.</param> /// <param name="ancestor">The optional import ancestor.</param> public CreateDocumentOptions(IResponse response, Encoding encoding = null, IDocument ancestor = null) { var contentType = response.GetContentType(MimeTypeNames.Html); var charset = contentType.GetParameter(AttributeNames.Charset); var defaultEncoding = encoding ?? Encoding.UTF8; var source = new TextSource(response.Content, defaultEncoding); if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset)) { source.CurrentEncoding = TextEncoding.Resolve(charset); } _source = source; _contentType = contentType; _response = response; _ancestor = ancestor; }
/// <summary> /// Gets the associated encoding, if any. /// </summary> /// <param name="element">The element to get the encoding from.</param> /// <returns>The discovered encoding or null.</returns> protected virtual Encoding GetEncoding(IHtmlMetaElement element) { var charset = element.Charset; if (charset != null) { charset = charset.Trim(); if (TextEncoding.IsSupported(charset)) { return(TextEncoding.Resolve(charset)); } } var shouldParseContent = element.HttpEquivalent.Isi(HeaderNames.ContentType); return(shouldParseContent ? TextEncoding.Parse(element.Content ?? String.Empty) : null); }
public Encoding GetEncoding() { var charset = Charset; if (charset != null) { charset = charset.Trim(); if (TextEncoding.IsSupported(charset)) { return(TextEncoding.Resolve(charset)); } } var equiv = HttpEquivalent; var shouldParseContent = equiv != null && equiv.Isi(HeaderNames.ContentType); return(shouldParseContent ? TextEncoding.Parse(Content ?? String.Empty) : null); }
void SetEncoding(String charSet) { if (TextEncoding.IsSupported(charSet)) { var encoding = TextEncoding.Resolve(charSet); if (encoding != null) { try { _document.Source.CurrentEncoding = encoding; } catch (NotSupportedException) { _currentMode = XmlTreeMode.Initial; _document.ReplaceAll(null, true); _openElements.Clear(); } } } }
/// <summary> /// Gets the specified charset, if there has been any specified. /// </summary> /// <returns>The encoding or null.</returns> public Encoding GetEncoding() { var charset = Charset; if (charset != null) { charset = charset.Trim(); if (TextEncoding.IsSupported(charset)) { return(TextEncoding.Resolve(charset)); } } var equiv = HttpEquivalent; if (equiv != null && equiv.Equals(HeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)) { return(TextEncoding.Parse(Content ?? String.Empty)); } return(null); }