LibWeb: Avoid division by zero in SourceSet width descriptor calculation

This commit is contained in:
Tim Ledbetter 2024-08-07 23:46:38 +01:00 committed by Alexander Kalenik
commit 087d400472
Notes: github-actions[bot] 2024-08-08 10:20:52 +00:00
3 changed files with 24 additions and 7 deletions

View file

@ -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<ImageSource::WidthDescriptorValue>()) {
auto& width_descriptor = image_source.descriptor.get<ImageSource::WidthDescriptorValue>();
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
};