mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
LibWeb: Add class to represent "list of available images" from HTML spec
This commit is contained in:
parent
596eabe9e6
commit
9281bf7a01
6 changed files with 158 additions and 0 deletions
|
@ -315,6 +315,7 @@ set(SOURCES
|
||||||
HTML/HTMLVideoElement.cpp
|
HTML/HTMLVideoElement.cpp
|
||||||
HTML/ImageData.cpp
|
HTML/ImageData.cpp
|
||||||
HTML/ImageRequest.cpp
|
HTML/ImageRequest.cpp
|
||||||
|
HTML/ListOfAvailableImages.cpp
|
||||||
HTML/Location.cpp
|
HTML/Location.cpp
|
||||||
HTML/MediaError.cpp
|
HTML/MediaError.cpp
|
||||||
HTML/MessageChannel.cpp
|
HTML/MessageChannel.cpp
|
||||||
|
|
|
@ -58,6 +58,7 @@
|
||||||
#include <LibWeb/HTML/HTMLLinkElement.h>
|
#include <LibWeb/HTML/HTMLLinkElement.h>
|
||||||
#include <LibWeb/HTML/HTMLScriptElement.h>
|
#include <LibWeb/HTML/HTMLScriptElement.h>
|
||||||
#include <LibWeb/HTML/HTMLTitleElement.h>
|
#include <LibWeb/HTML/HTMLTitleElement.h>
|
||||||
|
#include <LibWeb/HTML/ListOfAvailableImages.h>
|
||||||
#include <LibWeb/HTML/Location.h>
|
#include <LibWeb/HTML/Location.h>
|
||||||
#include <LibWeb/HTML/MessageEvent.h>
|
#include <LibWeb/HTML/MessageEvent.h>
|
||||||
#include <LibWeb/HTML/Navigable.h>
|
#include <LibWeb/HTML/Navigable.h>
|
||||||
|
@ -330,6 +331,8 @@ JS::ThrowCompletionOr<void> Document::initialize(JS::Realm& realm)
|
||||||
|
|
||||||
m_selection = MUST_OR_THROW_OOM(heap().allocate<Selection::Selection>(realm, realm, *this));
|
m_selection = MUST_OR_THROW_OOM(heap().allocate<Selection::Selection>(realm, realm, *this));
|
||||||
|
|
||||||
|
m_list_of_available_images = TRY_OR_THROW_OOM(realm.vm(), try_make<HTML::ListOfAvailableImages>());
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2567,4 +2570,14 @@ void Document::make_active()
|
||||||
HTML::relevant_settings_object(window).execution_ready = true;
|
HTML::relevant_settings_object(window).execution_ready = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HTML::ListOfAvailableImages& Document::list_of_available_images()
|
||||||
|
{
|
||||||
|
return *m_list_of_available_images;
|
||||||
|
}
|
||||||
|
|
||||||
|
HTML::ListOfAvailableImages const& Document::list_of_available_images() const
|
||||||
|
{
|
||||||
|
return *m_list_of_available_images;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -474,6 +474,9 @@ public:
|
||||||
|
|
||||||
void set_salvageable(bool value) { m_salvageable = value; };
|
void set_salvageable(bool value) { m_salvageable = value; };
|
||||||
|
|
||||||
|
HTML::ListOfAvailableImages& list_of_available_images();
|
||||||
|
HTML::ListOfAvailableImages const& list_of_available_images() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
|
||||||
virtual void visit_edges(Cell::Visitor&) override;
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
@ -639,6 +642,9 @@ private:
|
||||||
|
|
||||||
// NOTE: This is a cache to make finding the first <base href> element O(1).
|
// NOTE: This is a cache to make finding the first <base href> element O(1).
|
||||||
JS::GCPtr<HTML::HTMLBaseElement const> m_first_base_element_with_href_in_tree_order;
|
JS::GCPtr<HTML::HTMLBaseElement const> m_first_base_element_with_href_in_tree_order;
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/images.html#list-of-available-images
|
||||||
|
OwnPtr<HTML::ListOfAvailableImages> m_list_of_available_images;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
|
|
@ -373,6 +373,7 @@ class HTMLUnknownElement;
|
||||||
class HTMLVideoElement;
|
class HTMLVideoElement;
|
||||||
class ImageData;
|
class ImageData;
|
||||||
class ImageRequest;
|
class ImageRequest;
|
||||||
|
class ListOfAvailableImages;
|
||||||
class Location;
|
class Location;
|
||||||
class MediaError;
|
class MediaError;
|
||||||
class MessageChannel;
|
class MessageChannel;
|
||||||
|
|
66
Userland/Libraries/LibWeb/HTML/ListOfAvailableImages.cpp
Normal file
66
Userland/Libraries/LibWeb/HTML/ListOfAvailableImages.cpp
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/HTML/DecodedImageData.h>
|
||||||
|
#include <LibWeb/HTML/ListOfAvailableImages.h>
|
||||||
|
|
||||||
|
namespace Web::HTML {
|
||||||
|
|
||||||
|
ListOfAvailableImages::ListOfAvailableImages() = default;
|
||||||
|
ListOfAvailableImages::~ListOfAvailableImages() = default;
|
||||||
|
|
||||||
|
bool ListOfAvailableImages::Key::operator==(Key const& other) const
|
||||||
|
{
|
||||||
|
return url == other.url && mode == other.mode && origin == other.origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 ListOfAvailableImages::Key::hash() const
|
||||||
|
{
|
||||||
|
if (!cached_hash.has_value()) {
|
||||||
|
u32 url_hash = Traits<AK::URL>::hash(url);
|
||||||
|
u32 mode_hash = static_cast<u32>(mode);
|
||||||
|
u32 origin_hash = 0;
|
||||||
|
if (origin.has_value())
|
||||||
|
origin_hash = Traits<HTML::Origin>::hash(origin.value());
|
||||||
|
cached_hash = pair_int_hash(url_hash, pair_int_hash(mode_hash, origin_hash));
|
||||||
|
}
|
||||||
|
return cached_hash.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<ListOfAvailableImages::Entry>> ListOfAvailableImages::Entry::create(NonnullRefPtr<DecodedImageData> image_data, bool ignore_higher_layer_caching)
|
||||||
|
{
|
||||||
|
return adopt_nonnull_ref_or_enomem(new (nothrow) Entry(move(image_data), ignore_higher_layer_caching));
|
||||||
|
}
|
||||||
|
|
||||||
|
ListOfAvailableImages::Entry::Entry(NonnullRefPtr<DecodedImageData> data, bool ignore_higher_layer_caching)
|
||||||
|
: ignore_higher_layer_caching(ignore_higher_layer_caching)
|
||||||
|
, image_data(move(data))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ListOfAvailableImages::Entry::~Entry() = default;
|
||||||
|
|
||||||
|
ErrorOr<void> ListOfAvailableImages::add(Key const& key, NonnullRefPtr<DecodedImageData> image_data, bool ignore_higher_layer_caching)
|
||||||
|
{
|
||||||
|
auto entry = TRY(Entry::create(move(image_data), ignore_higher_layer_caching));
|
||||||
|
TRY(m_images.try_set(key, move(entry)));
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListOfAvailableImages::remove(Key const& key)
|
||||||
|
{
|
||||||
|
m_images.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<ListOfAvailableImages::Entry> ListOfAvailableImages::get(Key const& key) const
|
||||||
|
{
|
||||||
|
auto it = m_images.find(key);
|
||||||
|
if (it == m_images.end())
|
||||||
|
return nullptr;
|
||||||
|
return it->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
71
Userland/Libraries/LibWeb/HTML/ListOfAvailableImages.h
Normal file
71
Userland/Libraries/LibWeb/HTML/ListOfAvailableImages.h
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
|
#include <LibJS/Heap/Cell.h>
|
||||||
|
#include <LibWeb/Forward.h>
|
||||||
|
#include <LibWeb/HTML/CORSSettingAttribute.h>
|
||||||
|
#include <LibWeb/HTML/Origin.h>
|
||||||
|
|
||||||
|
namespace Web::HTML {
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/images.html#list-of-available-images
|
||||||
|
class ListOfAvailableImages {
|
||||||
|
public:
|
||||||
|
struct Key {
|
||||||
|
AK::URL url;
|
||||||
|
HTML::CORSSettingAttribute mode;
|
||||||
|
Optional<HTML::Origin> origin;
|
||||||
|
|
||||||
|
[[nodiscard]] bool operator==(Key const& other) const;
|
||||||
|
[[nodiscard]] u32 hash() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable Optional<u32> cached_hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Entry final : public RefCounted<Entry> {
|
||||||
|
static ErrorOr<NonnullRefPtr<Entry>> create(NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
|
||||||
|
~Entry();
|
||||||
|
|
||||||
|
bool ignore_higher_layer_caching { false };
|
||||||
|
NonnullRefPtr<DecodedImageData> image_data;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Entry(NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
|
||||||
|
};
|
||||||
|
|
||||||
|
ListOfAvailableImages();
|
||||||
|
~ListOfAvailableImages();
|
||||||
|
|
||||||
|
ErrorOr<void> add(Key const&, NonnullRefPtr<DecodedImageData>, bool ignore_higher_layer_caching);
|
||||||
|
void remove(Key const&);
|
||||||
|
[[nodiscard]] RefPtr<Entry> get(Key const&) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
HashMap<Key, NonnullRefPtr<Entry>> m_images;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace AK {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct Traits<Web::HTML::ListOfAvailableImages::Key> : public GenericTraits<Web::HTML::ListOfAvailableImages::Key> {
|
||||||
|
static unsigned hash(Web::HTML::ListOfAvailableImages::Key const& key)
|
||||||
|
{
|
||||||
|
return key.hash();
|
||||||
|
}
|
||||||
|
static bool equals(Web::HTML::ListOfAvailableImages::Key const& a, Web::HTML::ListOfAvailableImages::Key const& b)
|
||||||
|
{
|
||||||
|
return a == b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue