Bug 1655413 [wpt PR 24763] - Make CSP default-src without 'unsafe-eval' block eval...
[gecko.git] / dom / html / HTMLProgressElement.cpp
blobdfa322e7a4eca63bf24ae8c19f18d4ec6eb4b99a
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/. */
7 #include "mozilla/EventStates.h"
8 #include "mozilla/dom/HTMLProgressElement.h"
9 #include "mozilla/dom/HTMLProgressElementBinding.h"
11 NS_IMPL_NS_NEW_HTML_ELEMENT(Progress)
13 namespace mozilla {
14 namespace dom {
16 const double HTMLProgressElement::kIndeterminatePosition = -1.0;
17 const double HTMLProgressElement::kDefaultValue = 0.0;
18 const double HTMLProgressElement::kDefaultMax = 1.0;
20 HTMLProgressElement::HTMLProgressElement(
21 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
22 : nsGenericHTMLElement(std::move(aNodeInfo)) {
23 // We start out indeterminate
24 AddStatesSilently(NS_EVENT_STATE_INDETERMINATE);
27 HTMLProgressElement::~HTMLProgressElement() = default;
29 NS_IMPL_ELEMENT_CLONE(HTMLProgressElement)
31 EventStates HTMLProgressElement::IntrinsicState() const {
32 EventStates state = nsGenericHTMLElement::IntrinsicState();
34 if (IsIndeterminate()) {
35 state |= NS_EVENT_STATE_INDETERMINATE;
38 return state;
41 bool HTMLProgressElement::ParseAttribute(int32_t aNamespaceID,
42 nsAtom* aAttribute,
43 const nsAString& aValue,
44 nsIPrincipal* aMaybeScriptedPrincipal,
45 nsAttrValue& aResult) {
46 if (aNamespaceID == kNameSpaceID_None) {
47 if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) {
48 return aResult.ParseDoubleValue(aValue);
52 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
53 aMaybeScriptedPrincipal, aResult);
56 double HTMLProgressElement::Value() const {
57 const nsAttrValue* attrValue = mAttrs.GetAttr(nsGkAtoms::value);
58 if (!attrValue || attrValue->Type() != nsAttrValue::eDoubleValue ||
59 attrValue->GetDoubleValue() < 0.0) {
60 return kDefaultValue;
63 return std::min(attrValue->GetDoubleValue(), Max());
66 double HTMLProgressElement::Max() const {
67 const nsAttrValue* attrMax = mAttrs.GetAttr(nsGkAtoms::max);
68 if (!attrMax || attrMax->Type() != nsAttrValue::eDoubleValue ||
69 attrMax->GetDoubleValue() <= 0.0) {
70 return kDefaultMax;
73 return attrMax->GetDoubleValue();
76 double HTMLProgressElement::Position() const {
77 if (IsIndeterminate()) {
78 return kIndeterminatePosition;
81 return Value() / Max();
84 bool HTMLProgressElement::IsIndeterminate() const {
85 const nsAttrValue* attrValue = mAttrs.GetAttr(nsGkAtoms::value);
86 return !attrValue || attrValue->Type() != nsAttrValue::eDoubleValue;
89 JSObject* HTMLProgressElement::WrapNode(JSContext* aCx,
90 JS::Handle<JSObject*> aGivenProto) {
91 return HTMLProgressElement_Binding::Wrap(aCx, this, aGivenProto);
94 } // namespace dom
95 } // namespace mozilla