HTMLProgressElement.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/EventStates.h"
  6. #include "mozilla/dom/HTMLProgressElement.h"
  7. #include "mozilla/dom/HTMLProgressElementBinding.h"
  8. NS_IMPL_NS_NEW_HTML_ELEMENT(Progress)
  9. namespace mozilla {
  10. namespace dom {
  11. const double HTMLProgressElement::kIndeterminatePosition = -1.0;
  12. const double HTMLProgressElement::kDefaultValue = 0.0;
  13. const double HTMLProgressElement::kDefaultMax = 1.0;
  14. HTMLProgressElement::HTMLProgressElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  15. : nsGenericHTMLElement(aNodeInfo)
  16. {
  17. // We start out indeterminate
  18. AddStatesSilently(NS_EVENT_STATE_INDETERMINATE);
  19. }
  20. HTMLProgressElement::~HTMLProgressElement()
  21. {
  22. }
  23. NS_IMPL_ELEMENT_CLONE(HTMLProgressElement)
  24. EventStates
  25. HTMLProgressElement::IntrinsicState() const
  26. {
  27. EventStates state = nsGenericHTMLElement::IntrinsicState();
  28. if (IsIndeterminate()) {
  29. state |= NS_EVENT_STATE_INDETERMINATE;
  30. }
  31. return state;
  32. }
  33. bool
  34. HTMLProgressElement::ParseAttribute(int32_t aNamespaceID, nsIAtom* aAttribute,
  35. const nsAString& aValue, nsAttrValue& aResult)
  36. {
  37. if (aNamespaceID == kNameSpaceID_None) {
  38. if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) {
  39. return aResult.ParseDoubleValue(aValue);
  40. }
  41. }
  42. return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute,
  43. aValue, aResult);
  44. }
  45. double
  46. HTMLProgressElement::Value() const
  47. {
  48. const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
  49. if (!attrValue || attrValue->Type() != nsAttrValue::eDoubleValue ||
  50. attrValue->GetDoubleValue() < 0.0) {
  51. return kDefaultValue;
  52. }
  53. return std::min(attrValue->GetDoubleValue(), Max());
  54. }
  55. double
  56. HTMLProgressElement::Max() const
  57. {
  58. const nsAttrValue* attrMax = mAttrsAndChildren.GetAttr(nsGkAtoms::max);
  59. if (!attrMax || attrMax->Type() != nsAttrValue::eDoubleValue ||
  60. attrMax->GetDoubleValue() <= 0.0) {
  61. return kDefaultMax;
  62. }
  63. return attrMax->GetDoubleValue();
  64. }
  65. double
  66. HTMLProgressElement::Position() const
  67. {
  68. if (IsIndeterminate()) {
  69. return kIndeterminatePosition;
  70. }
  71. return Value() / Max();
  72. }
  73. bool
  74. HTMLProgressElement::IsIndeterminate() const
  75. {
  76. const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
  77. return !attrValue || attrValue->Type() != nsAttrValue::eDoubleValue;
  78. }
  79. JSObject*
  80. HTMLProgressElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  81. {
  82. return HTMLProgressElementBinding::Wrap(aCx, this, aGivenProto);
  83. }
  84. } // namespace dom
  85. } // namespace mozilla