HTMLImageElement.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/OwnPtr.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibGfx/ImmutableBitmap.h>
  12. #include <LibJS/Heap/HeapFunction.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/CORSSettingAttribute.h>
  17. #include <LibWeb/HTML/FormAssociatedElement.h>
  18. #include <LibWeb/HTML/HTMLElement.h>
  19. #include <LibWeb/HTML/LazyLoadingElement.h>
  20. #include <LibWeb/HTML/SourceSet.h>
  21. #include <LibWeb/Layout/ImageProvider.h>
  22. namespace Web::HTML {
  23. class HTMLImageElement final
  24. : public HTMLElement
  25. , public FormAssociatedElement
  26. , public LazyLoadingElement<HTMLImageElement>
  27. , public Layout::ImageProvider
  28. , public DOM::Document::ViewportClient {
  29. WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
  30. JS_DECLARE_ALLOCATOR(HTMLImageElement);
  31. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement);
  32. LAZY_LOADING_ELEMENT(HTMLImageElement);
  33. public:
  34. virtual ~HTMLImageElement() override;
  35. virtual void form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value) override;
  36. Optional<String> alternative_text() const override
  37. {
  38. if (auto alt = get_attribute(HTML::AttributeNames::alt); alt.has_value())
  39. return alt.release_value();
  40. return {};
  41. }
  42. String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
  43. String src() const { return get_attribute_value(HTML::AttributeNames::src); }
  44. RefPtr<Gfx::ImmutableBitmap> immutable_bitmap() const;
  45. unsigned width() const;
  46. WebIDL::ExceptionOr<void> set_width(unsigned);
  47. unsigned height() const;
  48. WebIDL::ExceptionOr<void> set_height(unsigned);
  49. unsigned natural_width() const;
  50. unsigned natural_height() const;
  51. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
  52. bool complete() const;
  53. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-currentsrc
  54. String current_src() const;
  55. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
  56. [[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> decode() const;
  57. virtual Optional<ARIA::Role> default_role() const override;
  58. // https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
  59. void react_to_changes_in_the_environment();
  60. // https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
  61. ErrorOr<void> update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = false);
  62. // https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
  63. [[nodiscard]] bool uses_srcset_or_picture() const;
  64. // https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
  65. void restart_the_animation();
  66. // https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
  67. [[nodiscard]] Optional<ImageSourceAndPixelDensity> select_an_image_source();
  68. StringView decoding() const;
  69. void set_decoding(String);
  70. void set_source_set(SourceSet);
  71. ImageRequest& current_request() { return *m_current_request; }
  72. ImageRequest const& current_request() const { return *m_current_request; }
  73. size_t current_frame_index() const { return m_current_frame_index; }
  74. // https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
  75. void upgrade_pending_request_to_current_request();
  76. // https://html.spec.whatwg.org/multipage/embedded-content.html#allows-auto-sizes
  77. bool allows_auto_sizes() const;
  78. // ^Layout::ImageProvider
  79. virtual bool is_image_available() const override;
  80. virtual Optional<CSSPixels> intrinsic_width() const override;
  81. virtual Optional<CSSPixels> intrinsic_height() const override;
  82. virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
  83. virtual RefPtr<Gfx::ImmutableBitmap> current_image_bitmap(Gfx::IntSize = {}) const override;
  84. virtual void set_visible_in_viewport(bool) override;
  85. virtual JS::NonnullGCPtr<DOM::Element const> to_html_element() const override { return *this; }
  86. virtual void visit_edges(Cell::Visitor&) override;
  87. private:
  88. HTMLImageElement(DOM::Document&, DOM::QualifiedName);
  89. virtual bool is_html_image_element() const override { return true; }
  90. virtual void initialize(JS::Realm&) override;
  91. virtual void finalize() override;
  92. virtual void adopted_from(DOM::Document&) override;
  93. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  94. // https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attributes
  95. virtual bool supports_dimension_attributes() const override { return true; }
  96. virtual JS::GCPtr<Layout::Node> create_layout_node(CSS::StyleProperties) override;
  97. virtual void did_set_viewport_rect(CSSPixelRect const&) override;
  98. void handle_successful_fetch(URL::URL const&, StringView mime_type, ImageRequest&, ByteBuffer, bool maybe_omit_events, URL::URL const& previous_url);
  99. void handle_failed_fetch();
  100. void add_callbacks_to_image_request(JS::NonnullGCPtr<ImageRequest>, bool maybe_omit_events, URL::URL const& url_string, URL::URL const& previous_url);
  101. void animate();
  102. RefPtr<Core::Timer> m_animation_timer;
  103. size_t m_current_frame_index { 0 };
  104. size_t m_loops_completed { 0 };
  105. Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
  106. CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };
  107. // https://html.spec.whatwg.org/multipage/images.html#last-selected-source
  108. // Each img element has a last selected source, which must initially be null.
  109. Optional<String> m_last_selected_source;
  110. // https://html.spec.whatwg.org/multipage/images.html#current-request
  111. JS::GCPtr<ImageRequest> m_current_request;
  112. // https://html.spec.whatwg.org/multipage/images.html#pending-request
  113. JS::GCPtr<ImageRequest> m_pending_request;
  114. SourceSet m_source_set;
  115. CSSPixelSize m_last_seen_viewport_size;
  116. // https://html.spec.whatwg.org/multipage/images.html#image-decoding-hint
  117. enum class ImageDecodingHint {
  118. Auto,
  119. Sync,
  120. Async
  121. };
  122. ImageDecodingHint m_decoding_hint = ImageDecodingHint::Auto;
  123. };
  124. }
  125. namespace Web::DOM {
  126. template<>
  127. inline bool Node::fast_is<HTML::HTMLImageElement>() const { return is_html_image_element(); }
  128. }