Line data Source code
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/. */
6 :
7 : #include "HTMLMeterElement.h"
8 : #include "mozilla/EventStates.h"
9 : #include "mozilla/dom/HTMLMeterElementBinding.h"
10 :
11 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Meter)
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : const double HTMLMeterElement::kDefaultValue = 0.0;
17 : const double HTMLMeterElement::kDefaultMin = 0.0;
18 : const double HTMLMeterElement::kDefaultMax = 1.0;
19 :
20 :
21 0 : HTMLMeterElement::HTMLMeterElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
22 0 : : nsGenericHTMLElement(aNodeInfo)
23 : {
24 0 : }
25 :
26 0 : HTMLMeterElement::~HTMLMeterElement()
27 : {
28 0 : }
29 :
30 0 : NS_IMPL_ELEMENT_CLONE(HTMLMeterElement)
31 :
32 : EventStates
33 0 : HTMLMeterElement::IntrinsicState() const
34 : {
35 0 : EventStates state = nsGenericHTMLElement::IntrinsicState();
36 :
37 0 : state |= GetOptimumState();
38 :
39 0 : return state;
40 : }
41 :
42 : bool
43 0 : HTMLMeterElement::ParseAttribute(int32_t aNamespaceID, nsIAtom* aAttribute,
44 : const nsAString& aValue, nsAttrValue& aResult)
45 : {
46 0 : if (aNamespaceID == kNameSpaceID_None) {
47 0 : if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max ||
48 0 : aAttribute == nsGkAtoms::min || aAttribute == nsGkAtoms::low ||
49 0 : aAttribute == nsGkAtoms::high || aAttribute == nsGkAtoms::optimum) {
50 0 : return aResult.ParseDoubleValue(aValue);
51 : }
52 : }
53 :
54 0 : return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute,
55 0 : aValue, aResult);
56 : }
57 :
58 : /*
59 : * Value getters :
60 : * const getters used by XPCOM methods and by IntrinsicState
61 : */
62 :
63 : double
64 0 : HTMLMeterElement::Min() const
65 : {
66 : /**
67 : * If the attribute min is defined, the minimum is this value.
68 : * Otherwise, the minimum is the default value.
69 : */
70 0 : const nsAttrValue* attrMin = mAttrsAndChildren.GetAttr(nsGkAtoms::min);
71 0 : if (attrMin && attrMin->Type() == nsAttrValue::eDoubleValue) {
72 0 : return attrMin->GetDoubleValue();
73 : }
74 0 : return kDefaultMin;
75 : }
76 :
77 : double
78 0 : HTMLMeterElement::Max() const
79 : {
80 : /**
81 : * If the attribute max is defined, the maximum is this value.
82 : * Otherwise, the maximum is the default value.
83 : * If the maximum value is less than the minimum value,
84 : * the maximum value is the same as the minimum value.
85 : */
86 : double max;
87 :
88 0 : const nsAttrValue* attrMax = mAttrsAndChildren.GetAttr(nsGkAtoms::max);
89 0 : if (attrMax && attrMax->Type() == nsAttrValue::eDoubleValue) {
90 0 : max = attrMax->GetDoubleValue();
91 : } else {
92 0 : max = kDefaultMax;
93 : }
94 :
95 0 : return std::max(max, Min());
96 : }
97 :
98 : double
99 0 : HTMLMeterElement::Value() const
100 : {
101 : /**
102 : * If the attribute value is defined, the actual value is this value.
103 : * Otherwise, the actual value is the default value.
104 : * If the actual value is less than the minimum value,
105 : * the actual value is the same as the minimum value.
106 : * If the actual value is greater than the maximum value,
107 : * the actual value is the same as the maximum value.
108 : */
109 : double value;
110 :
111 0 : const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
112 0 : if (attrValue && attrValue->Type() == nsAttrValue::eDoubleValue) {
113 0 : value = attrValue->GetDoubleValue();
114 : } else {
115 0 : value = kDefaultValue;
116 : }
117 :
118 0 : double min = Min();
119 :
120 0 : if (value <= min) {
121 0 : return min;
122 : }
123 :
124 0 : return std::min(value, Max());
125 : }
126 :
127 : double
128 0 : HTMLMeterElement::Low() const
129 : {
130 : /**
131 : * If the low value is defined, the low value is this value.
132 : * Otherwise, the low value is the minimum value.
133 : * If the low value is less than the minimum value,
134 : * the low value is the same as the minimum value.
135 : * If the low value is greater than the maximum value,
136 : * the low value is the same as the maximum value.
137 : */
138 :
139 0 : double min = Min();
140 :
141 0 : const nsAttrValue* attrLow = mAttrsAndChildren.GetAttr(nsGkAtoms::low);
142 0 : if (!attrLow || attrLow->Type() != nsAttrValue::eDoubleValue) {
143 0 : return min;
144 : }
145 :
146 0 : double low = attrLow->GetDoubleValue();
147 :
148 0 : if (low <= min) {
149 0 : return min;
150 : }
151 :
152 0 : return std::min(low, Max());
153 : }
154 :
155 : double
156 0 : HTMLMeterElement::High() const
157 : {
158 : /**
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.
165 : */
166 :
167 0 : double max = Max();
168 :
169 0 : const nsAttrValue* attrHigh = mAttrsAndChildren.GetAttr(nsGkAtoms::high);
170 0 : if (!attrHigh || attrHigh->Type() != nsAttrValue::eDoubleValue) {
171 0 : return max;
172 : }
173 :
174 0 : double high = attrHigh->GetDoubleValue();
175 :
176 0 : if (high >= max) {
177 0 : return max;
178 : }
179 :
180 0 : return std::max(high, Low());
181 : }
182 :
183 : double
184 0 : HTMLMeterElement::Optimum() const
185 : {
186 : /**
187 : * If the optimum value is defined, the optimum value is this value.
188 : * Otherwise, the optimum value is the midpoint between
189 : * the minimum value and the maximum value :
190 : * min + (max - min)/2 = (min + max)/2
191 : * If the optimum value is less than the minimum value,
192 : * the optimum value is the same as the minimum value.
193 : * If the optimum value is greater than the maximum value,
194 : * the optimum value is the same as the maximum value.
195 : */
196 :
197 0 : double max = Max();
198 :
199 0 : double min = Min();
200 :
201 : const nsAttrValue* attrOptimum =
202 0 : mAttrsAndChildren.GetAttr(nsGkAtoms::optimum);
203 0 : if (!attrOptimum || attrOptimum->Type() != nsAttrValue::eDoubleValue) {
204 0 : return (min + max) / 2.0;
205 : }
206 :
207 0 : double optimum = attrOptimum->GetDoubleValue();
208 :
209 0 : if (optimum <= min) {
210 0 : return min;
211 : }
212 :
213 0 : return std::min(optimum, max);
214 : }
215 :
216 : EventStates
217 0 : HTMLMeterElement::GetOptimumState() const
218 : {
219 : /*
220 : * If the optimum value is in [minimum, low[,
221 : * return if the value is in optimal, suboptimal or sub-suboptimal region
222 : *
223 : * If the optimum value is in [low, high],
224 : * return if the value is in optimal or suboptimal region
225 : *
226 : * If the optimum value is in ]high, maximum],
227 : * return if the value is in optimal, suboptimal or sub-suboptimal region
228 : */
229 0 : double value = Value();
230 0 : double low = Low();
231 0 : double high = High();
232 0 : double optimum = Optimum();
233 :
234 0 : if (optimum < low) {
235 0 : if (value < low) {
236 0 : return NS_EVENT_STATE_OPTIMUM;
237 : }
238 0 : if (value <= high) {
239 0 : return NS_EVENT_STATE_SUB_OPTIMUM;
240 : }
241 0 : return NS_EVENT_STATE_SUB_SUB_OPTIMUM;
242 : }
243 0 : if (optimum > high) {
244 0 : if (value > high) {
245 0 : return NS_EVENT_STATE_OPTIMUM;
246 : }
247 0 : if (value >= low) {
248 0 : return NS_EVENT_STATE_SUB_OPTIMUM;
249 : }
250 0 : return NS_EVENT_STATE_SUB_SUB_OPTIMUM;
251 : }
252 : // optimum in [low, high]
253 0 : if (value >= low && value <= high) {
254 0 : return NS_EVENT_STATE_OPTIMUM;
255 : }
256 0 : return NS_EVENT_STATE_SUB_OPTIMUM;
257 : }
258 :
259 : JSObject*
260 0 : HTMLMeterElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
261 : {
262 0 : return HTMLMeterElementBinding::Wrap(aCx, this, aGivenProto);
263 : }
264 :
265 : } // namespace dom
266 : } // namespace mozilla
|