Skip to content
Permalink
Browse files
Implement CSSNumericValue.equals
https://bugs.webkit.org/show_bug.cgi?id=241378

Reviewed by Chris Dumez.

This is an off-by-default experimental feature.

* LayoutTests/imported/w3c/web-platform-tests/css/css-typed-om/stylevalue-subclasses/numeric-objects/equals.tentative-expected.txt:
* Source/WebCore/bindings/js/JSCSSStyleValueCustom.cpp:
(WebCore::toJSNewlyCreated):
* Source/WebCore/css/typedom/CSSNumericValue.cpp:
(WebCore::CSSNumericValue::equals):
* Source/WebCore/css/typedom/CSSNumericValue.h:
(): Deleted.
* Source/WebCore/css/typedom/CSSStyleValue.h:
(WebCore::isCSSNumericValue):
(WebCore::isCSSMathValue):
* Source/WebCore/css/typedom/CSSUnitValue.cpp:
(WebCore::CSSUnitValue::equals const):
* Source/WebCore/css/typedom/CSSUnitValue.h:
* Source/WebCore/css/typedom/numeric/CSSMathInvert.cpp:
(WebCore::CSSMathInvert::CSSMathInvert):
(WebCore::CSSMathInvert::equals const):
* Source/WebCore/css/typedom/numeric/CSSMathInvert.h:
* Source/WebCore/css/typedom/numeric/CSSMathMax.h:
* Source/WebCore/css/typedom/numeric/CSSMathMin.h:
* Source/WebCore/css/typedom/numeric/CSSMathNegate.cpp:
(WebCore::CSSMathNegate::CSSMathNegate):
(WebCore::CSSMathNegate::equals const):
* Source/WebCore/css/typedom/numeric/CSSMathNegate.h:
* Source/WebCore/css/typedom/numeric/CSSMathProduct.h:
* Source/WebCore/css/typedom/numeric/CSSMathSum.h:
* Source/WebCore/css/typedom/numeric/CSSMathValue.h:
(WebCore::CSSMathValue::CSSMathValue):
(WebCore::CSSMathValue::equalsImpl const):

Canonical link: https://commits.webkit.org/251721@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295716 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
achristensen07 committed Jun 22, 2022
1 parent d5e81d9 commit a2fdaa58d0ef09443e167b91e475a6b00838bcaf
Showing 16 changed files with 74 additions and 18 deletions.
@@ -1,11 +1,11 @@

FAIL Two CSSUnitValues with same value and unit are equal assert_true: expected true got false
PASS Two CSSUnitValues with same value and unit are equal
PASS Two CSSUnitValues with different values are not equal
PASS Two CSSUnitValues with different units are not equal
PASS Two CSSMathValues with different types are not equal
PASS Two CSSMathValues with different number of values are not equal
PASS Two CSSMathValues with different values are not equal
FAIL Two CSSMathValues with same structure are equal assert_true: expected true got false
FAIL Multiple CSSMathValues with same structure are equal assert_true: expected true got false
PASS Two CSSMathValues with same structure are equal
PASS Multiple CSSMathValues with same structure are equal
PASS Multiple CSSMathValues with one different are not equal

@@ -51,8 +51,6 @@ JSValue toJSNewlyCreated(JSGlobalObject*, JSDOMGlobalObject* globalObject, Ref<C
switch (value->getType()) {
case CSSStyleValueType::CSSStyleImageValue:
return createWrapper<CSSStyleImageValue>(globalObject, WTFMove(value));
case CSSStyleValueType::CSSNumericValue:
return createWrapper<CSSNumericValue>(globalObject, WTFMove(value));
case CSSStyleValueType::CSSMathInvert:
return createWrapper<CSSMathInvert>(globalObject, WTFMove(value));
case CSSStyleValueType::CSSMathMin:
@@ -225,12 +225,13 @@ Ref<CSSNumericValue> CSSNumericValue::rectifyNumberish(CSSNumberish&& numberish)
});
}

bool CSSNumericValue::equals(FixedVector<CSSNumberish>&& value)
bool CSSNumericValue::equals(FixedVector<CSSNumberish>&& values)
{
UNUSED_PARAM(value);
// https://drafts.css-houdini.org/css-typed-om/#dom-cssnumericvalue-equals
// FIXME: add impl.
return false;
auto numericValues = WTF::map(WTFMove(values), rectifyNumberish);
return WTF::allOf(numericValues, [&] (const Ref<CSSNumericValue>& value) {
return this->equals(value.get());
});
}

ExceptionOr<Ref<CSSUnitValue>> CSSNumericValue::to(String&& unit)
@@ -64,8 +64,6 @@ class CSSNumericValue : public CSSStyleValue {
static ExceptionOr<Ref<CSSNumericValue>> parse(String&&);
static Ref<CSSNumericValue> rectifyNumberish(CSSNumberish&&);

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 {
@@ -74,6 +72,7 @@ class CSSNumericValue : public CSSStyleValue {
};
using SumValue = Vector<Addend>;
virtual std::optional<SumValue> toSumValue() const = 0;
virtual bool equals(const CSSNumericValue&) const = 0;

protected:
ExceptionOr<Ref<CSSNumericValue>> addInternal(Vector<Ref<CSSNumericValue>>&&);
@@ -44,7 +44,6 @@ enum class CSSStyleValueType : uint8_t {
CSSStyleValue,
CSSStyleImageValue,
CSSTransformValue,
CSSNumericValue,
CSSMathInvert,
CSSMathMin,
CSSMathMax,
@@ -59,7 +58,6 @@ enum class CSSStyleValueType : uint8_t {
inline bool isCSSNumericValue(CSSStyleValueType type)
{
switch (type) {
case CSSStyleValueType::CSSNumericValue:
case CSSStyleValueType::CSSMathInvert:
case CSSStyleValueType::CSSMathMin:
case CSSStyleValueType::CSSMathMax:
@@ -88,7 +86,6 @@ inline bool isCSSMathValue(CSSStyleValueType type)
case CSSStyleValueType::CSSMathProduct:
case CSSStyleValueType::CSSMathSum:
return true;
case CSSStyleValueType::CSSNumericValue:
case CSSStyleValueType::CSSUnitValue:
case CSSStyleValueType::CSSStyleValue:
case CSSStyleValueType::CSSStyleImageValue:
@@ -174,6 +174,15 @@ auto CSSUnitValue::toSumValue() const -> std::optional<SumValue>
return { { { convertedValue, { { canonicalUnit, 1 } } } } };
}

bool CSSUnitValue::equals(const CSSNumericValue& other) const
{
// https://drafts.css-houdini.org/css-typed-om/#equal-numeric-value
auto* otherUnitValue = dynamicDowncast<CSSUnitValue>(other);
if (!otherUnitValue)
return false;
return m_value == otherUnitValue->m_value && m_unit == otherUnitValue->m_unit;
}

} // namespace WebCore

#endif
@@ -56,8 +56,9 @@ class CSSUnitValue final : public CSSNumericValue {
private:
CSSUnitValue(double, CSSUnitType);

std::optional<SumValue> toSumValue() const final;
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSUnitValue; }
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue&) const final;

double m_value;
const CSSUnitType m_unit;
@@ -69,7 +69,7 @@ static CSSNumericType negatedType(const CSSNumberish& numberish)

CSSMathInvert::CSSMathInvert(CSSNumberish&& numberish)
: CSSMathValue(negatedType(numberish))
, m_value(CSSNumericValue::rectifyNumberish(WTFMove(numberish)))
, m_value(rectifyNumberish(WTFMove(numberish)))
{
}

@@ -105,6 +105,16 @@ auto CSSMathInvert::toSumValue() const -> std::optional<SumValue>
return values;
}

bool CSSMathInvert::equals(const CSSNumericValue& other) const
{
// https://drafts.css-houdini.org/css-typed-om/#equal-numeric-value
auto* otherInvert = dynamicDowncast<CSSMathInvert>(other);
if (!otherInvert)
return false;
return m_value->equals(otherInvert->value());

}

} // namespace WebCore

#endif
@@ -43,6 +43,7 @@ class CSSMathInvert final : public CSSMathValue {
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathInvert; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue&) const final;

CSSMathInvert(CSSNumberish&&);
Ref<CSSNumericValue> m_value;
@@ -46,6 +46,7 @@ class CSSMathMax final : public CSSMathValue {
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathMax; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue& other) const final { return equalsImpl<CSSMathMax>(other); }

CSSMathMax(Vector<Ref<CSSNumericValue>>&&, CSSNumericType&&);
Ref<CSSNumericArray> m_values;
@@ -46,6 +46,7 @@ class CSSMathMin final : public CSSMathValue {
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathMin; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue& other) const final { return equalsImpl<CSSMathMin>(other); }

CSSMathMin(Vector<Ref<CSSNumericValue>>&&, CSSNumericType&&);
Ref<CSSNumericArray> m_values;
@@ -51,7 +51,7 @@ static CSSNumericType copyType(const CSSNumberish& numberish)

CSSMathNegate::CSSMathNegate(CSSNumberish&& numberish)
: CSSMathValue(copyType(numberish))
, m_value(CSSNumericValue::rectifyNumberish(WTFMove(numberish)))
, m_value(rectifyNumberish(WTFMove(numberish)))
{
}

@@ -77,6 +77,16 @@ auto CSSMathNegate::toSumValue() const -> std::optional<SumValue>
return values;
}

bool CSSMathNegate::equals(const CSSNumericValue& other) const
{
// https://drafts.css-houdini.org/css-typed-om/#equal-numeric-value
auto* otherNegate = dynamicDowncast<CSSMathNegate>(other);
if (!otherNegate)
return false;
return m_value->equals(otherNegate->value());

}

} // namespace WebCore

#endif
@@ -46,6 +46,7 @@ class CSSMathNegate final : public CSSMathValue {
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathNegate; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue& other) const final;

CSSMathNegate(CSSNumberish&&);
Ref<CSSNumericValue> m_value;
@@ -46,6 +46,7 @@ class CSSMathProduct final : public CSSMathValue {
CSSStyleValueType getType() const final { return CSSStyleValueType::CSSMathProduct; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue& other) const final { return equalsImpl<CSSMathProduct>(other); }

CSSMathProduct(Vector<Ref<CSSNumericValue>>, CSSNumericType);
Ref<CSSNumericArray> m_values;
@@ -47,6 +47,7 @@ class CSSMathSum final : public CSSMathValue {
CSSStyleValueType getType() const override { return CSSStyleValueType::CSSMathSum; }
void serialize(StringBuilder&, OptionSet<SerializationArguments>) const final;
std::optional<SumValue> toSumValue() const final;
bool equals(const CSSNumericValue& other) const final { return equalsImpl<CSSMathSum>(other); }

CSSMathSum(Vector<Ref<CSSNumericValue>>, CSSNumericType);
Ref<CSSNumericArray> m_values;
@@ -28,16 +28,41 @@
#if ENABLE(CSS_TYPED_OM)

#include "CSSMathOperator.h"
#include "CSSNumericArray.h"
#include "CSSNumericValue.h"
#include "CSSStyleValue.h"

namespace WebCore {

class CSSMathValue : public CSSNumericValue {
public:
CSSMathValue(CSSNumericType type = { })
CSSMathValue(CSSNumericType type)
: CSSNumericValue(WTFMove(type)) { }
virtual CSSMathOperator getOperator() const = 0;

template <typename T>
bool equalsImpl(const CSSNumericValue& other) const
{
// https://drafts.css-houdini.org/css-typed-om/#equal-numeric-value
auto* otherT = dynamicDowncast<T>(other);
if (!otherT)
return false;

ASSERT(getType() == other.getType());
auto& thisValues = static_cast<const T*>(this)->values();
auto& otherValues = otherT->values();
auto length = thisValues.length();
if (length != otherValues.length())
return false;

for (size_t i = 0 ; i < length; ++i) {
if (!thisValues.array()[i]->equals(otherValues.array()[i].get()))
return false;
}

return true;

}
};

} // namespace WebCore

0 comments on commit a2fdaa5

Please sign in to comment.