1
- using System ;
2
- using System . Collections ;
1
+ using System . Collections ;
3
2
using System . Collections . Generic ;
4
- using System . Linq ;
5
3
6
4
namespace PreMailer . Net {
7
- public class CssAttributeCollection : IDictionary < string , CssAttribute > {
8
- private readonly IDictionary < string , CssValue > _attributes ;
9
- private int _currentPosition ;
10
-
11
- public CssAttributeCollection ( )
12
- {
13
- _attributes = new Dictionary < string , CssValue > ( StringComparer . CurrentCultureIgnoreCase ) ;
14
- _currentPosition = 0 ;
15
- }
16
-
17
- /// <summary>
18
- /// Add a CssAttribute and set it's position to overwrite all previous CssAttributes in the same Collection.
19
- /// </summary>
20
- public void Add ( KeyValuePair < string , CssAttribute > item )
21
- {
22
- _attributes . Add ( item . Key , new CssValue ( position : ++ _currentPosition , attribute : item . Value ) ) ;
23
- }
24
-
25
- /// <summary>
26
- /// Add a CssAttribute and set it's position to overwrite all previous CssAttributes in the same Collection.
27
- /// </summary>
28
- public void Add ( string key , CssAttribute value )
29
- {
30
- _attributes . Add ( key , new CssValue ( position : ++ _currentPosition , attribute : value ) ) ;
31
- }
5
+ public class CssAttributeCollection : IEnumerable < CssAttribute > {
6
+ private readonly List < CssAttribute > _attributes = new List < CssAttribute > ( ) ;
32
7
33
8
/// <summary>
34
9
/// Add or Update a CssAttribute without changing it's position in the case of an update.
35
10
/// </summary>
36
11
public CssAttribute this [ string key ] {
37
- get => _attributes [ key ] . Attribute ;
12
+ get {
13
+ var index = IndexOfKey ( key ) ;
14
+ return index == - 1 ? null : _attributes [ index ] ;
15
+ }
38
16
set {
39
- if ( _attributes . TryGetValue ( key , out var existing ) ) {
40
- _attributes [ key ] = new CssValue ( position : existing . Position , attribute : value ) ;
17
+ var index = IndexOfKey ( key ) ;
18
+ if ( index == - 1 )
19
+ {
20
+ _attributes . Add ( value ) ;
21
+
41
22
}
42
- else {
43
- _attributes [ key ] = new CssValue ( position : ++ _currentPosition , attribute : value ) ;
23
+ else
24
+ {
25
+ _attributes [ index ] = value ;
44
26
}
45
27
}
46
28
}
@@ -51,110 +33,72 @@ public CssAttribute this[string key] {
51
33
public void Merge ( CssAttribute attribute )
52
34
{
53
35
var key = attribute . Style ;
54
- var value = attribute ;
55
36
56
- _attributes [ key ] = new CssValue ( position : ++ _currentPosition , attribute : value ) ;
57
- }
37
+ // Remove previous to instead append at the end
38
+ Remove ( key ) ;
58
39
59
- /// <summary>
60
- /// Copy the entries of this Collection, ordered by position, to the destination Array.
61
- /// </summary>
62
- public void CopyTo ( KeyValuePair < string , CssAttribute > [ ] array , int arrayIndex )
63
- {
64
- var arr = _attributes . OrderBy ( _ => _ . Value . Position ) . Select ( _ => new KeyValuePair < string , CssAttribute > ( _ . Key , _ . Value . Attribute ) ) . ToArray ( ) ;
65
- Array . Copy ( arr , 0 , array , arrayIndex , arr . Length ) ;
40
+ this [ key ] = attribute ;
66
41
}
67
42
68
43
/// <summary>
69
44
/// Gets an Enumerator of the attributes in this collection, ordered by position.
70
45
/// </summary>
71
- public IEnumerator < KeyValuePair < string , CssAttribute > > GetEnumerator ( )
46
+ public IEnumerator < CssAttribute > GetEnumerator ( )
72
47
{
73
- return _attributes
74
- . OrderBy ( _ => _ . Value . Position )
75
- . Select ( pair => new KeyValuePair < string , CssAttribute > ( pair . Key , pair . Value . Attribute ) ) . GetEnumerator ( ) ;
48
+ return _attributes . GetEnumerator ( ) ;
76
49
}
77
50
78
- /// <summary>
79
- /// Gets an Enumerator of the attributes in this collection, ordered by position.
80
- /// </summary>
81
51
IEnumerator IEnumerable . GetEnumerator ( )
82
52
{
83
- return GetEnumerator ( ) ;
53
+ return _attributes . GetEnumerator ( ) ;
84
54
}
85
55
86
- /// <summary>
87
- /// Gets all Keys in this Collection ordered by position.
88
- /// </summary>
89
- public ICollection < string > Keys => _attributes . OrderBy ( _ => _ . Value . Position ) . Select ( _ => _ . Key ) . ToList ( ) ;
90
-
91
- /// <summary>
92
- /// Gets all Values in this Collection ordered by position.
93
- /// </summary>
94
- public ICollection < CssAttribute > Values => _attributes . Values . OrderBy ( _ => _ . Position ) . Select ( _ => _ . Attribute ) . ToList ( ) ;
95
-
96
- /// <inheritdoc />
97
56
public int Count => _attributes . Count ;
98
57
99
- /// <inheritdoc />
100
- public bool IsReadOnly => _attributes . IsReadOnly ;
101
-
102
- /// <inheritdoc />
103
58
public bool TryGetValue ( string key , out CssAttribute value )
104
59
{
105
- if ( _attributes . TryGetValue ( key , out var data ) ) {
106
- value = data . Attribute ;
60
+ var index = IndexOfKey ( key ) ;
61
+ if ( index != - 1 )
62
+ {
63
+ value = _attributes [ index ] ;
107
64
return true ;
108
65
}
109
66
110
67
value = default ;
111
68
return false ;
112
69
}
113
70
114
- /// <inheritdoc />
115
- public bool Contains ( KeyValuePair < string , CssAttribute > item )
71
+ private int IndexOfKey ( string key )
116
72
{
117
- return _attributes . TryGetValue ( item . Key , out var value ) && value . Attribute == item . Value ;
118
- }
73
+ for ( int i = 0 ; i < _attributes . Count ; i ++ )
74
+ {
75
+ var attribute = _attributes [ i ] ;
76
+ if ( string . Equals ( attribute . Style , key , System . StringComparison . OrdinalIgnoreCase ) )
77
+ {
78
+ return i ;
79
+ }
80
+ }
119
81
120
- /// <inheritdoc />
121
- public bool ContainsKey ( string key )
122
- {
123
- return _attributes . ContainsKey ( key ) ;
82
+ return - 1 ;
124
83
}
125
84
126
- /// <inheritdoc />
127
- public bool Remove ( string key )
85
+ public bool ContainsKey ( string key )
128
86
{
129
- return _attributes . Remove ( key ) ;
87
+ return IndexOfKey ( key ) != - 1 ;
130
88
}
131
89
132
- /// <inheritdoc />
133
- public bool Remove ( KeyValuePair < string , CssAttribute > item )
90
+ public void Remove ( string key )
134
91
{
135
- if ( _attributes . TryGetValue ( item . Key , out var value ) && value . Attribute == item . Value ) {
136
- return _attributes . Remove ( new KeyValuePair < string , CssValue > ( item . Key , value ) ) ;
92
+ var index = IndexOfKey ( key ) ;
93
+ if ( index != - 1 )
94
+ {
95
+ _attributes . RemoveAt ( index ) ;
137
96
}
138
-
139
- return false ;
140
97
}
141
-
142
- /// <inheritdoc />
98
+
143
99
public void Clear ( )
144
100
{
145
101
_attributes . Clear ( ) ;
146
102
}
147
-
148
-
149
- private readonly struct CssValue {
150
- public int Position { get ; }
151
- public CssAttribute Attribute { get ; }
152
-
153
- public CssValue ( int position , CssAttribute attribute )
154
- {
155
- Position = position ;
156
- Attribute = attribute ;
157
- }
158
- }
159
103
}
160
104
}
0 commit comments