parser/html/nsHtml5MetaScannerCppSupplement.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/nsHtml5MetaScannerCppSupplement.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,45 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 + 
     1.8 +#include "nsEncoderDecoderUtils.h"
     1.9 +#include "nsISupportsImpl.h"
    1.10 +
    1.11 +#include "mozilla/dom/EncodingUtils.h"
    1.12 +
    1.13 +using mozilla::dom::EncodingUtils;
    1.14 +
    1.15 +void
    1.16 +nsHtml5MetaScanner::sniff(nsHtml5ByteReadable* bytes, nsACString& charset)
    1.17 +{
    1.18 +  readable = bytes;
    1.19 +  stateLoop(stateSave);
    1.20 +  readable = nullptr;
    1.21 +  charset.Assign(mCharset);
    1.22 +}
    1.23 +
    1.24 +bool
    1.25 +nsHtml5MetaScanner::tryCharset(nsString* charset)
    1.26 +{
    1.27 +  // This code needs to stay in sync with
    1.28 +  // nsHtml5StreamParser::internalEncodingDeclaration. Unfortunately, the
    1.29 +  // trickery with member fields here leads to some copy-paste reuse. :-(
    1.30 +  nsAutoCString label;
    1.31 +  CopyUTF16toUTF8(*charset, label);
    1.32 +  nsAutoCString encoding;
    1.33 +  if (!EncodingUtils::FindEncodingForLabel(label, encoding)) {
    1.34 +    return false;
    1.35 +  }
    1.36 +  if (encoding.EqualsLiteral("UTF-16BE") ||
    1.37 +      encoding.EqualsLiteral("UTF-16LE")) {
    1.38 +    mCharset.Assign("UTF-8");
    1.39 +    return true;
    1.40 +  }
    1.41 +  if (encoding.EqualsLiteral("x-user-defined")) {
    1.42 +    // WebKit/Blink hack for Indian and Armenian legacy sites
    1.43 +    mCharset.Assign("windows-1252");
    1.44 +    return true;
    1.45 +  }
    1.46 +  mCharset.Assign(encoding);
    1.47 +  return true;
    1.48 +}

mercurial