Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(530)

Unified Diff: third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp

Issue 1721373002: UTF-8 detector for pages missing encoding info (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp
diff --git a/third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp b/third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp
index 49897d7c3ac409bd860dbaf44bbc4786a39ad3b4..5e27ba9ba25127e309e0083ef6604eaf0ac442d4 100644
--- a/third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp
+++ b/third_party/WebKit/Source/core/html/parser/TextResourceDecoder.cpp
@@ -28,6 +28,7 @@
#include "wtf/StringExtras.h"
#include "wtf/text/TextCodec.h"
#include "wtf/text/TextEncodingRegistry.h"
+#include "wtf/text/UTF8.h"
using namespace WTF;
@@ -402,11 +403,7 @@ String TextResourceDecoder::decode(const char* data, size_t len)
if (m_contentType == HTMLContent && !m_checkedForMetaCharset)
checkForMetaCharset(dataForDecode, lengthForDecode);
- if (shouldAutoDetect()) {
- WTF::TextEncoding detectedEncoding;
- if (detectTextEncoding(data, len, m_hintEncoding, &detectedEncoding))
- setEncoding(detectedEncoding, EncodingFromContentSniffing);
- }
+ detectTextEncoding(data, len);
ASSERT(m_encoding.isValid());
@@ -419,16 +416,28 @@ String TextResourceDecoder::decode(const char* data, size_t len)
return result;
}
+void TextResourceDecoder::detectTextEncoding(const char* data, size_t len)
+{
+ if (shouldAutoDetect()) {
+ WTF::TextEncoding detectedEncoding;
+ if (detectTextEncodingUniversal(data, len, m_hintEncoding, &detectedEncoding))
+ setEncoding(detectedEncoding, EncodingFromContentSniffing);
+ return;
+ }
+ if ((m_source == DefaultEncoding || (m_source == EncodingFromParentFrame && m_hintEncoding))) {
jungshik at Google 2016/03/24 06:15:08 nit: The above condition is shared by shouldAuto
Jinsuk Kim 2016/03/25 02:15:42 Done.
+ if (WTF::Unicode::isUTF8Encoded(data, len))
+ setEncoding(UTF8Encoding(), EncodingFromContentSniffing);
jungshik at Google 2016/03/24 06:15:08 Given that isUTF8Encoded excludes 'ASCII' (by chec
Jinsuk Kim 2016/03/25 02:15:42 Makes sense. Done.
+ }
+}
+
String TextResourceDecoder::flush()
{
// If we can not identify the encoding even after a document is completely
// loaded, we need to detect the encoding if other conditions for
// autodetection is satisfied.
- if (m_buffer.size() && shouldAutoDetect()
+ if (m_buffer.size()
&& ((!m_checkedForXMLCharset && (m_contentType == HTMLContent || m_contentType == XMLContent)) || (!m_checkedForCSSCharset && (m_contentType == CSSContent)))) {
- WTF::TextEncoding detectedEncoding;
- if (detectTextEncoding(m_buffer.data(), m_buffer.size(), m_hintEncoding, &detectedEncoding))
- setEncoding(detectedEncoding, EncodingFromContentSniffing);
+ detectTextEncoding(m_buffer.data(), m_buffer.size());
}
if (!m_codec)

Powered by Google App Engine
This is Rietveld 408576698