Coverage Report

Created: 2026-02-14 08:01

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