Menu

[r3166]: / trunk / Src / UCSSBuilder.pas  Maximize  Restore  History

Download this file

225 lines (195 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Classes that help create and manage cascading style sheet code.
}
unit UCSSBuilder;
interface
uses
// Delphi
Generics.Collections,
// Project
UIStringList;
type
{
TCSSSelector:
Class that encapsulates a CSS selector and outputs its CSS code.
}
TCSSSelector = class(TObject)
strict private
var
fProperties: IStringList; // List of properties for this selector
fSelector: string; // Name of selector
public
constructor Create(const Selector: string);
{Constructor. Creates a CSS selector object with given name.
@param Selector [in] Name of selector.
}
function AsString: string;
{Generates the CSS code representing the selector.
@return Required CSS code.
}
function IsEmpty: Boolean;
{Checks whether the selector is empty, i.e. contains no code.
@return True if selector is empty and false if not.
}
procedure AddProperty(const CSSProp: string);
{Adds a new CSS property to the selector.
@param CSSProp [in] CSS property to be added.
}
property Selector: string read fSelector;
{Name of selector}
end;
{
TCSSBuilder:
Class that creates a CSS style sheet and outputs its CSS code.
}
TCSSBuilder = class(TObject)
strict private
type
// Class that maps CSS selector names to selector objects
TCSSSelectorMap = TObjectDictionary<string,TCSSSelector>;
var
fSelectors: TCSSSelectorMap; // Maps selector names to selector objects
function GetSelector(const Selector: string): TCSSSelector;
{Read access method for Selectors property. Returns selector object with
given name.
@param Selector [in] Name of required selector.
@return Selector object with given name or nil if not found.
}
public
constructor Create;
{Constructor. Sets up object.
}
destructor Destroy; override;
{Destructor. Tears down object.
}
function AddSelector(const Selector: string): TCSSSelector;
{Adds a new empty selector with given name to style sheet.
@param Selector [in] Name of new selector.
@return New empty selector object.
}
procedure Clear;
{Clears all selectors from style sheet and frees selector objects.
}
function AsString: string;
{Generates CSS code representing the style sheet.
@return Required CSS code.
}
property Selectors[const Selector: string]: TCSSSelector
read GetSelector;
{Array of CSS selectors in style sheet, indexed by selector name}
end;
implementation
uses
// Project
UComparers, UConsts;
{ TCSSSelector }
procedure TCSSSelector.AddProperty(const CSSProp: string);
{Adds a new CSS property to the selector.
@param CSSProp [in] CSS property to be added.
}
begin
fProperties.Add(CSSProp);
end;
function TCSSSelector.AsString: string;
{Generates the CSS code representing the selector.
@return Required CSS code.
}
var
Lines: IStringList; // lines of CSS
begin
Lines := TIStringList.Create;
// Compose CSS selector statement
Lines.Add(Selector + ' {');
if fProperties.Count > 0 then
Lines.Add(fProperties);
Lines.Add('}');
// Write CSS in string
Result := Lines.GetText(EOL, False) + EOL;
end;
constructor TCSSSelector.Create(const Selector: string);
{Constructor. Creates a CSS selector object with given name.
@param Selector [in] Name of selector.
}
begin
Assert(Selector <> '', ClassName + '.Create: selector is empty string');
inherited Create;
fSelector := Selector;
fProperties := TIStringList.Create;
end;
function TCSSSelector.IsEmpty: Boolean;
{Checks whether the selector is empty, i.e. contains no code.
@return True if selector is empty and false if not.
}
begin
// Selector is empty if background colour not set, there is no font,
// margin is not defined and there is no extra CSS
Result := fProperties.Count = 0;
end;
{ TCSSBuilder }
function TCSSBuilder.AddSelector(const Selector: string): TCSSSelector;
{Adds a new empty selector with given name to style sheet.
@param Selector [in] Name of new selector.
@return New empty selector object.
}
begin
Result := TCSSSelector.Create(Selector);
fSelectors.Add(Selector, Result);
end;
function TCSSBuilder.AsString: string;
{Generates CSS code representing the style sheet.
@return Required CSS code.
}
var
Selector: TCSSSelector; // reference to each selector in map
begin
Result := '';
for Selector in fSelectors.Values do
if not Selector.IsEmpty then
Result := Result + Selector.AsString;
end;
procedure TCSSBuilder.Clear;
{Clears all selectors from style sheet and frees selector objects.
}
begin
fSelectors.Clear; // frees selector objects in .Values[]
end;
constructor TCSSBuilder.Create;
{Constructor. Sets up object.
}
begin
inherited;
// fSelectors treats selector names are not case sensitive
// fSelectors owns value objects and frees them when they are removed from map
fSelectors := TCSSSelectorMap.Create(
[doOwnsValues], TTextEqualityComparer.Create
);
end;
destructor TCSSBuilder.Destroy;
{Destructor. Tears down object.
}
begin
fSelectors.Free; // frees selector objects in fSelectors.Values[]
inherited;
end;
function TCSSBuilder.GetSelector(const Selector: string): TCSSSelector;
{Read access method for Selectors property. Returns selector object with given
name.
@param Selector [in] Name of required selector.
@return Selector object with given name or nil if not found.
}
begin
if fSelectors.ContainsKey(Selector) then
Result := fSelectors[Selector]
else
Result := nil;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.