HTMLProgressElement.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/ShadowRoot.h>
  9. #include <LibWeb/HTML/HTMLProgressElement.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/Node.h>
  12. #include <LibWeb/Layout/Progress.h>
  13. #include <stdlib.h>
  14. namespace Web::HTML {
  15. HTMLProgressElement::HTMLProgressElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : HTMLElement(document, move(qualified_name))
  17. {
  18. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLProgressElement"));
  19. }
  20. HTMLProgressElement::~HTMLProgressElement() = default;
  21. RefPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
  22. {
  23. if (style->appearance().value_or(CSS::Appearance::Auto) == CSS::Appearance::None) {
  24. return adopt_ref(*new Layout::BlockContainer(document(), this, move(style)));
  25. }
  26. return adopt_ref(*new Layout::Progress(document(), *this, move(style)));
  27. }
  28. bool HTMLProgressElement::using_system_appearance() const
  29. {
  30. if (layout_node())
  31. return is<Layout::Progress>(*layout_node());
  32. return false;
  33. }
  34. void HTMLProgressElement::progress_position_updated()
  35. {
  36. if (using_system_appearance())
  37. layout_node()->set_needs_display();
  38. else
  39. document().invalidate_layout();
  40. }
  41. double HTMLProgressElement::value() const
  42. {
  43. auto value_characters = attribute(HTML::AttributeNames::value).characters();
  44. if (value_characters == nullptr)
  45. return 0;
  46. auto parsed_value = strtod(value_characters, nullptr);
  47. if (!isfinite(parsed_value) || parsed_value < 0)
  48. return 0;
  49. return min(parsed_value, max());
  50. }
  51. void HTMLProgressElement::set_value(double value)
  52. {
  53. if (value < 0)
  54. return;
  55. set_attribute(HTML::AttributeNames::value, String::number(value));
  56. progress_position_updated();
  57. }
  58. double HTMLProgressElement::max() const
  59. {
  60. auto max_characters = attribute(HTML::AttributeNames::max).characters();
  61. if (max_characters == nullptr)
  62. return 1;
  63. auto parsed_value = strtod(max_characters, nullptr);
  64. if (!isfinite(parsed_value) || parsed_value <= 0)
  65. return 1;
  66. return parsed_value;
  67. }
  68. void HTMLProgressElement::set_max(double value)
  69. {
  70. if (value <= 0)
  71. return;
  72. set_attribute(HTML::AttributeNames::max, String::number(value));
  73. progress_position_updated();
  74. }
  75. double HTMLProgressElement::position() const
  76. {
  77. if (!is_determinate())
  78. return -1;
  79. return value() / max();
  80. }
  81. }