Skip to content

Truly independent web browser

9.6 KB
Raw Blame
/*
 * Copyright (c) 2018-2023, Andreas Kling <[email protected]>
 * Copyright (c) 2024, Aliaksandr Kalenik <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteBuffer.h>
#include <AK/OwnPtr.h>
#include <AK/Utf16String.h>
#include <AK/Utf16View.h>
#include <LibGC/Function.h>
#include <LibGfx/Forward.h>
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/DOM/DocumentLoadEventDelayer.h>
#include <LibWeb/DOM/ViewportClient.h>
#include <LibWeb/HTML/CORSSettingAttribute.h>
#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/LazyLoadingElement.h>
#include <LibWeb/HTML/SharedResourceRequest.h>
#include <LibWeb/HTML/SourceSet.h>
#include <LibWeb/Layout/ImageProvider.h>

namespace Web::HTML {

class HTMLImageElement final
    : public HTMLElement
    , public LazyLoadingElement<HTMLImageElement>
    , public Layout::ImageProvider
    , public DOM::ViewportClient
    , public DecodedImageData::Client {
    WEB_WRAPPABLE(HTMLImageElement, HTMLElement);
    GC_DECLARE_ALLOCATOR(HTMLImageElement);
    LAZY_LOADING_ELEMENT(HTMLImageElement);

public:
    virtual ~HTMLImageElement() override;

    // ^FormAssociatedElement
    virtual bool is_form_associated_element() const override { return true; }

    virtual void form_associated_element_attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;

    Optional<Utf16String> alternative_text() const override
    {
        if (auto alt = get_attribute(HTML::AttributeNames::alt); alt.has_value())
            return alt.release_value();
        return {};
    }

    Utf16String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
    void set_alt(Utf16View alt) { set_attribute_value(HTML::AttributeNames::alt, alt); }

    WebIDL::UnsignedLong width() const;
    void set_width(WebIDL::UnsignedLong);

    WebIDL::UnsignedLong height() const;
    void set_height(WebIDL::UnsignedLong);

    unsigned natural_width() const;
    unsigned natural_height() const;

    int x() const;
    int y() const;

    // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-complete
    bool complete() const;

    // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-currentsrc
    Utf16String current_src() const;

    // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
    GC::Ref<WebIDL::Promise> decode() const;
    void decode(GC::Ref<WebIDL::Promise>) const;

    GC::Ptr<HTMLMapElement> associated_map_element();

    virtual Optional<ARIA::Role> default_role() const override;

    // https://html.spec.whatwg.org/multipage/images.html#img-environment-changes
    void react_to_changes_in_the_environment();

    // https://html.spec.whatwg.org/multipage/images.html#update-the-image-data
    void update_the_image_data(bool restart_the_animations = false, bool maybe_omit_events = 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);

    // NB: The next update of the image data presents this resource instead of fetching the image.
    void set_resource_from_navigation_response(GC::Ref<SharedResourceRequest>);

    // https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attribute-source
    DOM::Element const& dimension_attribute_source() const;
    void set_dimension_attribute_source(DOM::Element const*);

    ImageRequest& current_request() { return *m_current_request; }
    ImageRequest const& current_request() const { return *m_current_request; }

    // https://html.spec.whatwg.org/multipage/images.html#upgrade-the-pending-request-to-the-current-request
    void upgrade_pending_request_to_current_request();

    // https://html.spec.whatwg.org/multipage/embedded-content.html#allows-auto-sizes
    bool allows_auto_sizes() const;

    Optional<CSSPixels> concrete_object_size_width_if_rendered() const;

    // https://html.spec.whatwg.org/multipage/images.html#last-auto-sizes-width
    Optional<CSSPixels> last_auto_sizes_width() const { return m_last_auto_sizes_width; }
    void set_last_auto_sizes_width(CSSPixels width) const { m_last_auto_sizes_width = width; }

    // ^Layout::ImageProvider
    virtual bool is_image_pending() const override;

    virtual GC::Ptr<DecodedImageData> decoded_image_data() const override;
    virtual Optional<CSSPixels> intrinsic_width() const override;
    virtual Optional<CSSPixels> intrinsic_height() const override;
    virtual Optional<CSSPixelFraction> intrinsic_aspect_ratio() const override;

    virtual void visit_edges(Cell::Visitor&) override;

private:
    void associated_image_request_became_completely_available(ImageRequest const&);

    CSSPixelSize density_corrected_natural_width_and_height() const;
    struct Dimensions {
        double width { 0 };
        double height { 0 };
    };
    Dimensions dimensions(DOM::UpdateLayoutReason) const;
    Dimensions determine_the_dimensions(Optional<CSSPixelSize> natural_dimensions, DOM::UpdateLayoutReason) const;

    HTMLImageElement(DOM::Document&, DOM::QualifiedName);

    void update_the_image_data_impl(bool restart_the_animations, bool maybe_omit_events, u64 update_the_image_data_count);

    virtual bool is_html_image_element() const override { return true; }

    virtual void initialize_element() override;
    virtual void finalize() override;

    virtual void adopted_from(DOM::Document&) override;
    virtual void inserted() override;
    virtual void moved_from(IsSubtreeRoot, GC::Ptr<DOM::Node> old_ancestor) override;
    virtual void removed_from(IsSubtreeRoot, DOM::Node* old_ancestor, DOM::Node& old_root) override;

    virtual bool is_presentational_hint(Utf16FlyString const&) const override;
    virtual void apply_presentational_hints(Vector<CSS::StyleProperty>&) const override;

    // https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attributes
    virtual bool supports_dimension_attributes() const override { return true; }

    virtual CSS::ElementBoxKind box_kind() const override;

    virtual void did_set_viewport_rect(CSSPixelRect const&) override;

    void handle_failed_fetch();
    enum class LazyLoadOutcome {
        Resumed,
        Superseded,
    };
    void settle_decodes_waiting_for_lazy_load(LazyLoadOutcome);
    void retire_lazy_load_continuation();
    void add_callbacks_to_image_request(GC::Ref<ImageRequest>, GC::Ref<DOM::Document> originating_document, bool maybe_omit_events, Utf16View url_string, Utf16View previous_url, SharedResourceRequest::DelaysLoadEvent);
    void stop_delaying_the_load_event_for_aborted_requests();
    void update_image_document_zoom();

    void create_alt_text_shadow_tree();
    void remove_alt_text_shadow_tree();
    void update_alt_text_shadow_tree();
    void set_needs_layout_update_or_repaint_after_image_data_change();
    void record_box_kind();

    virtual void decoded_image_data_did_update() override;
    virtual bool decoded_image_data_may_be_rendered() const override { return has_layout_box(); }

    Optional<DOM::DocumentLoadEventDelayer> m_load_event_delayer;

    GC::Ptr<DOM::DocumentObserver> m_document_observer;

    CORSSettingAttribute m_cors_setting { CORSSettingAttribute::NoCORS };

    // https://html.spec.whatwg.org/multipage/images.html#last-auto-sizes-width
    // NB: Parsing a sizes attribute records this through a const img, so it is mutable.
    mutable Optional<CSSPixels> m_last_auto_sizes_width;

    // 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<Utf16String> m_last_selected_source;

    // https://html.spec.whatwg.org/multipage/images.html#current-request
    GC::Ptr<SharedResourceRequest> m_resource_from_navigation_response;
    GC::Ptr<ImageRequest> m_current_request;

    // https://html.spec.whatwg.org/multipage/images.html#pending-request
    GC::Ptr<ImageRequest> m_pending_request;

    GC::Ptr<DOM::Text> m_alt_text_node;
    // The box kind last published for the image, which its box was built with unless it is marked to be built again.
    CSS::ElementBoxKind m_recorded_box_kind { CSS::ElementBoxKind::Image };

    SourceSet m_source_set;

    CSSPixelSize m_last_seen_viewport_size;
    Optional<double> m_image_document_zoom_level;

    bool m_is_child_of_picture { false };

    // https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element:dimension-attribute-source
    // Each img element has a dimension attribute source, which must initially be the img element itself.
    GC::Ptr<DOM::Element const> m_dimension_attribute_source;

    u64 m_update_the_image_data_count { 0 };

    bool m_has_resumed_lazy_loading { false };
    bool m_is_retiring_lazy_load_continuation { false };

    // NB: decode() calls on a deferred lazy image, which wait until its lazy load resumption steps run.
    Vector<GC::Ref<GC::Function<void(LazyLoadOutcome)>>> m_decodes_waiting_for_lazy_load;

    GC::Ptr<HTMLMapElement> m_cached_associated_map_element;
    Optional<u64> m_cached_associated_map_element_dom_tree_version;
};

void update_the_source_set(DOM::Element&);

}

namespace Web::DOM {

template<>
inline bool Node::fast_is<HTML::HTMLImageElement>() const { return is_html_image_element(); }

}