Skip to content

Commit dbf8aca

Browse files
committed
Uniformize CssValue.Equals implementation
1 parent 102cc32 commit dbf8aca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+446
-198
lines changed

src/AngleSharp.Css/Values/Composites/CssBackgroundLayerValue.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
sealed class CssBackgroundLayerValue : ICssCompositeValue, IEquatable<CssBackgroundLayerValue>
89
{
@@ -139,11 +140,17 @@ public String CssText
139140
/// <returns>True if both are equal, otherwise false.</returns>
140141
public Boolean Equals(CssBackgroundLayerValue other)
141142
{
142-
return _clip.Equals(other._clip) &&
143-
_repeat.Equals(other._repeat) &&
144-
_attachment.Equals(other._attachment) &&
145-
_origin.Equals(other._origin) &&
146-
_size.Equals(other._size);
143+
if(other is not null)
144+
{
145+
var comparer = EqualityComparer<ICssValue>.Default;
146+
return comparer.Equals(_clip, other._clip) &&
147+
comparer.Equals(_repeat, other._repeat) &&
148+
comparer.Equals(_attachment, other._attachment) &&
149+
comparer.Equals(_origin, other._origin) &&
150+
comparer.Equals(_size, other._size);
151+
}
152+
153+
return false;
147154
}
148155

149156
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBackgroundLayerValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssBackgroundSizeValue.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
22
{
33
using AngleSharp.Css.Dom;
44
using System;
5+
using System.Collections.Generic;
56

67
/// <summary>
78
/// Represents a CSS background size definition.
@@ -94,7 +95,16 @@ public String CssText
9495
/// </summary>
9596
/// <param name="other">The other background size.</param>
9697
/// <returns>True if both are equivalent, otherwise false.</returns>
97-
public Boolean Equals(CssBackgroundSizeValue other) => _mode == other._mode && _height.Equals(other._height) && _width.Equals(other._width);
98+
public Boolean Equals(CssBackgroundSizeValue other)
99+
{
100+
if (other is not null)
101+
{
102+
var comparer = EqualityComparer<ICssValue>.Default;
103+
return _mode == other._mode && comparer.Equals(_height, other._height) && comparer.Equals(_width, other._width);
104+
}
105+
106+
return false;
107+
}
98108

99109
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBackgroundSizeValue value && Equals(value);
100110

src/AngleSharp.Css/Values/Composites/CssBackgroundValue.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
22
{
33
using AngleSharp.Css.Dom;
44
using System;
5+
using System.Collections.Generic;
56

67
/// <summary>
78
/// Represents a CSS background definition.
@@ -78,7 +79,13 @@ public String CssText
7879
/// <returns>True if both are equal, otherwise false.</returns>
7980
public Boolean Equals(CssBackgroundValue other)
8081
{
81-
return _color.Equals(other._color) && _layers.Equals(other._layers);
82+
if (other is not null)
83+
{
84+
var comparer = EqualityComparer<ICssValue>.Default;
85+
return comparer.Equals(_color, other._color) && comparer.Equals(_layers, other._layers);
86+
}
87+
88+
return false;
8289
}
8390

8491
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBackgroundValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssBorderImageSliceValue.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
/// <summary>
89
/// Represents the CSS border image slice definition.
@@ -124,7 +125,13 @@ public String CssText
124125
/// <returns>True if both are equal, otherwise false.</returns>
125126
public Boolean Equals(CssBorderImageSliceValue other)
126127
{
127-
return _filled == other._filled && _bottom.Equals(other._bottom) && _left.Equals(other._left) && _right.Equals(other._right) && _top.Equals(other._top);
128+
if (other is not null)
129+
{
130+
var comparer = EqualityComparer<ICssValue>.Default;
131+
return _filled == other._filled && comparer.Equals(_bottom, other._bottom) && comparer.Equals(_left, other._left) && comparer.Equals(_right, other._right) && comparer.Equals(_top, other._top);
132+
}
133+
134+
return false;
128135
}
129136

130137
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBorderImageSliceValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssBorderImageValue.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
/// <summary>
89
/// Represents a CSS border image definition.
@@ -111,7 +112,7 @@ public String CssText
111112

112113
#endregion
113114

114-
#region
115+
#region Methods
115116

116117
/// <summary>
117118
/// Checks if the current value is equal to the provided one.
@@ -120,11 +121,17 @@ public String CssText
120121
/// <returns>True if both are equal, otherwise false.</returns>
121122
public Boolean Equals(CssBorderImageValue other)
122123
{
123-
return _image.Equals(other._image) &&
124-
_slice.Equals(other._slice) &&
125-
_widths.Equals(other._widths) &&
126-
_outsets.Equals(other._outsets) &&
127-
_repeat.Equals(other._repeat);
124+
if (other is not null)
125+
{
126+
var comparer = EqualityComparer<ICssValue>.Default;
127+
return comparer.Equals(_image, other._image) &&
128+
comparer.Equals(_slice, other._slice) &&
129+
comparer.Equals(_widths, other._widths) &&
130+
comparer.Equals(_outsets, other._outsets) &&
131+
comparer.Equals(_repeat, other._repeat);
132+
}
133+
134+
return false;
128135
}
129136

130137
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBorderImageValue value && Equals(value);
@@ -137,7 +144,7 @@ ICssValue ICssValue.Compute(ICssComputeContext context)
137144
var offsets = _outsets.Compute(context);
138145
var repeat = _repeat.Compute(context);
139146
return new CssBorderImageValue(image, slice, widths, offsets, repeat);
140-
}
147+
}
141148

142149
#endregion
143150
}

src/AngleSharp.Css/Values/Composites/CssBorderRadiusValue.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
22
{
33
using AngleSharp.Css.Dom;
44
using System;
5+
using System.Collections.Generic;
56

67
/// <summary>
78
/// Represents a border radius value.
@@ -74,7 +75,13 @@ public String CssText
7475
/// <returns>True if both are equal, otherwise false.</returns>
7576
public Boolean Equals(CssBorderRadiusValue other)
7677
{
77-
return _horizontal.Equals(other._horizontal) && _vertical.Equals(other._vertical);
78+
if (other is not null)
79+
{
80+
var comparer = EqualityComparer<ICssValue>.Default;
81+
return comparer.Equals(_horizontal, other._horizontal) && comparer.Equals(_vertical, other._vertical);
82+
}
83+
84+
return false;
7885
}
7986

8087
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBorderRadiusValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssCursorValue.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67
using System.Linq;
78

89
/// <summary>
@@ -74,22 +75,26 @@ public String CssText
7475
/// <returns>True if both are equal, otherwise false.</returns>
7576
public Boolean Equals(CssCursorValue other)
7677
{
77-
var l = _definitions.Length;
78-
79-
if (_cursor.Equals(other._cursor) && l == other._definitions.Length)
78+
if (other is not null)
8079
{
81-
for (var i = 0; i < l; i++)
82-
{
83-
var a = _definitions[i];
84-
var b = other._definitions[i];
80+
var comparer = EqualityComparer<ICssValue>.Default;
81+
var l = _definitions.Length;
8582

86-
if (!a.Equals(b))
83+
if (comparer.Equals(_cursor, other._cursor) && l == other._definitions.Length)
84+
{
85+
for (var i = 0; i < l; i++)
8786
{
88-
return false;
87+
var a = _definitions[i];
88+
var b = other._definitions[i];
89+
90+
if (!comparer.Equals(a, b))
91+
{
92+
return false;
93+
}
8994
}
90-
}
9195

92-
return true;
96+
return true;
97+
}
9398
}
9499

95100
return false;

src/AngleSharp.Css/Values/Composites/CssCustomCursorValue.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
sealed class CssCustomCursorValue : ICssCompositeValue, IEquatable<CssCustomCursorValue>
89
{
@@ -72,7 +73,14 @@ public String CssText
7273
/// <returns>True if both are equal, otherwise false.</returns>
7374
public Boolean Equals(CssCustomCursorValue other)
7475
{
75-
return _position.Equals(other._position) && _source.Equals(other._source);
76+
if (other is not null)
77+
{
78+
var comparer = EqualityComparer<ICssValue>.Default;
79+
return comparer.Equals(_position, other._position) &&
80+
comparer.Equals(_source, other._source);
81+
}
82+
83+
return false;
7684
}
7785

7886
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssCustomCursorValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssFontValue.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
/// <summary>
89
/// Represents a CSS font definition.
@@ -145,13 +146,19 @@ public String CssText
145146
/// <returns>True if both are equal, otherwise false.</returns>
146147
public Boolean Equals(CssFontValue other)
147148
{
148-
return _lineHeight.Equals(other._lineHeight) &&
149-
_size.Equals(other._size) &&
150-
_weight.Equals(other._weight) &&
151-
_stretch.Equals(other._stretch) &&
152-
_variant.Equals(other._variant) &&
153-
_style.Equals(other._style) &&
154-
_fontFamilies.Equals(other._fontFamilies);
149+
if (other is not null)
150+
{
151+
var comparer = EqualityComparer<ICssValue>.Default;
152+
return comparer.Equals(_lineHeight, other._lineHeight) &&
153+
comparer.Equals(_size, other._size) &&
154+
comparer.Equals(_weight, other._weight) &&
155+
comparer.Equals(_stretch, other._stretch) &&
156+
comparer.Equals(_variant, other._variant) &&
157+
comparer.Equals(_style, other._style) &&
158+
comparer.Equals(_fontFamilies, other._fontFamilies);
159+
}
160+
161+
return false;
155162
}
156163

157164
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssFontValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssGradientStopValue.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
22
{
33
using AngleSharp.Css.Dom;
44
using System;
5+
using System.Collections.Generic;
56

67
/// <summary>
78
/// More information can be found at the W3C:
@@ -69,7 +70,13 @@ public CssGradientStopValue(CssColorValue color, ICssValue location = null)
6970
/// <returns>True if both are equal, otherwise false.</returns>
7071
public Boolean Equals(CssGradientStopValue other)
7172
{
72-
return _color.Equals(other._color) && _location.Equals(other._location);
73+
if (other is not null)
74+
{
75+
var comparer = EqualityComparer<ICssValue>.Default;
76+
return comparer.Equals(_color, other._color) && comparer.Equals(_location, other._location);
77+
}
78+
79+
return false;
7380
}
7481

7582
ICssValue ICssValue.Compute(ICssComputeContext context)

src/AngleSharp.Css/Values/Composites/CssGridTemplateValue.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,15 @@ public String CssText
114114
/// <returns>True if both are equal, otherwise false.</returns>
115115
public Boolean Equals(CssGridTemplateValue other)
116116
{
117-
return _areas.Equals(other._areas) && _columns.Equals(other._columns) && _rows.Equals(other._rows);
117+
if (other is not null)
118+
{
119+
var comparer = EqualityComparer<ICssValue>.Default;
120+
return comparer.Equals(_areas, other._areas) &&
121+
comparer.Equals(_columns, other._columns) &&
122+
comparer.Equals(_rows, other._rows);
123+
}
124+
125+
return false;
118126
}
119127

120128
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssGridTemplateValue value && Equals(value);

src/AngleSharp.Css/Values/Composites/CssGridValue.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,28 @@ public String CssText
9999
/// <returns>True if both are equal, otherwise false.</returns>
100100
public Boolean Equals(CssGridValue other)
101101
{
102-
if (_rows.Equals(other._rows) && _columns.Equals(other._columns) && _dense == other._dense)
102+
if (other is not null)
103103
{
104-
var l = _sizes.Length;
105-
106-
if (l == other._sizes.Length)
104+
var comparer = EqualityComparer<ICssValue>.Default;
105+
if (comparer.Equals(_rows, other._rows) && comparer.Equals(_columns, other._columns) && _dense == other._dense)
107106
{
108-
for (var i = 0; i < l; i++)
109-
{
110-
var a = _sizes[i];
111-
var b = other._sizes[i];
107+
var l = _sizes.Length;
112108

113-
if (!a.Equals(b))
109+
if (l == other._sizes.Length)
110+
{
111+
for (var i = 0; i < l; i++)
114112
{
115-
return false;
113+
var a = _sizes[i];
114+
var b = other._sizes[i];
115+
116+
if (!comparer.Equals(a, b))
117+
{
118+
return false;
119+
}
116120
}
117-
}
118121

119-
return true;
122+
return true;
123+
}
120124
}
121125
}
122126

src/AngleSharp.Css/Values/Composites/CssImageRepeatsValue.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
33
using AngleSharp.Css.Dom;
44
using AngleSharp.Text;
55
using System;
6+
using System.Collections.Generic;
67

78
/// <summary>
89
/// Represents a CSS image repeat definition.
@@ -81,7 +82,13 @@ public String CssText
8182
/// <returns>True if both are equal, otherwise false.</returns>
8283
public Boolean Equals(CssImageRepeatsValue other)
8384
{
84-
return _horizontal.Equals(other._horizontal) && _vertical.Equals(other._vertical);
85+
if (other is not null)
86+
{
87+
var comparer = EqualityComparer<ICssValue>.Default;
88+
return comparer.Equals(_horizontal, other._horizontal) && comparer.Equals(_vertical, other._vertical);
89+
}
90+
91+
return false;
8592
}
8693

8794
Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssImageRepeatsValue value && Equals(value);

0 commit comments

Comments
 (0)