LibWeb: Clamp CSS z-index to the range of a 32-bit integer

This appears to be consistent with other engines, and fixes many pages
where we were misinterpreting super large z-index values as something
else entirely.
This commit is contained in:
Andreas Kling 2023-04-26 07:19:07 +02:00
parent cead039e7e
commit 7465362fe7
Notes: sideshowbarker 2024-07-17 06:24:08 +09:00

View file

@ -216,8 +216,16 @@ Optional<int> StyleProperties::z_index() const
auto value = property(CSS::PropertyID::ZIndex);
if (value->has_auto())
return {};
if (value->has_integer())
return value->to_integer();
if (value->has_integer()) {
// Clamp z-index to the range of a signed 32-bit integer for consistency with other engines.
// NOTE: Casting between 32-bit float and 32-bit integer is finicky here, since INT32_MAX is not representable as a 32-bit float!
auto integer = value->to_integer();
if (integer >= static_cast<float>(NumericLimits<int>::max()))
return NumericLimits<int>::max();
if (integer <= static_cast<float>(NumericLimits<int>::min()))
return NumericLimits<int>::min();
return static_cast<int>(integer);
}
return {};
}