HTMLImageElement.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.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 <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/CORSSettingAttribute.h>
  14. #include <LibWeb/HTML/FormAssociatedElement.h>
  15. #include <LibWeb/HTML/HTMLElement.h>
  16. #include <LibWeb/HTML/SourceSet.h>
  17. #include <LibWeb/Layout/ImageProvider.h>
  18. namespace Web::HTML {
  19. class HTMLImageElement final
  20. : public HTMLElement
  21. , public FormAssociatedElement
  22. , public Layout::ImageProvider
  23. , public DOM::Document::ViewportClient {
  24. WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
  25. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement)
  26. public:
  27. virtual ~HTMLImageElement() override;
  28. virtual void attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  29. DeprecatedString alt() const { return deprecated_attribute(HTML::AttributeNames::alt); }
  30. DeprecatedString src() const { return deprecated_attribute(HTML::AttributeNames::src); }
  31. RefPtr<Gfx::Bitmap const> bitmap() const;
  32. unsigned width() const;
  33. WebIDL::ExceptionOr<void> set_width(unsigned);
  34. unsigned height() const;
  35. WebIDL::ExceptionOr<void> set_height(unsigned);
  36. unsigned natural_width() const;
  37. unsigned natural_height() const;
  38. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
  39. bool complete() const;
  40. virtual Optional<ARIA::Role> default_role() const override;
  41. // https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
  42. void react_to_changes_in_the_environment();
  43. // https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
  44. ErrorOr<void> update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = false);
  45. // https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
  46. [[nodiscard]] bool uses_srcset_or_picture() const;
  47. // https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
  48. void restart_the_animation();
  49. // https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
  50. [[nodiscard]] Optional<ImageSourceAndPixelDensity> select_an_image_source();
  51. void set_source_set(SourceSet);
  52. ImageRequest& current_request() { return *m_current_request; }
  53. ImageRequest const& current_request() const { return *m_current_request; }
  54. size_t current_frame_index() const { return m_current_frame_index; }
  55. enum class LazyLoading {
  56. Lazy,
  57. Eager,
  58. };
  59. [[nodiscard]] LazyLoading lazy_loading() const;
  60. [[nodiscard]] bool will_lazy_load() const;
  61. // https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
  62. void upgrade_pending_request_to_current_request();
  63. // ^Layout::ImageProvider
  64. virtual Optional<CSSPixels> intrinsic_width() const override;
  65. virtual Optional<CSSPixels> intrinsic_height() const override;
  66. virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;
  67. virtual RefPtr<Gfx::Bitmap const> current_image_bitmap(Gfx::IntSize = {}) const override;
  68. virtual void set_visible_in_viewport(bool) override;
  69. JS::SafeFunction<void()> take_lazy_load_resumption_steps(Badge<DOM::Document>);
  70. virtual void visit_edges(Cell::Visitor&) override;
  71. private:
  72. HTMLImageElement(DOM::Document&, DOM::QualifiedName);
  73. virtual void initialize(JS::Realm&) override;
  74. virtual void finalize() override;
  75. virtual void adopted_from(DOM::Document&) override;
  76. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  77. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  78. virtual void did_set_viewport_rect(CSSPixelRect const&) override;
  79. void handle_successful_fetch(AK::URL const&, StringView mime_type, ImageRequest&, ByteBuffer, bool maybe_omit_events, AK::URL const& previous_url);
  80. void handle_failed_fetch();
  81. void add_callbacks_to_image_request(JS::NonnullGCPtr<ImageRequest>, bool maybe_omit_events, AK::URL const& url_string, AK::URL const& previous_url);
  82. void animate();
  83. RefPtr<Core::Timer> m_animation_timer;
  84. size_t m_current_frame_index { 0 };
  85. size_t m_loops_completed { 0 };
  86. Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
  87. CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };
  88. // https://html.spec.whatwg.org/multipage/images.html#last-selected-source
  89. // Each img element has a last selected source, which must initially be null.
  90. Optional<String> m_last_selected_source;
  91. // https://html.spec.whatwg.org/multipage/images.html#current-request
  92. JS::GCPtr<ImageRequest> m_current_request;
  93. // https://html.spec.whatwg.org/multipage/images.html#pending-request
  94. JS::GCPtr<ImageRequest> m_pending_request;
  95. SourceSet m_source_set;
  96. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-resumption-steps
  97. // Each img and iframe element has associated lazy load resumption steps, initially null.
  98. JS::SafeFunction<void()> m_lazy_load_resumption_steps;
  99. CSSPixelSize m_last_seen_viewport_size;
  100. };
  101. }