Diff
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/cssRotate.tentative-expected.txt (295218 => 295219)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/cssRotate.tentative-expected.txt 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/cssRotate.tentative-expected.txt 2022-06-03 19:59:27 UTC (rev 295219)
@@ -13,7 +13,7 @@
PASS Updating CSSRotate.angle to a CSSMathValue that doesn't match <angle> throws a TypeError
PASS CSSRotate can be constructed from a single angle
PASS CSSRotate can be constructed from numberish coordinates
-FAIL CSSRotate can be constructed from CSSMathValues Type error
+PASS CSSRotate can be constructed from CSSMathValues
PASS CSSRotate.x can be updated to a double
PASS CSSRotate.x can be updated to a number CSSUnitValue
PASS CSSRotate.x can be updated to a CSSMathValue matching <number>
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathValue.tentative-expected.txt (295218 => 295219)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathValue.tentative-expected.txt 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/cssMathValue.tentative-expected.txt 2022-06-03 19:59:27 UTC (rev 295219)
@@ -7,11 +7,11 @@
PASS CSSMathProduct can be constructed from a single number CSSUnitValue
PASS CSSMathProduct can be constructed from more than one number CSSUnitValue
PASS CSSMathProduct.operator is readonly
-FAIL Constructing a CSSMathMin with no arguments throws a SyntaxError assert_throws_dom: function "() => new subclass()" did not throw
+PASS Constructing a CSSMathMin with no arguments throws a SyntaxError
PASS CSSMathMin can be constructed from a single number CSSUnitValue
PASS CSSMathMin can be constructed from more than one number CSSUnitValue
PASS CSSMathMin.operator is readonly
-FAIL Constructing a CSSMathMax with no arguments throws a SyntaxError assert_throws_dom: function "() => new subclass()" did not throw
+PASS Constructing a CSSMathMax with no arguments throws a SyntaxError
PASS CSSMathMax can be constructed from a single number CSSUnitValue
PASS CSSMathMax can be constructed from more than one number CSSUnitValue
PASS CSSMathMax.operator is readonly
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/to.tentative-expected.txt (295218 => 295219)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/to.tentative-expected.txt 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/to.tentative-expected.txt 2022-06-03 19:59:27 UTC (rev 295219)
@@ -1,17 +1,17 @@
-FAIL Converting a CSSUnitValue to an invalid unit throws SyntaxError assert_throws_dom: function "() => CSS.px(1).to('lemon')" did not throw
-FAIL Converting a CSSNumericValue with invalid sum value throws TypeError assert_throws_js: function "() => new CSSMathMax(1, CSS.px(1)).to('number')" did not throw
-FAIL Converting a CSSNumericValue with sum value containing more than one value throws TypeError assert_throws_js: function "() => new CSSMathProduct(CSS.px(1), CSS.s(1)).to('number')" did not throw
-FAIL Converting a CSSUnitValue to an incompatible unit throws TypeError assert_throws_js: function "() => CSS.px(1).to('number')" did not throw
-FAIL Converting a CSSUnitValue to its own unit returns itself assert_equals: expected "percent" but got "number"
-FAIL Converting a CSSUnitValue to its canonical unit returns correct value assert_approx_equals: expected 37.7952755 +/- 0.000001 but got 1
-FAIL Converting a CSSMathSum to a single unit adds the values assert_approx_equals: expected 2 +/- 0.000001 but got 1
-FAIL Converting a CSSMathProduct to a single unit multiplies the values assert_approx_equals: expected 6 +/- 0.000001 but got 1
-FAIL Converting a CSSMathMin to a single unit finds the min value assert_equals: expected "mm" but got "number"
-FAIL Converting a CSSMathMin to a single unit with different units throws a TypeError assert_throws_js: function "() => new CSSMathMin(CSS.px(2), CSS.s(3)).to('px')" did not throw
-FAIL Converting a CSSMathMax to a single unit finds the max value assert_approx_equals: expected 10 +/- 0.000001 but got 1
-FAIL Converting a CSSMathMax to a single unit with different units throws a TypeError assert_throws_js: function "() => new CSSMathMax(CSS.px(2), CSS.s(3)).to('px')" did not throw
-FAIL Converting a CSSMathNegate to a single unit negates its value assert_approx_equals: expected -1 +/- 0.000001 but got 1
-FAIL Converting a CSSMathInvert to a single unit inverts its value and units assert_approx_equals: expected 2 +/- 0.000001 but got 1
-FAIL Converting a complex _expression_ to a single unit Type error
+PASS Converting a CSSUnitValue to an invalid unit throws SyntaxError
+PASS Converting a CSSNumericValue with invalid sum value throws TypeError
+PASS Converting a CSSNumericValue with sum value containing more than one value throws TypeError
+PASS Converting a CSSUnitValue to an incompatible unit throws TypeError
+PASS Converting a CSSUnitValue to its own unit returns itself
+PASS Converting a CSSUnitValue to its canonical unit returns correct value
+PASS Converting a CSSMathSum to a single unit adds the values
+PASS Converting a CSSMathProduct to a single unit multiplies the values
+PASS Converting a CSSMathMin to a single unit finds the min value
+PASS Converting a CSSMathMin to a single unit with different units throws a TypeError
+PASS Converting a CSSMathMax to a single unit finds the max value
+PASS Converting a CSSMathMax to a single unit with different units throws a TypeError
+PASS Converting a CSSMathNegate to a single unit negates its value
+PASS Converting a CSSMathInvert to a single unit inverts its value and units
+PASS Converting a complex _expression_ to a single unit
Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/toSum.tentative-expected.txt (295218 => 295219)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/toSum.tentative-expected.txt 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/toSum.tentative-expected.txt 2022-06-03 19:59:27 UTC (rev 295219)
@@ -1,6 +1,6 @@
FAIL Converting a CSSNumericValue to a sum with invalid units throws SyntaxError assert_throws_dom: function "() => CSS.px(1).toSum('px', 'lemon')" did not throw
-FAIL Converting a CSSNumericValue with an invalid sum value to a sum throws TypeError assert_throws_js: function "() => new CSSMathMax(1, CSS.px(1)).toSum('number')" did not throw
+PASS Converting a CSSNumericValue with an invalid sum value to a sum throws TypeError
FAIL Converting a CSSNumericValue with compound units to a sum throws TypeError assert_throws_js: function "() => new CSSMathProduct(CSS.px(1), CSS.px(1)).to('px')" did not throw
FAIL Converting a CSSNumericValue to a sum with an incompatible unit throws TypeError assert_throws_js: function "() => CSS.px(1).toSum('number')" did not throw
FAIL Converting a CSSNumericValue to a sum with units that are not addable throws TypeError assert_throws_js: function "() => CSS.px(1).toSum('px', 's')" did not throw
Modified: trunk/Source/WebCore/css/CSSUnits.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/CSSUnits.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/CSSUnits.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -147,11 +147,6 @@
return CSSUnitType::CSS_UNKNOWN;
}
-CSSUnitType canonicalUnitType(CSSUnitType unitType)
-{
- return canonicalUnitTypeForCategory(unitCategory(unitType));
-}
-
TextStream& operator<<(TextStream& ts, CSSUnitCategory category)
{
switch (category) {
Modified: trunk/Source/WebCore/css/CSSUnits.h (295218 => 295219)
--- trunk/Source/WebCore/css/CSSUnits.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/CSSUnits.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -146,7 +146,6 @@
CSSUnitCategory unitCategory(CSSUnitType);
CSSUnitType canonicalUnitTypeForCategory(CSSUnitCategory);
-CSSUnitType canonicalUnitType(CSSUnitType);
WTF::TextStream& operator<<(WTF::TextStream&, CSSUnitCategory);
WTF::TextStream& operator<<(WTF::TextStream&, CSSUnitType);
Modified: trunk/Source/WebCore/css/typedom/CSSNumericValue.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/CSSNumericValue.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/CSSNumericValue.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -88,7 +88,7 @@
auto& firstUnitValue = downcast<CSSUnitValue>(values[0].get());
auto unit = firstUnitValue.unitEnum();
double result = firstUnitValue.value();
- for (size_t i = 1; i < values.size(); i++)
+ for (size_t i = 1; i < values.size(); ++i)
result = operation(result, downcast<CSSUnitValue>(values[i].get()).value());
return CSSUnitValue::create(result, unit);
}
@@ -143,7 +143,7 @@
if (allUnitValues) {
bool multipleUnitsFound { false };
std::optional<size_t> nonNumberUnitIndex;
- for (size_t i = 0; i < values.size(); i++) {
+ for (size_t i = 0; i < values.size(); ++i) {
auto unit = downcast<CSSUnitValue>(values[i].get()).unitEnum();
if (unit == CSSUnitType::CSS_NUMBER)
continue;
@@ -194,10 +194,10 @@
if (auto result = operationOnValuesOfSameUnit<const double&(*)(const double&, const double&)>(std::min<double>, values))
return { *result };
- if (!CSSNumericType::addTypes(values))
- return Exception { TypeError };
-
- return { CSSMathMin::create(WTFMove(values)) };
+ auto result = CSSMathMin::create(WTFMove(values));
+ if (result.hasException())
+ return result.releaseException();
+ return { result.releaseReturnValue() };
}
ExceptionOr<Ref<CSSNumericValue>> CSSNumericValue::max(FixedVector<CSSNumberish>&& numberishes)
@@ -208,10 +208,10 @@
if (auto result = operationOnValuesOfSameUnit<const double&(*)(const double&, const double&)>(std::max<double>, values))
return { *result };
- if (!CSSNumericType::addTypes(values))
- return Exception { TypeError };
-
- return { CSSMathMax::create(WTFMove(values)) };
+ auto result = CSSMathMax::create(WTFMove(values));
+ if (result.hasException())
+ return result.releaseException();
+ return { result.releaseReturnValue() };
}
Ref<CSSNumericValue> CSSNumericValue::rectifyNumberish(CSSNumberish&& numberish)
@@ -233,12 +233,41 @@
return false;
}
-Ref<CSSUnitValue> CSSNumericValue::to(String&& unit)
+ExceptionOr<Ref<CSSUnitValue>> CSSNumericValue::to(String&& unit)
{
- UNUSED_PARAM(unit);
+ return to(CSSUnitValue::parseUnit(unit));
+}
+
+ExceptionOr<Ref<CSSUnitValue>> CSSNumericValue::to(CSSUnitType unit)
+{
// https://drafts.css-houdini.org/css-typed-om/#dom-cssnumericvalue-to
- // FIXME: add impl.
- return CSSUnitValue::create(1.0, CSSUnitType::CSS_NUMBER);
+ auto type = CSSNumericType::create(unit);
+ if (!type)
+ return Exception { SyntaxError };
+
+ auto sumValue = toSumValue();
+ if (!sumValue || sumValue->size() != 1)
+ return Exception { TypeError };
+
+ auto& addend = (*sumValue)[0];
+ auto unconverted = [] (const auto& addend) -> RefPtr<CSSUnitValue> {
+ switch (addend.units.size()) {
+ case 0:
+ return CSSUnitValue::create(addend.value, CSSUnitType::CSS_NUMBER);
+ case 1:
+ return CSSUnitValue::create(addend.value, addend.units.begin()->key);
+ default:
+ break;
+ }
+ return nullptr;
+ } (addend);
+ if (!unconverted)
+ return Exception { TypeError };
+
+ auto converted = unconverted->convertTo(unit);
+ if (!converted)
+ return Exception { TypeError };
+ return converted.releaseNonNull();
}
ExceptionOr<Ref<CSSMathSum>> CSSNumericValue::toSum(FixedVector<String>&& units)
Modified: trunk/Source/WebCore/css/typedom/CSSNumericValue.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/CSSNumericValue.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/CSSNumericValue.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -55,7 +55,8 @@
bool equals(FixedVector<CSSNumberish>&&);
- Ref<CSSUnitValue> to(String&&);
+ ExceptionOr<Ref<CSSUnitValue>> to(String&&);
+ ExceptionOr<Ref<CSSUnitValue>> to(CSSUnitType);
ExceptionOr<Ref<CSSMathSum>> toSum(FixedVector<String>&&);
const CSSNumericType& type() const { return m_type; }
@@ -65,6 +66,15 @@
CSSStyleValueType getType() const override { return CSSStyleValueType::CSSNumericValue; }
+ // https://drafts.css-houdini.org/css-typed-om/#sum-value-value
+ using UnitMap = HashMap<CSSUnitType, int, WTF::IntHash<CSSUnitType>, WTF::StrongEnumHashTraits<CSSUnitType>>;
+ struct Addend {
+ double value;
+ UnitMap units;
+ };
+ using SumValue = Vector<Addend>;
+ virtual std::optional<SumValue> toSumValue() const = 0;
+
protected:
ExceptionOr<Ref<CSSNumericValue>> addInternal(Vector<Ref<CSSNumericValue>>&&);
ExceptionOr<Ref<CSSNumericValue>> multiplyInternal(Vector<Ref<CSSNumericValue>>&&);
Modified: trunk/Source/WebCore/css/typedom/CSSUnitValue.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/CSSUnitValue.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/CSSUnitValue.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -40,44 +40,8 @@
WTF_MAKE_ISO_ALLOCATED_IMPL(CSSUnitValue);
-static std::optional<CSSNumericType> numericType(CSSUnitType unit)
+CSSUnitType CSSUnitValue::parseUnit(const String& unit)
{
- // https://drafts.css-houdini.org/css-typed-om/#cssnumericvalue-create-a-type
- CSSNumericType type;
- switch (unitCategory(unit)) {
- case CSSUnitCategory::Number:
- return { WTFMove(type) };
- case CSSUnitCategory::Percent:
- type.percent = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Length:
- type.length = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Angle:
- type.angle = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Time:
- type.time = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Frequency:
- type.frequency = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Resolution:
- type.resolution = 1;
- return { WTFMove(type) };
- case CSSUnitCategory::Other:
- if (unit == CSSUnitType::CSS_FR) {
- type.flex = 1;
- return { WTFMove(type) };
- }
- break;
- }
-
- return std::nullopt;
-}
-
-static CSSUnitType parseUnit(const String& unit)
-{
if (unit == "number"_s)
return CSSUnitType::CSS_NUMBER;
if (unit == "percent"_s)
@@ -122,7 +86,7 @@
auto parsedUnit = parseUnit(unit);
if (parsedUnit == CSSUnitType::CSS_UNKNOWN)
return Exception { TypeError };
- auto type = numericType(parsedUnit);
+ auto type = CSSNumericType::create(parsedUnit);
if (!type)
return Exception { TypeError };
auto unitValue = adoptRef(*new CSSUnitValue(value, parsedUnit));
@@ -131,12 +95,85 @@
}
CSSUnitValue::CSSUnitValue(double value, CSSUnitType unit)
- : CSSNumericValue(numericType(unit).value_or(CSSNumericType()))
+ : CSSNumericValue(CSSNumericType::create(unit).value_or(CSSNumericType()))
, m_value(value)
, m_unit(unit)
{
}
+static double conversionToCanonicalUnitsScaleFactor(CSSUnitType unit)
+{
+ constexpr double pixelsPerInch = 96;
+ constexpr double pixelsPerCentimeter = pixelsPerInch / 2.54;
+ constexpr double pointsPerInch = 72;
+ constexpr double picasPerInch = 6;
+
+ switch (unit) {
+ case CSSUnitType::CSS_MS:
+ return 0.001;
+ case CSSUnitType::CSS_CM:
+ return pixelsPerCentimeter;
+ case CSSUnitType::CSS_DPCM:
+ return 1.0 / pixelsPerCentimeter;
+ case CSSUnitType::CSS_MM:
+ return pixelsPerCentimeter / 10;
+ case CSSUnitType::CSS_Q:
+ return pixelsPerCentimeter / 40;
+ case CSSUnitType::CSS_IN:
+ return pixelsPerInch;
+ case CSSUnitType::CSS_DPI:
+ return 1.0 / pixelsPerInch;
+ case CSSUnitType::CSS_PT:
+ return pixelsPerInch / pointsPerInch;
+ case CSSUnitType::CSS_PC:
+ return pixelsPerInch / picasPerInch;
+ case CSSUnitType::CSS_RAD:
+ return 180 / piDouble;
+ case CSSUnitType::CSS_GRAD:
+ return 0.9;
+ case CSSUnitType::CSS_TURN:
+ return 360;
+ case CSSUnitType::CSS_KHZ:
+ return 1000;
+ default:
+ return 1.0;
+ }
+}
+
+RefPtr<CSSUnitValue> CSSUnitValue::convertTo(CSSUnitType unit) const
+{
+ // https://drafts.css-houdini.org/css-typed-om/#convert-a-cssunitvalue
+ if (unitCategory(unitEnum()) != unitCategory(unit))
+ return nullptr;
+
+ return create(m_value * conversionToCanonicalUnitsScaleFactor(unitEnum()) / conversionToCanonicalUnitsScaleFactor(unit), unit);
+}
+
+auto CSSUnitValue::toSumValue() const -> std::optional<SumValue>
+{
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ auto canonicalUnit = [] (CSSUnitType unit) {
+ // FIXME: We probably want to change the definition of canonicalUnitTypeForCategory so this lambda isn't necessary.
+ auto category = unitCategory(unit);
+ switch (category) {
+ case CSSUnitCategory::Percent:
+ return CSSUnitType::CSS_PERCENTAGE;
+ case CSSUnitCategory::Other:
+ if (unit == CSSUnitType::CSS_FR)
+ return CSSUnitType::CSS_FR;
+ break;
+ default:
+ break;
+ }
+ return canonicalUnitTypeForCategory(category);
+ } (m_unit);
+ auto convertedValue = m_value * conversionToCanonicalUnitsScaleFactor(unitEnum()) / conversionToCanonicalUnitsScaleFactor(canonicalUnit);
+
+ if (m_unit == CSSUnitType::CSS_NUMBER)
+ return { { { convertedValue, { } } } };
+ return { { { convertedValue, { { canonicalUnit, 1 } } } } };
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/CSSUnitValue.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/CSSUnitValue.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/CSSUnitValue.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -50,9 +50,13 @@
ASCIILiteral unitSerialization() const;
CSSUnitType unitEnum() const { return m_unit; }
+ RefPtr<CSSUnitValue> convertTo(CSSUnitType) const;
+ static CSSUnitType parseUnit(const String& unit);
+
private:
CSSUnitValue(double, CSSUnitType);
+ std::optional<SumValue> toSumValue() const final;
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSUnitValue; }
double m_value;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -84,6 +84,27 @@
builder.append(')');
}
+auto CSSMathInvert::toSumValue() const -> std::optional<SumValue>
+{
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ auto values = m_value->toSumValue();
+ if (!values)
+ return std::nullopt;
+ if (values->size() != 1)
+ return std::nullopt;
+ auto& value = (*values)[0];
+ if (!value.value)
+ return std::nullopt;
+ value.value = 1.0 / value.value;
+
+ UnitMap negatedExponents;
+ for (auto& pair : value.units)
+ negatedExponents.add(pair.key, -1 * pair.value);
+ value.units = WTFMove(negatedExponents);
+
+ return values;
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathInvert.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -42,6 +42,7 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Invert; }
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathInvert; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
+ std::optional<SumValue> toSumValue() const final;
CSSMathInvert(CSSNumberish&&);
Ref<CSSNumericValue> m_value;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -30,6 +30,8 @@
#if ENABLE(CSS_TYPED_OM)
+#include "ExceptionOr.h"
+#include <wtf/Algorithms.h>
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/StringBuilder.h>
@@ -37,16 +39,29 @@
WTF_MAKE_ISO_ALLOCATED_IMPL(CSSMathMax);
-CSSMathMax::CSSMathMax(FixedVector<CSSNumberish>&& numberishes)
- : m_values(CSSNumericArray::create(WTFMove(numberishes)))
+ExceptionOr<Ref<CSSMathMax>> CSSMathMax::create(FixedVector<CSSNumberish>&& numberishes)
{
+ return create(WTF::map(WTFMove(numberishes), rectifyNumberish));
}
-CSSMathMax::CSSMathMax(Vector<Ref<CSSNumericValue>>&& values)
- : m_values(CSSNumericArray::create(WTFMove(values)))
+ExceptionOr<Ref<CSSMathMax>> CSSMathMax::create(Vector<Ref<CSSNumericValue>>&& values)
{
+ if (values.isEmpty())
+ return Exception { SyntaxError };
+
+ auto type = CSSNumericType::addTypes(values);
+ if (!type)
+ return Exception { TypeError };
+
+ return adoptRef(*new CSSMathMax(WTFMove(values), WTFMove(*type)));
}
+CSSMathMax::CSSMathMax(Vector<Ref<CSSNumericValue>>&& values, CSSNumericType&& type)
+ : CSSMathValue(WTFMove(type))
+ , m_values(CSSNumericArray::create(WTFMove(values)))
+{
+}
+
const CSSNumericArray& CSSMathMax::values() const
{
return m_values.get();
@@ -66,6 +81,25 @@
builder.append(')');
}
+auto CSSMathMax::toSumValue() const -> std::optional<SumValue>
+{
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ auto& valuesArray = m_values->array();
+ std::optional<SumValue> currentMax = valuesArray[0]->toSumValue();
+ if (!currentMax || currentMax->size() != 1)
+ return std::nullopt;
+ for (size_t i = 1; i < valuesArray.size(); ++i) {
+ auto currentValue = valuesArray[i]->toSumValue();
+ if (!currentValue
+ || currentValue->size() != 1
+ || (*currentValue)[0].units != (*currentMax)[0].units)
+ return std::nullopt;
+ if ((*currentValue)[0].value > (*currentMax)[0].value)
+ currentMax = WTFMove(currentValue);
+ }
+ return currentMax;
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathMax.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -37,7 +37,8 @@
class CSSMathMax final : public CSSMathValue {
WTF_MAKE_ISO_ALLOCATED(CSSMathMax);
public:
- template<typename... Args> static Ref<CSSMathMax> create(Args&&... args) { return adoptRef(*new CSSMathMax(std::forward<Args>(args)...)); }
+ static ExceptionOr<Ref<CSSMathMax>> create(FixedVector<CSSNumberish>&&);
+ static ExceptionOr<Ref<CSSMathMax>> create(Vector<Ref<CSSNumericValue>>&&);
const CSSNumericArray& values() const;
private:
@@ -44,9 +45,9 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Max; }
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathMax; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
+ std::optional<SumValue> toSumValue() const final;
- CSSMathMax(FixedVector<CSSNumberish>&&);
- CSSMathMax(Vector<Ref<CSSNumericValue>>&&);
+ CSSMathMax(Vector<Ref<CSSNumericValue>>&&, CSSNumericType&&);
Ref<CSSNumericArray> m_values;
};
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -26,10 +26,10 @@
#include "config.h"
#include "CSSMathMin.h"
-#include "CSSNumericArray.h"
-
#if ENABLE(CSS_TYPED_OM)
+#include "CSSNumericArray.h"
+#include "ExceptionOr.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/StringBuilder.h>
@@ -37,16 +37,29 @@
WTF_MAKE_ISO_ALLOCATED_IMPL(CSSMathMin);
-CSSMathMin::CSSMathMin(FixedVector<CSSNumberish>&& numberishes)
- : m_values(CSSNumericArray::create(WTFMove(numberishes)))
+ExceptionOr<Ref<CSSMathMin>> CSSMathMin::create(FixedVector<CSSNumberish>&& numberishes)
{
+ return create(WTF::map(WTFMove(numberishes), rectifyNumberish));
}
-CSSMathMin::CSSMathMin(Vector<Ref<CSSNumericValue>>&& values)
- : m_values(CSSNumericArray::create(WTFMove(values)))
+ExceptionOr<Ref<CSSMathMin>> CSSMathMin::create(Vector<Ref<CSSNumericValue>>&& values)
{
+ if (values.isEmpty())
+ return Exception { SyntaxError };
+
+ auto type = CSSNumericType::addTypes(values);
+ if (!type)
+ return Exception { TypeError };
+
+ return adoptRef(*new CSSMathMin(WTFMove(values), WTFMove(*type)));
}
+CSSMathMin::CSSMathMin(Vector<Ref<CSSNumericValue>>&& values, CSSNumericType&& type)
+ : CSSMathValue(WTFMove(type))
+ , m_values(CSSNumericArray::create(WTFMove(values)))
+{
+}
+
const CSSNumericArray& CSSMathMin::values() const
{
return m_values.get();
@@ -66,6 +79,25 @@
builder.append(')');
}
+auto CSSMathMin::toSumValue() const -> std::optional<SumValue>
+{
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ auto& valuesArray = m_values->array();
+ std::optional<SumValue> currentMax = valuesArray[0]->toSumValue();
+ if (!currentMax || currentMax->size() != 1)
+ return std::nullopt;
+ for (size_t i = 1; i < valuesArray.size(); ++i) {
+ auto currentValue = valuesArray[i]->toSumValue();
+ if (!currentValue
+ || currentValue->size() != 1
+ || (*currentValue)[0].units != (*currentMax)[0].units)
+ return std::nullopt;
+ if ((*currentValue)[0].value < (*currentMax)[0].value)
+ currentMax = WTFMove(currentValue);
+ }
+ return currentMax;
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathMin.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -37,7 +37,8 @@
class CSSMathMin final : public CSSMathValue {
WTF_MAKE_ISO_ALLOCATED(CSSMathMin);
public:
- template<typename... Args> static Ref<CSSMathMin> create(Args&&... args) { return adoptRef(*new CSSMathMin(std::forward<Args>(args)...)); }
+ static ExceptionOr<Ref<CSSMathMin>> create(FixedVector<CSSNumberish>&&);
+ static ExceptionOr<Ref<CSSMathMin>> create(Vector<Ref<CSSNumericValue>>&&);
const CSSNumericArray& values() const;
private:
@@ -44,9 +45,9 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Min; }
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathMin; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
+ std::optional<SumValue> toSumValue() const final;
- CSSMathMin(FixedVector<CSSNumberish>&&);
- CSSMathMin(Vector<Ref<CSSNumericValue>>&&);
+ CSSMathMin(Vector<Ref<CSSNumericValue>>&&, CSSNumericType&&);
Ref<CSSNumericArray> m_values;
};
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -66,6 +66,17 @@
builder.append(')');
}
+auto CSSMathNegate::toSumValue() const -> std::optional<SumValue>
+{
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ auto values = m_value->toSumValue();
+ if (!values)
+ return std::nullopt;
+ for (auto& value : *values)
+ value.value = value.value * -1;
+ return values;
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathNegate.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -45,6 +45,7 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Negate; }
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathNegate; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
+ std::optional<SumValue> toSumValue() const final;
CSSMathNegate(CSSNumberish&&);
Ref<CSSNumericValue> m_value;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -82,6 +82,39 @@
builder.append(')');
}
+auto CSSMathProduct::toSumValue() const -> std::optional<SumValue>
+{
+ auto productOfUnits = [] (const auto& units1, const auto& units2) {
+ // https://drafts.css-houdini.org/css-typed-om/#product-of-two-unit-maps
+ auto result = units1;
+ for (auto& pair : units2) {
+ auto addResult = result.add(pair.key, pair.value);
+ if (!addResult.isNewEntry)
+ addResult.iterator->value += pair.value;
+ if (!addResult.iterator->value)
+ result.remove(pair.key);
+ }
+ return result;
+ };
+
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ SumValue values { Addend { 1.0, { } } };
+ for (auto& item : m_values->array()) {
+ auto newValues = item->toSumValue();
+ if (!newValues)
+ return std::nullopt;
+ SumValue temp;
+ for (auto& item1 : values) {
+ for (auto& item2 : *newValues) {
+ Addend item { item1.value * item2.value, productOfUnits(item1.units, item2.units) };
+ temp.append(WTFMove(item));
+ }
+ }
+ values = WTFMove(temp);
+ }
+ return { WTFMove(values) };
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathProduct.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -45,6 +45,7 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Product; }
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathProduct; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const;
+ std::optional<SumValue> toSumValue() const final;
CSSMathProduct(Vector<Ref<CSSNumericValue>>, CSSNumericType);
Ref<CSSNumericArray> m_values;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -83,6 +83,55 @@
builder.append(')');
}
+auto CSSMathSum::toSumValue() const -> std::optional<SumValue>
+{
+ auto convertToNumericType = [] (const UnitMap& units) -> std::optional<CSSNumericType> {
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-type-from-a-unit-map
+ CSSNumericType type;
+ for (auto& pair : units) {
+ auto unit = CSSNumericType::create(pair.key, pair.value);
+ if (!unit)
+ return std::nullopt;
+ auto multipliedType = CSSNumericType::multiplyTypes(type, *unit);
+ if (!multipliedType)
+ return std::nullopt;
+ type = WTFMove(*multipliedType);
+ }
+ return type;
+ };
+
+ // https://drafts.css-houdini.org/css-typed-om/#create-a-sum-value
+ SumValue values;
+ for (auto& item : m_values->array()) {
+ auto value = item->toSumValue();
+ if (!value)
+ return std::nullopt;
+ for (auto& subvalue : *value) {
+ auto index = values.findIf([&](auto& value) {
+ return value.units == subvalue.units;
+ });
+ if (index == notFound)
+ values.append(WTFMove(subvalue));
+ else
+ values[index].value += subvalue.value;
+ }
+ }
+
+ auto type = convertToNumericType(values[0].units);
+ if (!type)
+ return std::nullopt;
+ for (size_t i = 1; i < values.size(); ++i) {
+ auto thisType = convertToNumericType(values[i].units);
+ if (!thisType)
+ return std::nullopt;
+ type = CSSNumericType::addTypes(*type, *thisType);
+ if (!type)
+ return std::nullopt;
+ }
+
+ return { WTFMove(values) };
+}
+
} // namespace WebCore
#endif
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSMathSum.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -46,6 +46,7 @@
CSSMathOperator getOperator() const final { return CSSMathOperator::Sum; }
CSSStyleValueType getType() const override { return CSSStyleValueType::CSSMathSum; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
+ std::optional<SumValue> toSumValue() const final;
CSSMathSum(Vector<Ref<CSSNumericValue>>, CSSNumericType);
Ref<CSSNumericArray> m_values;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSNumericArray.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSNumericArray.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSNumericArray.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -67,7 +67,7 @@
void CSSNumericArray::forEach(Function<void(const CSSNumericValue&, bool first)> function)
{
- for (size_t i = 0; i < m_array.size(); i++)
+ for (size_t i = 0; i < m_array.size(); ++i)
function(m_array[i], !i);
}
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -26,11 +26,48 @@
#include "config.h"
#include "CSSNumericType.h"
#include "CSSNumericValue.h"
+#include "CSSUnits.h"
#if ENABLE(CSS_TYPED_OM)
namespace WebCore {
+std::optional<CSSNumericType> CSSNumericType::create(CSSUnitType unit, int exponent)
+{
+ // https://drafts.css-houdini.org/css-typed-om/#cssnumericvalue-create-a-type
+ CSSNumericType type;
+ switch (unitCategory(unit)) {
+ case CSSUnitCategory::Number:
+ return { WTFMove(type) };
+ case CSSUnitCategory::Percent:
+ type.percent = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Length:
+ type.length = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Angle:
+ type.angle = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Time:
+ type.time = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Frequency:
+ type.frequency = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Resolution:
+ type.resolution = exponent;
+ return { WTFMove(type) };
+ case CSSUnitCategory::Other:
+ if (unit == CSSUnitType::CSS_FR) {
+ type.flex = exponent;
+ return { WTFMove(type) };
+ }
+ break;
+ }
+
+ return std::nullopt;
+}
+
std::optional<CSSNumericType> CSSNumericType::addTypes(CSSNumericType a, CSSNumericType b)
{
// https://drafts.css-houdini.org/css-typed-om/#cssnumericvalue-add-two-types
@@ -64,7 +101,7 @@
if (values.isEmpty())
return std::nullopt;
std::optional<CSSNumericType> type = values[0]->type();
- for (size_t i = 1; i < values.size(); i++) {
+ for (size_t i = 1; i < values.size(); ++i) {
type = function(*type, values[i]->type());
if (!type)
return std::nullopt;
Modified: trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.h (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.h 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/numeric/CSSNumericType.h 2022-06-03 19:59:27 UTC (rev 295219)
@@ -35,6 +35,7 @@
namespace WebCore {
class CSSNumericValue;
+enum class CSSUnitType : uint8_t;
// https://drafts.css-houdini.org/css-typed-om/#dom-cssnumericvalue-type
class CSSNumericType {
@@ -49,9 +50,13 @@
BaseTypeStorage percent;
Markable<CSSNumericBaseType, EnumMarkableTraits<CSSNumericBaseType>> percentHint;
+ static std::optional<CSSNumericType> create(CSSUnitType, int exponent = 1);
bool operator==(const CSSNumericType& other) const;
+ bool operator!=(const CSSNumericType& other) const { return !(*this == other); }
static std::optional<CSSNumericType> addTypes(const Vector<Ref<CSSNumericValue>>&);
+ static std::optional<CSSNumericType> addTypes(CSSNumericType, CSSNumericType);
static std::optional<CSSNumericType> multiplyTypes(const Vector<Ref<CSSNumericValue>>&);
+ static std::optional<CSSNumericType> multiplyTypes(const CSSNumericType&, const CSSNumericType&);
BaseTypeStorage& valueForType(CSSNumericBaseType);
const BaseTypeStorage& valueForType(CSSNumericBaseType type) const { return const_cast<CSSNumericType*>(this)->valueForType(type); }
void applyPercentHint(CSSNumericBaseType);
@@ -80,9 +85,6 @@
}
String debugString() const;
-private:
- static std::optional<CSSNumericType> addTypes(CSSNumericType, CSSNumericType);
- static std::optional<CSSNumericType> multiplyTypes(const CSSNumericType&, const CSSNumericType&);
};
} // namespace WebCore
Modified: trunk/Source/WebCore/css/typedom/transform/CSSTransformValue.cpp (295218 => 295219)
--- trunk/Source/WebCore/css/typedom/transform/CSSTransformValue.cpp 2022-06-03 19:57:21 UTC (rev 295218)
+++ trunk/Source/WebCore/css/typedom/transform/CSSTransformValue.cpp 2022-06-03 19:59:27 UTC (rev 295219)
@@ -94,7 +94,7 @@
void CSSTransformValue::serialize(StringBuilder& builder, OptionSet<SerializationArguments>) const
{
// https://drafts.css-houdini.org/css-typed-om/#serialize-a-csstransformvalue
- for (size_t i = 0; i < m_components.size(); i++) {
+ for (size_t i = 0; i < m_components.size(); ++i) {
if (i)
builder.append(' ');
m_components[i]->serialize(builder);