OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "wtf/text/TextCodecReplacement.h" | |
6 | |
7 #include "wtf/PtrUtil.h" | |
8 #include "wtf/text/CharacterNames.h" | |
9 #include "wtf/text/WTFString.h" | |
10 #include <memory> | |
11 | |
12 namespace WTF { | |
13 | |
14 TextCodecReplacement::TextCodecReplacement() | |
15 : m_replacementErrorReturned(false) {} | |
16 | |
17 void TextCodecReplacement::registerEncodingNames( | |
18 EncodingNameRegistrar registrar) { | |
19 // The 'replacement' label itself should not be referenceable by | |
20 // resources or script - it's a specification convenience - but much of | |
21 // the wtf/text API asserts that an encoding name is a label for itself. | |
22 // This is handled in TextEncoding by marking it as not valid. | |
23 registrar("replacement", "replacement"); | |
24 | |
25 registrar("csiso2022kr", "replacement"); | |
26 registrar("hz-gb-2312", "replacement"); | |
27 registrar("iso-2022-cn", "replacement"); | |
28 registrar("iso-2022-cn-ext", "replacement"); | |
29 registrar("iso-2022-kr", "replacement"); | |
30 } | |
31 | |
32 static std::unique_ptr<TextCodec> newStreamingTextDecoderReplacement( | |
33 const TextEncoding&, | |
34 const void*) { | |
35 return WTF::wrapUnique(new TextCodecReplacement); | |
36 } | |
37 | |
38 void TextCodecReplacement::registerCodecs(TextCodecRegistrar registrar) { | |
39 registrar("replacement", newStreamingTextDecoderReplacement, 0); | |
40 } | |
41 | |
42 String TextCodecReplacement::decode(const char*, | |
43 size_t length, | |
44 FlushBehavior, | |
45 bool, | |
46 bool& sawError) { | |
47 // https://encoding.spec.whatwg.org/#replacement-decoder | |
48 | |
49 // 1. If byte is end-of-stream, return finished. | |
50 if (!length) | |
51 return String(); | |
52 | |
53 // 2. If replacement error returned flag is unset, set the replacement | |
54 // error returned flag and return error. | |
55 if (!m_replacementErrorReturned) { | |
56 m_replacementErrorReturned = true; | |
57 sawError = true; | |
58 return String(&replacementCharacter, 1); | |
59 } | |
60 | |
61 // 3. Return finished. | |
62 return String(); | |
63 } | |
64 | |
65 } // namespace WTF | |
OLD | NEW |