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

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

Issue 232513003: Encoding API: Add ignoreBOM flag to decoder options (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebased Created 6 years, 8 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
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 WTF::TextEncoding encoding(encodingLabel); 45 WTF::TextEncoding encoding(encodingLabel);
46 if (!encoding.isValid()) { 46 if (!encoding.isValid()) {
47 exceptionState.throwTypeError("The encoding label provided ('" + encodin gLabel + "') is invalid."); 47 exceptionState.throwTypeError("The encoding label provided ('" + encodin gLabel + "') is invalid.");
48 return 0; 48 return 0;
49 } 49 }
50 50
51 bool fatal = false; 51 bool fatal = false;
52 options.get("fatal", fatal); 52 options.get("fatal", fatal);
53 53
54 return new TextDecoder(encoding.name(), fatal); 54 bool ignoreBOM = false;
55 options.get("ignoreBOM", ignoreBOM);
56
57 return new TextDecoder(encoding.name(), fatal, ignoreBOM);
55 } 58 }
56 59
57 60
58 TextDecoder::TextDecoder(const String& encoding, bool fatal) 61 TextDecoder::TextDecoder(const String& encoding, bool fatal, bool ignoreBOM)
59 : m_encoding(encoding) 62 : m_encoding(encoding)
60 , m_codec(newTextCodec(m_encoding)) 63 , m_codec(newTextCodec(m_encoding))
61 , m_fatal(fatal) 64 , m_fatal(fatal)
65 , m_ignoreBOM(ignoreBOM)
62 , m_bomSeen(false) 66 , m_bomSeen(false)
63 { 67 {
64 } 68 }
65 69
66 TextDecoder::~TextDecoder() 70 TextDecoder::~TextDecoder()
67 { 71 {
68 } 72 }
69 73
70 String TextDecoder::encoding() const 74 String TextDecoder::encoding() const
71 { 75 {
(...skipping 16 matching lines...) Expand all
88 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; 92 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF;
89 93
90 bool sawError = false; 94 bool sawError = false;
91 String s = m_codec->decode(start, length, flush, m_fatal, sawError); 95 String s = m_codec->decode(start, length, flush, m_fatal, sawError);
92 96
93 if (m_fatal && sawError) { 97 if (m_fatal && sawError) {
94 exceptionState.throwDOMException(EncodingError, "The encoded data was no t valid."); 98 exceptionState.throwDOMException(EncodingError, "The encoded data was no t valid.");
95 return String(); 99 return String();
96 } 100 }
97 101
98 if (!m_bomSeen && !s.isEmpty()) { 102 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) {
99 m_bomSeen = true; 103 m_bomSeen = true;
100 String name(m_encoding.name()); 104 String name(m_encoding.name());
101 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0 ] == 0xFEFF) 105 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0 ] == 0xFEFF)
102 s.remove(0); 106 s.remove(0);
103 } 107 }
104 108
105 if (flush) 109 if (flush)
106 m_bomSeen = false; 110 m_bomSeen = false;
107 111
108 return s; 112 return s;
109 } 113 }
110 114
111 } // namespace WebCore 115 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698