1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 et tw=78: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_TextTrackCue_h
8 #define mozilla_dom_TextTrackCue_h
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/DocumentFragment.h"
12 #include "mozilla/dom/VTTCueBinding.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsIWebVTTParserWrapper.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/dom/HTMLDivElement.h"
17 #include "mozilla/dom/TextTrack.h"
18 #include "mozilla/StateWatching.h"
20 namespace mozilla::dom
{
23 class HTMLTrackElement
;
24 class TextTrackRegion
;
26 class TextTrackCue final
: public DOMEventTargetHelper
{
28 NS_DECL_ISUPPORTS_INHERITED
29 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextTrackCue
, DOMEventTargetHelper
)
31 // TextTrackCue WebIDL
32 // See bug 868509 about splitting out the WebVTT-specific interfaces.
33 static already_AddRefed
<TextTrackCue
> Constructor(GlobalObject
& aGlobal
,
36 const nsAString
& aText
,
38 nsCOMPtr
<nsPIDOMWindowInner
> window
=
39 do_QueryInterface(aGlobal
.GetAsSupports());
40 RefPtr
<TextTrackCue
> ttcue
=
41 new TextTrackCue(window
, aStartTime
, aEndTime
, aText
, aRv
);
42 return ttcue
.forget();
44 TextTrackCue(nsPIDOMWindowInner
* aGlobal
, double aStartTime
, double aEndTime
,
45 const nsAString
& aText
, ErrorResult
& aRv
);
47 TextTrackCue(nsPIDOMWindowInner
* aGlobal
, double aStartTime
, double aEndTime
,
48 const nsAString
& aText
, HTMLTrackElement
* aTrackElement
,
51 JSObject
* WrapObject(JSContext
* aCx
,
52 JS::Handle
<JSObject
*> aGivenProto
) override
;
54 TextTrack
* GetTrack() const { return mTrack
; }
56 void GetId(nsAString
& aId
) const { aId
= mId
; }
58 void SetId(const nsAString
& aId
) {
66 double StartTime() const { return mStartTime
; }
68 void SetStartTime(double aStartTime
) {
69 if (mStartTime
== aStartTime
) {
73 mStartTime
= aStartTime
;
75 NotifyCueUpdated(this);
78 double EndTime() const { return mEndTime
; }
80 void SetEndTime(double aEndTime
) {
81 if (mEndTime
== aEndTime
) {
87 NotifyCueUpdated(this);
90 bool PauseOnExit() { return mPauseOnExit
; }
92 void SetPauseOnExit(bool aPauseOnExit
) {
93 if (mPauseOnExit
== aPauseOnExit
) {
97 mPauseOnExit
= aPauseOnExit
;
98 NotifyCueUpdated(nullptr);
101 TextTrackRegion
* GetRegion();
102 void SetRegion(TextTrackRegion
* aRegion
);
104 DirectionSetting
Vertical() const { return mVertical
; }
106 void SetVertical(const DirectionSetting
& aVertical
) {
107 if (mVertical
== aVertical
) {
112 mVertical
= aVertical
;
115 bool SnapToLines() { return mSnapToLines
; }
117 void SetSnapToLines(bool aSnapToLines
) {
118 if (mSnapToLines
== aSnapToLines
) {
123 mSnapToLines
= aSnapToLines
;
126 void GetLine(OwningDoubleOrAutoKeyword
& aLine
) const {
127 if (mLineIsAutoKeyword
) {
128 aLine
.SetAsAutoKeyword() = AutoKeyword::Auto
;
131 aLine
.SetAsDouble() = mLine
;
134 void SetLine(const DoubleOrAutoKeyword
& aLine
) {
135 if (aLine
.IsDouble() &&
136 (mLineIsAutoKeyword
|| (aLine
.GetAsDouble() != mLine
))) {
137 mLineIsAutoKeyword
= false;
138 mLine
= aLine
.GetAsDouble();
142 if (aLine
.IsAutoKeyword() && !mLineIsAutoKeyword
) {
143 mLineIsAutoKeyword
= true;
148 LineAlignSetting
LineAlign() const { return mLineAlign
; }
150 void SetLineAlign(LineAlignSetting
& aLineAlign
, ErrorResult
& aRv
) {
151 if (mLineAlign
== aLineAlign
) {
156 mLineAlign
= aLineAlign
;
159 void GetPosition(OwningDoubleOrAutoKeyword
& aPosition
) const {
160 if (mPositionIsAutoKeyword
) {
161 aPosition
.SetAsAutoKeyword() = AutoKeyword::Auto
;
164 aPosition
.SetAsDouble() = mPosition
;
167 void SetPosition(const DoubleOrAutoKeyword
& aPosition
, ErrorResult
& aRv
) {
168 if (!aPosition
.IsAutoKeyword() &&
169 (aPosition
.GetAsDouble() > 100.0 || aPosition
.GetAsDouble() < 0.0)) {
170 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
174 if (aPosition
.IsDouble() &&
175 (mPositionIsAutoKeyword
|| (aPosition
.GetAsDouble() != mPosition
))) {
176 mPositionIsAutoKeyword
= false;
177 mPosition
= aPosition
.GetAsDouble();
182 if (aPosition
.IsAutoKeyword() && !mPositionIsAutoKeyword
) {
183 mPositionIsAutoKeyword
= true;
188 PositionAlignSetting
PositionAlign() const { return mPositionAlign
; }
190 void SetPositionAlign(PositionAlignSetting aPositionAlign
, ErrorResult
& aRv
) {
191 if (mPositionAlign
== aPositionAlign
) {
196 mPositionAlign
= aPositionAlign
;
199 double Size() const { return mSize
; }
201 void SetSize(double aSize
, ErrorResult
& aRv
) {
202 if (mSize
== aSize
) {
206 if (aSize
< 0.0 || aSize
> 100.0) {
207 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
215 AlignSetting
Align() const { return mAlign
; }
217 void SetAlign(AlignSetting
& aAlign
) {
218 if (mAlign
== aAlign
) {
226 void GetText(nsAString
& aText
) const { aText
= mText
; }
228 void SetText(const nsAString
& aText
) {
229 if (mText
== aText
) {
237 IMPL_EVENT_HANDLER(enter
)
238 IMPL_EVENT_HANDLER(exit
)
240 HTMLDivElement
* GetDisplayState() {
241 return static_cast<HTMLDivElement
*>(mDisplayState
.get());
244 void SetDisplayState(HTMLDivElement
* aDisplayState
) {
245 mDisplayState
= aDisplayState
;
249 void Reset() { mReset
= true; }
251 bool HasBeenReset() { return mReset
; }
253 double ComputedLine();
254 double ComputedPosition();
255 PositionAlignSetting
ComputedPositionAlign();
257 // Helper functions for implementation.
258 const nsAString
& Id() const { return mId
; }
260 void SetTrack(TextTrack
* aTextTrack
) {
262 if (!mHaveStartedWatcher
&& aTextTrack
) {
263 mHaveStartedWatcher
= true;
264 mWatchManager
.Watch(mReset
, &TextTrackCue::NotifyDisplayStatesChanged
);
265 } else if (mHaveStartedWatcher
&& !aTextTrack
) {
266 mHaveStartedWatcher
= false;
267 mWatchManager
.Unwatch(mReset
, &TextTrackCue::NotifyDisplayStatesChanged
);
272 * Produces a tree of anonymous content based on the tree of the processed
275 * Returns a DocumentFragment that is the head of the tree of anonymous
278 already_AddRefed
<DocumentFragment
> GetCueAsHTML();
280 void SetTrackElement(HTMLTrackElement
* aTrackElement
);
282 void SetActive(bool aActive
);
284 bool GetActive() { return mActive
; }
289 void NotifyCueUpdated(TextTrackCue
* aCue
) {
291 mTrack
->NotifyCueUpdated(aCue
);
295 void NotifyDisplayStatesChanged();
297 void SetDefaultCueSettings();
298 nsresult
StashDocument();
300 bool IsTextBaseDirectionLTR() const;
302 RefPtr
<Document
> mDocument
;
307 RefPtr
<TextTrack
> mTrack
;
308 RefPtr
<HTMLTrackElement
> mTrackElement
;
311 bool mPositionIsAutoKeyword
;
312 PositionAlignSetting mPositionAlign
;
316 RefPtr
<TextTrackRegion
> mRegion
;
317 DirectionSetting mVertical
;
318 bool mLineIsAutoKeyword
;
321 LineAlignSetting mLineAlign
;
323 // Holds the computed DOM elements that represent the parsed cue text.
324 // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-display-state
325 RefPtr
<nsGenericHTMLElement
> mDisplayState
;
326 // Tells whether or not we need to recompute mDisplayState. This is set
327 // anytime a property that relates to the display of the TextTrackCue is
329 Watchable
<bool> mReset
;
333 static StaticRefPtr
<nsIWebVTTParserWrapper
> sParserWrapper
;
335 // Only start watcher after the cue has text track.
336 bool mHaveStartedWatcher
;
337 WatchManager
<TextTrackCue
> mWatchManager
;
340 } // namespace mozilla::dom
342 #endif // mozilla_dom_TextTrackCue_h