HTMLImageElement.h 3.9 KB

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