HTMLProgressElement.cpp (3163B)
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/dom/HTMLProgressElement.h" 8 9 #include "mozilla/dom/HTMLProgressElementBinding.h" 10 11 NS_IMPL_NS_NEW_HTML_ELEMENT(Progress) 12 13 namespace mozilla::dom { 14 15 HTMLProgressElement::HTMLProgressElement( 16 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) 17 : nsGenericHTMLElement(std::move(aNodeInfo)) { 18 // We start out indeterminate 19 AddStatesSilently(ElementState::INDETERMINATE); 20 } 21 22 HTMLProgressElement::~HTMLProgressElement() = default; 23 24 NS_IMPL_ELEMENT_CLONE(HTMLProgressElement) 25 26 bool HTMLProgressElement::ParseAttribute(int32_t aNamespaceID, 27 nsAtom* aAttribute, 28 const nsAString& aValue, 29 nsIPrincipal* aMaybeScriptedPrincipal, 30 nsAttrValue& aResult) { 31 if (aNamespaceID == kNameSpaceID_None) { 32 if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) { 33 return aResult.ParseDoubleValue(aValue); 34 } 35 } 36 37 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue, 38 aMaybeScriptedPrincipal, aResult); 39 } 40 41 void HTMLProgressElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, 42 const nsAttrValue* aValue, 43 const nsAttrValue* aOldValue, 44 nsIPrincipal* aSubjectPrincipal, 45 bool aNotify) { 46 if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::value) { 47 const bool indeterminate = 48 !aValue || aValue->Type() != nsAttrValue::eDoubleValue; 49 SetStates(ElementState::INDETERMINATE, indeterminate, aNotify); 50 } 51 return nsGenericHTMLElement::AfterSetAttr( 52 aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify); 53 } 54 55 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 0.0; 60 } 61 62 return std::min(attrValue->GetDoubleValue(), Max()); 63 } 64 65 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 1.0; 70 } 71 72 return attrMax->GetDoubleValue(); 73 } 74 75 double HTMLProgressElement::Position() const { 76 if (State().HasState(ElementState::INDETERMINATE)) { 77 return -1.0; 78 } 79 80 return Value() / Max(); 81 } 82 83 JSObject* HTMLProgressElement::WrapNode(JSContext* aCx, 84 JS::Handle<JSObject*> aGivenProto) { 85 return HTMLProgressElement_Binding::Wrap(aCx, this, aGivenProto); 86 } 87 88 } // namespace mozilla::dom