#ifndef VTTParser_h
#define VTTParser_h
#include "HTMLNames.h"
#include "RuntimeEnabledFeatures.h"
#include "core/dom/DocumentFragment.h"
#include "core/html/parser/TextResourceDecoder.h"
#include "core/html/track/vtt/BufferedLineReader.h"
#include "core/html/track/vtt/VTTCue.h"
#include "core/html/track/vtt/VTTRegion.h"
#include "core/html/track/vtt/VTTTokenizer.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
class Document;
class VTTScanner;
class VTTParserClient {
public:
virtual ~VTTParserClient() { }
virtual void newCuesParsed() = 0;
virtual void newRegionsParsed() = 0;
virtual void fileFailedToParse() = 0;
};
class VTTParser FINAL {
public:
enum ParseState {
Initial,
Header,
Id,
TimingsAndSettings,
CueText,
BadCue
};
static PassOwnPtr<VTTParser> create(VTTParserClient* client, Document& document)
{
return adoptPtr(new VTTParser(client, document));
}
static inline bool isRecognizedTag(const AtomicString& tagName)
{
return tagName == HTMLNames::iTag
|| tagName == HTMLNames::bTag
|| tagName == HTMLNames::uTag
|| tagName == HTMLNames::rubyTag
|| tagName == HTMLNames::rtTag;
}
static inline bool isASpace(UChar c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
}
static inline bool isValidSettingDelimiter(UChar c)
{
return c == ' ' || c == '\t';
}
static bool collectTimeStamp(const String&, double& timeStamp);
static bool parseFloatPercentageValue(VTTScanner& valueScanner, float& percentage);
static bool parseFloatPercentageValuePair(VTTScanner&, char, FloatPoint&);
static PassRefPtr<DocumentFragment> createDocumentFragmentFromCueText(Document&, const String&);
void parseBytes(const char* data, unsigned length);
void flush();
void getNewCues(Vector<RefPtr<VTTCue> >&);
void getNewRegions(Vector<RefPtr<VTTRegion> >&);
private:
VTTParser(VTTParserClient*, Document&);
Document* m_document;
ParseState m_state;
void parse();
void flushPendingCue();
bool hasRequiredFileIdentifier(const String& line);
ParseState collectCueId(const String&);
ParseState collectTimingsAndSettings(const String&);
ParseState collectCueText(const String&);
ParseState recoverCue(const String&);
ParseState ignoreBadCue(const String&);
void createNewCue();
void resetCueValues();
void collectMetadataHeader(const String&);
void createNewRegion(const String& headerValue);
static bool collectTimeStamp(VTTScanner& input, double& timeStamp);
BufferedLineReader m_lineReader;
OwnPtr<TextResourceDecoder> m_decoder;
AtomicString m_currentId;
double m_currentStartTime;
double m_currentEndTime;
StringBuilder m_currentContent;
String m_currentSettings;
VTTParserClient* m_client;
Vector<RefPtr<VTTCue> > m_cuelist;
Vector<RefPtr<VTTRegion> > m_regionList;
};
}
#endif