#ifndef WebVTTParser_h
#define WebVTTParser_h
#if ENABLE(VIDEO_TRACK)
#include "Document.h"
#include "DocumentFragment.h"
#include "HTMLNames.h"
#include "TextTrackCue.h"
#include "WebVTTTokenizer.h"
#include <wtf/PassOwnPtr.h>
#include <wtf/text/StringBuilder.h>
namespace WebCore {
using namespace HTMLNames;
class WebVTTParserClient {
public:
virtual ~WebVTTParserClient() { }
virtual void newCuesParsed() = 0;
virtual void fileFailedToParse() = 0;
};
class WebVTTParser {
public:
virtual ~WebVTTParser() { }
enum ParseState { Initial, Header, Id, TimingsAndSettings, CueText, BadCue };
static PassOwnPtr<WebVTTParser> create(WebVTTParserClient* client, ScriptExecutionContext* context)
{
return adoptPtr(new WebVTTParser(client, context));
}
static inline bool isRecognizedTag(const AtomicString& tagName)
{
return tagName == iTag
|| tagName == bTag
|| tagName == uTag
|| tagName == rubyTag
|| tagName == rtTag;
}
static inline bool isASpace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
}
static inline bool isValidSettingDelimiter(char c)
{
return c == ' ' || c == '\t';
}
static String collectDigits(const String&, unsigned*);
static String collectWord(const String&, unsigned*);
void parseBytes(const char* data, unsigned length);
void getNewCues(Vector<RefPtr<TextTrackCue> >&);
PassRefPtr<DocumentFragment> createDocumentFragmentFromCueText(const String&);
protected:
WebVTTParser(WebVTTParserClient*, ScriptExecutionContext*);
ScriptExecutionContext* m_scriptExecutionContext;
ParseState m_state;
private:
bool hasRequiredFileIdentifier();
ParseState collectCueId(const String&);
ParseState collectTimingsAndSettings(const String&);
ParseState collectCueText(const String&, unsigned length, unsigned);
ParseState ignoreBadCue(const String&);
void createNewCue();
void resetCueValues();
double collectTimeStamp(const String&, unsigned*);
void skipWhiteSpace(const String&, unsigned*);
static void skipLineTerminator(const char* data, unsigned length, unsigned*);
static String collectNextLine(const char* data, unsigned length, unsigned*);
void constructTreeFromToken(Document*);
Vector<char> m_identifierData;
String m_currentId;
double m_currentStartTime;
double m_currentEndTime;
StringBuilder m_currentContent;
String m_currentSettings;
WebVTTToken m_token;
OwnPtr<WebVTTTokenizer> m_tokenizer;
RefPtr<ContainerNode> m_currentNode;
WebVTTParserClient* m_client;
Vector<RefPtr<TextTrackCue> > m_cuelist;
};
}
#endif
#endif