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>
|
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>
|
2022-07-22 16:08:03 +01:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
|
|
|
#include <LibWeb/Layout/Node.h>
|
2022-02-16 19:12:54 +01:00
|
|
|
#include <LibWeb/Layout/Progress.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
|
|
|
}
|
|
|
|
|
2022-10-17 14:41:50 +02:00
|
|
|
JS::GCPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
|
2022-02-16 19:12:54 +01:00
|
|
|
{
|
2022-10-08 22:15:32 +01:00
|
|
|
if (style->appearance().value_or(CSS::Appearance::Auto) == CSS::Appearance::None)
|
|
|
|
return HTMLElement::create_layout_node(style);
|
2022-10-17 14:41:50 +02:00
|
|
|
return heap().allocate_without_realm<Layout::Progress>(document(), *this, move(style));
|
2022-07-22 16:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTMLProgressElement::using_system_appearance() const
|
|
|
|
{
|
|
|
|
if (layout_node())
|
|
|
|
return is<Layout::Progress>(*layout_node());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTMLProgressElement::progress_position_updated()
|
|
|
|
{
|
|
|
|
if (using_system_appearance())
|
|
|
|
layout_node()->set_needs_display();
|
|
|
|
else
|
|
|
|
document().invalidate_layout();
|
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-11-15 19:54:01 +01:00
|
|
|
auto maybe_value_string = get_attribute(HTML::AttributeNames::value);
|
|
|
|
if (!maybe_value_string.has_value())
|
2022-03-03 13:50:39 +01:00
|
|
|
return 0;
|
2023-11-15 19:54:01 +01:00
|
|
|
auto maybe_value = parse_floating_point_number(maybe_value_string.value());
|
|
|
|
if (!maybe_value.has_value())
|
2022-02-16 19:12:54 +01:00
|
|
|
return 0;
|
2023-11-15 19:54:01 +01:00
|
|
|
return clamp(maybe_value.value(), 0, max());
|
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))));
|
2022-07-22 16:08:03 +01:00
|
|
|
progress_position_updated();
|
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-11-15 19:54:01 +01:00
|
|
|
auto maybe_max_string = get_attribute(HTML::AttributeNames::max);
|
|
|
|
if (!maybe_max_string.has_value())
|
2022-03-03 13:50:39 +01:00
|
|
|
return 1;
|
2023-11-15 19:54:01 +01:00
|
|
|
auto maybe_max = parse_floating_point_number(maybe_max_string.value());
|
|
|
|
if (!maybe_max.has_value())
|
2022-02-16 19:12:54 +01:00
|
|
|
return 1;
|
2023-11-15 19:54:01 +01:00
|
|
|
return AK::max(maybe_max.value(), 0);
|
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))));
|
2022-07-22 16:08:03 +01:00
|
|
|
progress_position_updated();
|
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();
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:05:43 +01:00
|
|
|
}
|