Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: Source/core/html/track/WebVTTParser.cpp

Issue 52713005: Decouple tokenizer/tree builder state from WebVTTParser (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/html/track/WebVTTParser.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 isValidSetting = isFirstValueValid && isSecondValueValid; 109 isValidSetting = isFirstValueValid && isSecondValueValid;
110 return FloatPoint(firstCoord, secondCoord); 110 return FloatPoint(firstCoord, secondCoord);
111 } 111 }
112 112
113 WebVTTParser::WebVTTParser(WebVTTParserClient* client, Document& document) 113 WebVTTParser::WebVTTParser(WebVTTParserClient* client, Document& document)
114 : m_document(&document) 114 : m_document(&document)
115 , m_state(Initial) 115 , m_state(Initial)
116 , m_decoder(TextResourceDecoder::create("text/plain", UTF8Encoding())) 116 , m_decoder(TextResourceDecoder::create("text/plain", UTF8Encoding()))
117 , m_currentStartTime(0) 117 , m_currentStartTime(0)
118 , m_currentEndTime(0) 118 , m_currentEndTime(0)
119 , m_tokenizer(WebVTTTokenizer::create())
120 , m_client(client) 119 , m_client(client)
121 { 120 {
122 } 121 }
123 122
124 void WebVTTParser::getNewCues(Vector<RefPtr<TextTrackCue> >& outputCues) 123 void WebVTTParser::getNewCues(Vector<RefPtr<TextTrackCue> >& outputCues)
125 { 124 {
126 outputCues = m_cuelist; 125 outputCues = m_cuelist;
127 m_cuelist.clear(); 126 m_cuelist.clear();
128 } 127 }
129 128
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 return CueText; 292 return CueText;
294 } 293 }
295 294
296 WebVTTParser::ParseState WebVTTParser::ignoreBadCue(const String& line) 295 WebVTTParser::ParseState WebVTTParser::ignoreBadCue(const String& line)
297 { 296 {
298 if (!line.isEmpty()) 297 if (!line.isEmpty())
299 return BadCue; 298 return BadCue;
300 return Id; 299 return Id;
301 } 300 }
302 301
303 PassRefPtr<DocumentFragment> WebVTTParser::createDocumentFragmentFromCueText(co nst String& text) 302 // A helper class for the construction of a "cue fragment" from the cue text.
303 class WebVTTTreeBuilder {
304 public:
305 WebVTTTreeBuilder(Document& document)
306 : m_document(document) { }
307
308 PassRefPtr<DocumentFragment> buildFromString(const String& cueText);
309
310 private:
311 void constructTreeFromToken(Document&);
312
313 WebVTTToken m_token;
314 RefPtr<ContainerNode> m_currentNode;
315 Vector<AtomicString> m_languageStack;
316 Document& m_document;
317 };
318
319 PassRefPtr<DocumentFragment> WebVTTTreeBuilder::buildFromString(const String& cu eText)
304 { 320 {
305 // Cue text processing based on 321 // Cue text processing based on
306 // 4.8.10.13.4 WebVTT cue text parsing rules and 322 // 4.8.10.13.4 WebVTT cue text parsing rules and
307 // 4.8.10.13.5 WebVTT cue text DOM construction rules. 323 // 4.8.10.13.5 WebVTT cue text DOM construction rules.
308 324
309 RefPtr<DocumentFragment> fragment = DocumentFragment::create(*m_document); 325 RefPtr<DocumentFragment> fragment = DocumentFragment::create(m_document);
310 326
311 if (!text.length()) { 327 if (cueText.isEmpty()) {
312 fragment->parserAppendChild(Text::create(*m_document, "")); 328 fragment->parserAppendChild(Text::create(m_document, ""));
313 return fragment; 329 return fragment;
314 } 330 }
315 331
316 m_currentNode = fragment; 332 m_currentNode = fragment;
317 m_tokenizer->reset(); 333
334 OwnPtr<WebVTTTokenizer> tokenizer(WebVTTTokenizer::create());
318 m_token.clear(); 335 m_token.clear();
336 m_languageStack.clear();
319 337
320 m_languageStack.clear(); 338 SegmentedString content(cueText);
321 SegmentedString content(text); 339 while (tokenizer->nextToken(content, m_token))
322 while (m_tokenizer->nextToken(content, m_token)) 340 constructTreeFromToken(m_document);
323 constructTreeFromToken(*m_document);
324 341
325 return fragment.release(); 342 return fragment.release();
326 } 343 }
327 344
345 PassRefPtr<DocumentFragment> WebVTTParser::createDocumentFragmentFromCueText(Doc ument& document, const String& cueText)
346 {
347 WebVTTTreeBuilder treeBuilder(document);
348 return treeBuilder.buildFromString(cueText);
349 }
350
328 void WebVTTParser::createNewCue() 351 void WebVTTParser::createNewCue()
329 { 352 {
330 if (!m_currentContent.length()) 353 if (!m_currentContent.length())
331 return; 354 return;
332 355
333 RefPtr<TextTrackCue> cue = TextTrackCue::create(*m_document, m_currentStartT ime, m_currentEndTime, m_currentContent.toString()); 356 RefPtr<TextTrackCue> cue = TextTrackCue::create(*m_document, m_currentStartT ime, m_currentEndTime, m_currentContent.toString());
334 cue->setId(m_currentId); 357 cue->setId(m_currentId);
335 cue->setCueSettings(m_currentSettings); 358 cue->setCueSettings(m_currentSettings);
336 359
337 m_cuelist.append(cue); 360 m_cuelist.append(cue);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 case 4: 472 case 4:
450 if (token.name()[0] == 'r' && token.name()[1] == 'u' && token.name()[2] == 'b' && token.name()[3] == 'y') 473 if (token.name()[0] == 'r' && token.name()[1] == 'u' && token.name()[2] == 'b' && token.name()[3] == 'y')
451 return WebVTTNodeTypeRuby; 474 return WebVTTNodeTypeRuby;
452 if (token.name()[0] == 'l' && token.name()[1] == 'a' && token.name()[2] == 'n' && token.name()[3] == 'g') 475 if (token.name()[0] == 'l' && token.name()[1] == 'a' && token.name()[2] == 'n' && token.name()[3] == 'g')
453 return WebVTTNodeTypeLanguage; 476 return WebVTTNodeTypeLanguage;
454 break; 477 break;
455 } 478 }
456 return WebVTTNodeTypeNone; 479 return WebVTTNodeTypeNone;
457 } 480 }
458 481
459 void WebVTTParser::constructTreeFromToken(Document& document) 482 void WebVTTTreeBuilder::constructTreeFromToken(Document& document)
460 { 483 {
461 QualifiedName tagName(nullAtom, AtomicString(m_token.name()), xhtmlNamespace URI); 484 QualifiedName tagName(nullAtom, AtomicString(m_token.name()), xhtmlNamespace URI);
462 485
463 // http://dev.w3.org/html5/webvtt/#webvtt-cue-text-dom-construction-rules 486 // http://dev.w3.org/html5/webvtt/#webvtt-cue-text-dom-construction-rules
464 487
465 switch (m_token.type()) { 488 switch (m_token.type()) {
466 case WebVTTTokenTypes::Character: { 489 case WebVTTTokenTypes::Character: {
467 String content(m_token.characters()); // FIXME: This should be 8bit if p ossible. 490 String content(m_token.characters()); // FIXME: This should be 8bit if p ossible.
468 RefPtr<Text> child = Text::create(document, content); 491 RefPtr<Text> child = Text::create(document, content);
469 m_currentNode->parserAppendChild(child); 492 m_currentNode->parserAppendChild(child);
(...skipping 27 matching lines...) Expand all
497 if (nodeType == WebVTTNodeTypeLanguage && m_currentNode->isWebVTTEle ment() && toWebVTTElement(m_currentNode.get())->webVTTNodeType() == WebVTTNodeTy peLanguage) 520 if (nodeType == WebVTTNodeTypeLanguage && m_currentNode->isWebVTTEle ment() && toWebVTTElement(m_currentNode.get())->webVTTNodeType() == WebVTTNodeTy peLanguage)
498 m_languageStack.removeLast(); 521 m_languageStack.removeLast();
499 if (m_currentNode->parentNode()) 522 if (m_currentNode->parentNode())
500 m_currentNode = m_currentNode->parentNode(); 523 m_currentNode = m_currentNode->parentNode();
501 } 524 }
502 break; 525 break;
503 } 526 }
504 case WebVTTTokenTypes::TimestampTag: { 527 case WebVTTTokenTypes::TimestampTag: {
505 unsigned position = 0; 528 unsigned position = 0;
506 String charactersString(StringImpl::create8BitIfPossible(m_token.charact ers())); 529 String charactersString(StringImpl::create8BitIfPossible(m_token.charact ers()));
507 double time = collectTimeStamp(charactersString, &position); 530 double time = WebVTTParser::collectTimeStamp(charactersString, &position );
508 if (time != malformedTime) 531 if (time != malformedTime)
509 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString)); 532 m_currentNode->parserAppendChild(ProcessingInstruction::create(docum ent, "timestamp", charactersString));
510 break; 533 break;
511 } 534 }
512 default: 535 default:
513 break; 536 break;
514 } 537 }
515 m_token.clear(); 538 m_token.clear();
516 } 539 }
517 540
(...skipping 20 matching lines...) Expand all
538 unsigned oldPosition = *position; 561 unsigned oldPosition = *position;
539 while (*position < data.length() && data[*position] != '\r' && data[*positio n] != '\n') 562 while (*position < data.length() && data[*position] != '\r' && data[*positio n] != '\n')
540 (*position)++; 563 (*position)++;
541 String line = data.substring(oldPosition, *position - oldPosition); 564 String line = data.substring(oldPosition, *position - oldPosition);
542 skipLineTerminator(data, position); 565 skipLineTerminator(data, position);
543 return line; 566 return line;
544 } 567 }
545 568
546 } 569 }
547 570
OLDNEW
« no previous file with comments | « Source/core/html/track/WebVTTParser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698