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

Side by Side Diff: Source/modules/encoding/TextDecoder.cpp

Issue 534133002: [WIP] bindings: Introduce PropertyBag (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 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
« no previous file with comments | « Source/modules/crypto/SubtleCrypto.cpp ('k') | Source/modules/filesystem/FileSystemFlags.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) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 { 43 {
44 WTF::TextEncoding encoding(label); 44 WTF::TextEncoding encoding(label);
45 // The replacement encoding is not valid, but the Encoding API also 45 // The replacement encoding is not valid, but the Encoding API also
46 // rejects aliases of the replacement encoding. 46 // rejects aliases of the replacement encoding.
47 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { 47 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) {
48 exceptionState.throwTypeError("The encoding label provided ('" + label + "') is invalid."); 48 exceptionState.throwTypeError("The encoding label provided ('" + label + "') is invalid.");
49 return 0; 49 return 0;
50 } 50 }
51 51
52 bool fatal = false; 52 bool fatal = false;
53 DictionaryHelper::get(options, "fatal", fatal); 53 options.get("fatal", fatal);
54 54
55 bool ignoreBOM = false; 55 bool ignoreBOM = false;
56 DictionaryHelper::get(options, "ignoreBOM", ignoreBOM); 56 options.get("ignoreBOM", ignoreBOM);
57 57
58 return new TextDecoder(encoding, fatal, ignoreBOM); 58 return new TextDecoder(encoding, fatal, ignoreBOM);
59 } 59 }
60 60
61 61
62 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign oreBOM) 62 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign oreBOM)
63 : m_encoding(encoding) 63 : m_encoding(encoding)
64 , m_codec(newTextCodec(encoding)) 64 , m_codec(newTextCodec(encoding))
65 , m_fatal(fatal) 65 , m_fatal(fatal)
66 , m_ignoreBOM(ignoreBOM) 66 , m_ignoreBOM(ignoreBOM)
(...skipping 11 matching lines...) Expand all
78 // Where possible, encoding aliases should be handled by changes to Chromium 's ICU or Blink's WTF. 78 // Where possible, encoding aliases should be handled by changes to Chromium 's ICU or Blink's WTF.
79 // The same codec is used, but WTF maintains a different name/identity for t hese. 79 // The same codec is used, but WTF maintains a different name/identity for t hese.
80 if (name == "iso-8859-1" || name == "us-ascii") 80 if (name == "iso-8859-1" || name == "us-ascii")
81 return "windows-1252"; 81 return "windows-1252";
82 return name; 82 return name;
83 } 83 }
84 84
85 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex ceptionState& exceptionState) 85 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex ceptionState& exceptionState)
86 { 86 {
87 bool stream = false; 87 bool stream = false;
88 DictionaryHelper::get(options, "stream", stream); 88 options.get("stream", stream);
89 89
90 const char* start = input ? static_cast<const char*>(input->baseAddress()) : 0; 90 const char* start = input ? static_cast<const char*>(input->baseAddress()) : 0;
91 size_t length = input ? input->byteLength() : 0; 91 size_t length = input ? input->byteLength() : 0;
92 92
93 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; 93 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF;
94 94
95 bool sawError = false; 95 bool sawError = false;
96 String s = m_codec->decode(start, length, flush, m_fatal, sawError); 96 String s = m_codec->decode(start, length, flush, m_fatal, sawError);
97 97
98 if (m_fatal && sawError) { 98 if (m_fatal && sawError) {
99 exceptionState.throwDOMException(EncodingError, "The encoded data was no t valid."); 99 exceptionState.throwDOMException(EncodingError, "The encoded data was no t valid.");
100 return String(); 100 return String();
101 } 101 }
102 102
103 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { 103 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) {
104 m_bomSeen = true; 104 m_bomSeen = true;
105 String name(m_encoding.name()); 105 String name(m_encoding.name());
106 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0 ] == 0xFEFF) 106 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0 ] == 0xFEFF)
107 s.remove(0); 107 s.remove(0);
108 } 108 }
109 109
110 if (flush) 110 if (flush)
111 m_bomSeen = false; 111 m_bomSeen = false;
112 112
113 return s; 113 return s;
114 } 114 }
115 115
116 } // namespace blink 116 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/crypto/SubtleCrypto.cpp ('k') | Source/modules/filesystem/FileSystemFlags.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698