Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de) | 2 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de) |
| 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved. | 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved. |
| 4 Copyright (C) 2005, 2006, 2007 Alexey Proskuryakov (ap@nypop.com) | 4 Copyright (C) 2005, 2006, 2007 Alexey Proskuryakov (ap@nypop.com) |
| 5 | 5 |
| 6 This library is free software; you can redistribute it and/or | 6 This library is free software; you can redistribute it and/or |
| 7 modify it under the terms of the GNU Library General Public | 7 modify it under the terms of the GNU Library General Public |
| 8 License as published by the Free Software Foundation; either | 8 License as published by the Free Software Foundation; either |
| 9 version 2 of the License, or (at your option) any later version. | 9 version 2 of the License, or (at your option) any later version. |
| 10 | 10 |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 memcpy(m_buffer.data() + oldSize, data, len); | 395 memcpy(m_buffer.data() + oldSize, data, len); |
| 396 } | 396 } |
| 397 | 397 |
| 398 dataForDecode = m_buffer.data() + lengthOfBOM; | 398 dataForDecode = m_buffer.data() + lengthOfBOM; |
| 399 lengthForDecode = m_buffer.size() - lengthOfBOM; | 399 lengthForDecode = m_buffer.size() - lengthOfBOM; |
| 400 } | 400 } |
| 401 | 401 |
| 402 if (m_contentType == HTMLContent && !m_checkedForMetaCharset) | 402 if (m_contentType == HTMLContent && !m_checkedForMetaCharset) |
| 403 checkForMetaCharset(dataForDecode, lengthForDecode); | 403 checkForMetaCharset(dataForDecode, lengthForDecode); |
| 404 | 404 |
| 405 if (shouldAutoDetect()) { | 405 tryDetectingTextEncoding(data, len); |
| 406 WTF::TextEncoding detectedEncoding; | |
| 407 if (detectTextEncoding(data, len, m_hintEncoding, &detectedEncoding)) | |
| 408 setEncoding(detectedEncoding, EncodingFromContentSniffing); | |
| 409 } | |
| 410 | 406 |
| 411 ASSERT(m_encoding.isValid()); | 407 ASSERT(m_encoding.isValid()); |
| 412 | 408 |
| 413 if (!m_codec) | 409 if (!m_codec) |
| 414 m_codec = newTextCodec(m_encoding); | 410 m_codec = newTextCodec(m_encoding); |
| 415 | 411 |
| 416 String result = m_codec->decode(dataForDecode, lengthForDecode, DoNotFlush, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError); | 412 String result = m_codec->decode(dataForDecode, lengthForDecode, DoNotFlush, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError); |
| 417 | 413 |
| 418 m_buffer.clear(); | 414 m_buffer.clear(); |
| 419 return result; | 415 return result; |
| 420 } | 416 } |
| 421 | 417 |
| 418 void TextResourceDecoder::tryDetectingTextEncoding(const char* data, size_t len) | |
| 419 { | |
| 420 if (shouldAutoDetect()) { | |
|
esprehn
2016/02/26 09:37:25
Can we use early return instead?
put a return at
Jinsuk Kim
2016/03/02 00:08:32
Done. (I suppose you meant 'if' not 'return' state
| |
| 421 WTF::TextEncoding detectedEncoding; | |
| 422 if (detectTextEncoding(data, len, m_hintEncoding, &detectedEncoding)) | |
| 423 setEncoding(detectedEncoding, EncodingFromContentSniffing); | |
| 424 } else if ((m_source == DefaultEncoding || (m_source == EncodingFromParentFr ame && m_hintEncoding)) && isUTF8Encoded(data, len)) { | |
| 425 setEncoding(UTF8Encoding(), EncodingFromContentSniffing); | |
| 426 } | |
| 427 } | |
| 428 | |
| 422 String TextResourceDecoder::flush() | 429 String TextResourceDecoder::flush() |
| 423 { | 430 { |
| 424 // If we can not identify the encoding even after a document is completely | 431 // If we can not identify the encoding even after a document is completely |
| 425 // loaded, we need to detect the encoding if other conditions for | 432 // loaded, we need to detect the encoding if other conditions for |
| 426 // autodetection is satisfied. | 433 // autodetection is satisfied. |
| 427 if (m_buffer.size() && shouldAutoDetect() | 434 if (m_buffer.size() |
| 428 && ((!m_checkedForXMLCharset && (m_contentType == HTMLContent || m_conte ntType == XMLContent)) || (!m_checkedForCSSCharset && (m_contentType == CSSConte nt)))) { | 435 && ((!m_checkedForXMLCharset && (m_contentType == HTMLContent || m_conte ntType == XMLContent)) || (!m_checkedForCSSCharset && (m_contentType == CSSConte nt)))) { |
| 429 WTF::TextEncoding detectedEncoding; | 436 tryDetectingTextEncoding(m_buffer.data(), m_buffer.size()); |
| 430 if (detectTextEncoding(m_buffer.data(), m_buffer.size(), m_hintEncoding, &detectedEncoding)) | |
| 431 setEncoding(detectedEncoding, EncodingFromContentSniffing); | |
| 432 } | 437 } |
| 433 | 438 |
| 434 if (!m_codec) | 439 if (!m_codec) |
| 435 m_codec = newTextCodec(m_encoding); | 440 m_codec = newTextCodec(m_encoding); |
| 436 | 441 |
| 437 String result = m_codec->decode(m_buffer.data(), m_buffer.size(), FetchEOF, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError); | 442 String result = m_codec->decode(m_buffer.data(), m_buffer.size(), FetchEOF, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError); |
| 438 m_buffer.clear(); | 443 m_buffer.clear(); |
| 439 m_codec.clear(); | 444 m_codec.clear(); |
| 440 m_checkedForBOM = false; // Skip BOM again when re-decoding. | 445 m_checkedForBOM = false; // Skip BOM again when re-decoding. |
| 441 return result; | 446 return result; |
| 442 } | 447 } |
| 443 | 448 |
| 444 } // namespace blink | 449 } // namespace blink |
| OLD | NEW |