Skip to content

Commit a2c3ea3

Browse files
committed
CSSOM primitive improvements
1 parent 894bd52 commit a2c3ea3

File tree

10 files changed

+219
-16
lines changed

10 files changed

+219
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released on Sunday, May 12 2019.
1414
- Opened CSSOM, e.g., declared `ICssFunctionValue` `public` (#24)
1515
- Introduced special converter for the `src` declaration (#25)
1616
- Fixed bug regarding CSS grid serialization (#27)
17+
- Added `DefaultRenderDevice` implementation
1718

1819
# 0.10.1
1920

src/AngleSharp.Css.Tests/Rules/CssSupports.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace AngleSharp.Css.Tests.Rules
22
{
33
using AngleSharp.Css.Dom;
4-
using AngleSharp.Css.Tests.Mocks;
54
using NUnit.Framework;
65
using static CssConstructionFunctions;
76

src/AngleSharp.Css/Parser/CssBuilder.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace AngleSharp.Css.Parser
55
using AngleSharp.Dom;
66
using AngleSharp.Text;
77
using System;
8-
using System.Collections.Generic;
98

109
/// <summary>
1110
/// See http://dev.w3.org/csswg/css-syntax/#parsing for details.
@@ -598,10 +597,7 @@ private void JumpToDeclEnd(ref CssToken current)
598597
}
599598
}
600599

601-
private CssToken NextToken()
602-
{
603-
return _tokenizer.Get();
604-
}
600+
private CssToken NextToken() => _tokenizer.Get();
605601

606602
private void CollectTrivia(ref CssToken token)
607603
{
@@ -625,10 +621,8 @@ private void SkipDeclarations(CssToken token)
625621
JumpToRuleEnd(ref token);
626622
}
627623

628-
private void RaiseErrorOccurred(CssParseError code, TextPosition position)
629-
{
624+
private void RaiseErrorOccurred(CssParseError code, TextPosition position) =>
630625
_tokenizer.RaiseErrorOccurred(code, position);
631-
}
632626

633627
#endregion
634628

src/AngleSharp.Css/Values/Primitives/Constant.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace AngleSharp.Css.Values
55
/// <summary>
66
/// Represents a selected CSS enum value.
77
/// </summary>
8-
struct Constant<T> : ICssPrimitiveValue
8+
struct Constant<T> : ICssPrimitiveValue, IEquatable<Constant<T>>
99
{
1010
#region Fields
1111

@@ -42,5 +42,31 @@ public Constant(String key, T data)
4242
public String CssText => _key;
4343

4444
#endregion
45+
46+
#region Methods
47+
48+
/// <summary>
49+
/// Checks for equality against the given constant.
50+
/// </summary>
51+
/// <param name="other">The other constant to check against.</param>
52+
/// <returns>True if both are equal, otherwise false.</returns>
53+
public Boolean Equals(Constant<T> other) => Object.Equals(other.Value, Value);
54+
55+
/// <summary>
56+
/// Checks for equality against the given object.
57+
/// Only matches if the provided object is also a constant.
58+
/// </summary>
59+
/// <param name="obj">The object to check against.</param>
60+
/// <returns>True if both are equal, otherwise false.</returns>
61+
public override Boolean Equals(Object obj) =>
62+
obj is Constant<T> constant ? Equals(constant) : false;
63+
64+
/// <summary>
65+
/// Gets the computed hash code of the constant.
66+
/// </summary>
67+
/// <returns>The hash code of the object.</returns>
68+
public override Int32 GetHashCode() => _data.GetHashCode();
69+
70+
#endregion
4571
}
4672
}

src/AngleSharp.Css/Values/Primitives/CounterDefinition.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace AngleSharp.Css.Values
22
{
3+
using AngleSharp.Text;
34
using System;
45

56
/// <summary>
67
/// Represents a CSS counter.
78
/// </summary>
8-
struct CounterDefinition : ICssPrimitiveValue
9+
struct CounterDefinition : ICssPrimitiveValue, IEquatable<CounterDefinition>
910
{
1011
#region Fields
1112

@@ -55,5 +56,35 @@ public CounterDefinition(String identifier, String listStyle, String separator)
5556
public String DefinedSeparator => _separator;
5657

5758
#endregion
59+
60+
#region Methods
61+
62+
/// <summary>
63+
/// Checks the two counter definitions for equality.
64+
/// </summary>
65+
/// <param name="other">The other counter to check against.</param>
66+
/// <returns>True if both are equal, otherwise false.</returns>
67+
public Boolean Equals(CounterDefinition other) =>
68+
CounterIdentifier.Is(other.CounterIdentifier) &&
69+
ListStyle.Is(other.ListStyle) &&
70+
DefinedSeparator.Is(other.DefinedSeparator);
71+
72+
/// <summary>
73+
/// Checks for equality against the given object, if
74+
/// the provided object is no coutner definition the
75+
/// result is false.
76+
/// </summary>
77+
/// <param name="obj">The object to check against.</param>
78+
/// <returns>True if both are equal, otherwise false.</returns>
79+
public override Boolean Equals(Object obj) =>
80+
obj is CounterDefinition cd ? Equals(cd) : false;
81+
82+
/// <summary>
83+
/// Gets the hash code of the object.
84+
/// </summary>
85+
/// <returns>The computed hash code.</returns>
86+
public override Int32 GetHashCode() => CssText.GetHashCode();
87+
88+
#endregion
5889
}
5990
}

src/AngleSharp.Css/Values/Primitives/CounterValue.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace AngleSharp.Css.Values
22
{
3+
using AngleSharp.Text;
34
using System;
45

56
/// <summary>
67
/// Sets a CSS counter.
78
/// </summary>
8-
struct CounterValue : ICssPrimitiveValue
9+
struct CounterValue : ICssPrimitiveValue, IEquatable<CounterValue>
910
{
1011
#region Fields
1112

@@ -47,5 +48,32 @@ public CounterValue(String name, Int32 value)
4748
public Int32 Value => _value;
4849

4950
#endregion
51+
52+
#region Methods
53+
54+
/// <summary>
55+
/// Checks the two counter values for equality.
56+
/// </summary>
57+
/// <param name="other">The other counter to check against.</param>
58+
/// <returns>True if both are equal, otherwise false.</returns>
59+
public Boolean Equals(CounterValue other) => Name.Is(other.Name) && Value == other.Value;
60+
61+
/// <summary>
62+
/// Checks for equality against the given object,
63+
/// if the provided object is no counter vlaue the
64+
/// result is false.
65+
/// </summary>
66+
/// <param name="obj">The object to check against.</param>
67+
/// <returns>True if both are equal, otherwise false.</returns>
68+
public override Boolean Equals(Object obj) =>
69+
obj is CounterValue cv ? Equals(cv) : false;
70+
71+
/// <summary>
72+
/// Gets the hash code of the object.
73+
/// </summary>
74+
/// <returns>The computed hash code.</returns>
75+
public override Int32 GetHashCode() => CssText.GetHashCode();
76+
77+
#endregion
5078
}
5179
}

src/AngleSharp.Css/Values/Primitives/Identifier.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
namespace AngleSharp.Css.Values
22
{
3+
using AngleSharp.Text;
34
using System;
45

56
/// <summary>
67
/// Represents a CSS identifier value.
78
/// </summary>
8-
struct Identifier : ICssPrimitiveValue
9+
struct Identifier : ICssPrimitiveValue, IEquatable<Identifier>
910
{
1011
#region Fields
1112

@@ -39,5 +40,32 @@ public Identifier(String text)
3940
public String CssText => _text;
4041

4142
#endregion
43+
44+
#region Methods
45+
46+
/// <summary>
47+
/// Checks the two identifiers for equality.
48+
/// </summary>
49+
/// <param name="other">The other identifier to check against.</param>
50+
/// <returns>True if both are equal, otherwise false.</returns>
51+
public Boolean Equals(Identifier other) => Value.Is(other.Value);
52+
53+
/// <summary>
54+
/// Checks for equality against the given object, if
55+
/// the provided object is no identifier the result is
56+
/// false.
57+
/// </summary>
58+
/// <param name="obj">The object to check against.</param>
59+
/// <returns>True if both are equal, otherwise false.</returns>
60+
public override Boolean Equals(Object obj) =>
61+
obj is Identifier ident ? Equals(ident) : false;
62+
63+
/// <summary>
64+
/// Gets the hash code of the object.
65+
/// </summary>
66+
/// <returns>The computed hash code.</returns>
67+
public override Int32 GetHashCode() => _text.GetHashCode();
68+
69+
#endregion
4270
}
4371
}

src/AngleSharp.Css/Values/Primitives/Label.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AngleSharp.Css.Values
66
/// <summary>
77
/// Represents a CSS label ("string") value.
88
/// </summary>
9-
struct Label : ICssPrimitiveValue
9+
struct Label : ICssPrimitiveValue, IEquatable<Label>
1010
{
1111
#region Fields
1212

@@ -40,5 +40,32 @@ public Label(String value)
4040
public String CssText => _value.CssString();
4141

4242
#endregion
43+
44+
#region Methods
45+
46+
/// <summary>
47+
/// Checks the two labels for equality.
48+
/// </summary>
49+
/// <param name="other">The other label to check against.</param>
50+
/// <returns>True if both are equal, otherwise false.</returns>
51+
public Boolean Equals(Label other) => Value.Is(other.Value);
52+
53+
/// <summary>
54+
/// Checks for equality against the given object,
55+
/// if the provided object is no label the result
56+
/// is false.
57+
/// </summary>
58+
/// <param name="obj">The object to check against.</param>
59+
/// <returns>True if both are equal, otherwise false.</returns>
60+
public override Boolean Equals(Object obj) =>
61+
obj is Label label ? Equals(label) : false;
62+
63+
/// <summary>
64+
/// Gets the hash code of the object.
65+
/// </summary>
66+
/// <returns>The computed hash code.</returns>
67+
public override Int32 GetHashCode() => _value.GetHashCode();
68+
69+
#endregion
4370
}
4471
}

src/AngleSharp.Css/Values/Primitives/LineNames.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
namespace AngleSharp.Css.Values
22
{
3+
using AngleSharp.Text;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67

78
/// <summary>
89
/// Represents a CSS line names definition.
910
/// </summary>
10-
struct LineNames : ICssPrimitiveValue
11+
struct LineNames : ICssPrimitiveValue, IEquatable<LineNames>
1112
{
13+
#region Fields
14+
1215
private readonly String[] _names;
1316

17+
#endregion
18+
19+
#region ctor
20+
1421
/// <summary>
1522
/// Creates a new line names definition.
1623
/// </summary>
@@ -20,6 +27,10 @@ public LineNames(IEnumerable<String> names)
2027
_names = names.ToArray();
2128
}
2229

30+
#endregion
31+
32+
#region Properties
33+
2334
/// <summary>
2435
/// Gets the contained line names.
2536
/// </summary>
@@ -29,5 +40,36 @@ public LineNames(IEnumerable<String> names)
2940
/// Gets the CSS text representation.
3041
/// </summary>
3142
public String CssText => $"[{String.Join(" ", _names)}]";
43+
44+
#endregion
45+
46+
#region Methods
47+
48+
/// <summary>
49+
/// Checks the two line names for equality.
50+
/// </summary>
51+
/// <param name="other">The other names to check against.</param>
52+
/// <returns>True if both are equal, otherwise false.</returns>
53+
public Boolean Equals(LineNames other) =>
54+
Names.Length == other.Names.Length &&
55+
Names.Zip(other.Names, (a, b) => a.Is(b)).All(matches => matches);
56+
57+
/// <summary>
58+
/// Checks for equality against the given object, if
59+
/// the provided object are no line names the result is
60+
/// false.
61+
/// </summary>
62+
/// <param name="obj">The object to check against.</param>
63+
/// <returns>True if both are equal, otherwise false.</returns>
64+
public override Boolean Equals(Object obj) =>
65+
obj is LineNames names ? Equals(names) : false;
66+
67+
/// <summary>
68+
/// Gets the hash code of the object.
69+
/// </summary>
70+
/// <returns>The computed hash code.</returns>
71+
public override Int32 GetHashCode() => CssText.GetHashCode();
72+
73+
#endregion
3274
}
3375
}

src/AngleSharp.Css/Values/Primitives/Quote.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AngleSharp.Css.Values
66
/// <summary>
77
/// Represents a CSS quote.
88
/// </summary>
9-
struct Quote : ICssPrimitiveValue
9+
struct Quote : ICssPrimitiveValue, IEquatable<Quote>
1010
{
1111
#region Fields
1212

@@ -48,5 +48,32 @@ public Quote(String open, String close)
4848
public String CssText => String.Concat(_open.CssString(), " ", _close.CssString());
4949

5050
#endregion
51+
52+
#region Methods
53+
54+
/// <summary>
55+
/// Checks the two quotes for equality.
56+
/// </summary>
57+
/// <param name="other">The other quote to check against.</param>
58+
/// <returns>True if both are equal, otherwise false.</returns>
59+
public Boolean Equals(Quote other) => Open.Is(other.Open) && Close.Is(other.Close);
60+
61+
/// <summary>
62+
/// Checks for equality against the given object,
63+
/// if the provided object is no quote the result
64+
/// is false.
65+
/// </summary>
66+
/// <param name="obj">The object to check against.</param>
67+
/// <returns>True if both are equal, otherwise false.</returns>
68+
public override Boolean Equals(Object obj) =>
69+
obj is Quote quote ? Equals(quote) : false;
70+
71+
/// <summary>
72+
/// Gets the hash code of the object.
73+
/// </summary>
74+
/// <returns>The computed hash code.</returns>
75+
public override Int32 GetHashCode() => CssText.GetHashCode();
76+
77+
#endregion
5178
}
5279
}

0 commit comments

Comments
 (0)