mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +00:00
LibWeb: Add a class to represent the "source set" concept from HTML
Also comes with a little extra CSS parser helper for parsing "sizes" attributes in images.
This commit is contained in:
parent
9281bf7a01
commit
6fe4fcb74b
5 changed files with 500 additions and 0 deletions
55
Userland/Libraries/LibWeb/HTML/SourceSet.h
Normal file
55
Userland/Libraries/LibWeb/HTML/SourceSet.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/URL.h>
|
||||
#include <AK/Variant.h>
|
||||
#include <LibWeb/CSS/Length.h>
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/images.html#image-source
|
||||
struct ImageSource {
|
||||
struct PixelDensityDescriptorValue {
|
||||
float value { 0 };
|
||||
};
|
||||
|
||||
struct WidthDescriptorValue {
|
||||
CSSPixels value { 0 };
|
||||
};
|
||||
|
||||
String url;
|
||||
Variant<Empty, PixelDensityDescriptorValue, WidthDescriptorValue> descriptor;
|
||||
};
|
||||
|
||||
struct ImageSourceAndPixelDensity {
|
||||
ImageSource source;
|
||||
float pixel_density { 1.0f };
|
||||
};
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/images.html#source-set
|
||||
struct SourceSet {
|
||||
static SourceSet create(DOM::Document const&, String default_source, String srcset, String sizes);
|
||||
|
||||
[[nodiscard]] bool is_empty() const;
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/images.html#select-an-image-source-from-a-source-set
|
||||
[[nodiscard]] ImageSourceAndPixelDensity select_an_image_source();
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/images.html#normalise-the-source-densities
|
||||
void normalize_source_densities();
|
||||
|
||||
SourceSet();
|
||||
|
||||
Vector<ImageSource> m_sources;
|
||||
CSS::Length m_source_size;
|
||||
};
|
||||
|
||||
SourceSet parse_a_srcset_attribute(StringView);
|
||||
CSS::Length parse_a_sizes_attribute(DOM::Document const&, StringView);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue