Ejemplo n.º 1
0
 private bool TryGetHtmlCharsetFromMetaTag(uint maxBytesToSearch, out Charset htmlBodyCharset)
 {
     htmlBodyCharset = null;
     using (MemoryStream memoryStream = this.LoadHtmlBodyInMemory())
     {
         memoryStream.Position = 0L;
         Charset itemMimeCharset = ConvertUtils.GetItemMimeCharset(this.coreItem.PropertyBag);
         using (HtmlReader htmlReader = new HtmlReader(memoryStream, itemMimeCharset.GetEncoding()))
         {
             while (htmlReader.ReadNextToken())
             {
                 if ((long)htmlReader.CurrentOffset > (long)((ulong)maxBytesToSearch))
                 {
                     return(false);
                 }
                 if (htmlReader.TokenKind == HtmlTokenKind.EmptyElementTag && htmlReader.TagId == HtmlTagId.Meta)
                 {
                     bool   flag  = false;
                     bool   flag2 = false;
                     string text  = string.Empty;
                     HtmlAttributeReader attributeReader = htmlReader.AttributeReader;
                     while (!flag && attributeReader.ReadNext())
                     {
                         if (attributeReader.Id == HtmlAttributeId.Charset && attributeReader.HasValue)
                         {
                             text = attributeReader.ReadValue();
                             flag = true;
                         }
                         else if (attributeReader.Id == HtmlAttributeId.Content && attributeReader.HasValue)
                         {
                             text = attributeReader.ReadValue();
                             if (flag2)
                             {
                                 flag = true;
                             }
                         }
                         else if (attributeReader.Id == HtmlAttributeId.HttpEquiv && attributeReader.HasValue)
                         {
                             string a = attributeReader.ReadValue();
                             if (string.Equals(a, "content-type", StringComparison.OrdinalIgnoreCase) || string.Equals(a, "charset", StringComparison.OrdinalIgnoreCase))
                             {
                                 flag2 = true;
                                 if (!string.IsNullOrEmpty(text))
                                 {
                                     flag = true;
                                 }
                             }
                         }
                     }
                     if (flag)
                     {
                         text = HtmlReader.CharsetFromString(text, flag2);
                         if (!string.IsNullOrEmpty(text) && Charset.TryGetCharset(text, out htmlBodyCharset))
                         {
                             return(true);
                         }
                         return(false);
                     }
                 }
             }
         }
     }
     return(false);
 }