2020-07-31 23:05:43 -03:00
|
|
|
/*
|
2022-02-16 15:12:54 -03:00
|
|
|
* Copyright (c) 2020-2022, the SerenityOS developers.
|
2022-07-22 12:08:03 -03:00
|
|
|
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
|
2020-07-31 23:05:43 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 23:05:43 -03:00
|
|
|
*/
|
|
|
|
|
|
2022-07-22 12:08:03 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2020-07-31 23:05:43 -03:00
|
|
|
#include <LibWeb/HTML/HTMLProgressElement.h>
|
2022-07-22 12:08:03 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
|
|
|
|
#include <LibWeb/Layout/Node.h>
|
2022-02-16 15:12:54 -03:00
|
|
|
#include <LibWeb/Layout/Progress.h>
|
2020-07-31 23:05:43 -03:00
|
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
2023-11-19 15:47:52 -03:00
|
|
|
JS_DEFINE_ALLOCATOR(HTMLProgressElement);
|
|
|
|
|
|
2022-02-18 17:00:52 -03:00
|
|
|
HTMLProgressElement::HTMLProgressElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2021-02-07 07:20:15 -03:00
|
|
|
: HTMLElement(document, move(qualified_name))
|
2020-07-31 23:05:43 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-14 16:21:51 -03:00
|
|
|
HTMLProgressElement::~HTMLProgressElement() = default;
|
2020-07-31 23:05:43 -03:00
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
void HTMLProgressElement::initialize(JS::Realm& realm)
|
2023-01-10 08:28:20 -03:00
|
|
|
{
|
2023-08-07 03:41:28 -03:00
|
|
|
Base::initialize(realm);
|
2023-11-21 20:55:21 -03:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLProgressElementPrototype>(realm, "HTMLProgressElement"_fly_string));
|
2023-01-10 08:28:20 -03:00
|
|
|
}
|
|
|
|
|
|
2022-10-17 09:41:50 -03:00
|
|
|
JS::GCPtr<Layout::Node> HTMLProgressElement::create_layout_node(NonnullRefPtr<CSS::StyleProperties> style)
|
2022-02-16 15:12:54 -03:00
|
|
|
{
|
2022-10-08 18:15:32 -03:00
|
|
|
if (style->appearance().value_or(CSS::Appearance::Auto) == CSS::Appearance::None)
|
|
|
|
|
return HTMLElement::create_layout_node(style);
|
2022-10-17 09:41:50 -03:00
|
|
|
return heap().allocate_without_realm<Layout::Progress>(document(), *this, move(style));
|
2022-07-22 12:08:03 -03: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 15:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HTMLProgressElement::value() const
|
|
|
|
|
{
|
2023-09-02 23:58:18 -03:00
|
|
|
auto const& value_characters = deprecated_attribute(HTML::AttributeNames::value);
|
2022-03-03 09:50:39 -03:00
|
|
|
if (value_characters == nullptr)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2022-10-11 21:24:14 -03: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 15:12:54 -03:00
|
|
|
return 0;
|
|
|
|
|
|
2022-10-11 21:24:14 -03:00
|
|
|
return min(maybe_double.value(), max());
|
2022-02-16 15:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 16:37:57 -03:00
|
|
|
WebIDL::ExceptionOr<void> HTMLProgressElement::set_value(double value)
|
2022-02-16 15:12:54 -03:00
|
|
|
{
|
|
|
|
|
if (value < 0)
|
2023-05-25 16:37:57 -03:00
|
|
|
return {};
|
2022-02-16 15:12:54 -03:00
|
|
|
|
2023-10-07 19:42:00 -03:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::value, MUST(String::number(value))));
|
2022-07-22 12:08:03 -03:00
|
|
|
progress_position_updated();
|
2023-05-25 16:37:57 -03:00
|
|
|
return {};
|
2022-02-16 15:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HTMLProgressElement::max() const
|
|
|
|
|
{
|
2023-09-02 23:58:18 -03:00
|
|
|
auto const& max_characters = deprecated_attribute(HTML::AttributeNames::max);
|
2022-03-03 09:50:39 -03:00
|
|
|
if (max_characters == nullptr)
|
|
|
|
|
return 1;
|
|
|
|
|
|
2022-10-11 21:24:14 -03: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 15:12:54 -03:00
|
|
|
return 1;
|
|
|
|
|
|
2022-10-11 21:24:14 -03:00
|
|
|
return double_or_none.value();
|
2022-02-16 15:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 16:37:57 -03:00
|
|
|
WebIDL::ExceptionOr<void> HTMLProgressElement::set_max(double value)
|
2022-02-16 15:12:54 -03:00
|
|
|
{
|
|
|
|
|
if (value <= 0)
|
2023-05-25 16:37:57 -03:00
|
|
|
return {};
|
2022-02-16 15:12:54 -03:00
|
|
|
|
2023-10-07 19:42:00 -03:00
|
|
|
TRY(set_attribute(HTML::AttributeNames::max, MUST(String::number(value))));
|
2022-07-22 12:08:03 -03:00
|
|
|
progress_position_updated();
|
2023-05-25 16:37:57 -03:00
|
|
|
return {};
|
2022-02-16 15:12:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HTMLProgressElement::position() const
|
|
|
|
|
{
|
|
|
|
|
if (!is_determinate())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return value() / max();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 23:05:43 -03:00
|
|
|
}
|