2020-08-01 03:05:43 +01:00
|
|
|
/*
|
2022-02-16 19:12:54 +01:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2022-07-22 16:08:03 +01:00
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
2023-12-05 21:18:15 +01:00
|
|
|
* Copyright (c) 2023, Bastiaan van der Plaat <bastiaan.v.d.plaat@gmail.com>
|
2020-08-01 03:05:43 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-01 03:05:43 +01:00
|
|
|
*/
|
|
|
|
|
2022-07-22 16:08:03 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2020-08-01 03:05:43 +01:00
|
|
|
#include <LibWeb/HTML/HTMLProgressElement.h>
|
2023-11-15 19:54:01 +01:00
|
|
|
#include <LibWeb/HTML/Numbers.h>
|
2020-08-01 03:05:43 +01:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
2023-11-19 19:47:52 +01:00
|
|
|
JS_DEFINE_ALLOCATOR(HTMLProgressElement);
|
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
HTMLProgressElement::HTMLProgressElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 11:20:15 +01:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-08-01 03:05:43 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
HTMLProgressElement::~HTMLProgressElement() = default;
|
2020-08-01 03:05:43 +01:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void HTMLProgressElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2023-11-22 12:55:21 +13:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLProgressElementPrototype>(realm, "HTMLProgressElement"_fly_string));
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-12-05 21:18:15 +01:00
|
|
|
void HTMLProgressElement::visit_edges(Cell::Visitor& visitor)
|
2022-02-16 19:12:54 +01:00
|
|
|
{
|
2023-12-05 21:18:15 +01:00
|
|
|
Base::visit_edges(visitor);
|
|
|
|
visitor.visit(m_progress_value_element);
|
2022-02-16 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 19:54:01 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-value
|
2022-02-16 19:12:54 +01:00
|
|
|
double HTMLProgressElement::value() const
|
|
|
|
{
|
2023-12-07 18:40:54 +01:00
|
|
|
if (auto value_string = get_attribute(HTML::AttributeNames::value); value_string.has_value()) {
|
|
|
|
if (auto value = parse_floating_point_number(*value_string); value.has_value())
|
|
|
|
return clamp(*value, 0, max());
|
|
|
|
}
|
|
|
|
return 0;
|
2022-02-16 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 20:37:57 +01:00
|
|
|
WebIDL::ExceptionOr<void> HTMLProgressElement::set_value(double value)
|
2022-02-16 19:12:54 +01:00
|
|
|
{
|
2023-11-15 19:54:01 +01:00
|
|
|
if (value < 0 || value > max())
|
2023-05-25 20:37:57 +01:00
|
|
|
return {};
|
2022-02-16 19:12:54 +01:00
|
|
|
|
2023-10-08 11:42:00 +13:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::value, MUST(String::number(value))));
|
2023-12-05 21:18:15 +01:00
|
|
|
update_progress_value_element();
|
|
|
|
document().invalidate_layout();
|
2023-05-25 20:37:57 +01:00
|
|
|
return {};
|
2022-02-16 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
2023-11-15 19:54:01 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/form-elements.html#dom-progress-max
|
2022-02-16 19:12:54 +01:00
|
|
|
double HTMLProgressElement::max() const
|
|
|
|
{
|
2023-12-07 18:40:54 +01:00
|
|
|
if (auto max_string = get_attribute(HTML::AttributeNames::max); max_string.has_value()) {
|
|
|
|
if (auto max = parse_floating_point_number(*max_string); max.has_value())
|
|
|
|
return AK::max(*max, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
2022-02-16 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
2023-05-25 20:37:57 +01:00
|
|
|
WebIDL::ExceptionOr<void> HTMLProgressElement::set_max(double value)
|
2022-02-16 19:12:54 +01:00
|
|
|
{
|
|
|
|
if (value <= 0)
|
2023-05-25 20:37:57 +01:00
|
|
|
return {};
|
2022-02-16 19:12:54 +01:00
|
|
|
|
2023-10-08 11:42:00 +13:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::max, MUST(String::number(value))));
|
2023-12-05 21:18:15 +01:00
|
|
|
update_progress_value_element();
|
|
|
|
document().invalidate_layout();
|
2023-05-25 20:37:57 +01:00
|
|
|
return {};
|
2022-02-16 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double HTMLProgressElement::position() const
|
|
|
|
{
|
|
|
|
if (!is_determinate())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return value() / max();
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:18:15 +01:00
|
|
|
void HTMLProgressElement::inserted()
|
|
|
|
{
|
|
|
|
create_shadow_tree_if_needed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLProgressElement::removed_from(DOM::Node*)
|
|
|
|
{
|
|
|
|
set_shadow_root(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLProgressElement::create_shadow_tree_if_needed()
|
|
|
|
{
|
|
|
|
if (shadow_root_internal())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto shadow_root = heap().allocate<DOM::ShadowRoot>(realm(), document(), *this, Bindings::ShadowRootMode::Closed);
|
|
|
|
set_shadow_root(shadow_root);
|
|
|
|
|
|
|
|
auto progress_bar_element = heap().allocate<ProgressBarElement>(realm(), document());
|
|
|
|
MUST(shadow_root->append_child(*progress_bar_element));
|
|
|
|
|
|
|
|
m_progress_value_element = heap().allocate<ProgressValueElement>(realm(), document());
|
|
|
|
MUST(progress_bar_element->append_child(*m_progress_value_element));
|
|
|
|
update_progress_value_element();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLProgressElement::update_progress_value_element()
|
|
|
|
{
|
|
|
|
MUST(m_progress_value_element->set_attribute(HTML::AttributeNames::style, MUST(String::formatted("width: {}%;", position() * 100))));
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:05:43 +01:00
|
|
|
}
|