Commit 4bb7df98 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[css-typed-om] Implement CSSNumericValue.equals

This patch implements CSSNumericValue.equals, which compares if two or
more CSSNumericValues are equal.

Spec: https://drafts.css-houdini.org/css-typed-om-1/#dom-cssnumericvalue-equals

Bug: 776173
Change-Id: I1ea3d16e9e872ad7856e67312e5f2586c9dbb750
Reviewed-on: https://chromium-review.googlesource.com/768167
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarRenée Wright <rjwright@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516983}
parent 76ef4bb1
......@@ -49,6 +49,7 @@ CONSOLE MESSAGE: line 147: interface CSSNumericValue : CSSStyleValue
CONSOLE MESSAGE: line 147: method add
CONSOLE MESSAGE: line 147: method constructor
CONSOLE MESSAGE: line 147: method div
CONSOLE MESSAGE: line 147: method equals
CONSOLE MESSAGE: line 147: method max
CONSOLE MESSAGE: line 147: method min
CONSOLE MESSAGE: line 147: method mul
......@@ -341,6 +342,7 @@ CONSOLE MESSAGE: line 147: interface CSSNumericValue : CSSStyleValue
CONSOLE MESSAGE: line 147: method add
CONSOLE MESSAGE: line 147: method constructor
CONSOLE MESSAGE: line 147: method div
CONSOLE MESSAGE: line 147: method equals
CONSOLE MESSAGE: line 147: method max
CONSOLE MESSAGE: line 147: method min
CONSOLE MESSAGE: line 147: method mul
......
<meta charset="utf-8">
<title>CSSNumericValue.equals tests</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#dom-cssnumericvalue-equals">
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../resources/testhelper.js"></script>
<script>
'use strict';
test(() => {
assert_true(CSS.px(1).equals(CSS.px(1)));
}, 'Two CSSUnitValues with same value and unit are equal');
test(() => {
assert_false(CSS.px(0).equals(CSS.px(1)));
}, 'Two CSSUnitValues with different values are not equal');
test(() => {
assert_false(CSS.px(1).equals(CSS.number(1)));
}, 'Two CSSUnitValues with different units are not equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathProduct(0, new CSSMathNegate(0));
assert_false(a.equals(b));
}, 'Two CSSMathValues with different types are not equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathSum(0);
assert_false(a.equals(b));
}, 'Two CSSMathValues with different number of values are not equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathSum(0, new CSSMathNegate(1));
assert_false(a.equals(b));
}, 'Two CSSMathValues with different values are not equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathSum(0, new CSSMathNegate(0));
assert_true(a.equals(b));
}, 'Two CSSMathValues with same structure are equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathSum(0, new CSSMathNegate(0));
const c = new CSSMathSum(0, new CSSMathNegate(0));
const d = new CSSMathSum(0, new CSSMathNegate(0));
assert_true(a.equals(b, c, d));
}, 'Multiple CSSMathValues with same structure are equal');
test(() => {
const a = new CSSMathSum(0, new CSSMathNegate(0));
const b = new CSSMathSum(0, new CSSMathNegate(0));
const c = new CSSMathSum(0, new CSSMathNegate(1));
const d = new CSSMathSum(0, new CSSMathNegate(0));
assert_false(a.equals(b, c, d));
}, 'Multiple CSSMathValues with one different are not equal');
</script>
......@@ -697,6 +697,7 @@ interface CSSNumericValue : CSSStyleValue
method add
method constructor
method div
method equals
method max
method min
method mul
......
......@@ -41,6 +41,15 @@ class CORE_EXPORT CSSMathInvert : public CSSMathValue {
CSSMathValue::Trace(visitor);
}
bool Equals(const CSSNumericValue& other) const final {
if (other.GetType() != kNegateType)
return false;
// We can safely cast here as we know 'other' has the same type as us.
const auto& other_invert = static_cast<const CSSMathInvert&>(other);
return value_->Equals(*other_invert.value_);
}
private:
CSSMathInvert(CSSNumericValue* value, const CSSNumericValueType& type)
: CSSMathValue(type), value_(value) {}
......
......@@ -40,6 +40,15 @@ class CORE_EXPORT CSSMathNegate : public CSSMathValue {
CSSMathValue::Trace(visitor);
}
bool Equals(const CSSNumericValue& other) const final {
if (other.GetType() != kNegateType)
return false;
// We can safely cast here as we know 'other' has the same type as us.
const auto& other_negate = static_cast<const CSSMathNegate&>(other);
return value_->Equals(*other_negate.value_);
}
private:
CSSMathNegate(CSSNumericValue* value, const CSSNumericValueType& type)
: CSSMathValue(type), value_(value) {}
......
......@@ -27,6 +27,19 @@ class CORE_EXPORT CSSMathVariadic : public CSSMathValue {
CSSMathValue::Trace(visitor);
}
bool Equals(const CSSNumericValue& other) const final {
if (GetType() != other.GetType())
return false;
// We can safely cast here as we know 'other' has the same type as us.
const auto& other_variadic = static_cast<const CSSMathVariadic&>(other);
return std::equal(
NumericValues().begin(), NumericValues().end(),
other_variadic.NumericValues().begin(),
other_variadic.NumericValues().end(),
[](const auto& a, const auto& b) { return a->Equals(*b); });
}
protected:
CSSMathVariadic(CSSNumericArray* values, const CSSNumericValueType& type)
: CSSMathValue(type), values_(values) {}
......
......@@ -199,6 +199,12 @@ CSSNumericValue* CSSNumericValue::max(
return CSSMathMax::Create(std::move(values));
}
bool CSSNumericValue::equals(const HeapVector<CSSNumberish>& args) {
CSSNumericValueVector values = CSSNumberishesToNumericValues(args);
return std::all_of(values.begin(), values.end(),
[this](const auto& v) { return Equals(*v); });
}
CSSNumericValue* CSSNumericValue::Negate() {
return CSSMathNegate::Create(this);
}
......
......@@ -40,6 +40,7 @@ class CORE_EXPORT CSSNumericValue : public CSSStyleValue {
CSSNumericValue* div(const HeapVector<CSSNumberish>&, ExceptionState&);
CSSNumericValue* min(const HeapVector<CSSNumberish>&, ExceptionState&);
CSSNumericValue* max(const HeapVector<CSSNumberish>&, ExceptionState&);
bool equals(const HeapVector<CSSNumberish>&);
// Converts between compatible types, as defined in the IDL.
CSSNumericValue* to(const String&, ExceptionState&);
......@@ -49,6 +50,7 @@ class CORE_EXPORT CSSNumericValue : public CSSStyleValue {
virtual CSSUnitValue* to(CSSPrimitiveValue::UnitType) const = 0;
virtual bool IsUnitValue() const = 0;
virtual bool Equals(const CSSNumericValue&) const = 0;
const CSSNumericValueType& Type() const { return type_; }
protected:
......
......@@ -16,6 +16,8 @@ typedef (double or CSSNumericValue) CSSNumberish;
[RaisesException, NewObject] CSSNumericValue min(CSSNumberish... values);
[RaisesException, NewObject] CSSNumericValue max(CSSNumberish... values);
boolean equals(CSSNumberish... values);
[RaisesException, NewObject] CSSNumericValue to(DOMString unit);
// Putting Exposed=Window in the next line makes |parse| not exposed to PaintWorklet.
......
......@@ -300,4 +300,12 @@ double CSSUnitValue::ConvertAngle(CSSPrimitiveValue::UnitType unit) const {
}
}
bool CSSUnitValue::Equals(const CSSNumericValue& other) const {
if (!other.IsUnitValue())
return false;
const CSSUnitValue& other_unit_value = ToCSSUnitValue(other);
return value_ == other_unit_value.value_ && unit_ == other_unit_value.unit_;
}
} // namespace blink
......@@ -39,6 +39,7 @@ class CORE_EXPORT CSSUnitValue final : public CSSNumericValue {
// From CSSNumericValue.
CSSUnitValue* to(CSSPrimitiveValue::UnitType) const final;
bool IsUnitValue() const final { return true; }
bool Equals(const CSSNumericValue&) const final;
// From CSSStyleValue.
StyleValueType GetType() const final;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment