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 "mozilla/EventStates.h"
8 : #include "mozilla/dom/HTMLProgressElement.h"
9 : #include "mozilla/dom/HTMLProgressElementBinding.h"
10 :
11 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Progress)
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : const double HTMLProgressElement::kIndeterminatePosition = -1.0;
17 : const double HTMLProgressElement::kDefaultValue = 0.0;
18 : const double HTMLProgressElement::kDefaultMax = 1.0;
19 :
20 :
21 0 : HTMLProgressElement::HTMLProgressElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
22 0 : : nsGenericHTMLElement(aNodeInfo)
23 : {
24 : // We start out indeterminate
25 0 : AddStatesSilently(NS_EVENT_STATE_INDETERMINATE);
26 0 : }
27 :
28 0 : HTMLProgressElement::~HTMLProgressElement()
29 : {
30 0 : }
31 :
32 0 : NS_IMPL_ELEMENT_CLONE(HTMLProgressElement)
33 :
34 :
35 : EventStates
36 0 : HTMLProgressElement::IntrinsicState() const
37 : {
38 0 : EventStates state = nsGenericHTMLElement::IntrinsicState();
39 :
40 0 : if (IsIndeterminate()) {
41 0 : state |= NS_EVENT_STATE_INDETERMINATE;
42 : }
43 :
44 0 : return state;
45 : }
46 :
47 : bool
48 0 : HTMLProgressElement::ParseAttribute(int32_t aNamespaceID, nsIAtom* aAttribute,
49 : const nsAString& aValue, nsAttrValue& aResult)
50 : {
51 0 : if (aNamespaceID == kNameSpaceID_None) {
52 0 : if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) {
53 0 : return aResult.ParseDoubleValue(aValue);
54 : }
55 : }
56 :
57 0 : return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute,
58 0 : aValue, aResult);
59 : }
60 :
61 : double
62 0 : HTMLProgressElement::Value() const
63 : {
64 0 : const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
65 0 : if (!attrValue || attrValue->Type() != nsAttrValue::eDoubleValue ||
66 0 : attrValue->GetDoubleValue() < 0.0) {
67 0 : return kDefaultValue;
68 : }
69 :
70 0 : return std::min(attrValue->GetDoubleValue(), Max());
71 : }
72 :
73 : double
74 0 : HTMLProgressElement::Max() const
75 : {
76 0 : const nsAttrValue* attrMax = mAttrsAndChildren.GetAttr(nsGkAtoms::max);
77 0 : if (!attrMax || attrMax->Type() != nsAttrValue::eDoubleValue ||
78 0 : attrMax->GetDoubleValue() <= 0.0) {
79 0 : return kDefaultMax;
80 : }
81 :
82 0 : return attrMax->GetDoubleValue();
83 : }
84 :
85 : double
86 0 : HTMLProgressElement::Position() const
87 : {
88 0 : if (IsIndeterminate()) {
89 0 : return kIndeterminatePosition;
90 : }
91 :
92 0 : return Value() / Max();
93 : }
94 :
95 : bool
96 0 : HTMLProgressElement::IsIndeterminate() const
97 : {
98 0 : const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
99 0 : return !attrValue || attrValue->Type() != nsAttrValue::eDoubleValue;
100 : }
101 :
102 : JSObject*
103 0 : HTMLProgressElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
104 : {
105 0 : return HTMLProgressElementBinding::Wrap(aCx, this, aGivenProto);
106 : }
107 :
108 : } // namespace dom
109 : } // namespace mozilla
|