HTMLImageElement.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/DocumentLoadEventDelayer.h>
  11. #include <LibWeb/HTML/CORSSettingAttribute.h>
  12. #include <LibWeb/HTML/FormAssociatedElement.h>
  13. #include <LibWeb/HTML/HTMLElement.h>
  14. #include <LibWeb/HTML/SourceSet.h>
  15. #include <LibWeb/Layout/ImageProvider.h>
  16. namespace Web::HTML {
  17. class HTMLImageElement final
  18. : public HTMLElement
  19. , public FormAssociatedElement
  20. , public Layout::ImageProvider {
  21. WEB_PLATFORM_OBJECT(HTMLImageElement, HTMLElement);
  22. FORM_ASSOCIATED_ELEMENT(HTMLElement, HTMLImageElement)
  23. public:
  24. virtual ~HTMLImageElement() override;
  25. virtual void attribute_changed(DeprecatedFlyString const& name, DeprecatedString const& value) override;
  26. DeprecatedString alt() const { return attribute(HTML::AttributeNames::alt); }
  27. DeprecatedString src() const { return attribute(HTML::AttributeNames::src); }
  28. RefPtr<Gfx::Bitmap const> bitmap() const;
  29. unsigned width() const;
  30. WebIDL::ExceptionOr<void> set_width(unsigned);
  31. unsigned height() const;
  32. WebIDL::ExceptionOr<void> set_height(unsigned);
  33. unsigned natural_width() const;
  34. unsigned natural_height() const;
  35. // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
  36. bool complete() const;
  37. virtual Optional<ARIA::Role> default_role() const override;
  38. // https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
  39. ErrorOr<void> update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = false);
  40. // https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
  41. [[nodiscard]] bool uses_srcset_or_picture() const;
  42. // https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
  43. void restart_the_animation();
  44. // https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
  45. [[nodiscard]] Optional<ImageSourceAndPixelDensity> select_an_image_source();
  46. void set_source_set(SourceSet);
  47. ImageRequest& current_request() { return *m_current_request; }
  48. ImageRequest const& current_request() const { return *m_current_request; }
  49. size_t current_frame_index() const { return m_current_frame_index; }
  50. enum class LazyLoading {
  51. Lazy,
  52. Eager,
  53. };
  54. [[nodiscard]] LazyLoading lazy_loading() const;
  55. // https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
  56. void upgrade_pending_request_to_current_request();
  57. // ^Layout::ImageProvider
  58. virtual Optional<CSSPixels> intrinsic_width() const override;
  59. virtual Optional<CSSPixels> intrinsic_height() const override;
  60. virtual Optional<float> intrinsic_aspect_ratio() const override;
  61. virtual RefPtr<Gfx::Bitmap const> current_image_bitmap(Gfx::IntSize = {}) const override;
  62. virtual void set_visible_in_viewport(bool) override;
  63. private:
  64. HTMLImageElement(DOM::Document&, DOM::QualifiedName);
  65. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  66. virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
  67. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
  68. void handle_successful_fetch(AK::URL const&, StringView mime_type, ImageRequest&, ByteBuffer, bool maybe_omit_events, AK::URL const& previous_url);
  69. void handle_failed_fetch();
  70. void animate();
  71. RefPtr<Core::Timer> m_animation_timer;
  72. size_t m_current_frame_index { 0 };
  73. size_t m_loops_completed { 0 };
  74. Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
  75. CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };
  76. // https://html.spec.whatwg.org/multipage/images.html#last-selected-source
  77. // Each img element has a last selected source, which must initially be null.
  78. Optional<String> m_last_selected_source;
  79. // https://html.spec.whatwg.org/multipage/images.html#current-request
  80. RefPtr<ImageRequest> m_current_request;
  81. // https://html.spec.whatwg.org/multipage/images.html#pending-request
  82. RefPtr<ImageRequest> m_pending_request;
  83. SourceSet m_source_set;
  84. };
  85. }