diff --git a/Tests/LibWeb/Layout/expected/srcset-sizes-crash.txt b/Tests/LibWeb/Layout/expected/srcset-sizes-crash.txt
new file mode 100644
index 00000000000..293a6b7e384
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/srcset-sizes-crash.txt
@@ -0,0 +1,14 @@
+Viewport <#document> at (0,0) content-size 800x600 children: not-inline
+ BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline
+ BlockContainer
at (8,8) content-size 784x17 children: inline
+ frag 0 from ImageBox start: 0, length: 0, rect: [8,20 1x1] baseline: 1
+ frag 1 from TextNode start: 0, length: 19, rect: [9,8 162.109375x17] baseline: 13.296875
+ "PASS (didn't crash)"
+ ImageBox
at (8,20) content-size 1x1 children: not-inline
+ TextNode <#text>
+
+ViewportPaintable (Viewport<#document>) [0,0 800x600]
+ PaintableWithLines (BlockContainer) [0,0 800x600]
+ PaintableWithLines (BlockContainer) [8,8 784x17]
+ ImagePaintable (ImageBox
) [8,20 1x1]
+ TextPaintable (TextNode<#text>)
diff --git a/Tests/LibWeb/Layout/input/srcset-sizes-crash.html b/Tests/LibWeb/Layout/input/srcset-sizes-crash.html
new file mode 100644
index 00000000000..31d4c1cb2e9
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/srcset-sizes-crash.html
@@ -0,0 +1 @@
+
PASS (didn't crash)
diff --git a/Userland/Libraries/LibWeb/HTML/SourceSet.cpp b/Userland/Libraries/LibWeb/HTML/SourceSet.cpp
index 2c29ba84567..da53794453c 100644
--- a/Userland/Libraries/LibWeb/HTML/SourceSet.cpp
+++ b/Userland/Libraries/LibWeb/HTML/SourceSet.cpp
@@ -418,22 +418,24 @@ void SourceSet::normalize_source_densities(DOM::Element const& element)
// 2. Otherwise, if the image source has a width descriptor,
// replace the width descriptor with a pixel density descriptor
// with a value of the width descriptor value divided by the source size and a unit of x.
+ auto descriptor_value_set = false;
if (image_source.descriptor.has()) {
auto& width_descriptor = image_source.descriptor.get();
if (source_size.is_absolute()) {
- image_source.descriptor = ImageSource::PixelDensityDescriptorValue {
- .value = (width_descriptor.value / source_size.absolute_length_to_px()).to_double()
- };
+ auto source_size_in_pixels = source_size.absolute_length_to_px();
+ if (source_size_in_pixels != 0) {
+ image_source.descriptor = ImageSource::PixelDensityDescriptorValue {
+ .value = (width_descriptor.value / source_size_in_pixels).to_double()
+ };
+ descriptor_value_set = true;
+ }
} else {
dbgln("FIXME: Image element has unresolved relative length '{}' in sizes attribute", source_size);
- image_source.descriptor = ImageSource::PixelDensityDescriptorValue {
- .value = 1,
- };
}
}
// 3. Otherwise, give the image source a pixel density descriptor of 1x.
- else {
+ if (!descriptor_value_set) {
image_source.descriptor = ImageSource::PixelDensityDescriptorValue {
.value = 1.0f
};