From 5a42b7a40fd758b619bf3600bde593e827d59e2c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 11 Sep 2025 15:58:10 +0100 Subject: [PATCH] LibWeb/CSS: Implement product_of_two_unit_maps() Helper function for combining two UnitMaps. --- Libraries/LibWeb/CSS/NumericType.cpp | 17 +++++++++++++++++ Libraries/LibWeb/CSS/NumericType.h | 1 + 2 files changed, 18 insertions(+) diff --git a/Libraries/LibWeb/CSS/NumericType.cpp b/Libraries/LibWeb/CSS/NumericType.cpp index 4fa0304c8ff..6358dd9b425 100644 --- a/Libraries/LibWeb/CSS/NumericType.cpp +++ b/Libraries/LibWeb/CSS/NumericType.cpp @@ -16,6 +16,23 @@ namespace Web::CSS { +// https://drafts.css-houdini.org/css-typed-om-1/#product-of-two-unit-maps +UnitMap product_of_two_unit_maps(UnitMap const& units1, UnitMap const& units2) +{ + // 1. Let result be a copy of units1. + auto result = units1; + + // 2. For each unit → power in units2: + for (auto const& [unit, power] : units2) { + // 1. If result[unit] exists, increment result[unit] by power. + // 2. Otherwise, set result[unit] to power. + result.ensure(unit) += power; + } + + // 3. Return result. + return result; +} + Optional NumericType::base_type_from_value_type(ValueType value_type) { switch (value_type) { diff --git a/Libraries/LibWeb/CSS/NumericType.h b/Libraries/LibWeb/CSS/NumericType.h index 67a25e89149..c69752214d1 100644 --- a/Libraries/LibWeb/CSS/NumericType.h +++ b/Libraries/LibWeb/CSS/NumericType.h @@ -14,6 +14,7 @@ namespace Web::CSS { using UnitMap = HashMap; +UnitMap product_of_two_unit_maps(UnitMap const&, UnitMap const&); // https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-type class NumericType {