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 "HTMLMeterElement.h"
8 #include "mozilla/dom/HTMLMeterElementBinding.h"
10 NS_IMPL_NS_NEW_HTML_ELEMENT(Meter
)
12 namespace mozilla::dom
{
14 static const double kDefaultValue
= 0.0;
15 static const double kDefaultMin
= 0.0;
16 static const double kDefaultMax
= 1.0;
18 HTMLMeterElement::HTMLMeterElement(
19 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
20 : nsGenericHTMLElement(std::move(aNodeInfo
)) {}
22 HTMLMeterElement::~HTMLMeterElement() = default;
24 NS_IMPL_ELEMENT_CLONE(HTMLMeterElement
)
26 static bool IsInterestingAttr(int32_t aNamespaceID
, nsAtom
* aAttribute
) {
27 if (aNamespaceID
!= kNameSpaceID_None
) {
30 return aAttribute
== nsGkAtoms::value
|| aAttribute
== nsGkAtoms::max
||
31 aAttribute
== nsGkAtoms::min
|| aAttribute
== nsGkAtoms::low
||
32 aAttribute
== nsGkAtoms::high
|| aAttribute
== nsGkAtoms::optimum
;
35 bool HTMLMeterElement::ParseAttribute(int32_t aNamespaceID
, nsAtom
* aAttribute
,
36 const nsAString
& aValue
,
37 nsIPrincipal
* aMaybeScriptedPrincipal
,
38 nsAttrValue
& aResult
) {
39 if (IsInterestingAttr(aNamespaceID
, aAttribute
)) {
40 return aResult
.ParseDoubleValue(aValue
);
42 return nsGenericHTMLElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
43 aMaybeScriptedPrincipal
, aResult
);
46 void HTMLMeterElement::AfterSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
47 const nsAttrValue
* aValue
,
48 const nsAttrValue
* aOldValue
,
49 nsIPrincipal
* aSubjectPrincipal
,
51 if (IsInterestingAttr(aNameSpaceID
, aName
)) {
52 UpdateOptimumState(aNotify
);
54 nsGenericHTMLElement::AfterSetAttr(aNameSpaceID
, aName
, aValue
, aOldValue
,
55 aSubjectPrincipal
, aNotify
);
58 void HTMLMeterElement::UpdateOptimumState(bool aNotify
) {
59 AutoStateChangeNotifier
notifier(*this, aNotify
);
60 RemoveStatesSilently(ElementState::METER_OPTIMUM_STATES
);
61 AddStatesSilently(GetOptimumState());
64 double HTMLMeterElement::Min() const {
66 * If the attribute min is defined, the minimum is this value.
67 * Otherwise, the minimum is the default value.
69 const nsAttrValue
* attrMin
= mAttrs
.GetAttr(nsGkAtoms::min
);
70 if (attrMin
&& attrMin
->Type() == nsAttrValue::eDoubleValue
) {
71 return attrMin
->GetDoubleValue();
76 double HTMLMeterElement::Max() const {
78 * If the attribute max is defined, the maximum is this value.
79 * Otherwise, the maximum is the default value.
80 * If the maximum value is less than the minimum value,
81 * the maximum value is the same as the minimum value.
85 const nsAttrValue
* attrMax
= mAttrs
.GetAttr(nsGkAtoms::max
);
86 if (attrMax
&& attrMax
->Type() == nsAttrValue::eDoubleValue
) {
87 max
= attrMax
->GetDoubleValue();
92 return std::max(max
, Min());
95 double HTMLMeterElement::Value() const {
97 * If the attribute value is defined, the actual value is this value.
98 * Otherwise, the actual value is the default value.
99 * If the actual value is less than the minimum value,
100 * the actual value is the same as the minimum value.
101 * If the actual value is greater than the maximum value,
102 * the actual value is the same as the maximum value.
106 const nsAttrValue
* attrValue
= mAttrs
.GetAttr(nsGkAtoms::value
);
107 if (attrValue
&& attrValue
->Type() == nsAttrValue::eDoubleValue
) {
108 value
= attrValue
->GetDoubleValue();
110 value
= kDefaultValue
;
119 return std::min(value
, Max());
122 double HTMLMeterElement::Position() const {
123 const double max
= Max();
124 const double min
= Min();
125 const double value
= Value();
127 double range
= max
- min
;
128 return range
!= 0.0 ? (value
- min
) / range
: 1.0;
131 double HTMLMeterElement::Low() const {
133 * If the low value is defined, the low value is this value.
134 * Otherwise, the low value is the minimum value.
135 * If the low value is less than the minimum value,
136 * the low value is the same as the minimum value.
137 * If the low value is greater than the maximum value,
138 * the low value is the same as the maximum value.
143 const nsAttrValue
* attrLow
= mAttrs
.GetAttr(nsGkAtoms::low
);
144 if (!attrLow
|| attrLow
->Type() != nsAttrValue::eDoubleValue
) {
148 double low
= attrLow
->GetDoubleValue();
154 return std::min(low
, Max());
157 double HTMLMeterElement::High() const {
159 * If the high value is defined, the high value is this value.
160 * Otherwise, the high value is the maximum value.
161 * If the high value is less than the low value,
162 * the high value is the same as the low value.
163 * If the high value is greater than the maximum value,
164 * the high value is the same as the maximum value.
169 const nsAttrValue
* attrHigh
= mAttrs
.GetAttr(nsGkAtoms::high
);
170 if (!attrHigh
|| attrHigh
->Type() != nsAttrValue::eDoubleValue
) {
174 double high
= attrHigh
->GetDoubleValue();
180 return std::max(high
, Low());
183 double HTMLMeterElement::Optimum() const {
185 * If the optimum value is defined, the optimum value is this value.
186 * Otherwise, the optimum value is the midpoint between
187 * the minimum value and the maximum value :
188 * min + (max - min)/2 = (min + max)/2
189 * If the optimum value is less than the minimum value,
190 * the optimum value is the same as the minimum value.
191 * If the optimum value is greater than the maximum value,
192 * the optimum value is the same as the maximum value.
199 const nsAttrValue
* attrOptimum
= mAttrs
.GetAttr(nsGkAtoms::optimum
);
200 if (!attrOptimum
|| attrOptimum
->Type() != nsAttrValue::eDoubleValue
) {
201 return (min
+ max
) / 2.0;
204 double optimum
= attrOptimum
->GetDoubleValue();
206 if (optimum
<= min
) {
210 return std::min(optimum
, max
);
213 ElementState
HTMLMeterElement::GetOptimumState() const {
215 * If the optimum value is in [minimum, low[,
216 * return if the value is in optimal, suboptimal or sub-suboptimal region
218 * If the optimum value is in [low, high],
219 * return if the value is in optimal or suboptimal region
221 * If the optimum value is in ]high, maximum],
222 * return if the value is in optimal, suboptimal or sub-suboptimal region
224 double value
= Value();
226 double high
= High();
227 double optimum
= Optimum();
231 return ElementState::OPTIMUM
;
234 return ElementState::SUB_OPTIMUM
;
236 return ElementState::SUB_SUB_OPTIMUM
;
238 if (optimum
> high
) {
240 return ElementState::OPTIMUM
;
243 return ElementState::SUB_OPTIMUM
;
245 return ElementState::SUB_SUB_OPTIMUM
;
247 // optimum in [low, high]
248 if (value
>= low
&& value
<= high
) {
249 return ElementState::OPTIMUM
;
251 return ElementState::SUB_OPTIMUM
;
254 JSObject
* HTMLMeterElement::WrapNode(JSContext
* aCx
,
255 JS::Handle
<JSObject
*> aGivenProto
) {
256 return HTMLMeterElement_Binding::Wrap(aCx
, this, aGivenProto
);
259 } // namespace mozilla::dom