2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2023-05-12 07:48:59 +02:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-10-05 22:07:45 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-06-13 22:22:54 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-02-14 23:28:42 +01:00
|
|
|
#include <LibGfx/Forward.h>
|
2023-11-24 14:45:45 +01:00
|
|
|
#include <LibGfx/ImmutableBitmap.h>
|
2023-09-25 18:47:34 +02:00
|
|
|
#include <LibJS/Heap/HeapFunction.h>
|
2023-08-23 18:58:42 +02:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2023-04-01 15:15:23 +02:00
|
|
|
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
|
2023-07-29 10:20:53 +02:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2023-05-11 19:23:36 +02:00
|
|
|
#include <LibWeb/HTML/CORSSettingAttribute.h>
|
2021-10-14 16:18:49 +01:00
|
|
|
#include <LibWeb/HTML/FormAssociatedElement.h>
|
2022-03-23 18:55:54 -04:00
|
|
|
#include <LibWeb/HTML/HTMLElement.h>
|
2023-05-11 19:23:36 +02:00
|
|
|
#include <LibWeb/HTML/SourceSet.h>
|
2023-05-12 07:17:01 +02:00
|
|
|
#include <LibWeb/Layout/ImageProvider.h>
|
2019-10-05 22:07:45 +02:00
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2019-12-18 20:57:18 +01:00
|
|
|
|
2022-03-23 18:55:54 -04:00
|
|
|
class HTMLImageElement final
|
|
|
|
: public HTMLElement
|
2023-05-12 07:17:01 +02:00
|
|
|
, public FormAssociatedElement
|
2023-07-29 10:20:53 +02:00
|
|
|
, public Layout::ImageProvider
|
2023-08-23 18:58:42 +02:00
|
|
|
, public DOM::Document::ViewportClient {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
JS_DECLARE_ALLOCATOR(HTMLImageElement);
|
2022-03-23 18:55:54 -04:00
|
|
|
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement)
|
|
|
|
|
2019-10-05 22:07:45 +02:00
|
|
|
public:
|
|
|
|
virtual ~HTMLImageElement() override;
|
|
|
|
|
2023-11-19 18:10:36 +13:00
|
|
|
virtual void attribute_changed(FlyString const& name, Optional<String> const& value) override;
|
2019-10-06 23:03:51 +11:00
|
|
|
|
2023-09-03 14:58:18 +12:00
|
|
|
DeprecatedString alt() const { return deprecated_attribute(HTML::AttributeNames::alt); }
|
|
|
|
DeprecatedString src() const { return deprecated_attribute(HTML::AttributeNames::src); }
|
2019-10-05 23:20:35 +02:00
|
|
|
|
2023-11-24 14:45:45 +01:00
|
|
|
RefPtr<Gfx::ImmutableBitmap> immutable_bitmap() const;
|
|
|
|
RefPtr<Gfx::Bitmap const> bitmap() const { return immutable_bitmap()->bitmap(); }
|
2019-10-05 23:41:14 +02:00
|
|
|
|
2022-02-25 18:42:37 +01:00
|
|
|
unsigned width() const;
|
2023-05-25 20:37:57 +01:00
|
|
|
WebIDL::ExceptionOr<void> set_width(unsigned);
|
2022-02-25 18:42:37 +01:00
|
|
|
|
|
|
|
unsigned height() const;
|
2023-05-25 20:37:57 +01:00
|
|
|
WebIDL::ExceptionOr<void> set_height(unsigned);
|
2022-02-25 18:42:37 +01:00
|
|
|
|
2022-04-12 13:20:43 -03:00
|
|
|
unsigned natural_width() const;
|
|
|
|
unsigned natural_height() const;
|
|
|
|
|
2022-10-03 14:54:27 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
|
|
|
|
bool complete() const;
|
|
|
|
|
2023-01-28 22:23:16 +00:00
|
|
|
virtual Optional<ARIA::Role> default_role() const override;
|
2022-11-28 17:58:13 -06:00
|
|
|
|
2023-07-29 10:20:53 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
|
|
|
|
void react_to_changes_in_the_environment();
|
|
|
|
|
2023-05-11 19:23:36 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
|
2023-06-11 08:08:44 +02:00
|
|
|
ErrorOr<void> update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = false);
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
|
|
|
|
[[nodiscard]] bool uses_srcset_or_picture() const;
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
|
|
|
|
void restart_the_animation();
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
|
|
|
|
[[nodiscard]] Optional<ImageSourceAndPixelDensity> select_an_image_source();
|
|
|
|
|
|
|
|
void set_source_set(SourceSet);
|
|
|
|
|
|
|
|
ImageRequest& current_request() { return *m_current_request; }
|
|
|
|
ImageRequest const& current_request() const { return *m_current_request; }
|
|
|
|
|
|
|
|
size_t current_frame_index() const { return m_current_frame_index; }
|
|
|
|
enum class LazyLoading {
|
|
|
|
Lazy,
|
|
|
|
Eager,
|
|
|
|
};
|
|
|
|
[[nodiscard]] LazyLoading lazy_loading() const;
|
2023-07-07 00:00:06 +01:00
|
|
|
[[nodiscard]] bool will_lazy_load() const;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
|
|
|
|
void upgrade_pending_request_to_current_request();
|
|
|
|
|
2023-05-12 07:17:01 +02:00
|
|
|
// ^Layout::ImageProvider
|
2023-05-20 16:27:31 +02:00
|
|
|
virtual Optional<CSSPixels> intrinsic_width() const override;
|
|
|
|
virtual Optional<CSSPixels> intrinsic_height() const override;
|
2023-09-03 17:33:58 -05:00
|
|
|
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
|
2023-11-24 14:45:45 +01:00
|
|
|
virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
|
2023-05-12 07:17:01 +02:00
|
|
|
virtual void set_visible_in_viewport(bool) override;
|
|
|
|
|
2023-09-25 18:47:34 +02:00
|
|
|
void set_lazy_load_resumption_steps(Function<void()>);
|
|
|
|
JS::GCPtr<JS::HeapFunction<void()>> take_lazy_load_resumption_steps(Badge<DOM::Document>);
|
2023-07-07 00:00:06 +01:00
|
|
|
|
2023-08-18 12:56:21 +02:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
2019-10-05 23:20:35 +02:00
|
|
|
private:
|
2022-08-28 13:42:07 +02:00
|
|
|
HTMLImageElement(DOM::Document&, DOM::QualifiedName);
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-07-29 10:20:53 +02:00
|
|
|
virtual void finalize() override;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
2023-08-23 18:58:42 +02:00
|
|
|
virtual void adopted_from(DOM::Document&) override;
|
|
|
|
|
2020-07-26 20:01:35 +02:00
|
|
|
virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
|
2020-07-22 01:16:27 +02:00
|
|
|
|
2022-10-17 14:41:50 +02:00
|
|
|
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
|
2019-10-05 23:41:14 +02:00
|
|
|
|
2023-08-23 18:58:42 +02:00
|
|
|
virtual void did_set_viewport_rect(CSSPixelRect const&) override;
|
2023-07-29 10:20:53 +02:00
|
|
|
|
2023-06-11 08:08:44 +02:00
|
|
|
void handle_successful_fetch(AK::URL const&, StringView mime_type, ImageRequest&, ByteBuffer, bool maybe_omit_events, AK::URL const& previous_url);
|
2023-05-11 19:23:36 +02:00
|
|
|
void handle_failed_fetch();
|
2023-08-18 12:56:21 +02:00
|
|
|
void add_callbacks_to_image_request(JS::NonnullGCPtr<ImageRequest>, bool maybe_omit_events, AK::URL const& url_string, AK::URL const& previous_url);
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
void animate();
|
|
|
|
|
|
|
|
RefPtr<Core::Timer> m_animation_timer;
|
|
|
|
size_t m_current_frame_index { 0 };
|
|
|
|
size_t m_loops_completed { 0 };
|
|
|
|
|
2023-04-01 15:15:23 +02:00
|
|
|
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#last-selected-source
|
|
|
|
// Each img element has a last selected source, which must initially be null.
|
|
|
|
Optional<String> m_last_selected_source;
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#current-request
|
2023-08-18 12:56:21 +02:00
|
|
|
JS::GCPtr<ImageRequest> m_current_request;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#pending-request
|
2023-08-18 12:56:21 +02:00
|
|
|
JS::GCPtr<ImageRequest> m_pending_request;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
SourceSet m_source_set;
|
2023-07-07 00:00:06 +01:00
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-resumption-steps
|
|
|
|
// Each img and iframe element has associated lazy load resumption steps, initially null.
|
2023-09-25 18:47:34 +02:00
|
|
|
JS::GCPtr<JS::HeapFunction<void()>> m_lazy_load_resumption_steps;
|
2023-07-29 10:20:53 +02:00
|
|
|
|
|
|
|
CSSPixelSize m_last_seen_viewport_size;
|
2019-10-05 22:07:45 +02:00
|
|
|
};
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2020-04-14 20:35:49 +02:00
|
|
|
}
|