1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef WebVTTParser_h 32 #define WebVTTParser_h 33 34 #include "HTMLNames.h" 35 #include "core/dom/DocumentFragment.h" 36 #include "core/html/track/TextTrackCue.h" 37 #include "core/html/track/TextTrackRegion.h" 38 #include "core/html/track/WebVTTTokenizer.h" 39 #include "wtf/PassOwnPtr.h" 40 #include "wtf/text/StringBuilder.h" 41 42 namespace WebCore { 43 44 using namespace HTMLNames; 45 46 class Document; 47 48 class WebVTTParserClient { 49 public: 50 virtual ~WebVTTParserClient() { } 51 52 virtual void newCuesParsed() = 0; 53 #if ENABLE(WEBVTT_REGIONS) 54 virtual void newRegionsParsed() = 0; 55 #endif 56 virtual void fileFailedToParse() = 0; 57 }; 58 59 class WebVTTParser { 60 public: 61 virtual ~WebVTTParser() { } 62 63 enum ParseState { 64 Initial, 65 Header, 66 #if ENABLE(WEBVTT_REGIONS) 67 Metadata, 68 #endif 69 Id, 70 TimingsAndSettings, 71 CueText, 72 BadCue 73 }; 74 75 static PassOwnPtr<WebVTTParser> create(WebVTTParserClient* client, ScriptExecutionContext* context) 76 { 77 return adoptPtr(new WebVTTParser(client, context)); 78 } 79 80 static inline bool isRecognizedTag(const AtomicString& tagName) 81 { 82 return tagName == iTag 83 || tagName == bTag 84 || tagName == uTag 85 || tagName == rubyTag 86 || tagName == rtTag; 87 } 88 89 static inline bool isASpace(char c) 90 { 91 // WebVTT space characters are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF), U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR). 92 return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'; 93 } 94 static inline bool isValidSettingDelimiter(char c) 95 { 96 // ... a WebVTT cue consists of zero or more of the following components, in any order, separated from each other by one or more 97 // U+0020 SPACE characters or U+0009 CHARACTER TABULATION (tab) characters. 98 return c == ' ' || c == '\t'; 99 } 100 static String collectDigits(const String&, unsigned*); 101 static String collectWord(const String&, unsigned*); 102 103 #if ENABLE(WEBVTT_REGIONS) 104 // Useful functions for parsing percentage settings. 105 static float parseFloatPercentageValue(const String&, bool&); 106 static FloatPoint parseFloatPercentageValuePair(const String&, char, bool&); 107 #endif 108 109 // Input data to the parser to parse. 110 void parseBytes(const char* data, unsigned length); 111 112 // Transfers ownership of last parsed cues to caller. 113 void getNewCues(Vector<RefPtr<TextTrackCue> >&); 114 #if ENABLE(WEBVTT_REGIONS) 115 void getNewRegions(Vector<RefPtr<TextTrackRegion> >&); 116 #endif 117 118 PassRefPtr<DocumentFragment> createDocumentFragmentFromCueText(const String&); 119 double collectTimeStamp(const String&, unsigned*); 120 121 protected: 122 WebVTTParser(WebVTTParserClient*, ScriptExecutionContext*); 123 124 ScriptExecutionContext* m_scriptExecutionContext; 125 ParseState m_state; 126 127 private: 128 bool hasRequiredFileIdentifier(); 129 ParseState collectCueId(const String&); 130 ParseState collectTimingsAndSettings(const String&); 131 ParseState collectCueText(const String&, unsigned length, unsigned); 132 ParseState ignoreBadCue(const String&); 133 134 void createNewCue(); 135 void resetCueValues(); 136 137 #if ENABLE(WEBVTT_REGIONS) 138 void collectHeader(const String&); 139 void createNewRegion(); 140 #endif 141 142 void skipWhiteSpace(const String&, unsigned*); 143 static void skipLineTerminator(const char* data, unsigned length, unsigned*); 144 static String collectNextLine(const char* data, unsigned length, unsigned*); 145 146 void constructTreeFromToken(Document*); 147 148 String m_currentHeaderName; 149 String m_currentHeaderValue; 150 151 Vector<char> m_identifierData; 152 String m_currentId; 153 double m_currentStartTime; 154 double m_currentEndTime; 155 StringBuilder m_currentContent; 156 String m_currentSettings; 157 158 WebVTTToken m_token; 159 OwnPtr<WebVTTTokenizer> m_tokenizer; 160 161 RefPtr<ContainerNode> m_currentNode; 162 163 WebVTTParserClient* m_client; 164 165 Vector<AtomicString> m_languageStack; 166 Vector<RefPtr<TextTrackCue> > m_cuelist; 167 168 #if ENABLE(WEBVTT_REGIONS) 169 Vector<RefPtr<TextTrackRegion> > m_regionList; 170 #endif 171 }; 172 173 } // namespace WebCore 174 175 #endif 176