2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if ENABLE(VIDEO_TRACK)
36 #include "TextTrackCue.h"
38 #include "CSSPropertyNames.h"
39 #include "CSSValueKeywords.h"
40 #include "DocumentFragment.h"
42 #include "HTMLDivElement.h"
43 #include "HTMLSpanElement.h"
45 #include "NodeTraversal.h"
46 #include "RenderTextTrackCue.h"
48 #include "TextTrack.h"
49 #include "TextTrackCueList.h"
50 #include "WebVTTElement.h"
51 #include "WebVTTParser.h"
52 #include <wtf/MathExtras.h>
53 #include <wtf/text/StringBuilder.h>
57 static const int invalidCueIndex = -1;
58 static const int undefinedPosition = -1;
59 static const int autoSize = 0;
61 static const String& startKeyword()
63 DEFINE_STATIC_LOCAL(const String, start, (ASCIILiteral("start")));
67 static const String& middleKeyword()
69 DEFINE_STATIC_LOCAL(const String, middle, (ASCIILiteral("middle")));
73 static const String& endKeyword()
75 DEFINE_STATIC_LOCAL(const String, end, (ASCIILiteral("end")));
79 static const String& horizontalKeyword()
84 static const String& verticalGrowingLeftKeyword()
86 DEFINE_STATIC_LOCAL(const String, verticalrl, (ASCIILiteral("rl")));
90 static const String& verticalGrowingRightKeyword()
92 DEFINE_STATIC_LOCAL(const String, verticallr, (ASCIILiteral("lr")));
96 // ----------------------------
98 TextTrackCueBox::TextTrackCueBox(Document* document, TextTrackCue* cue)
99 : HTMLElement(divTag, document)
102 setPseudo(textTrackCueBoxShadowPseudoId());
105 TextTrackCue* TextTrackCueBox::getCue() const
110 void TextTrackCueBox::applyCSSProperties(const IntSize&)
112 // FIXME: Apply all the initial CSS positioning properties. http://wkb.ug/79916
114 // 3.5.1 On the (root) List of WebVTT Node Objects:
116 // the 'position' property must be set to 'absolute'
117 setInlineStyleProperty(CSSPropertyPosition, CSSValueAbsolute);
119 // the 'unicode-bidi' property must be set to 'plaintext'
120 setInlineStyleProperty(CSSPropertyUnicodeBidi, CSSValueWebkitPlaintext);
122 // the 'direction' property must be set to direction
123 setInlineStyleProperty(CSSPropertyDirection, m_cue->getCSSWritingDirection());
125 // the 'writing-mode' property must be set to writing-mode
126 setInlineStyleProperty(CSSPropertyWebkitWritingMode, m_cue->getCSSWritingMode(), false);
128 std::pair<float, float> position = m_cue->getCSSPosition();
130 // the 'top' property must be set to top,
131 setInlineStyleProperty(CSSPropertyTop, static_cast<double>(position.second), CSSPrimitiveValue::CSS_PERCENTAGE);
133 // the 'left' property must be set to left
134 setInlineStyleProperty(CSSPropertyLeft, static_cast<double>(position.first), CSSPrimitiveValue::CSS_PERCENTAGE);
136 // the 'width' property must be set to width, and the 'height' property must be set to height
137 if (m_cue->vertical() == horizontalKeyword()) {
138 setInlineStyleProperty(CSSPropertyWidth, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
139 setInlineStyleProperty(CSSPropertyHeight, CSSValueAuto);
141 setInlineStyleProperty(CSSPropertyWidth, CSSValueAuto);
142 setInlineStyleProperty(CSSPropertyHeight, static_cast<double>(m_cue->getCSSSize()), CSSPrimitiveValue::CSS_PERCENTAGE);
145 // The 'text-align' property on the (root) List of WebVTT Node Objects must
146 // be set to the value in the second cell of the row of the table below
147 // whose first cell is the value of the corresponding cue's text track cue
149 if (m_cue->align() == startKeyword())
150 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueStart);
151 else if (m_cue->align() == endKeyword())
152 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueEnd);
154 setInlineStyleProperty(CSSPropertyTextAlign, CSSValueCenter);
156 if (!m_cue->snapToLines()) {
157 // 10.13.1 Set up x and y:
158 // Note: x and y are set through the CSS left and top above.
160 // 10.13.2 Position the boxes in boxes such that the point x% along the
161 // width of the bounding box of the boxes in boxes is x% of the way
162 // across the width of the video's rendering area, and the point y%
163 // along the height of the bounding box of the boxes in boxes is y%
164 // of the way across the height of the video's rendering area, while
165 // maintaining the relative positions of the boxes in boxes to each
167 setInlineStyleProperty(CSSPropertyWebkitTransform,
168 String::format("translate(-%.2f%%, -%.2f%%)", position.first, position.second));
170 setInlineStyleProperty(CSSPropertyWhiteSpace, CSSValuePre);
174 const AtomicString& TextTrackCueBox::textTrackCueBoxShadowPseudoId()
176 DEFINE_STATIC_LOCAL(const AtomicString, trackDisplayBoxShadowPseudoId, ("-webkit-media-text-track-display", AtomicString::ConstructFromLiteral));
177 return trackDisplayBoxShadowPseudoId;
180 RenderObject* TextTrackCueBox::createRenderer(RenderArena* arena, RenderStyle*)
182 return new (arena) RenderTextTrackCue(this);
185 // ----------------------------
187 TextTrackCue::TextTrackCue(ScriptExecutionContext* context, double start, double end, const String& content)
191 , m_linePosition(undefinedPosition)
192 , m_computedLinePosition(undefinedPosition)
195 , m_cueIndex(invalidCueIndex)
196 , m_processingCueChanges(0)
197 , m_writingDirection(Horizontal)
198 , m_cueAlignment(Middle)
199 , m_webVTTNodeTree(0)
201 , m_scriptExecutionContext(context)
203 , m_pauseOnExit(false)
204 , m_snapToLines(true)
205 , m_cueBackgroundBox(HTMLSpanElement::create(spanTag, toDocument(context)))
206 , m_displayTreeShouldChange(true)
207 , m_displayDirection(CSSValueLtr)
209 ASSERT(m_scriptExecutionContext->isDocument());
211 // 4. If the text track cue writing direction is horizontal, then let
212 // writing-mode be 'horizontal-tb'. Otherwise, if the text track cue writing
213 // direction is vertical growing left, then let writing-mode be
214 // 'vertical-rl'. Otherwise, the text track cue writing direction is
215 // vertical growing right; let writing-mode be 'vertical-lr'.
216 m_displayWritingModeMap[Horizontal] = CSSValueHorizontalTb;
217 m_displayWritingModeMap[VerticalGrowingLeft] = CSSValueVerticalRl;
218 m_displayWritingModeMap[VerticalGrowingRight] = CSSValueVerticalLr;
221 TextTrackCue::~TextTrackCue()
226 PassRefPtr<TextTrackCueBox> TextTrackCue::createDisplayTree()
228 return TextTrackCueBox::create(ownerDocument(), this);
231 TextTrackCueBox* TextTrackCue::displayTreeInternal()
234 m_displayTree = createDisplayTree();
235 return m_displayTree.get();
238 void TextTrackCue::willChange()
240 if (++m_processingCueChanges > 1)
244 m_track->cueWillChange(this);
247 void TextTrackCue::didChange()
249 ASSERT(m_processingCueChanges);
250 if (--m_processingCueChanges)
254 m_track->cueDidChange(this);
256 m_displayTreeShouldChange = true;
259 TextTrack* TextTrackCue::track() const
264 void TextTrackCue::setTrack(TextTrack* track)
269 void TextTrackCue::setId(const String& id)
279 void TextTrackCue::setStartTime(double value, ExceptionCode& ec)
281 // NaN, Infinity and -Infinity values should trigger a TypeError.
282 if (std::isinf(value) || std::isnan(value)) {
287 // TODO(93143): Add spec-compliant behavior for negative time values.
288 if (m_startTime == value || value < 0)
296 void TextTrackCue::setEndTime(double value, ExceptionCode& ec)
298 // NaN, Infinity and -Infinity values should trigger a TypeError.
299 if (std::isinf(value) || std::isnan(value)) {
304 // TODO(93143): Add spec-compliant behavior for negative time values.
305 if (m_endTime == value || value < 0)
313 void TextTrackCue::setPauseOnExit(bool value)
315 if (m_pauseOnExit == value)
318 m_pauseOnExit = value;
321 const String& TextTrackCue::vertical() const
323 switch (m_writingDirection) {
325 return horizontalKeyword();
326 case VerticalGrowingLeft:
327 return verticalGrowingLeftKeyword();
328 case VerticalGrowingRight:
329 return verticalGrowingRightKeyword();
331 ASSERT_NOT_REACHED();
332 return emptyString();
336 void TextTrackCue::setVertical(const String& value, ExceptionCode& ec)
338 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-vertical
339 // On setting, the text track cue writing direction must be set to the value given
340 // in the first cell of the row in the table above whose second cell is a
341 // case-sensitive match for the new value, if any. If none of the values match, then
342 // the user agent must instead throw a SyntaxError exception.
344 WritingDirection direction = m_writingDirection;
345 if (value == horizontalKeyword())
346 direction = Horizontal;
347 else if (value == verticalGrowingLeftKeyword())
348 direction = VerticalGrowingLeft;
349 else if (value == verticalGrowingRightKeyword())
350 direction = VerticalGrowingRight;
354 if (direction == m_writingDirection)
358 m_writingDirection = direction;
362 void TextTrackCue::setSnapToLines(bool value)
364 if (m_snapToLines == value)
368 m_snapToLines = value;
372 void TextTrackCue::setLine(int position, ExceptionCode& ec)
374 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-line
375 // On setting, if the text track cue snap-to-lines flag is not set, and the new
376 // value is negative or greater than 100, then throw an IndexSizeError exception.
377 if (!m_snapToLines && (position < 0 || position > 100)) {
382 // Otherwise, set the text track cue line position to the new value.
383 if (m_linePosition == position)
387 m_linePosition = position;
388 m_computedLinePosition = calculateComputedLinePosition();
392 void TextTrackCue::setPosition(int position, ExceptionCode& ec)
394 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-position
395 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError exception.
396 // Otherwise, set the text track cue text position to the new value.
397 if (position < 0 || position > 100) {
402 // Otherwise, set the text track cue line position to the new value.
403 if (m_textPosition == position)
407 m_textPosition = position;
411 void TextTrackCue::setSize(int size, ExceptionCode& ec)
413 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-size
414 // On setting, if the new value is negative or greater than 100, then throw an IndexSizeError
415 // exception. Otherwise, set the text track cue size to the new value.
416 if (size < 0 || size > 100) {
421 // Otherwise, set the text track cue line position to the new value.
422 if (m_cueSize == size)
430 const String& TextTrackCue::align() const
432 switch (m_cueAlignment) {
434 return startKeyword();
436 return middleKeyword();
440 ASSERT_NOT_REACHED();
441 return emptyString();
445 void TextTrackCue::setAlign(const String& value, ExceptionCode& ec)
447 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#dom-texttrackcue-align
448 // On setting, the text track cue alignment must be set to the value given in the
449 // first cell of the row in the table above whose second cell is a case-sensitive
450 // match for the new value, if any. If none of the values match, then the user
451 // agent must instead throw a SyntaxError exception.
453 CueAlignment alignment = m_cueAlignment;
454 if (value == startKeyword())
456 else if (value == middleKeyword())
458 else if (value == endKeyword())
463 if (alignment == m_cueAlignment)
467 m_cueAlignment = alignment;
471 void TextTrackCue::setText(const String& text)
473 if (m_content == text)
477 // Clear the document fragment but don't bother to create it again just yet as we can do that
478 // when it is requested.
479 m_webVTTNodeTree = 0;
484 int TextTrackCue::cueIndex()
486 if (m_cueIndex == invalidCueIndex) {
488 ASSERT(track()->cues());
489 if (TextTrackCueList* cueList = track()->cues())
490 m_cueIndex = cueList->getCueIndex(this);
496 void TextTrackCue::invalidateCueIndex()
498 m_cueIndex = invalidCueIndex;
501 void TextTrackCue::createWebVTTNodeTree()
503 if (!m_webVTTNodeTree)
504 m_webVTTNodeTree = WebVTTParser::create(0, m_scriptExecutionContext)->createDocumentFragmentFromCueText(m_content);
507 void TextTrackCue::copyWebVTTNodeToDOMTree(ContainerNode* webVTTNode, ContainerNode* parent)
509 for (Node* node = webVTTNode->firstChild(); node; node = node->nextSibling()) {
510 RefPtr<Node> clonedNode;
511 if (node->isWebVTTElement())
512 clonedNode = toWebVTTElement(node)->createEquivalentHTMLElement(ownerDocument());
514 clonedNode = node->cloneNode(false);
515 parent->appendChild(clonedNode, ASSERT_NO_EXCEPTION);
516 if (node->isContainerNode())
517 copyWebVTTNodeToDOMTree(toContainerNode(node), toContainerNode(clonedNode.get()));
521 PassRefPtr<DocumentFragment> TextTrackCue::getCueAsHTML()
523 createWebVTTNodeTree();
524 if (!m_webVTTNodeTree)
527 RefPtr<DocumentFragment> clonedFragment = DocumentFragment::create(ownerDocument());
528 copyWebVTTNodeToDOMTree(m_webVTTNodeTree.get(), clonedFragment.get());
529 return clonedFragment.release();
532 PassRefPtr<DocumentFragment> TextTrackCue::createCueRenderingTree()
534 RefPtr<DocumentFragment> clonedFragment;
535 createWebVTTNodeTree();
536 if (!m_webVTTNodeTree)
539 clonedFragment = DocumentFragment::create(ownerDocument());
540 m_webVTTNodeTree->cloneChildNodes(clonedFragment.get());
541 return clonedFragment.release();
544 bool TextTrackCue::dispatchEvent(PassRefPtr<Event> event)
546 // When a TextTrack's mode is disabled: no cues are active, no events fired.
547 if (!track() || track()->mode() == TextTrack::disabledKeyword())
550 return EventTarget::dispatchEvent(event);
553 #if ENABLE(WEBVTT_REGIONS)
554 void TextTrackCue::setRegionId(const String& regionId)
556 if (m_regionId == regionId)
560 m_regionId = regionId;
565 bool TextTrackCue::isActive()
567 return m_isActive && track() && track()->mode() != TextTrack::disabledKeyword();
570 void TextTrackCue::setIsActive(bool active)
575 // Remove the display tree as soon as the cue becomes inactive.
576 displayTreeInternal()->remove(ASSERT_NO_EXCEPTION);
580 int TextTrackCue::calculateComputedLinePosition()
582 // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-computed-line-position
584 // If the text track cue line position is numeric, then that is the text
585 // track cue computed line position.
586 if (m_linePosition != undefinedPosition)
587 return m_linePosition;
589 // If the text track cue snap-to-lines flag of the text track cue is not
590 // set, the text track cue computed line position is the value 100;
594 // Otherwise, it is the value returned by the following algorithm:
596 // If cue is not associated with a text track, return -1 and abort these
601 // Let n be the number of text tracks whose text track mode is showing or
602 // showing by default and that are in the media element's list of text
603 // tracks before track.
604 int n = track()->trackIndexRelativeToRenderedTracks();
606 // Increment n by one.
615 static bool isCueParagraphSeparator(UChar character)
617 // Within a cue, paragraph boundaries are only denoted by Type B characters,
618 // such as U+000A LINE FEED (LF), U+0085 NEXT LINE (NEL), and U+2029 PARAGRAPH SEPARATOR.
619 return WTF::Unicode::category(character) & WTF::Unicode::Separator_Paragraph;
622 void TextTrackCue::determineTextDirection()
624 DEFINE_STATIC_LOCAL(const String, rtTag, (ASCIILiteral("rt")));
625 createWebVTTNodeTree();
626 if (!m_webVTTNodeTree)
629 // Apply the Unicode Bidirectional Algorithm's Paragraph Level steps to the
630 // concatenation of the values of each WebVTT Text Object in nodes, in a
631 // pre-order, depth-first traversal, excluding WebVTT Ruby Text Objects and
632 // their descendants.
633 StringBuilder paragraphBuilder;
634 for (Node* node = m_webVTTNodeTree->firstChild(); node; node = NodeTraversal::next(node, m_webVTTNodeTree.get())) {
635 if (!node->isTextNode() || node->localName() == rtTag)
638 paragraphBuilder.append(node->nodeValue());
641 String paragraph = paragraphBuilder.toString();
642 if (!paragraph.length())
645 for (size_t i = 0; i < paragraph.length(); ++i) {
646 UChar current = paragraph[i];
647 if (!current || isCueParagraphSeparator(current))
650 if (UChar current = paragraph[i]) {
651 WTF::Unicode::Direction charDirection = WTF::Unicode::direction(current);
652 if (charDirection == WTF::Unicode::LeftToRight) {
653 m_displayDirection = CSSValueLtr;
656 if (charDirection == WTF::Unicode::RightToLeft
657 || charDirection == WTF::Unicode::RightToLeftArabic) {
658 m_displayDirection = CSSValueRtl;
665 void TextTrackCue::calculateDisplayParameters()
668 determineTextDirection();
670 // 10.4 If the text track cue writing direction is horizontal, then let
671 // block-flow be 'tb'. Otherwise, if the text track cue writing direction is
672 // vertical growing left, then let block-flow be 'lr'. Otherwise, the text
673 // track cue writing direction is vertical growing right; let block-flow be
675 m_displayWritingMode = m_displayWritingModeMap[m_writingDirection];
677 // 10.5 Determine the value of maximum size for cue as per the appropriate
678 // rules from the following list:
679 int maximumSize = m_textPosition;
680 if ((m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueLtr)
681 || (m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueRtl)
682 || (m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Start)
683 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Start)) {
684 maximumSize = 100 - m_textPosition;
685 } else if ((m_writingDirection == Horizontal && m_cueAlignment == End && m_displayDirection == CSSValueLtr)
686 || (m_writingDirection == Horizontal && m_cueAlignment == Start && m_displayDirection == CSSValueRtl)
687 || (m_writingDirection == VerticalGrowingLeft && m_cueAlignment == End)
688 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == End)) {
689 maximumSize = m_textPosition;
690 } else if (m_cueAlignment == Middle) {
691 maximumSize = m_textPosition <= 50 ? m_textPosition : (100 - m_textPosition);
692 maximumSize = maximumSize * 2;
695 // 10.6 If the text track cue size is less than maximum size, then let size
696 // be text track cue size. Otherwise, let size be maximum size.
697 m_displaySize = std::min(m_cueSize, maximumSize);
699 // 10.8 Determine the value of x-position or y-position for cue as per the
700 // appropriate rules from the following list:
701 if (m_writingDirection == Horizontal) {
702 if (m_cueAlignment == Start) {
703 if (m_displayDirection == CSSValueLtr)
704 m_displayPosition.first = m_textPosition;
706 m_displayPosition.first = 100 - m_textPosition - m_displaySize;
707 } else if (m_cueAlignment == End) {
708 if (m_displayDirection == CSSValueRtl)
709 m_displayPosition.first = 100 - m_textPosition;
711 m_displayPosition.first = m_textPosition - m_displaySize;
715 if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Start)
716 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Start)) {
717 m_displayPosition.second = m_textPosition;
718 } else if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == End)
719 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == End)) {
720 m_displayPosition.second = 100 - m_textPosition;
723 if (m_writingDirection == Horizontal && m_cueAlignment == Middle) {
724 if (m_displayDirection == CSSValueLtr)
725 m_displayPosition.first = m_textPosition - m_displaySize / 2;
727 m_displayPosition.first = 100 - m_textPosition - m_displaySize / 2;
730 if ((m_writingDirection == VerticalGrowingLeft && m_cueAlignment == Middle)
731 || (m_writingDirection == VerticalGrowingRight && m_cueAlignment == Middle))
732 m_displayPosition.second = m_textPosition - m_displaySize / 2;
734 // 10.9 Determine the value of whichever of x-position or y-position is not
735 // yet calculated for cue as per the appropriate rules from the following
737 if (m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
738 m_displayPosition.second = 0;
740 if (!m_snapToLines && m_displayPosition.second == undefinedPosition && m_writingDirection == Horizontal)
741 m_displayPosition.second = m_computedLinePosition;
743 if (m_snapToLines && m_displayPosition.first == undefinedPosition
744 && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
745 m_displayPosition.first = 0;
747 if (!m_snapToLines && (m_writingDirection == VerticalGrowingLeft || m_writingDirection == VerticalGrowingRight))
748 m_displayPosition.first = m_computedLinePosition;
750 // A text track cue has a text track cue computed line position whose value
751 // is defined in terms of the other aspects of the cue.
752 m_computedLinePosition = calculateComputedLinePosition();
755 void TextTrackCue::markFutureAndPastNodes(ContainerNode* root, double previousTimestamp, double movieTime)
757 DEFINE_STATIC_LOCAL(const String, timestampTag, (ASCIILiteral("timestamp")));
759 bool isPastNode = true;
760 double currentTimestamp = previousTimestamp;
761 if (currentTimestamp > movieTime)
764 for (Node* child = root->firstChild(); child; child = NodeTraversal::next(child, root)) {
765 if (child->nodeName() == timestampTag) {
766 unsigned position = 0;
767 String timestamp = child->nodeValue();
768 double currentTimestamp = WebVTTParser::create(0, m_scriptExecutionContext)->collectTimeStamp(timestamp, &position);
769 ASSERT(currentTimestamp != -1);
771 if (currentTimestamp > movieTime)
775 if (child->isWebVTTElement()) {
776 toWebVTTElement(child)->setIsPastNode(isPastNode);
777 // Make an elemenet id match a cue id for style matching purposes.
779 toElement(child)->setIdAttribute(AtomicString(m_id.characters(), m_id.length()));
784 void TextTrackCue::updateDisplayTree(double movieTime)
786 // The display tree may contain WebVTT timestamp objects representing
787 // timestamps (processing instructions), along with displayable nodes.
789 if (!track()->isRendered())
792 // Clear the contents of the set.
793 m_cueBackgroundBox->removeChildren();
795 // Update the two sets containing past and future WebVTT objects.
796 RefPtr<DocumentFragment> referenceTree = createCueRenderingTree();
800 markFutureAndPastNodes(referenceTree.get(), startTime(), movieTime);
801 m_cueBackgroundBox->appendChild(referenceTree);
804 TextTrackCueBox* TextTrackCue::getDisplayTree(const IntSize& videoSize)
806 RefPtr<TextTrackCueBox> displayTree = displayTreeInternal();
807 if (!m_displayTreeShouldChange || !track()->isRendered())
808 return displayTree.get();
811 calculateDisplayParameters();
813 // 10.11. Apply the terms of the CSS specifications to nodes within the
814 // following constraints, thus obtaining a set of CSS boxes positioned
815 // relative to an initial containing block:
816 displayTree->removeChildren();
818 // The document tree is the tree of WebVTT Node Objects rooted at nodes.
820 // The children of the nodes must be wrapped in an anonymous box whose
821 // 'display' property has the value 'inline'. This is the WebVTT cue
824 // Note: This is contained by default in m_cueBackgroundBox.
825 m_cueBackgroundBox->setPseudo(cueShadowPseudoId());
826 displayTree->appendChild(m_cueBackgroundBox, ASSERT_NO_EXCEPTION, AttachLazily);
828 // FIXME(BUG 79916): Runs of children of WebVTT Ruby Objects that are not
829 // WebVTT Ruby Text Objects must be wrapped in anonymous boxes whose
830 // 'display' property has the value 'ruby-base'.
832 // FIXME(BUG 79916): Text runs must be wrapped according to the CSS
833 // line-wrapping rules, except that additionally, regardless of the value of
834 // the 'white-space' property, lines must be wrapped at the edge of their
835 // containing blocks, even if doing so requires splitting a word where there
836 // is no line breaking opportunity. (Thus, normally text wraps as needed,
837 // but if there is a particularly long word, it does not overflow as it
838 // normally would in CSS, it is instead forcibly wrapped at the box's edge.)
839 displayTree->applyCSSProperties(videoSize);
841 m_displayTreeShouldChange = false;
843 // 10.15. Let cue's text track cue display state have the CSS boxes in
845 return displayTree.get();
848 void TextTrackCue::removeDisplayTree()
850 displayTreeInternal()->remove(ASSERT_NO_EXCEPTION);
853 std::pair<double, double> TextTrackCue::getPositionCoordinates() const
855 // This method is used for setting x and y when snap to lines is not set.
856 std::pair<double, double> coordinates;
858 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueLtr) {
859 coordinates.first = m_textPosition;
860 coordinates.second = m_computedLinePosition;
865 if (m_writingDirection == Horizontal && m_displayDirection == CSSValueRtl) {
866 coordinates.first = 100 - m_textPosition;
867 coordinates.second = m_computedLinePosition;
872 if (m_writingDirection == VerticalGrowingLeft) {
873 coordinates.first = 100 - m_computedLinePosition;
874 coordinates.second = m_textPosition;
879 if (m_writingDirection == VerticalGrowingRight) {
880 coordinates.first = m_computedLinePosition;
881 coordinates.second = m_textPosition;
886 ASSERT_NOT_REACHED();
891 TextTrackCue::CueSetting TextTrackCue::settingName(const String& name)
893 DEFINE_STATIC_LOCAL(const String, verticalKeyword, (ASCIILiteral("vertical")));
894 DEFINE_STATIC_LOCAL(const String, lineKeyword, (ASCIILiteral("line")));
895 DEFINE_STATIC_LOCAL(const String, positionKeyword, (ASCIILiteral("position")));
896 DEFINE_STATIC_LOCAL(const String, sizeKeyword, (ASCIILiteral("size")));
897 DEFINE_STATIC_LOCAL(const String, alignKeyword, (ASCIILiteral("align")));
898 #if ENABLE(WEBVTT_REGIONS)
899 DEFINE_STATIC_LOCAL(const String, regionIdKeyword, (ASCIILiteral("region")));
902 if (name == verticalKeyword)
904 else if (name == lineKeyword)
906 else if (name == positionKeyword)
908 else if (name == sizeKeyword)
910 else if (name == alignKeyword)
912 #if ENABLE(WEBVTT_REGIONS)
913 else if (name == regionIdKeyword)
920 void TextTrackCue::setCueSettings(const String& input)
923 unsigned position = 0;
925 while (position < input.length()) {
927 // The WebVTT cue settings part of a WebVTT cue consists of zero or more of the following components, in any order,
928 // separated from each other by one or more U+0020 SPACE characters or U+0009 CHARACTER TABULATION (tab) characters.
929 while (position < input.length() && WebVTTParser::isValidSettingDelimiter(input[position]))
931 if (position >= input.length())
934 // When the user agent is to parse the WebVTT settings given by a string input for a text track cue cue,
935 // the user agent must run the following steps:
936 // 1. Let settings be the result of splitting input on spaces.
937 // 2. For each token setting in the list settings, run the following substeps:
938 // 1. If setting does not contain a U+003A COLON character (:), or if the first U+003A COLON character (:)
939 // in setting is either the first or last character of setting, then jump to the step labeled next setting.
940 unsigned endOfSetting = position;
941 String setting = WebVTTParser::collectWord(input, &endOfSetting);
943 size_t colonOffset = setting.find(':', 1);
944 if (colonOffset == notFound || colonOffset == 0 || colonOffset == setting.length() - 1)
947 // 2. Let name be the leading substring of setting up to and excluding the first U+003A COLON character (:) in that string.
948 name = settingName(setting.substring(0, colonOffset));
950 // 3. Let value be the trailing substring of setting starting from the character immediately after the first U+003A COLON character (:) in that string.
951 position += colonOffset + 1;
952 if (position >= input.length())
955 // 4. Run the appropriate substeps that apply for the value of name, as follows:
959 // If name is a case-sensitive match for "vertical"
960 // 1. If value is a case-sensitive match for the string "rl", then let cue's text track cue writing direction
961 // be vertical growing left.
962 String writingDirection = WebVTTParser::collectWord(input, &position);
963 if (writingDirection == verticalGrowingLeftKeyword())
964 m_writingDirection = VerticalGrowingLeft;
966 // 2. Otherwise, if value is a case-sensitive match for the string "lr", then let cue's text track cue writing
967 // direction be vertical growing right.
968 else if (writingDirection == verticalGrowingRightKeyword())
969 m_writingDirection = VerticalGrowingRight;
974 // 1-2 - Collect chars that are either '-', '%', or a digit.
975 // 1. If value contains any characters other than U+002D HYPHEN-MINUS characters (-), U+0025 PERCENT SIGN
976 // characters (%), and characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump
977 // to the step labeled next setting.
978 StringBuilder linePositionBuilder;
979 while (position < input.length() && (input[position] == '-' || input[position] == '%' || isASCIIDigit(input[position])))
980 linePositionBuilder.append(input[position++]);
981 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
984 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT
985 // NINE (9), then jump to the step labeled next setting.
986 // 3. If any character in value other than the first character is a U+002D HYPHEN-MINUS character (-), then
987 // jump to the step labeled next setting.
988 // 4. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%), then
989 // jump to the step labeled next setting.
990 String linePosition = linePositionBuilder.toString();
991 if (linePosition.find('-', 1) != notFound || linePosition.reverseFind("%", linePosition.length() - 2) != notFound)
994 // 5. If the first character in value is a U+002D HYPHEN-MINUS character (-) and the last character in value is a
995 // U+0025 PERCENT SIGN character (%), then jump to the step labeled next setting.
996 if (linePosition[0] == '-' && linePosition[linePosition.length() - 1] == '%')
999 // 6. Ignoring the trailing percent sign, if any, interpret value as a (potentially signed) integer, and
1000 // let number be that number.
1001 // NOTE: toInt ignores trailing non-digit characters, such as '%'.
1003 int number = linePosition.toInt(&validNumber);
1007 // 7. If the last character in value is a U+0025 PERCENT SIGN character (%), but number is not in the range
1008 // 0 ≤ number ≤ 100, then jump to the step labeled next setting.
1009 // 8. Let cue's text track cue line position be number.
1010 // 9. If the last character in value is a U+0025 PERCENT SIGN character (%), then let cue's text track cue
1011 // snap-to-lines flag be false. Otherwise, let it be true.
1012 if (linePosition[linePosition.length() - 1] == '%') {
1013 if (number < 0 || number > 100)
1016 // 10 - If '%' then set snap-to-lines flag to false.
1017 m_snapToLines = false;
1020 m_linePosition = number;
1025 // 1. If value contains any characters other than U+0025 PERCENT SIGN characters (%) and characters in the range
1026 // U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump to the step labeled next setting.
1027 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
1028 // then jump to the step labeled next setting.
1029 String textPosition = WebVTTParser::collectDigits(input, &position);
1030 if (textPosition.isEmpty())
1032 if (position >= input.length())
1035 // 3. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%), then jump
1036 // to the step labeled next setting.
1037 // 4. If the last character in value is not a U+0025 PERCENT SIGN character (%), then jump to the step labeled
1039 if (input[position++] != '%')
1041 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
1044 // 5. Ignoring the trailing percent sign, interpret value as an integer, and let number be that number.
1045 // 6. If number is not in the range 0 ≤ number ≤ 100, then jump to the step labeled next setting.
1046 // NOTE: toInt ignores trailing non-digit characters, such as '%'.
1048 int number = textPosition.toInt(&validNumber);
1051 if (number < 0 || number > 100)
1054 // 7. Let cue's text track cue text position be number.
1055 m_textPosition = number;
1060 // 1. If value contains any characters other than U+0025 PERCENT SIGN characters (%) and characters in the
1061 // range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9), then jump to the step labeled next setting.
1062 // 2. If value does not contain at least one character in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT
1063 // NINE (9), then jump to the step labeled next setting.
1064 String cueSize = WebVTTParser::collectDigits(input, &position);
1065 if (cueSize.isEmpty())
1067 if (position >= input.length())
1070 // 3. If any character in value other than the last character is a U+0025 PERCENT SIGN character (%),
1071 // then jump to the step labeled next setting.
1072 // 4. If the last character in value is not a U+0025 PERCENT SIGN character (%), then jump to the step
1073 // labeled next setting.
1074 if (input[position++] != '%')
1076 if (position < input.length() && !WebVTTParser::isValidSettingDelimiter(input[position]))
1079 // 5. Ignoring the trailing percent sign, interpret value as an integer, and let number be that number.
1080 // 6. If number is not in the range 0 ≤ number ≤ 100, then jump to the step labeled next setting.
1082 int number = cueSize.toInt(&validNumber);
1085 if (number < 0 || number > 100)
1088 // 7. Let cue's text track cue size be number.
1094 String cueAlignment = WebVTTParser::collectWord(input, &position);
1096 // 1. If value is a case-sensitive match for the string "start", then let cue's text track cue alignment be start alignment.
1097 if (cueAlignment == startKeyword())
1098 m_cueAlignment = Start;
1100 // 2. If value is a case-sensitive match for the string "middle", then let cue's text track cue alignment be middle alignment.
1101 else if (cueAlignment == middleKeyword())
1102 m_cueAlignment = Middle;
1104 // 3. If value is a case-sensitive match for the string "end", then let cue's text track cue alignment be end alignment.
1105 else if (cueAlignment == endKeyword())
1106 m_cueAlignment = End;
1109 #if ENABLE(WEBVTT_REGIONS)
1111 m_regionId = WebVTTParser::collectWord(input, &position);
1119 position = endOfSetting;
1121 #if ENABLE(WEBVTT_REGIONS)
1122 // If cue's line position is not auto or cue's size is not 100 or cue's
1123 // writing direction is not horizontal, but cue's region identifier is not
1124 // the empty string, let cue's region identifier be the empty string.
1125 if (m_regionId.isEmpty())
1128 if (m_linePosition != undefinedPosition || m_cueSize != 100 || m_writingDirection != Horizontal)
1129 m_regionId = emptyString();
1133 CSSValueID TextTrackCue::getCSSWritingDirection() const
1135 return m_displayDirection;
1138 CSSValueID TextTrackCue::getCSSWritingMode() const
1140 return m_displayWritingMode;
1143 int TextTrackCue::getCSSSize() const
1145 return m_displaySize;
1148 std::pair<double, double> TextTrackCue::getCSSPosition() const
1151 return getPositionCoordinates();
1153 return m_displayPosition;
1156 const AtomicString& TextTrackCue::interfaceName() const
1158 return eventNames().interfaceForTextTrackCue;
1161 ScriptExecutionContext* TextTrackCue::scriptExecutionContext() const
1163 return m_scriptExecutionContext;
1166 EventTargetData* TextTrackCue::eventTargetData()
1168 return &m_eventTargetData;
1171 EventTargetData* TextTrackCue::ensureEventTargetData()
1173 return &m_eventTargetData;
1176 bool TextTrackCue::isEqual(const TextTrackCue& cue, CueMatchRules match) const
1178 if (cueType() != cue.cueType())
1181 if (match != IgnoreDuration && m_endTime != cue.endTime())
1183 if (m_startTime != cue.startTime())
1185 if (m_content != cue.text())
1187 if (m_settings != cue.cueSettings())
1189 if (m_id != cue.id())
1191 if (m_textPosition != cue.position())
1193 if (m_linePosition != cue.line())
1195 if (m_cueSize != cue.size())
1197 if (align() != cue.align())
1203 bool TextTrackCue::isOrderedBefore(const TextTrackCue* other) const
1205 return startTime() < other->startTime() || (startTime() == other->startTime() && endTime() > other->endTime());
1208 void TextTrackCue::setFontSize(int fontSize, const IntSize&, bool important)
1210 if (!hasDisplayTree() || !fontSize)
1213 LOG(Media, "TextTrackCue::setFontSize - setting cue font size to %i", fontSize);
1215 displayTreeInternal()->setInlineStyleProperty(CSSPropertyFontSize, String::number(fontSize) + "px", important);
1218 } // namespace WebCore