Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / svg / DOMSVGNumber.cpp
blobd24332aca4dbba906d814bc0479c56690f6a4b2e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 #include "DOMSVGNumber.h"
8 #include "DOMSVGNumberList.h"
9 #include "DOMSVGAnimatedNumberList.h"
10 #include "SVGAnimatedNumberList.h"
11 #include "SVGElement.h"
12 #include "nsError.h"
13 #include "nsContentUtils.h" // for NS_ENSURE_FINITE
14 #include "mozilla/dom/SVGNumberBinding.h"
15 #include "mozilla/dom/SVGSVGElement.h"
17 // See the architecture comment in DOMSVGAnimatedNumberList.h.
19 namespace mozilla::dom {
21 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
22 // clear our list's weak ref to us to be safe. (The other option would be to
23 // not unlink and rely on the breaking of the other edges in the cycle, as
24 // NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
25 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumber)
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumber)
28 // We may not belong to a list, so we must null check tmp->mList.
29 if (tmp->mList) {
30 tmp->mList->mItems[tmp->mListIndex] = nullptr;
32 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
33 NS_IMPL_CYCLE_COLLECTION_UNLINK(mList)
34 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumber)
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mList)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
40 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumber)
41 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
42 NS_IMPL_CYCLE_COLLECTION_TRACE_END
44 DOMSVGNumber::DOMSVGNumber(DOMSVGNumberList* aList, uint8_t aAttrEnum,
45 uint32_t aListIndex, bool aIsAnimValItem)
46 : mList(aList),
47 mParent(aList),
48 mListIndex(aListIndex),
49 mAttrEnum(aAttrEnum),
50 mIsAnimValItem(aIsAnimValItem),
51 mValue(0.0f) {
52 // These shifts are in sync with the members in the header.
53 MOZ_ASSERT(aList && aAttrEnum < (1 << 4) && aListIndex <= MaxListIndex(),
54 "bad arg");
56 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
59 DOMSVGNumber::DOMSVGNumber(nsISupports* aParent)
60 : mList(nullptr),
61 mParent(aParent),
62 mListIndex(0),
63 mAttrEnum(0),
64 mIsAnimValItem(false),
65 mValue(0.0f) {}
67 DOMSVGNumber::DOMSVGNumber(SVGSVGElement* aParent)
68 : mList(nullptr),
69 mParent(ToSupports(aParent)),
70 mListIndex(0),
71 mAttrEnum(0),
72 mIsAnimValItem(false),
73 mValue(0.0f) {}
75 float DOMSVGNumber::Value() {
76 if (mIsAnimValItem && HasOwner()) {
77 Element()->FlushAnimations(); // May make HasOwner() == false
79 return HasOwner() ? InternalItem() : mValue;
82 void DOMSVGNumber::SetValue(float aValue, ErrorResult& aRv) {
83 if (mIsAnimValItem) {
84 aRv.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
85 return;
88 if (HasOwner()) {
89 if (InternalItem() == aValue) {
90 return;
92 AutoChangeNumberListNotifier notifier(this);
93 InternalItem() = aValue;
94 return;
97 mValue = aValue;
100 void DOMSVGNumber::InsertingIntoList(DOMSVGNumberList* aList, uint8_t aAttrEnum,
101 uint32_t aListIndex, bool aIsAnimValItem) {
102 NS_ASSERTION(!HasOwner(), "Inserting item that is already in a list");
104 mList = aList;
105 mAttrEnum = aAttrEnum;
106 mListIndex = aListIndex;
107 mIsAnimValItem = aIsAnimValItem;
109 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
112 void DOMSVGNumber::RemovingFromList() {
113 mValue = InternalItem();
114 mList = nullptr;
115 mIsAnimValItem = false;
118 float DOMSVGNumber::ToSVGNumber() {
119 return HasOwner() ? InternalItem() : mValue;
122 float& DOMSVGNumber::InternalItem() {
123 SVGAnimatedNumberList* alist = Element()->GetAnimatedNumberList(mAttrEnum);
124 return mIsAnimValItem && alist->mAnimVal ? (*alist->mAnimVal)[mListIndex]
125 : alist->mBaseVal[mListIndex];
128 #ifdef DEBUG
129 bool DOMSVGNumber::IndexIsValid() {
130 SVGAnimatedNumberList* alist = Element()->GetAnimatedNumberList(mAttrEnum);
131 return (mIsAnimValItem && mListIndex < alist->GetAnimValue().Length()) ||
132 (!mIsAnimValItem && mListIndex < alist->GetBaseValue().Length());
134 #endif
136 JSObject* DOMSVGNumber::WrapObject(JSContext* aCx,
137 JS::Handle<JSObject*> aGivenProto) {
138 return SVGNumber_Binding::Wrap(aCx, this, aGivenProto);
141 } // namespace mozilla::dom