2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2024-11-09 01:48:17 -03:00
|
|
|
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-10-05 17:07:45 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 17:41:10 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-06-13 17:22:54 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-02-14 19:28:42 -03:00
|
|
|
#include <LibGfx/Forward.h>
|
2023-11-24 10:45:45 -03:00
|
|
|
#include <LibGfx/ImmutableBitmap.h>
|
2023-09-25 13:47:34 -03:00
|
|
|
#include <LibJS/Heap/HeapFunction.h>
|
2023-08-23 13:58:42 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2023-04-01 10:15:23 -03:00
|
|
|
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
|
2023-07-29 05:20:53 -03:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2023-05-11 14:23:36 -03:00
|
|
|
#include <LibWeb/HTML/CORSSettingAttribute.h>
|
2021-10-14 12:18:49 -03:00
|
|
|
#include <LibWeb/HTML/FormAssociatedElement.h>
|
2022-03-23 19:55:54 -03:00
|
|
|
#include <LibWeb/HTML/HTMLElement.h>
|
2023-11-21 15:50:09 -03:00
|
|
|
#include <LibWeb/HTML/LazyLoadingElement.h>
|
2023-05-11 14:23:36 -03:00
|
|
|
#include <LibWeb/HTML/SourceSet.h>
|
2023-05-12 02:17:01 -03:00
|
|
|
#include <LibWeb/Layout/ImageProvider.h>
|
2019-10-05 17:07:45 -03:00
|
|
|
|
2020-07-28 13:20:36 -03:00
|
|
|
namespace Web::HTML {
|
2019-12-18 16:57:18 -03:00
|
|
|
|
2022-03-23 19:55:54 -03:00
|
|
|
class HTMLImageElement final
|
|
|
|
|
: public HTMLElement
|
2023-05-12 02:17:01 -03:00
|
|
|
, public FormAssociatedElement
|
2023-11-21 15:50:09 -03:00
|
|
|
, public LazyLoadingElement<HTMLImageElement>
|
2023-07-29 05:20:53 -03:00
|
|
|
, public Layout::ImageProvider
|
2023-08-23 13:58:42 -03:00
|
|
|
, public DOM::Document::ViewportClient {
|
2022-08-28 08:42:07 -03:00
|
|
|
WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
|
2023-11-19 15:47:52 -03:00
|
|
|
JS_DECLARE_ALLOCATOR(HTMLImageElement);
|
2023-11-21 15:50:09 -03:00
|
|
|
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement);
|
|
|
|
|
LAZY_LOADING_ELEMENT(HTMLImageElement);
|
2022-03-23 19:55:54 -03:00
|
|
|
|
2019-10-05 17:07:45 -03:00
|
|
|
public:
|
|
|
|
|
virtual ~HTMLImageElement() override;
|
|
|
|
|
|
2024-02-03 11:33:33 -03:00
|
|
|
virtual void form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value) override;
|
2019-10-06 09:03:51 -03:00
|
|
|
|
2024-11-09 11:45:09 -03:00
|
|
|
Optional<String> alternative_text() const override
|
|
|
|
|
{
|
|
|
|
|
if (auto alt = get_attribute(HTML::AttributeNames::alt); alt.has_value())
|
|
|
|
|
return alt.release_value();
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 15:04:45 -03:00
|
|
|
String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
|
|
|
|
|
String src() const { return get_attribute_value(HTML::AttributeNames::src); }
|
2019-10-05 18:20:35 -03:00
|
|
|
|
2023-11-24 10:45:45 -03:00
|
|
|
RefPtr<Gfx::ImmutableBitmap> immutable_bitmap() const;
|
2019-10-05 18:41:14 -03:00
|
|
|
|
2022-02-25 14:42:37 -03:00
|
|
|
unsigned width() const;
|
2023-05-25 16:37:57 -03:00
|
|
|
WebIDL::ExceptionOr<void> set_width(unsigned);
|
2022-02-25 14:42:37 -03:00
|
|
|
|
|
|
|
|
unsigned height() const;
|
2023-05-25 16:37:57 -03:00
|
|
|
WebIDL::ExceptionOr<void> set_height(unsigned);
|
2022-02-25 14:42:37 -03:00
|
|
|
|
2022-04-12 13:20:43 -03:00
|
|
|
unsigned natural_width() const;
|
|
|
|
|
unsigned natural_height() const;
|
|
|
|
|
|
2022-10-03 09:54:27 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
|
|
|
|
|
bool complete() const;
|
|
|
|
|
|
2024-05-23 20:08:03 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-currentsrc
|
|
|
|
|
String current_src() const;
|
|
|
|
|
|
2024-06-05 18:15:26 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
|
2024-10-25 15:38:19 -03:00
|
|
|
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> decode() const;
|
2024-06-05 18:15:26 -03:00
|
|
|
|
2023-01-28 19:23:16 -03:00
|
|
|
virtual Optional<ARIA::Role> default_role() const override;
|
2022-11-28 20:58:13 -03:00
|
|
|
|
2023-07-29 05:20:53 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
|
|
|
|
|
void react_to_changes_in_the_environment();
|
|
|
|
|
|
2023-05-11 14:23:36 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
|
2023-06-11 03:08:44 -03:00
|
|
|
ErrorOr<void> update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = false);
|
2023-05-11 14:23:36 -03: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();
|
|
|
|
|
|
2024-09-02 08:46:27 -03:00
|
|
|
StringView decoding() const;
|
|
|
|
|
|
|
|
|
|
void set_decoding(String);
|
|
|
|
|
|
2023-05-11 14:23:36 -03:00
|
|
|
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; }
|
|
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
|
|
|
|
|
void upgrade_pending_request_to_current_request();
|
|
|
|
|
|
2024-09-11 10:06:08 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#allows-auto-sizes
|
|
|
|
|
bool allows_auto_sizes() const;
|
|
|
|
|
|
2023-05-12 02:17:01 -03:00
|
|
|
// ^Layout::ImageProvider
|
2024-02-18 22:10:37 -03:00
|
|
|
virtual bool is_image_available() const override;
|
2023-05-20 11:27:31 -03:00
|
|
|
virtual Optional<CSSPixels> intrinsic_width() const override;
|
|
|
|
|
virtual Optional<CSSPixels> intrinsic_height() const override;
|
2023-09-03 19:33:58 -03:00
|
|
|
virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
|
2023-11-24 10:45:45 -03:00
|
|
|
virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
|
2023-05-12 02:17:01 -03:00
|
|
|
virtual void set_visible_in_viewport(bool) override;
|
2024-02-26 13:36:48 -03:00
|
|
|
virtual JS::NonnullGCPtr<DOM::Element const> to_html_element() const override { return *this; }
|
2023-05-12 02:17:01 -03:00
|
|
|
|
2023-08-18 07:56:21 -03:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
2019-10-05 18:20:35 -03:00
|
|
|
private:
|
2022-08-28 08:42:07 -03:00
|
|
|
HTMLImageElement(DOM::Document&, DOM::QualifiedName);
|
|
|
|
|
|
2023-12-24 10:41:33 -03:00
|
|
|
virtual bool is_html_image_element() const override { return true; }
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2023-07-29 05:20:53 -03:00
|
|
|
virtual void finalize() override;
|
2023-05-11 14:23:36 -03:00
|
|
|
|
2023-08-23 13:58:42 -03:00
|
|
|
virtual void adopted_from(DOM::Document&) override;
|
|
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
|
2020-07-21 20:16:27 -03:00
|
|
|
|
2024-04-10 22:44:11 -03:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attributes
|
|
|
|
|
virtual bool supports_dimension_attributes() const override { return true; }
|
|
|
|
|
|
2024-10-26 12:42:27 -03:00
|
|
|
virtual JS::GCPtr<Layout::Node> create_layout_node(CSS::StyleProperties) override;
|
2024-11-08 09:14:37 -03:00
|
|
|
virtual void adjust_computed_style(CSS::StyleProperties&) override;
|
2019-10-05 18:41:14 -03:00
|
|
|
|
2023-08-23 13:58:42 -03:00
|
|
|
virtual void did_set_viewport_rect(CSSPixelRect const&) override;
|
2023-07-29 05:20:53 -03:00
|
|
|
|
2024-03-18 00:22:27 -03:00
|
|
|
void handle_successful_fetch(URL::URL const&, StringView mime_type, ImageRequest&, ByteBuffer, bool maybe_omit_events, URL::URL const& previous_url);
|
2023-05-11 14:23:36 -03:00
|
|
|
void handle_failed_fetch();
|
2024-03-18 00:22:27 -03:00
|
|
|
void add_callbacks_to_image_request(JS::NonnullGCPtr<ImageRequest>, bool maybe_omit_events, URL::URL const& url_string, URL::URL const& previous_url);
|
2023-05-11 14:23:36 -03: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 10:15:23 -03:00
|
|
|
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
|
2023-05-11 14:23:36 -03: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 07:56:21 -03:00
|
|
|
JS::GCPtr<ImageRequest> m_current_request;
|
2023-05-11 14:23:36 -03:00
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#pending-request
|
2023-08-18 07:56:21 -03:00
|
|
|
JS::GCPtr<ImageRequest> m_pending_request;
|
2023-05-11 14:23:36 -03:00
|
|
|
|
|
|
|
|
SourceSet m_source_set;
|
2023-07-06 20:00:06 -03:00
|
|
|
|
2023-07-29 05:20:53 -03:00
|
|
|
CSSPixelSize m_last_seen_viewport_size;
|
2024-09-02 08:46:27 -03:00
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#image-decoding-hint
|
|
|
|
|
enum class ImageDecodingHint {
|
|
|
|
|
Auto,
|
|
|
|
|
Sync,
|
|
|
|
|
Async
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ImageDecodingHint m_decoding_hint = ImageDecodingHint::Auto;
|
2019-10-05 17:07:45 -03:00
|
|
|
};
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2020-04-14 15:35:49 -03:00
|
|
|
}
|
2023-12-24 10:41:33 -03:00
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
template<>
|
|
|
|
|
inline bool Node::fast_is<HTML::HTMLImageElement>() const { return is_html_image_element(); }
|
|
|
|
|
}
|