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>
|
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 {
|
|
|
|
|
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-01-28 12:33:35 -05:00
|
|
|
JS::ThrowCompletionOr<void> HTMLProgressElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2023-01-28 12:33:35 -05:00
|
|
|
MUST_OR_THROW_OOM(Base::initialize(realm));
|
2023-01-10 06:28:20 -05:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLProgressElementPrototype>(realm, "HTMLProgressElement"));
|
2023-01-28 12:33:35 -05:00
|
|
|
|
|
|
|
return {};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
double HTMLProgressElement::value() const
|
|
|
|
{
|
2022-10-12 02:24:14 +02:00
|
|
|
auto const& value_characters = attribute(HTML::AttributeNames::value);
|
2022-03-03 13:50:39 +01:00
|
|
|
if (value_characters == nullptr)
|
|
|
|
return 0;
|
|
|
|
|
2022-10-12 02:24:14 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
|
|
|
|
// 6. Skip ASCII whitespace within input given position.
|
|
|
|
auto maybe_double = value_characters.to_double(AK::TrimWhitespace::Yes);
|
|
|
|
if (!maybe_double.has_value())
|
|
|
|
return 0;
|
|
|
|
if (!isfinite(maybe_double.value()) || maybe_double.value() < 0)
|
2022-02-16 19:12:54 +01:00
|
|
|
return 0;
|
|
|
|
|
2022-10-12 02:24:14 +02:00
|
|
|
return min(maybe_double.value(), 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
|
|
|
{
|
|
|
|
if (value < 0)
|
2023-05-25 20:37:57 +01:00
|
|
|
return {};
|
2022-02-16 19:12:54 +01:00
|
|
|
|
2023-05-25 20:37:57 +01:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::value, DeprecatedString::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::max() const
|
|
|
|
{
|
2022-10-12 02:24:14 +02:00
|
|
|
auto const& max_characters = attribute(HTML::AttributeNames::max);
|
2022-03-03 13:50:39 +01:00
|
|
|
if (max_characters == nullptr)
|
|
|
|
return 1;
|
|
|
|
|
2022-10-12 02:24:14 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
|
|
|
|
// 6. Skip ASCII whitespace within input given position.
|
|
|
|
auto double_or_none = max_characters.to_double(AK::TrimWhitespace::Yes);
|
|
|
|
if (!double_or_none.has_value())
|
|
|
|
return 1;
|
|
|
|
if (!isfinite(double_or_none.value()) || double_or_none.value() <= 0)
|
2022-02-16 19:12:54 +01:00
|
|
|
return 1;
|
|
|
|
|
2022-10-12 02:24:14 +02:00
|
|
|
return double_or_none.value();
|
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-05-25 20:37:57 +01:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::max, DeprecatedString::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
|
|
|
}
|