void WebVTTParser::parseBytes(const char* data, unsigned length) { // 4.8.10.13.3 WHATWG WebVTT Parser algorithm. // 1-3 - Initial setup. unsigned position = 0; while (position < length) { String line = collectNextLine(data, length, &position); switch (m_state) { case Initial: // Buffer up at least 9 bytes before proceeding with checking for the file identifier. m_identifierData.append(data, length); if (m_identifierData.size() < bomLength + fileIdentifierLength) return; // 4-12 - Collect the first line and check for "WEBVTT". if (!hasRequiredFileIdentifier()) { if (m_client) m_client->fileFailedToParse(); return; } m_state = Header; m_identifierData.clear(); break; case Header: // 13-18 - Allow a header (comment area) under the WEBVTT line. if (line.isEmpty()) m_state = Id; break; case Id: // 19-29 - Allow any number of line terminators, then initialize new cue values. if (line.isEmpty()) break; resetCueValues(); // 30-39 - Check if this line contains an optional identifier or timing data. m_state = collectCueId(line); break; case TimingsAndSettings: // 40 - Collect cue timings and settings. m_state = collectTimingsAndSettings(line); break; case CueText: // 41-53 - Collect the cue text, create a cue, and add it to the output. m_state = collectCueText(line, length, position); break; case BadCue: // 54-62 - Collect and discard the remaining cue. m_state = ignoreBadCue(line); break; } } }
bool WebVTTParser::hasRequiredFileIdentifier() { // A WebVTT file identifier consists of an optional BOM character, // the string "WEBVTT" followed by an optional space or tab character, // and any number of characters that are not line terminators ... unsigned position = 0; if (m_identifierData.size() >= bomLength && m_identifierData[0] == '\xEF' && m_identifierData[1] == '\xBB' && m_identifierData[2] == '\xBF') position += bomLength; String line = collectNextLine(m_identifierData.data(), m_identifierData.size(), &position); if (line.length() < fileIdentifierLength) return false; if (line.substring(0, fileIdentifierLength) != "WEBVTT") return false; if (line.length() > fileIdentifierLength && line[fileIdentifierLength] != ' ' && line[fileIdentifierLength] != '\t') return false; return true; }
bool WebVTTParser::hasRequiredFileIdentifier(const char* data, unsigned length) { // A WebVTT file identifier consists of an optional BOM character, // the string "WEBVTT" followed by an optional space or tab character, // and any number of characters that are not line terminators ... unsigned position = 0; if (length >= bomLength && data[0] == '\xEF' && data[1] == '\xBB' && data[2] == '\xBF') position += bomLength; String line = collectNextLine(data, length, &position); if (line.length() < fileIdentiferLength) return false; if (line.length() == fileIdentiferLength && line != "WEBVTT") return false; if (!hasLongWebVTTIdentifier(line)) return false; return true; }