2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
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.
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.
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.
23 #include "core/html/HTMLProgressElement.h"
25 #include "bindings/core/v8/ExceptionMessages.h"
26 #include "bindings/core/v8/ExceptionState.h"
27 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
28 #include "core/HTMLNames.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/dom/shadow/ShadowRoot.h"
31 #include "core/frame/UseCounter.h"
32 #include "core/html/parser/HTMLParserIdioms.h"
33 #include "core/html/shadow/ProgressShadowElement.h"
34 #include "core/rendering/RenderProgress.h"
38 using namespace HTMLNames;
40 const double HTMLProgressElement::IndeterminatePosition = -1;
41 const double HTMLProgressElement::InvalidPosition = -2;
43 HTMLProgressElement::HTMLProgressElement(Document& document)
44 : LabelableElement(progressTag, document)
47 ScriptWrappable::init(this);
48 UseCounter::count(document, UseCounter::ProgressElement);
51 HTMLProgressElement::~HTMLProgressElement()
55 PassRefPtrWillBeRawPtr<HTMLProgressElement> HTMLProgressElement::create(Document& document)
57 RefPtrWillBeRawPtr<HTMLProgressElement> progress = adoptRefWillBeNoop(new HTMLProgressElement(document));
58 progress->ensureUserAgentShadowRoot();
59 return progress.release();
62 RenderObject* HTMLProgressElement::createRenderer(RenderStyle* style)
64 if (!style->hasAppearance() || hasAuthorShadowRoot())
65 return RenderObject::createObject(this, style);
67 return new RenderProgress(this);
70 RenderProgress* HTMLProgressElement::renderProgress() const
72 if (renderer() && renderer()->isProgress())
73 return toRenderProgress(renderer());
75 RenderObject* renderObject = userAgentShadowRoot()->firstChild()->renderer();
76 ASSERT_WITH_SECURITY_IMPLICATION(!renderObject || renderObject->isProgress());
77 return toRenderProgress(renderObject);
80 void HTMLProgressElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
82 if (name == valueAttr)
83 didElementStateChange();
84 else if (name == maxAttr)
85 didElementStateChange();
87 LabelableElement::parseAttribute(name, value);
90 void HTMLProgressElement::attach(const AttachContext& context)
92 LabelableElement::attach(context);
93 if (RenderProgress* render = renderProgress())
94 render->updateFromElement();
97 double HTMLProgressElement::value() const
99 double value = getFloatingPointAttribute(valueAttr);
100 // Otherwise, if the parsed value was greater than or equal to the maximum
101 // value, then the current value of the progress bar is the maximum value
102 // of the progress bar. Otherwise, if parsing the value attribute's value
103 // resulted in an error, or a number less than or equal to zero, then the
104 // current value of the progress bar is zero.
105 return !std::isfinite(value) || value < 0 ? 0 : std::min(value, max());
108 void HTMLProgressElement::setValue(double value)
110 setFloatingPointAttribute(valueAttr, std::max(value, 0.));
113 double HTMLProgressElement::max() const
115 double max = getFloatingPointAttribute(maxAttr);
116 // Otherwise, if the element has no max attribute, or if it has one but
117 // parsing it resulted in an error, or if the parsed value was less than or
118 // equal to zero, then the maximum value of the progress bar is 1.0.
119 return !std::isfinite(max) || max <= 0 ? 1 : max;
122 void HTMLProgressElement::setMax(double max)
124 // FIXME: The specification says we should ignore the input value if it is inferior or equal to 0.
125 setFloatingPointAttribute(maxAttr, max > 0 ? max : 1);
128 double HTMLProgressElement::position() const
130 if (!isDeterminate())
131 return HTMLProgressElement::IndeterminatePosition;
132 return value() / max();
135 bool HTMLProgressElement::isDeterminate() const
137 return fastHasAttribute(valueAttr);
140 void HTMLProgressElement::didElementStateChange()
142 m_value->setWidthPercentage(position() * 100);
143 if (RenderProgress* render = renderProgress()) {
144 bool wasDeterminate = render->isDeterminate();
145 render->updateFromElement();
146 if (wasDeterminate != isDeterminate())
147 didAffectSelector(AffectedSelectorIndeterminate);
151 void HTMLProgressElement::didAddUserAgentShadowRoot(ShadowRoot& root)
155 RefPtrWillBeRawPtr<ProgressInnerElement> inner = ProgressInnerElement::create(document());
156 inner->setShadowPseudoId(AtomicString("-webkit-progress-inner-element", AtomicString::ConstructFromLiteral));
157 root.appendChild(inner);
159 RefPtrWillBeRawPtr<ProgressBarElement> bar = ProgressBarElement::create(document());
160 bar->setShadowPseudoId(AtomicString("-webkit-progress-bar", AtomicString::ConstructFromLiteral));
161 RefPtrWillBeRawPtr<ProgressValueElement> value = ProgressValueElement::create(document());
162 m_value = value.get();
163 m_value->setShadowPseudoId(AtomicString("-webkit-progress-value", AtomicString::ConstructFromLiteral));
164 m_value->setWidthPercentage(HTMLProgressElement::IndeterminatePosition * 100);
165 bar->appendChild(m_value);
167 inner->appendChild(bar);
170 bool HTMLProgressElement::shouldAppearIndeterminate() const
172 return !isDeterminate();
175 void HTMLProgressElement::trace(Visitor* visitor)
177 visitor->trace(m_value);
178 LabelableElement::trace(visitor);