Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uniformize CssValue.Equals implementation #163

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/AngleSharp.Css/Values/Composites/CssBackgroundLayerValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

sealed class CssBackgroundLayerValue : ICssCompositeValue, IEquatable<CssBackgroundLayerValue>
{
Expand Down Expand Up @@ -139,11 +140,17 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssBackgroundLayerValue other)
{
return _clip.Equals(other._clip) &&
_repeat.Equals(other._repeat) &&
_attachment.Equals(other._attachment) &&
_origin.Equals(other._origin) &&
_size.Equals(other._size);
if(other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_clip, other._clip) &&
comparer.Equals(_repeat, other._repeat) &&
comparer.Equals(_attachment, other._attachment) &&
comparer.Equals(_origin, other._origin) &&
comparer.Equals(_size, other._size);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBackgroundLayerValue value && Equals(value);
Expand Down
12 changes: 11 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssBackgroundSizeValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
{
using AngleSharp.Css.Dom;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a CSS background size definition.
Expand Down Expand Up @@ -94,7 +95,16 @@ public String CssText
/// </summary>
/// <param name="other">The other background size.</param>
/// <returns>True if both are equivalent, otherwise false.</returns>
public Boolean Equals(CssBackgroundSizeValue other) => _mode == other._mode && _height.Equals(other._height) && _width.Equals(other._width);
public Boolean Equals(CssBackgroundSizeValue other)
{
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return _mode == other._mode && comparer.Equals(_height, other._height) && comparer.Equals(_width, other._width);
}

return false;
}

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

Expand Down
9 changes: 8 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssBackgroundValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
{
using AngleSharp.Css.Dom;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a CSS background definition.
Expand Down Expand Up @@ -78,7 +79,13 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssBackgroundValue other)
{
return _color.Equals(other._color) && _layers.Equals(other._layers);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_color, other._color) && comparer.Equals(_layers, other._layers);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBackgroundValue value && Equals(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents the CSS border image slice definition.
Expand Down Expand Up @@ -124,7 +125,13 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssBorderImageSliceValue other)
{
return _filled == other._filled && _bottom.Equals(other._bottom) && _left.Equals(other._left) && _right.Equals(other._right) && _top.Equals(other._top);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return _filled == other._filled && comparer.Equals(_bottom, other._bottom) && comparer.Equals(_left, other._left) && comparer.Equals(_right, other._right) && comparer.Equals(_top, other._top);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBorderImageSliceValue value && Equals(value);
Expand Down
21 changes: 14 additions & 7 deletions src/AngleSharp.Css/Values/Composites/CssBorderImageValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a CSS border image definition.
Expand Down Expand Up @@ -111,7 +112,7 @@ public String CssText

#endregion

#region
#region Methods

/// <summary>
/// Checks if the current value is equal to the provided one.
Expand All @@ -120,11 +121,17 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssBorderImageValue other)
{
return _image.Equals(other._image) &&
_slice.Equals(other._slice) &&
_widths.Equals(other._widths) &&
_outsets.Equals(other._outsets) &&
_repeat.Equals(other._repeat);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_image, other._image) &&
comparer.Equals(_slice, other._slice) &&
comparer.Equals(_widths, other._widths) &&
comparer.Equals(_outsets, other._outsets) &&
comparer.Equals(_repeat, other._repeat);
}

return false;
}

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

#endregion
}
Expand Down
9 changes: 8 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssBorderRadiusValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
{
using AngleSharp.Css.Dom;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a border radius value.
Expand Down Expand Up @@ -74,7 +75,13 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssBorderRadiusValue other)
{
return _horizontal.Equals(other._horizontal) && _vertical.Equals(other._vertical);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_horizontal, other._horizontal) && comparer.Equals(_vertical, other._vertical);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssBorderRadiusValue value && Equals(value);
Expand Down
27 changes: 16 additions & 11 deletions src/AngleSharp.Css/Values/Composites/CssCursorValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
Expand Down Expand Up @@ -74,22 +75,26 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssCursorValue other)
{
var l = _definitions.Length;

if (_cursor.Equals(other._cursor) && l == other._definitions.Length)
if (other is not null)
{
for (var i = 0; i < l; i++)
{
var a = _definitions[i];
var b = other._definitions[i];
var comparer = EqualityComparer<ICssValue>.Default;
var l = _definitions.Length;

if (!a.Equals(b))
if (comparer.Equals(_cursor, other._cursor) && l == other._definitions.Length)
{
for (var i = 0; i < l; i++)
{
return false;
var a = _definitions[i];
var b = other._definitions[i];

if (!comparer.Equals(a, b))
{
return false;
}
}
}

return true;
return true;
}
}

return false;
Expand Down
10 changes: 9 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssCustomCursorValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

sealed class CssCustomCursorValue : ICssCompositeValue, IEquatable<CssCustomCursorValue>
{
Expand Down Expand Up @@ -72,7 +73,14 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssCustomCursorValue other)
{
return _position.Equals(other._position) && _source.Equals(other._source);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_position, other._position) &&
comparer.Equals(_source, other._source);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssCustomCursorValue value && Equals(value);
Expand Down
21 changes: 14 additions & 7 deletions src/AngleSharp.Css/Values/Composites/CssFontValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a CSS font definition.
Expand Down Expand Up @@ -145,13 +146,19 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssFontValue other)
{
return _lineHeight.Equals(other._lineHeight) &&
_size.Equals(other._size) &&
_weight.Equals(other._weight) &&
_stretch.Equals(other._stretch) &&
_variant.Equals(other._variant) &&
_style.Equals(other._style) &&
_fontFamilies.Equals(other._fontFamilies);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_lineHeight, other._lineHeight) &&
comparer.Equals(_size, other._size) &&
comparer.Equals(_weight, other._weight) &&
comparer.Equals(_stretch, other._stretch) &&
comparer.Equals(_variant, other._variant) &&
comparer.Equals(_style, other._style) &&
comparer.Equals(_fontFamilies, other._fontFamilies);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssFontValue value && Equals(value);
Expand Down
9 changes: 8 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssGradientStopValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace AngleSharp.Css.Values
{
using AngleSharp.Css.Dom;
using System;
using System.Collections.Generic;

/// <summary>
/// More information can be found at the W3C:
Expand Down Expand Up @@ -69,7 +70,13 @@ public CssGradientStopValue(CssColorValue color, ICssValue location = null)
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssGradientStopValue other)
{
return _color.Equals(other._color) && _location.Equals(other._location);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_color, other._color) && comparer.Equals(_location, other._location);
}

return false;
}

ICssValue ICssValue.Compute(ICssComputeContext context)
Expand Down
10 changes: 9 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssGridTemplateValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssGridTemplateValue other)
{
return _areas.Equals(other._areas) && _columns.Equals(other._columns) && _rows.Equals(other._rows);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_areas, other._areas) &&
comparer.Equals(_columns, other._columns) &&
comparer.Equals(_rows, other._rows);
}

return false;
}

Boolean IEquatable<ICssValue>.Equals(ICssValue other) => other is CssGridTemplateValue value && Equals(value);
Expand Down
28 changes: 16 additions & 12 deletions src/AngleSharp.Css/Values/Composites/CssGridValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,28 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssGridValue other)
{
if (_rows.Equals(other._rows) && _columns.Equals(other._columns) && _dense == other._dense)
if (other is not null)
{
var l = _sizes.Length;

if (l == other._sizes.Length)
var comparer = EqualityComparer<ICssValue>.Default;
if (comparer.Equals(_rows, other._rows) && comparer.Equals(_columns, other._columns) && _dense == other._dense)
{
for (var i = 0; i < l; i++)
{
var a = _sizes[i];
var b = other._sizes[i];
var l = _sizes.Length;

if (!a.Equals(b))
if (l == other._sizes.Length)
{
for (var i = 0; i < l; i++)
{
return false;
var a = _sizes[i];
var b = other._sizes[i];

if (!comparer.Equals(a, b))
{
return false;
}
}
}

return true;
return true;
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/AngleSharp.Css/Values/Composites/CssImageRepeatsValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AngleSharp.Css.Values
using AngleSharp.Css.Dom;
using AngleSharp.Text;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a CSS image repeat definition.
Expand Down Expand Up @@ -81,7 +82,13 @@ public String CssText
/// <returns>True if both are equal, otherwise false.</returns>
public Boolean Equals(CssImageRepeatsValue other)
{
return _horizontal.Equals(other._horizontal) && _vertical.Equals(other._vertical);
if (other is not null)
{
var comparer = EqualityComparer<ICssValue>.Default;
return comparer.Equals(_horizontal, other._horizontal) && comparer.Equals(_vertical, other._vertical);
}

return false;
}

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