1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:48:14 +00:00

LibWeb: Implement enough of "update the image data" to load images

This first pass is enough to get us:
- Image loading via fetch
- Source selection via srcset and sizes attributes
This commit is contained in:
Andreas Kling 2023-05-11 19:23:36 +02:00
parent 6fe4fcb74b
commit 3cf73ca0b3
4 changed files with 661 additions and 45 deletions

View file

@ -10,8 +10,10 @@
#include <AK/OwnPtr.h>
#include <LibGfx/Forward.h>
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
#include <LibWeb/HTML/CORSSettingAttribute.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/SourceSet.h>
#include <LibWeb/Loader/ImageLoader.h>
namespace Web::HTML {
@ -26,11 +28,12 @@ public:
virtual ~HTMLImageElement() override;
virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) override;
virtual void did_remove_attribute(DeprecatedFlyString const& name) override;
DeprecatedString alt() const { return attribute(HTML::AttributeNames::alt); }
DeprecatedString src() const { return attribute(HTML::AttributeNames::src); }
Gfx::Bitmap const* bitmap() const;
RefPtr<Gfx::Bitmap const> bitmap() const;
unsigned width() const;
void set_width(unsigned);
@ -46,17 +49,68 @@ public:
virtual Optional<ARIA::Role> default_role() const override;
// https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
ErrorOr<void> update_the_image_data(bool restart_the_animations = false);
// https://html.spec.whatwg.org/multipage/images.html#use-srcset-or-picture
[[nodiscard]] bool uses_srcset_or_picture() const;
// https://html.spec.whatwg.org/multipage/rendering.html#restart-the-animation
void restart_the_animation();
// https://html.spec.whatwg.org/multipage/images.html#select-an-image-source
[[nodiscard]] Optional<ImageSourceAndPixelDensity> select_an_image_source();
void set_source_set(SourceSet);
ImageRequest& current_request() { return *m_current_request; }
ImageRequest const& current_request() const { return *m_current_request; }
size_t current_frame_index() const { return m_current_frame_index; }
enum class LazyLoading {
Lazy,
Eager,
};
[[nodiscard]] LazyLoading lazy_loading() const;
// https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
void upgrade_pending_request_to_current_request();
private:
HTMLImageElement(DOM::Document&, DOM::QualifiedName);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
virtual void apply_presentational_hints(CSS::StyleProperties&) const override;
virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>) override;
void handle_successful_fetch(AK::URL const&, ImageRequest&, ByteBuffer);
void handle_failed_fetch();
void animate();
RefPtr<Core::Timer> m_animation_timer;
size_t m_current_frame_index { 0 };
size_t m_loops_completed { 0 };
ImageLoader m_image_loader;
Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;
CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };
// https://html.spec.whatwg.org/multipage/images.html#last-selected-source
// Each img element has a last selected source, which must initially be null.
Optional<String> m_last_selected_source;
// https://html.spec.whatwg.org/multipage/images.html#current-request
RefPtr<ImageRequest> m_current_request;
// https://html.spec.whatwg.org/multipage/images.html#pending-request
RefPtr<ImageRequest> m_pending_request;
SourceSet m_source_set;
};
}