1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/SVGAnimationElement.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/ElementInlines.h"
#include "mozilla/SMILAnimationController.h"
#include "mozilla/SMILAnimationFunction.h"
#include "mozilla/SMILTimeContainer.h"
#include "mozilla/SVGObserverUtils.h"
#include "nsContentUtils.h"
#include "nsIContentInlines.h"
#include "nsIReferrerInfo.h"
#include "nsIURI.h"
#include "prtime.h"
namespace mozilla {
namespace dom {
//----------------------------------------------------------------------
// nsISupports methods
NS_IMPL_ADDREF_INHERITED(SVGAnimationElement, SVGAnimationElementBase)
NS_IMPL_RELEASE_INHERITED(SVGAnimationElement, SVGAnimationElementBase)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SVGAnimationElement)
NS_INTERFACE_MAP_ENTRY_CONCRETE(SVGAnimationElement)
NS_INTERFACE_MAP_ENTRY(mozilla::dom::SVGTests)
NS_INTERFACE_MAP_END_INHERITING(SVGAnimationElementBase)
NS_IMPL_CYCLE_COLLECTION_INHERITED(SVGAnimationElement, SVGAnimationElementBase,
mHrefTarget, mTimedElement)
//----------------------------------------------------------------------
// Implementation
SVGAnimationElement::SVGAnimationElement(
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
: SVGAnimationElementBase(std::move(aNodeInfo)), mHrefTarget(this) {}
nsresult SVGAnimationElement::Init() {
nsresult rv = SVGAnimationElementBase::Init();
NS_ENSURE_SUCCESS(rv, rv);
mTimedElement.SetAnimationElement(this);
AnimationFunction().SetAnimationElement(this);
mTimedElement.SetTimeClient(&AnimationFunction());
return NS_OK;
}
//----------------------------------------------------------------------
Element* SVGAnimationElement::GetTargetElementContent() {
if (HasAttr(kNameSpaceID_XLink, nsGkAtoms::href) ||
HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
return mHrefTarget.get();
}
MOZ_ASSERT(!mHrefTarget.get(),
"We shouldn't have a href target "
"if we don't have an xlink:href or href attribute");
// No "href" or "xlink:href" attribute --> I should target my parent.
//
// Note that we want to use GetParentElement instead of the flattened tree to
// allow <use><animate>, for example.
return GetParentElement();
}
bool SVGAnimationElement::GetTargetAttributeName(int32_t* aNamespaceID,
nsAtom** aLocalName) const {
const nsAttrValue* nameAttr = mAttrs.GetAttr(nsGkAtoms::attributeName);
if (!nameAttr) return false;
NS_ASSERTION(nameAttr->Type() == nsAttrValue::eAtom,
"attributeName should have been parsed as an atom");
return NS_SUCCEEDED(nsContentUtils::SplitQName(
this, nsDependentAtomString(nameAttr->GetAtomValue()), aNamespaceID,
aLocalName));
}
SMILTimedElement& SVGAnimationElement::TimedElement() { return mTimedElement; }
SVGElement* SVGAnimationElement::GetTargetElement() {
FlushAnimations();
// We'll just call the other GetTargetElement method, and QI to the right type
nsIContent* target = GetTargetElementContent();
return (target && target->IsSVGElement()) ? static_cast<SVGElement*>(target)
: nullptr;
}
float SVGAnimationElement::GetStartTime(ErrorResult& rv) {
FlushAnimations();
SMILTimeValue startTime = mTimedElement.GetStartTime();
if (!startTime.IsDefinite()) {
rv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return 0.f;
}
return float(double(startTime.GetMillis()) / PR_MSEC_PER_SEC);
}
float SVGAnimationElement::GetCurrentTimeAsFloat() {
// Not necessary to call FlushAnimations() for this
SMILTimeContainer* root = GetTimeContainer();
if (root) {
return float(double(root->GetCurrentTimeAsSMILTime()) / PR_MSEC_PER_SEC);
}
return 0.0f;
}
float SVGAnimationElement::GetSimpleDuration(ErrorResult& rv) {
// Not necessary to call FlushAnimations() for this
SMILTimeValue simpleDur = mTimedElement.GetSimpleDuration();
if (!simpleDur.IsDefinite()) {
rv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
return 0.f;
}
return float(double(simpleDur.GetMillis()) / PR_MSEC_PER_SEC);
}
//----------------------------------------------------------------------
// nsIContent methods
nsresult SVGAnimationElement::BindToTree(BindContext& aContext,
nsINode& aParent) {
MOZ_ASSERT(!mHrefTarget.get(),
"Shouldn't have href-target yet (or it should've been cleared)");
nsresult rv = SVGAnimationElementBase::BindToTree(aContext, aParent);
NS_ENSURE_SUCCESS(rv, rv);
// Add myself to the animation controller's master set of animation elements.
if (Document* doc = aContext.GetComposedDoc()) {
if (SMILAnimationController* controller = doc->GetAnimationController()) {
controller->RegisterAnimationElement(this);
}
const nsAttrValue* href =
HasAttr(kNameSpaceID_None, nsGkAtoms::href)
? mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_None)
: mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
if (href) {
nsAutoString hrefStr;
href->ToString(hrefStr);
UpdateHrefTarget(hrefStr);
}
mTimedElement.BindToTree(*this);
}
AnimationNeedsResample();
return NS_OK;
}
void SVGAnimationElement::UnbindFromTree(bool aNullParent) {
SMILAnimationController* controller = OwnerDoc()->GetAnimationController();
if (controller) {
controller->UnregisterAnimationElement(this);
}
mHrefTarget.Unlink();
mTimedElement.DissolveReferences();
AnimationNeedsResample();
SVGAnimationElementBase::UnbindFromTree(aNullParent);
}
bool SVGAnimationElement::ParseAttribute(int32_t aNamespaceID,
nsAtom* aAttribute,
const nsAString& aValue,
nsIPrincipal* aMaybeScriptedPrincipal,
nsAttrValue& aResult) {
if (aNamespaceID == kNameSpaceID_None) {
// Deal with target-related attributes here
if (aAttribute == nsGkAtoms::attributeName) {
aResult.ParseAtom(aValue);
AnimationNeedsResample();
return true;
}
nsresult rv = NS_ERROR_FAILURE;
// First let the animation function try to parse it...
bool foundMatch =
AnimationFunction().SetAttr(aAttribute, aValue, aResult, &rv);
// ... and if that didn't recognize the attribute, let the timed element
// try to parse it.
if (!foundMatch) {
foundMatch =
mTimedElement.SetAttr(aAttribute, aValue, aResult, *this, &rv);
}
if (foundMatch) {
AnimationNeedsResample();
if (NS_FAILED(rv)) {
ReportAttributeParseFailure(OwnerDoc(), aAttribute, aValue);
return false;
}
return true;
}
}
return SVGAnimationElementBase::ParseAttribute(
aNamespaceID, aAttribute, aValue, aMaybeScriptedPrincipal, aResult);
}
nsresult SVGAnimationElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
const nsAttrValue* aValue,
const nsAttrValue* aOldValue,
nsIPrincipal* aSubjectPrincipal,
bool aNotify) {
if (!aValue && aNamespaceID == kNameSpaceID_None) {
// Attribute is being removed.
if (AnimationFunction().UnsetAttr(aName) ||
mTimedElement.UnsetAttr(aName)) {
AnimationNeedsResample();
}
}
nsresult rv = SVGAnimationElementBase::AfterSetAttr(
aNamespaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
if (SVGTests::IsConditionalProcessingAttribute(aName)) {
bool isDisabled = !SVGTests::PassesConditionalProcessingTests();
if (mTimedElement.SetIsDisabled(isDisabled)) {
AnimationNeedsResample();
}
}
if (!IsInComposedDoc()) {
return rv;
}
if (!((aNamespaceID == kNameSpaceID_None ||
aNamespaceID == kNameSpaceID_XLink) &&
aName == nsGkAtoms::href)) {
return rv;
}
if (!aValue) {
if (aNamespaceID == kNameSpaceID_None) {
mHrefTarget.Unlink();
AnimationTargetChanged();
// After unsetting href, we may still have xlink:href, so we
// should try to add it back.
const nsAttrValue* xlinkHref =
mAttrs.GetAttr(nsGkAtoms::href, kNameSpaceID_XLink);
if (xlinkHref) {
UpdateHrefTarget(xlinkHref->GetStringValue());
}
} else if (!HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
mHrefTarget.Unlink();
AnimationTargetChanged();
} // else: we unset xlink:href, but we still have href attribute, so keep
// mHrefTarget linking to href.
} else if (!(aNamespaceID == kNameSpaceID_XLink &&
HasAttr(kNameSpaceID_None, nsGkAtoms::href))) {
// Note: "href" takes priority over xlink:href. So if "xlink:href" is being
// set here, we only let that update our target if "href" is *unset*.
MOZ_ASSERT(aValue->Type() == nsAttrValue::eString,
"Expected href attribute to be string type");
UpdateHrefTarget(aValue->GetStringValue());
} // else: we're not yet in a document -- we'll update the target on
// next BindToTree call.
return rv;
}
//----------------------------------------------------------------------
// SVG utility methods
void SVGAnimationElement::ActivateByHyperlink() {
FlushAnimations();
// The behavior for when the target is an animation element is defined in
// SMIL Animation:
// http://www.w3.org/TR/smil-animation/#HyperlinkSemantics
SMILTimeValue seekTime = mTimedElement.GetHyperlinkTime();
if (seekTime.IsDefinite()) {
SMILTimeContainer* timeContainer = GetTimeContainer();
if (timeContainer) {
timeContainer->SetCurrentTime(seekTime.GetMillis());
AnimationNeedsResample();
// As with SVGSVGElement::SetCurrentTime, we need to trigger
// a synchronous sample now.
FlushAnimations();
}
// else, silently fail. We mustn't be part of an SVG document fragment that
// is attached to the document tree so there's nothing we can do here
} else {
BeginElement(IgnoreErrors());
}
}
//----------------------------------------------------------------------
// Implementation helpers
SMILTimeContainer* SVGAnimationElement::GetTimeContainer() {
SVGSVGElement* element = SVGContentUtils::GetOuterSVGElement(this);
if (element) {
return element->GetTimedDocumentRoot();
}
return nullptr;
}
void SVGAnimationElement::BeginElementAt(float offset, ErrorResult& rv) {
// Make sure the timegraph is up-to-date
FlushAnimations();
// This will fail if we're not attached to a time container (SVG document
// fragment).
rv = mTimedElement.BeginElementAt(offset);
if (rv.Failed()) return;
AnimationNeedsResample();
// Force synchronous sample so that events resulting from this call arrive in
// the expected order and we get an up-to-date paint.
FlushAnimations();
}
void SVGAnimationElement::EndElementAt(float offset, ErrorResult& rv) {
// Make sure the timegraph is up-to-date
FlushAnimations();
rv = mTimedElement.EndElementAt(offset);
if (rv.Failed()) return;
AnimationNeedsResample();
// Force synchronous sample
FlushAnimations();
}
bool SVGAnimationElement::IsEventAttributeNameInternal(nsAtom* aName) {
return nsContentUtils::IsEventAttributeName(aName, EventNameType_SMIL);
}
void SVGAnimationElement::UpdateHrefTarget(const nsAString& aHrefStr) {
nsCOMPtr<nsIURI> baseURI = GetBaseURI();
if (nsContentUtils::IsLocalRefURL(aHrefStr)) {
baseURI = SVGObserverUtils::GetBaseURLForLocalRef(this, baseURI);
}
nsCOMPtr<nsIURI> targetURI;
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), aHrefStr,
OwnerDoc(), baseURI);
nsCOMPtr<nsIReferrerInfo> referrerInfo =
ReferrerInfo::CreateForSVGResources(OwnerDoc());
mHrefTarget.ResetToURIFragmentID(this, targetURI, referrerInfo);
AnimationTargetChanged();
}
void SVGAnimationElement::AnimationTargetChanged() {
mTimedElement.HandleTargetElementChange(GetTargetElementContent());
AnimationNeedsResample();
}
} // namespace dom
} // namespace mozilla
|