OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 128 |
129 void WebVTTParser::getNewRegions(Vector<RefPtr<VTTRegion> >& outputRegions) | 129 void WebVTTParser::getNewRegions(Vector<RefPtr<VTTRegion> >& outputRegions) |
130 { | 130 { |
131 outputRegions = m_regionList; | 131 outputRegions = m_regionList; |
132 m_regionList.clear(); | 132 m_regionList.clear(); |
133 } | 133 } |
134 | 134 |
135 void WebVTTParser::parseBytes(const char* data, unsigned length) | 135 void WebVTTParser::parseBytes(const char* data, unsigned length) |
136 { | 136 { |
137 String textData = m_decoder->decode(data, length); | 137 String textData = m_decoder->decode(data, length); |
| 138 parse(textData); |
| 139 } |
138 | 140 |
| 141 void WebVTTParser::flush() |
| 142 { |
| 143 String textData = m_decoder->flush(); |
| 144 parse(textData); |
| 145 flushPendingCue(); |
| 146 } |
| 147 |
| 148 void WebVTTParser::parse(const String& textData) |
| 149 { |
139 // 4.8.10.13.3 WHATWG WebVTT Parser algorithm. | 150 // 4.8.10.13.3 WHATWG WebVTT Parser algorithm. |
140 // 1-3 - Initial setup. | 151 // 1-3 - Initial setup. |
141 unsigned position = 0; | 152 unsigned position = 0; |
142 | 153 |
143 while (position < textData.length()) { | 154 while (position < textData.length()) { |
144 String line = collectNextLine(textData, &position); | 155 String line = collectNextLine(textData, &position); |
145 | 156 |
146 switch (m_state) { | 157 switch (m_state) { |
147 case Initial: | 158 case Initial: |
148 // 4-12 - Check for a valid WebVTT signature. | 159 // 4-12 - Check for a valid WebVTT signature. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 m_state = Id; | 195 m_state = Id; |
185 break; | 196 break; |
186 } | 197 } |
187 | 198 |
188 // 40 - Collect cue timings and settings. | 199 // 40 - Collect cue timings and settings. |
189 m_state = collectTimingsAndSettings(line); | 200 m_state = collectTimingsAndSettings(line); |
190 break; | 201 break; |
191 | 202 |
192 case CueText: | 203 case CueText: |
193 // 41-53 - Collect the cue text, create a cue, and add it to the out
put. | 204 // 41-53 - Collect the cue text, create a cue, and add it to the out
put. |
194 m_state = collectCueText(line, position >= textData.length()); | 205 m_state = collectCueText(line); |
195 break; | 206 break; |
196 | 207 |
197 case BadCue: | 208 case BadCue: |
198 // 54-62 - Collect and discard the remaining cue. | 209 // 54-62 - Collect and discard the remaining cue. |
199 m_state = ignoreBadCue(line); | 210 m_state = ignoreBadCue(line); |
200 break; | 211 break; |
201 } | 212 } |
202 } | 213 } |
203 } | 214 } |
204 | 215 |
| 216 void WebVTTParser::flushPendingCue() |
| 217 { |
| 218 // If we're in the CueText state when we run out of data, we emit the pendin
g cue. |
| 219 if (m_state == CueText) |
| 220 createNewCue(); |
| 221 } |
| 222 |
205 bool WebVTTParser::hasRequiredFileIdentifier(const String& line) | 223 bool WebVTTParser::hasRequiredFileIdentifier(const String& line) |
206 { | 224 { |
207 // A WebVTT file identifier consists of an optional BOM character, | 225 // A WebVTT file identifier consists of an optional BOM character, |
208 // the string "WEBVTT" followed by an optional space or tab character, | 226 // the string "WEBVTT" followed by an optional space or tab character, |
209 // and any number of characters that are not line terminators ... | 227 // and any number of characters that are not line terminators ... |
210 if (!line.startsWith("WEBVTT", fileIdentifierLength)) | 228 if (!line.startsWith("WEBVTT", fileIdentifierLength)) |
211 return false; | 229 return false; |
212 if (line.length() > fileIdentifierLength && !isASpace(line[fileIdentifierLen
gth])) | 230 if (line.length() > fileIdentifierLength && !isASpace(line[fileIdentifierLen
gth])) |
213 return false; | 231 return false; |
214 | 232 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 m_currentEndTime = collectTimeStamp(line, &position); | 292 m_currentEndTime = collectTimeStamp(line, &position); |
275 if (m_currentEndTime == malformedTime) | 293 if (m_currentEndTime == malformedTime) |
276 return BadCue; | 294 return BadCue; |
277 skipWhiteSpace(line, &position); | 295 skipWhiteSpace(line, &position); |
278 | 296 |
279 // 12 - Parse the WebVTT settings for the cue (conducted in TextTrackCue). | 297 // 12 - Parse the WebVTT settings for the cue (conducted in TextTrackCue). |
280 m_currentSettings = line.substring(position, line.length()-1); | 298 m_currentSettings = line.substring(position, line.length()-1); |
281 return CueText; | 299 return CueText; |
282 } | 300 } |
283 | 301 |
284 WebVTTParser::ParseState WebVTTParser::collectCueText(const String& line, bool i
sAtEnd) | 302 WebVTTParser::ParseState WebVTTParser::collectCueText(const String& line) |
285 { | 303 { |
286 if (line.isEmpty()) { | 304 if (line.isEmpty()) { |
287 createNewCue(); | 305 createNewCue(); |
288 return Id; | 306 return Id; |
289 } | 307 } |
290 if (!m_currentContent.isEmpty()) | 308 if (!m_currentContent.isEmpty()) |
291 m_currentContent.append("\n"); | 309 m_currentContent.append("\n"); |
292 m_currentContent.append(line); | 310 m_currentContent.append(line); |
293 | 311 |
294 if (isAtEnd) | |
295 createNewCue(); | |
296 | |
297 return CueText; | 312 return CueText; |
298 } | 313 } |
299 | 314 |
300 WebVTTParser::ParseState WebVTTParser::ignoreBadCue(const String& line) | 315 WebVTTParser::ParseState WebVTTParser::ignoreBadCue(const String& line) |
301 { | 316 { |
302 if (!line.isEmpty()) | 317 if (!line.isEmpty()) |
303 return BadCue; | 318 return BadCue; |
304 return Id; | 319 return Id; |
305 } | 320 } |
306 | 321 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 unsigned oldPosition = *position; | 581 unsigned oldPosition = *position; |
567 while (*position < data.length() && data[*position] != '\r' && data[*positio
n] != '\n') | 582 while (*position < data.length() && data[*position] != '\r' && data[*positio
n] != '\n') |
568 (*position)++; | 583 (*position)++; |
569 String line = data.substring(oldPosition, *position - oldPosition); | 584 String line = data.substring(oldPosition, *position - oldPosition); |
570 skipLineTerminator(data, position); | 585 skipLineTerminator(data, position); |
571 return line; | 586 return line; |
572 } | 587 } |
573 | 588 |
574 } | 589 } |
575 | 590 |
OLD | NEW |