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 NS_IMPL_NS_NEW_HTML_ELEMENT(Progress)
12
13 namespace mozilla::dom {
14
15 const double HTMLProgressElement::kIndeterminatePosition = -1.0;
16 const double HTMLProgressElement::kDefaultValue = 0.0;
17 const double HTMLProgressElement::kDefaultMax = 1.0;
18
HTMLProgressElement(already_AddRefed<mozilla::dom::NodeInfo> && aNodeInfo)19 HTMLProgressElement::HTMLProgressElement(
20 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
21 : nsGenericHTMLElement(std::move(aNodeInfo)) {
22 // We start out indeterminate
23 AddStatesSilently(NS_EVENT_STATE_INDETERMINATE);
24 }
25
26 HTMLProgressElement::~HTMLProgressElement() = default;
27
NS_IMPL_ELEMENT_CLONE(HTMLProgressElement)28 NS_IMPL_ELEMENT_CLONE(HTMLProgressElement)
29
30 EventStates HTMLProgressElement::IntrinsicState() const {
31 EventStates state = nsGenericHTMLElement::IntrinsicState();
32
33 if (IsIndeterminate()) {
34 state |= NS_EVENT_STATE_INDETERMINATE;
35 }
36
37 return state;
38 }
39
ParseAttribute(int32_t aNamespaceID,nsAtom * aAttribute,const nsAString & aValue,nsIPrincipal * aMaybeScriptedPrincipal,nsAttrValue & aResult)40 bool HTMLProgressElement::ParseAttribute(int32_t aNamespaceID,
41 nsAtom* aAttribute,
42 const nsAString& aValue,
43 nsIPrincipal* aMaybeScriptedPrincipal,
44 nsAttrValue& aResult) {
45 if (aNamespaceID == kNameSpaceID_None) {
46 if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) {
47 return aResult.ParseDoubleValue(aValue);
48 }
49 }
50
51 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
52 aMaybeScriptedPrincipal, aResult);
53 }
54
Value() const55 double HTMLProgressElement::Value() const {
56 const nsAttrValue* attrValue = mAttrs.GetAttr(nsGkAtoms::value);
57 if (!attrValue || attrValue->Type() != nsAttrValue::eDoubleValue ||
58 attrValue->GetDoubleValue() < 0.0) {
59 return kDefaultValue;
60 }
61
62 return std::min(attrValue->GetDoubleValue(), Max());
63 }
64
Max() const65 double HTMLProgressElement::Max() const {
66 const nsAttrValue* attrMax = mAttrs.GetAttr(nsGkAtoms::max);
67 if (!attrMax || attrMax->Type() != nsAttrValue::eDoubleValue ||
68 attrMax->GetDoubleValue() <= 0.0) {
69 return kDefaultMax;
70 }
71
72 return attrMax->GetDoubleValue();
73 }
74
Position() const75 double HTMLProgressElement::Position() const {
76 if (IsIndeterminate()) {
77 return kIndeterminatePosition;
78 }
79
80 return Value() / Max();
81 }
82
IsIndeterminate() const83 bool HTMLProgressElement::IsIndeterminate() const {
84 const nsAttrValue* attrValue = mAttrs.GetAttr(nsGkAtoms::value);
85 return !attrValue || attrValue->Type() != nsAttrValue::eDoubleValue;
86 }
87
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)88 JSObject* HTMLProgressElement::WrapNode(JSContext* aCx,
89 JS::Handle<JSObject*> aGivenProto) {
90 return HTMLProgressElement_Binding::Wrap(aCx, this, aGivenProto);
91 }
92
93 } // namespace mozilla::dom
94