-
Notifications
You must be signed in to change notification settings - Fork 0
/
CssTextWriter.cs
384 lines (331 loc) · 15.9 KB
/
CssTextWriter.cs
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
namespace RoboUI
{
internal sealed class CssTextWriter : TextWriter
{
private TextWriter _writer;
private static Hashtable attrKeyLookupTable;
private static CssTextWriter.AttributeInformation[] attrNameLookupArray;
public override Encoding Encoding => this._writer.Encoding;
public override string NewLine
{
get => this._writer.NewLine;
set => this._writer.NewLine = value;
}
static CssTextWriter()
{
CssTextWriter.attrKeyLookupTable = new Hashtable(43);
CssTextWriter.attrNameLookupArray = new CssTextWriter.AttributeInformation[43];
CssTextWriter.RegisterAttribute("background-color", HtmlTextWriterStyle.BackgroundColor);
CssTextWriter.RegisterAttribute("background-image", HtmlTextWriterStyle.BackgroundImage, true, true);
CssTextWriter.RegisterAttribute("border-collapse", HtmlTextWriterStyle.BorderCollapse);
CssTextWriter.RegisterAttribute("border-color", HtmlTextWriterStyle.BorderColor);
CssTextWriter.RegisterAttribute("border-style", HtmlTextWriterStyle.BorderStyle);
CssTextWriter.RegisterAttribute("border-width", HtmlTextWriterStyle.BorderWidth);
CssTextWriter.RegisterAttribute("color", HtmlTextWriterStyle.Color);
CssTextWriter.RegisterAttribute("cursor", HtmlTextWriterStyle.Cursor);
CssTextWriter.RegisterAttribute("direction", HtmlTextWriterStyle.Direction);
CssTextWriter.RegisterAttribute("display", HtmlTextWriterStyle.Display);
CssTextWriter.RegisterAttribute("filter", HtmlTextWriterStyle.Filter);
CssTextWriter.RegisterAttribute("font-family", HtmlTextWriterStyle.FontFamily, true);
CssTextWriter.RegisterAttribute("font-size", HtmlTextWriterStyle.FontSize);
CssTextWriter.RegisterAttribute("font-style", HtmlTextWriterStyle.FontStyle);
CssTextWriter.RegisterAttribute("font-variant", HtmlTextWriterStyle.FontVariant);
CssTextWriter.RegisterAttribute("font-weight", HtmlTextWriterStyle.FontWeight);
CssTextWriter.RegisterAttribute("height", HtmlTextWriterStyle.Height);
CssTextWriter.RegisterAttribute("left", HtmlTextWriterStyle.Left);
CssTextWriter.RegisterAttribute("list-style-image", HtmlTextWriterStyle.ListStyleImage, true, true);
CssTextWriter.RegisterAttribute("list-style-type", HtmlTextWriterStyle.ListStyleType);
CssTextWriter.RegisterAttribute("margin", HtmlTextWriterStyle.Margin);
CssTextWriter.RegisterAttribute("margin-bottom", HtmlTextWriterStyle.MarginBottom);
CssTextWriter.RegisterAttribute("margin-left", HtmlTextWriterStyle.MarginLeft);
CssTextWriter.RegisterAttribute("margin-right", HtmlTextWriterStyle.MarginRight);
CssTextWriter.RegisterAttribute("margin-top", HtmlTextWriterStyle.MarginTop);
CssTextWriter.RegisterAttribute("overflow-x", HtmlTextWriterStyle.OverflowX);
CssTextWriter.RegisterAttribute("overflow-y", HtmlTextWriterStyle.OverflowY);
CssTextWriter.RegisterAttribute("overflow", HtmlTextWriterStyle.Overflow);
CssTextWriter.RegisterAttribute("padding", HtmlTextWriterStyle.Padding);
CssTextWriter.RegisterAttribute("padding-bottom", HtmlTextWriterStyle.PaddingBottom);
CssTextWriter.RegisterAttribute("padding-left", HtmlTextWriterStyle.PaddingLeft);
CssTextWriter.RegisterAttribute("padding-right", HtmlTextWriterStyle.PaddingRight);
CssTextWriter.RegisterAttribute("padding-top", HtmlTextWriterStyle.PaddingTop);
CssTextWriter.RegisterAttribute("position", HtmlTextWriterStyle.Position);
CssTextWriter.RegisterAttribute("text-align", HtmlTextWriterStyle.TextAlign);
CssTextWriter.RegisterAttribute("text-decoration", HtmlTextWriterStyle.TextDecoration);
CssTextWriter.RegisterAttribute("text-overflow", HtmlTextWriterStyle.TextOverflow);
CssTextWriter.RegisterAttribute("top", HtmlTextWriterStyle.Top);
CssTextWriter.RegisterAttribute("vertical-align", HtmlTextWriterStyle.VerticalAlign);
CssTextWriter.RegisterAttribute("visibility", HtmlTextWriterStyle.Visibility);
CssTextWriter.RegisterAttribute("width", HtmlTextWriterStyle.Width);
CssTextWriter.RegisterAttribute("white-space", HtmlTextWriterStyle.WhiteSpace);
CssTextWriter.RegisterAttribute("z-index", HtmlTextWriterStyle.ZIndex);
}
public CssTextWriter(TextWriter writer)
{
this._writer = writer;
}
public override void Flush()
{
this._writer.Flush();
}
public static HtmlTextWriterStyle GetStyleKey(string styleName)
{
if (!string.IsNullOrEmpty(styleName))
{
object item = CssTextWriter.attrKeyLookupTable[styleName.ToLowerInvariant()];
if (item != null)
{
return (HtmlTextWriterStyle)item;
}
}
return HtmlTextWriterStyle.BackgroundImage | HtmlTextWriterStyle.BorderCollapse | HtmlTextWriterStyle.BorderColor | HtmlTextWriterStyle.BorderStyle | HtmlTextWriterStyle.BorderWidth | HtmlTextWriterStyle.Color | HtmlTextWriterStyle.FontFamily | HtmlTextWriterStyle.FontSize | HtmlTextWriterStyle.FontStyle | HtmlTextWriterStyle.FontWeight | HtmlTextWriterStyle.Height | HtmlTextWriterStyle.TextDecoration | HtmlTextWriterStyle.Width | HtmlTextWriterStyle.ListStyleImage | HtmlTextWriterStyle.ListStyleType | HtmlTextWriterStyle.Cursor | HtmlTextWriterStyle.Direction | HtmlTextWriterStyle.Display | HtmlTextWriterStyle.Filter | HtmlTextWriterStyle.FontVariant | HtmlTextWriterStyle.Left | HtmlTextWriterStyle.Margin | HtmlTextWriterStyle.MarginBottom | HtmlTextWriterStyle.MarginLeft | HtmlTextWriterStyle.MarginRight | HtmlTextWriterStyle.MarginTop | HtmlTextWriterStyle.Overflow | HtmlTextWriterStyle.OverflowX | HtmlTextWriterStyle.OverflowY | HtmlTextWriterStyle.Padding | HtmlTextWriterStyle.PaddingBottom | HtmlTextWriterStyle.PaddingLeft | HtmlTextWriterStyle.PaddingRight | HtmlTextWriterStyle.PaddingTop | HtmlTextWriterStyle.Position | HtmlTextWriterStyle.TextAlign | HtmlTextWriterStyle.VerticalAlign | HtmlTextWriterStyle.TextOverflow | HtmlTextWriterStyle.Top | HtmlTextWriterStyle.Visibility | HtmlTextWriterStyle.WhiteSpace | HtmlTextWriterStyle.ZIndex;
}
public static string GetStyleName(HtmlTextWriterStyle styleKey)
{
if (styleKey < HtmlTextWriterStyle.BackgroundColor || (int)styleKey >= (int)CssTextWriter.attrNameLookupArray.Length)
{
return string.Empty;
}
return CssTextWriter.attrNameLookupArray[(int)styleKey].name;
}
public static bool IsStyleEncoded(HtmlTextWriterStyle styleKey)
{
if (styleKey < HtmlTextWriterStyle.BackgroundColor || (int)styleKey >= (int)CssTextWriter.attrNameLookupArray.Length)
{
return true;
}
return CssTextWriter.attrNameLookupArray[(int)styleKey].encode;
}
internal static void RegisterAttribute(string name, HtmlTextWriterStyle key)
{
CssTextWriter.RegisterAttribute(name, key, false, false);
}
internal static void RegisterAttribute(string name, HtmlTextWriterStyle key, bool encode)
{
CssTextWriter.RegisterAttribute(name, key, encode, false);
}
internal static void RegisterAttribute(string name, HtmlTextWriterStyle key, bool encode, bool isUrl)
{
string lower = name.ToLowerInvariant();
CssTextWriter.attrKeyLookupTable.Add(lower, key);
if ((int)key < (int)CssTextWriter.attrNameLookupArray.Length)
{
CssTextWriter.attrNameLookupArray[(int)key] = new CssTextWriter.AttributeInformation(name, encode, isUrl);
}
}
public override void Write(string s)
{
this._writer.Write(s);
}
public override void Write(bool value)
{
this._writer.Write(value);
}
public override void Write(char value)
{
this._writer.Write(value);
}
public override void Write(char[] buffer)
{
this._writer.Write(buffer);
}
public override void Write(char[] buffer, int index, int count)
{
this._writer.Write(buffer, index, count);
}
public override void Write(double value)
{
this._writer.Write(value);
}
public override void Write(float value)
{
this._writer.Write(value);
}
public override void Write(int value)
{
this._writer.Write(value);
}
public override void Write(long value)
{
this._writer.Write(value);
}
public override void Write(object value)
{
this._writer.Write(value);
}
public override void Write(string format, object arg0)
{
this._writer.Write(format, arg0);
}
public override void Write(string format, object arg0, object arg1)
{
this._writer.Write(format, arg0, arg1);
}
public override void Write(string format, params object[] arg)
{
this._writer.Write(format, arg);
}
public void WriteAttribute(string name, string value)
{
CssTextWriter.WriteAttribute(this._writer, CssTextWriter.GetStyleKey(name), name, value);
}
public void WriteAttribute(HtmlTextWriterStyle key, string value)
{
CssTextWriter.WriteAttribute(this._writer, key, CssTextWriter.GetStyleName(key), value);
}
private static void WriteAttribute(TextWriter writer, HtmlTextWriterStyle key, string name, string value)
{
writer.Write(name);
writer.Write(':');
bool flag = false;
if (key != (HtmlTextWriterStyle.BackgroundImage | HtmlTextWriterStyle.BorderCollapse | HtmlTextWriterStyle.BorderColor | HtmlTextWriterStyle.BorderStyle | HtmlTextWriterStyle.BorderWidth | HtmlTextWriterStyle.Color | HtmlTextWriterStyle.FontFamily | HtmlTextWriterStyle.FontSize | HtmlTextWriterStyle.FontStyle | HtmlTextWriterStyle.FontWeight | HtmlTextWriterStyle.Height | HtmlTextWriterStyle.TextDecoration | HtmlTextWriterStyle.Width | HtmlTextWriterStyle.ListStyleImage | HtmlTextWriterStyle.ListStyleType | HtmlTextWriterStyle.Cursor | HtmlTextWriterStyle.Direction | HtmlTextWriterStyle.Display | HtmlTextWriterStyle.Filter | HtmlTextWriterStyle.FontVariant | HtmlTextWriterStyle.Left | HtmlTextWriterStyle.Margin | HtmlTextWriterStyle.MarginBottom | HtmlTextWriterStyle.MarginLeft | HtmlTextWriterStyle.MarginRight | HtmlTextWriterStyle.MarginTop | HtmlTextWriterStyle.Overflow | HtmlTextWriterStyle.OverflowX | HtmlTextWriterStyle.OverflowY | HtmlTextWriterStyle.Padding | HtmlTextWriterStyle.PaddingBottom | HtmlTextWriterStyle.PaddingLeft | HtmlTextWriterStyle.PaddingRight | HtmlTextWriterStyle.PaddingTop | HtmlTextWriterStyle.Position | HtmlTextWriterStyle.TextAlign | HtmlTextWriterStyle.VerticalAlign | HtmlTextWriterStyle.TextOverflow | HtmlTextWriterStyle.Top | HtmlTextWriterStyle.Visibility | HtmlTextWriterStyle.WhiteSpace | HtmlTextWriterStyle.ZIndex))
{
flag = CssTextWriter.attrNameLookupArray[(int)key].isUrl;
}
if (flag)
{
CssTextWriter.WriteUrlAttribute(writer, value);
}
else
{
writer.Write(value);
}
writer.Write(';');
}
internal static void WriteAttributes(TextWriter writer, RenderStyle[] styles, int count)
{
for (int i = 0; i < count; i++)
{
RenderStyle renderStyle = styles[i];
CssTextWriter.WriteAttribute(writer, renderStyle.key, renderStyle.name, renderStyle.@value);
}
}
public void WriteBeginCssRule(string selector)
{
this._writer.Write(selector);
this._writer.Write(" { ");
}
public void WriteEndCssRule()
{
this._writer.WriteLine(" }");
}
public override void WriteLine(string s)
{
this._writer.WriteLine(s);
}
public override void WriteLine()
{
this._writer.WriteLine();
}
public override void WriteLine(bool value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(char value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(char[] buffer)
{
this._writer.WriteLine(buffer);
}
public override void WriteLine(char[] buffer, int index, int count)
{
this._writer.WriteLine(buffer, index, count);
}
public override void WriteLine(double value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(float value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(int value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(long value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(object value)
{
this._writer.WriteLine(value);
}
public override void WriteLine(string format, object arg0)
{
this._writer.WriteLine(format, arg0);
}
public override void WriteLine(string format, object arg0, object arg1)
{
this._writer.WriteLine(format, arg0, arg1);
}
public override void WriteLine(string format, params object[] arg)
{
this._writer.WriteLine(format, arg);
}
public override void WriteLine(uint value)
{
this._writer.WriteLine(value);
}
internal static void WriteUrlAttribute(TextWriter writer, string url)
{
string str = url;
char[] chrArray = new char[] { '\'', '\"' };
char? nullable = null;
if (url.StartsWith("url("))
{
int num = 4;
int length = url.Length - 4;
if (url.EndsWith(")"))
{
length--;
}
str = url.Substring(num, length).Trim();
}
char[] chrArray1 = chrArray;
int num1 = 0;
while (num1 < (int)chrArray1.Length)
{
char chr = chrArray1[num1];
if (!str.StartsWith(chr.ToString()) || !str.EndsWith(chr.ToString()))
{
num1++;
}
else
{
str = str.Trim(new char[] { chr });
nullable = new char?(chr);
break;
}
}
writer.Write("url(");
if (nullable.HasValue)
{
writer.Write(nullable);
}
writer.Write(WebUtility.UrlEncode(str));
if (nullable.HasValue)
{
writer.Write(nullable);
}
writer.Write(")");
}
private struct AttributeInformation
{
public string name;
public bool isUrl;
public bool encode;
public AttributeInformation(string name, bool encode, bool isUrl)
{
this.name = name;
this.encode = encode;
this.isUrl = isUrl;
}
}
}
}