Skip to content

Commit 2d6dff0

Browse files
committed
Change how TCSSBuilder generates CSS
The order the selectors were generated by TCSSBuilder.AsString was indeterminate (it was the order a dictionary enumerated them). This was changed so the the selectors are now rendered in the order they were created. This was done for cases where the ordering of the CSS selectors matters.
1 parent baaf28e commit 2d6dff0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Src/UCSSBuilder.pas

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ TCSSBuilder = class(TObject)
7777
// Class that maps CSS selector names to selector objects
7878
TCSSSelectorMap = TObjectDictionary<string,TCSSSelector>;
7979
var
80-
fSelectors: TCSSSelectorMap; // Maps selector names to selector objects
80+
fSelectors: TCSSSelectorMap; // Maps selector names to selector objects
81+
fSelectorNames: TList<string>; // Lists selector names in order created
8182
function GetSelector(const Selector: string): TCSSSelector;
8283
{Read access method for Selectors property. Returns selector object with
8384
given name.
@@ -105,10 +106,13 @@ TCSSBuilder = class(TObject)
105106
procedure Clear;
106107
{Clears all selectors from style sheet and frees selector objects.
107108
}
109+
110+
/// <summary>Generates CSS code representing the style sheet.</summary>
111+
/// <returns><c>string</c>. The required CSS.</returns>
112+
/// <remarks>The selectors are returned in the order they were created.
113+
/// </remarks>
108114
function AsString: string;
109-
{Generates CSS code representing the style sheet.
110-
@return Required CSS code.
111-
}
115+
112116
property Selectors[const Selector: string]: TCSSSelector
113117
read GetSelector;
114118
{Array of CSS selectors in style sheet, indexed by selector name}
@@ -189,26 +193,29 @@ function TCSSBuilder.AddSelector(const Selector: string): TCSSSelector;
189193
begin
190194
Result := TCSSSelector.Create(Selector);
191195
fSelectors.Add(Selector, Result);
196+
fSelectorNames.Add(Selector);
192197
end;
193198

194199
function TCSSBuilder.AsString: string;
195-
{Generates CSS code representing the style sheet.
196-
@return Required CSS code.
197-
}
198200
var
201+
SelectorName: string; // name of each selector
199202
Selector: TCSSSelector; // reference to each selector in map
200203
begin
201204
Result := '';
202-
for Selector in fSelectors.Values do
205+
for SelectorName in fSelectorNames do
206+
begin
207+
Selector := fSelectors[SelectorName];
203208
if not Selector.IsEmpty then
204209
Result := Result + Selector.AsString;
210+
end;
205211
end;
206212

207213
procedure TCSSBuilder.Clear;
208214
{Clears all selectors from style sheet and frees selector objects.
209215
}
210216
begin
211-
fSelectors.Clear; // frees selector objects in .Values[]
217+
fSelectorNames.Clear;
218+
fSelectors.Clear; // frees owened selector objects in dictionary
212219
end;
213220

214221
constructor TCSSBuilder.Create;
@@ -221,13 +228,15 @@ constructor TCSSBuilder.Create;
221228
fSelectors := TCSSSelectorMap.Create(
222229
[doOwnsValues], TTextEqualityComparer.Create
223230
);
231+
fSelectorNames := TList<string>.Create;
224232
end;
225233

226234
destructor TCSSBuilder.Destroy;
227235
{Destructor. Tears down object.
228236
}
229237
begin
230-
fSelectors.Free; // frees selector objects in fSelectors.Values[]
238+
fSelectorNames.Free;
239+
fSelectors.Free; // frees owened selector objects in dictionary
231240
inherited;
232241
end;
233242

0 commit comments

Comments
 (0)