HTMLImageElement.h 6.3 KB

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