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

Side by Side Diff: Source/core/dom/DecodedDataDocumentParser.cpp

Issue 22909053: Store the Document's encoding on the Document (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Set document encoding in DecodedDataDocumentParser instead of DocumentWriter Created 7 years, 3 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/core/dom/Document.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 22 matching lines...) Expand all
33 #include "wtf/text/TextEncodingRegistry.h" 33 #include "wtf/text/TextEncodingRegistry.h"
34 34
35 namespace WebCore { 35 namespace WebCore {
36 36
37 namespace { 37 namespace {
38 38
39 class TitleEncodingFixer { 39 class TitleEncodingFixer {
40 public: 40 public:
41 explicit TitleEncodingFixer(Document* document) 41 explicit TitleEncodingFixer(Document* document)
42 : m_document(document) 42 : m_document(document)
43 , m_firstEncoding(document->decoder()->encoding()) 43 , m_firstEncoding(document->encoding())
44 { 44 {
45 } 45 }
46 46
47 // It's possible for the encoding of the document to change while we're deco ding 47 // It's possible for the encoding of the document to change while we're deco ding
48 // data. That can only occur while we're processing the <head> portion of th e 48 // data. That can only occur while we're processing the <head> portion of th e
49 // document. There isn't much user-visible content in the <head>, but there is 49 // document. There isn't much user-visible content in the <head>, but there is
50 // the <title> element. This function detects that situation and re-decodes the 50 // the <title> element. This function detects that situation and re-decodes the
51 // document's title so that the user doesn't see an incorrectly decoded titl e 51 // document's title so that the user doesn't see an incorrectly decoded titl e
52 // in the title bar. 52 // in the title bar.
53 inline void fixTitleEncodingIfNeeded() 53 inline void fixTitleEncodingIfNeeded()
54 { 54 {
55 if (m_firstEncoding == m_document->decoder()->encoding()) 55 if (m_firstEncoding == m_document->encoding())
56 return; // In the common case, the encoding doesn't change and there isn't any work to do. 56 return; // In the common case, the encoding doesn't change and there isn't any work to do.
57 fixTitleEncoding(); 57 fixTitleEncoding();
58 } 58 }
59 59
60 private: 60 private:
61 void fixTitleEncoding(); 61 void fixTitleEncoding();
62 62
63 Document* m_document; 63 Document* m_document;
64 WTF::TextEncoding m_firstEncoding; 64 WTF::TextEncoding m_firstEncoding;
65 }; 65 };
66 66
67 void TitleEncodingFixer::fixTitleEncoding() 67 void TitleEncodingFixer::fixTitleEncoding()
68 { 68 {
69 RefPtr<Element> titleElement = m_document->titleElement(); 69 RefPtr<Element> titleElement = m_document->titleElement();
70 if (!titleElement 70 if (!titleElement
71 || titleElement->firstElementChild() 71 || titleElement->firstElementChild()
72 || m_firstEncoding != Latin1Encoding() 72 || m_firstEncoding != Latin1Encoding()
73 || !titleElement->textContent().containsOnlyLatin1()) 73 || !titleElement->textContent().containsOnlyLatin1())
74 return; // Either we don't have a title yet or something bizzare as happ ened and we give up. 74 return; // Either we don't have a title yet or something bizzare as happ ened and we give up.
75 CString originalBytes = titleElement->textContent().latin1(); 75 CString originalBytes = titleElement->textContent().latin1();
76 OwnPtr<TextCodec> codec = newTextCodec(m_document->decoder()->encoding()); 76 OwnPtr<TextCodec> codec = newTextCodec(m_document->encoding());
77 String correctlyDecodedTitle = codec->decode(originalBytes.data(), originalB ytes.length(), true); 77 String correctlyDecodedTitle = codec->decode(originalBytes.data(), originalB ytes.length(), true);
78 titleElement->setTextContent(correctlyDecodedTitle, IGNORE_EXCEPTION); 78 titleElement->setTextContent(correctlyDecodedTitle, IGNORE_EXCEPTION);
79 } 79 }
80 80
81 } 81 }
82 82
83 DecodedDataDocumentParser::DecodedDataDocumentParser(Document* document) 83 DecodedDataDocumentParser::DecodedDataDocumentParser(Document* document)
84 : DocumentParser(document) 84 : DocumentParser(document)
85 { 85 {
86 } 86 }
87 87
88 size_t DecodedDataDocumentParser::appendBytes(const char* data, size_t length) 88 size_t DecodedDataDocumentParser::appendBytes(const char* data, size_t length)
89 { 89 {
90 if (!length) 90 if (!length)
91 return 0; 91 return 0;
92 92
93 TitleEncodingFixer encodingFixer(document()); 93 TitleEncodingFixer encodingFixer(document());
94 94
95 String decoded = document()->decoder()->decode(data, length); 95 String decoded = document()->decoder()->decode(data, length);
96 document()->setEncoding(document()->decoder()->encoding());
96 97
97 encodingFixer.fixTitleEncodingIfNeeded(); 98 encodingFixer.fixTitleEncodingIfNeeded();
98 99
99 if (decoded.isEmpty()) 100 if (decoded.isEmpty())
100 return 0; 101 return 0;
101 102
102 size_t consumedChars = decoded.length(); 103 size_t consumedChars = decoded.length();
103 append(decoded.releaseImpl()); 104 append(decoded.releaseImpl());
104 105
105 return consumedChars; 106 return consumedChars;
106 } 107 }
107 108
108 size_t DecodedDataDocumentParser::flush() 109 size_t DecodedDataDocumentParser::flush()
109 { 110 {
110 // null decoder indicates there is no data received. 111 // null decoder indicates there is no data received.
111 // We have nothing to do in that case. 112 // We have nothing to do in that case.
112 TextResourceDecoder* decoder = document()->decoder(); 113 TextResourceDecoder* decoder = document()->decoder();
113 if (!decoder) 114 if (!decoder)
114 return 0; 115 return 0;
115 String remainingData = decoder->flush(); 116 String remainingData = decoder->flush();
117 document()->setEncoding(document()->decoder()->encoding());
116 if (remainingData.isEmpty()) 118 if (remainingData.isEmpty())
117 return 0; 119 return 0;
118 120
119 size_t consumedChars = remainingData.length(); 121 size_t consumedChars = remainingData.length();
120 append(remainingData.releaseImpl()); 122 append(remainingData.releaseImpl());
121 123
122 return consumedChars; 124 return consumedChars;
123 } 125 }
124 126
125 }; 127 };
OLDNEW
« no previous file with comments | « no previous file | Source/core/dom/Document.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698