• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20 
21 #include "config.h"
22 
23 #include "core/html/HTMLMeterElement.h"
24 
25 #include "HTMLNames.h"
26 #include "bindings/v8/ExceptionMessages.h"
27 #include "bindings/v8/ExceptionState.h"
28 #include "bindings/v8/ExceptionStatePlaceholder.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/dom/shadow/ShadowRoot.h"
31 #include "core/html/parser/HTMLParserIdioms.h"
32 #include "core/html/shadow/MeterShadowElement.h"
33 #include "core/rendering/RenderMeter.h"
34 #include "core/rendering/RenderTheme.h"
35 
36 namespace WebCore {
37 
38 using namespace HTMLNames;
39 
HTMLMeterElement(Document & document)40 HTMLMeterElement::HTMLMeterElement(Document& document)
41     : LabelableElement(meterTag, document)
42 {
43     ScriptWrappable::init(this);
44 }
45 
~HTMLMeterElement()46 HTMLMeterElement::~HTMLMeterElement()
47 {
48 }
49 
create(Document & document)50 PassRefPtr<HTMLMeterElement> HTMLMeterElement::create(Document& document)
51 {
52     RefPtr<HTMLMeterElement> meter = adoptRef(new HTMLMeterElement(document));
53     meter->ensureUserAgentShadowRoot();
54     return meter;
55 }
56 
createRenderer(RenderStyle * style)57 RenderObject* HTMLMeterElement::createRenderer(RenderStyle* style)
58 {
59     if (hasAuthorShadowRoot() || !RenderTheme::theme().supportsMeter(style->appearance()))
60         return RenderObject::createObject(this, style);
61 
62     return new RenderMeter(this);
63 }
64 
parseAttribute(const QualifiedName & name,const AtomicString & value)65 void HTMLMeterElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
66 {
67     if (name == valueAttr || name == minAttr || name == maxAttr || name == lowAttr || name == highAttr || name == optimumAttr)
68         didElementStateChange();
69     else
70         LabelableElement::parseAttribute(name, value);
71 }
72 
min() const73 double HTMLMeterElement::min() const
74 {
75     return getFloatingPointAttribute(minAttr, 0);
76 }
77 
setMin(double min,ExceptionState & exceptionState)78 void HTMLMeterElement::setMin(double min, ExceptionState& exceptionState)
79 {
80     if (!std::isfinite(min)) {
81         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(min));
82         return;
83     }
84     setFloatingPointAttribute(minAttr, min);
85 }
86 
max() const87 double HTMLMeterElement::max() const
88 {
89     return std::max(getFloatingPointAttribute(maxAttr, std::max(1.0, min())), min());
90 }
91 
setMax(double max,ExceptionState & exceptionState)92 void HTMLMeterElement::setMax(double max, ExceptionState& exceptionState)
93 {
94     if (!std::isfinite(max)) {
95         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(max));
96         return;
97     }
98     setFloatingPointAttribute(maxAttr, max);
99 }
100 
value() const101 double HTMLMeterElement::value() const
102 {
103     double value = getFloatingPointAttribute(valueAttr, 0);
104     return std::min(std::max(value, min()), max());
105 }
106 
setValue(double value,ExceptionState & exceptionState)107 void HTMLMeterElement::setValue(double value, ExceptionState& exceptionState)
108 {
109     if (!std::isfinite(value)) {
110         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(value));
111         return;
112     }
113     setFloatingPointAttribute(valueAttr, value);
114 }
115 
low() const116 double HTMLMeterElement::low() const
117 {
118     double low = getFloatingPointAttribute(lowAttr, min());
119     return std::min(std::max(low, min()), max());
120 }
121 
setLow(double low,ExceptionState & exceptionState)122 void HTMLMeterElement::setLow(double low, ExceptionState& exceptionState)
123 {
124     if (!std::isfinite(low)) {
125         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(low));
126         return;
127     }
128     setFloatingPointAttribute(lowAttr, low);
129 }
130 
high() const131 double HTMLMeterElement::high() const
132 {
133     double high = getFloatingPointAttribute(highAttr, max());
134     return std::min(std::max(high, low()), max());
135 }
136 
setHigh(double high,ExceptionState & exceptionState)137 void HTMLMeterElement::setHigh(double high, ExceptionState& exceptionState)
138 {
139     if (!std::isfinite(high)) {
140         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(high));
141         return;
142     }
143     setFloatingPointAttribute(highAttr, high);
144 }
145 
optimum() const146 double HTMLMeterElement::optimum() const
147 {
148     double optimum = getFloatingPointAttribute(optimumAttr, (max() + min()) / 2);
149     return std::min(std::max(optimum, min()), max());
150 }
151 
setOptimum(double optimum,ExceptionState & exceptionState)152 void HTMLMeterElement::setOptimum(double optimum, ExceptionState& exceptionState)
153 {
154     if (!std::isfinite(optimum)) {
155         exceptionState.throwDOMException(NotSupportedError, ExceptionMessages::notAFiniteNumber(optimum));
156         return;
157     }
158     setFloatingPointAttribute(optimumAttr, optimum);
159 }
160 
gaugeRegion() const161 HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
162 {
163     double lowValue = low();
164     double highValue = high();
165     double theValue = value();
166     double optimumValue = optimum();
167 
168     if (optimumValue < lowValue) {
169         // The optimum range stays under low
170         if (theValue <= lowValue)
171             return GaugeRegionOptimum;
172         if (theValue <= highValue)
173             return GaugeRegionSuboptimal;
174         return GaugeRegionEvenLessGood;
175     }
176 
177     if (highValue < optimumValue) {
178         // The optimum range stays over high
179         if (highValue <= theValue)
180             return GaugeRegionOptimum;
181         if (lowValue <= theValue)
182             return GaugeRegionSuboptimal;
183         return GaugeRegionEvenLessGood;
184     }
185 
186     // The optimum range stays between high and low.
187     // According to the standard, <meter> never show GaugeRegionEvenLessGood in this case
188     // because the value is never less or greater than min or max.
189     if (lowValue <= theValue && theValue <= highValue)
190         return GaugeRegionOptimum;
191     return GaugeRegionSuboptimal;
192 }
193 
valueRatio() const194 double HTMLMeterElement::valueRatio() const
195 {
196     double min = this->min();
197     double max = this->max();
198     double value = this->value();
199 
200     if (max <= min)
201         return 0;
202     return (value - min) / (max - min);
203 }
204 
didElementStateChange()205 void HTMLMeterElement::didElementStateChange()
206 {
207     m_value->setWidthPercentage(valueRatio()*100);
208     m_value->updatePseudo();
209     if (RenderMeter* render = renderMeter())
210         render->updateFromElement();
211 }
212 
renderMeter() const213 RenderMeter* HTMLMeterElement::renderMeter() const
214 {
215     if (renderer() && renderer()->isMeter())
216         return toRenderMeter(renderer());
217 
218     RenderObject* renderObject = userAgentShadowRoot()->firstChild()->renderer();
219     return toRenderMeter(renderObject);
220 }
221 
didAddUserAgentShadowRoot(ShadowRoot & root)222 void HTMLMeterElement::didAddUserAgentShadowRoot(ShadowRoot& root)
223 {
224     ASSERT(!m_value);
225 
226     RefPtr<MeterInnerElement> inner = MeterInnerElement::create(document());
227     root.appendChild(inner);
228 
229     RefPtr<MeterBarElement> bar = MeterBarElement::create(document());
230     m_value = MeterValueElement::create(document());
231     m_value->setWidthPercentage(0);
232     m_value->updatePseudo();
233     bar->appendChild(m_value);
234 
235     inner->appendChild(bar);
236 }
237 
238 } // namespace
239