2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2024-11-09 05:48:17 +01:00
|
|
|
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
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>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Function.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-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-11-21 18:50:09 +00:00
|
|
|
#include <LibWeb/HTML/LazyLoadingElement.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-11-21 18:50:09 +00:00
|
|
|
, public LazyLoadingElement<HTMLImageElement>
|
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);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(HTMLImageElement);
|
2023-11-21 18:50:09 +00:00
|
|
|
FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement);
|
|
|
|
LAZY_LOADING_ELEMENT(HTMLImageElement);
|
2022-03-23 18:55:54 -04:00
|
|
|
|
2019-10-05 22:07:45 +02:00
|
|
|
public:
|
|
|
|
virtual ~HTMLImageElement() override;
|
|
|
|
|
2024-12-12 19:48:32 +00:00
|
|
|
virtual void form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value, Optional<FlyString> const&) override;
|
2019-10-06 23:03:51 +11:00
|
|
|
|
2024-11-09 23:45:09 +09: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 19:04:45 +01:00
|
|
|
String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
|
|
|
|
String src() const { return get_attribute_value(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;
|
2019-10-05 23:41:14 +02:00
|
|
|
|
2024-11-29 15:32:37 +00:00
|
|
|
WebIDL::UnsignedLong width() const;
|
|
|
|
WebIDL::ExceptionOr<void> set_width(WebIDL::UnsignedLong);
|
2022-02-25 18:42:37 +01:00
|
|
|
|
2024-11-29 15:32:37 +00:00
|
|
|
WebIDL::UnsignedLong height() const;
|
|
|
|
WebIDL::ExceptionOr<void> set_height(WebIDL::UnsignedLong);
|
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;
|
|
|
|
|
2024-05-24 00:08:03 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-currentsrc
|
|
|
|
String current_src() const;
|
|
|
|
|
2024-06-05 23:15:26 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> decode() const;
|
2024-06-05 23:15:26 +02:00
|
|
|
|
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; }
|
|
|
|
|
|
|
|
// 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 14:06:08 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#allows-auto-sizes
|
|
|
|
bool allows_auto_sizes() const;
|
|
|
|
|
2023-05-12 07:17:01 +02:00
|
|
|
// ^Layout::ImageProvider
|
2024-02-18 20:10:37 -05:00
|
|
|
virtual bool is_image_available() const override;
|
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;
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual GC::Ref<DOM::Element const> to_html_element() const override { return *this; }
|
2023-05-12 07:17:01 +02: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-12-24 14:41:33 +01:00
|
|
|
virtual bool is_html_image_element() const override { return true; }
|
|
|
|
|
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;
|
|
|
|
|
2024-12-23 17:51:10 +01:00
|
|
|
virtual bool is_presentational_hint(FlyString const&) const override;
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
virtual void apply_presentational_hints(GC::Ref<CSS::CascadedProperties>) const override;
|
2020-07-22 01:16:27 +02:00
|
|
|
|
2024-04-10 21:44:11 -04: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-12-20 16:35:12 +01:00
|
|
|
virtual GC::Ptr<Layout::Node> create_layout_node(GC::Ref<CSS::ComputedProperties>) override;
|
2024-12-20 11:32:17 +01:00
|
|
|
virtual void adjust_computed_style(CSS::ComputedProperties&) 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
|
|
|
|
2024-03-18 16:22:27 +13: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 19:23:36 +02:00
|
|
|
void handle_failed_fetch();
|
2025-02-22 20:16:45 +13:00
|
|
|
void add_callbacks_to_image_request(GC::Ref<ImageRequest>, bool maybe_omit_events, URL::URL const& url_string, String 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
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<ImageRequest> m_current_request;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/images.html#pending-request
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<ImageRequest> m_pending_request;
|
2023-05-11 19:23:36 +02:00
|
|
|
|
|
|
|
SourceSet m_source_set;
|
2023-07-07 00:00:06 +01:00
|
|
|
|
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
|
|
|
}
|
2023-12-24 14:41:33 +01:00
|
|
|
|
|
|
|
namespace Web::DOM {
|
2025-05-13 07:06:33 -04:00
|
|
|
|
2023-12-24 14:41:33 +01:00
|
|
|
template<>
|
|
|
|
inline bool Node::fast_is<HTML::HTMLImageElement>() const { return is_html_image_element(); }
|
2025-05-13 07:06:33 -04:00
|
|
|
|
2023-12-24 14:41:33 +01:00
|
|
|
}
|