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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Helpers.Resources;
namespace System.Web.Helpers
{
internal class HtmlObjectPrinter : ObjectVisitor
{
private const string Styles =
@"<style type=""text/css"">
.objectinfo { font-size: 13px; }
.objectinfo .type { color: #0000ff; }
.objectinfo .complexType { color: #2b91af; }
.objectinfo .name { color: Black; }
.objectinfo .value { color: Black; }
.objectinfo .quote { color: Brown; }
.objectinfo .null { color: Red; }
.objectinfo .exception { color:Red; }
.objectinfo .typeContainer { border-left: solid 2px #7C888A; padding-left: 3px; margin-left:3px; }
.objectinfo h3, h2 { margin:0; padding:0; }
.objectinfo ul { margin-top:0; margin-bottom:0; list-style-type:none; padding-left:10px; margin-left:10px; }
</style>
";
private static readonly HtmlElement _nullSpan = HtmlElement.CreateSpan("(null)", "null");
// List of chars to escape within strings
private static readonly Dictionary<char, string> _printableEscapeChars = new Dictionary<char, string>
{
{ '\0', "\\0" },
{ '\\', "\\\\" },
{ '\'', "'" },
{ '\"', "\\\"" },
{ '\a', "\\a" },
{ '\b', "\\b" },
{ '\f', "\\f" },
{ '\n', "\\n" },
{ '\r', "\\r" },
{ '\t', "\\t" },
{ '\v', "\\v" },
};
// We want to exclude the type name next to the value for members
private bool _excludeTypeName;
private Stack<HtmlElement> _elementStack = new Stack<HtmlElement>();
public HtmlObjectPrinter(int recursionLimit, int enumerationLimit)
: base(recursionLimit, enumerationLimit)
{
}
private HtmlElement Current
{
get
{
Debug.Assert(_elementStack.Count > 0);
return _elementStack.Peek();
}
}
public void WriteTo(object value, TextWriter writer)
{
HtmlElement rootElement = new HtmlElement("div");
rootElement.AddCssClass("objectinfo");
PushElement(rootElement);
Visit(value, 0);
PopElement();
Debug.Assert(_elementStack.Count == 0, "Stack should be empty");
// REVIEW: We should only do this once per page/request
writer.Write(Styles);
rootElement.WriteTo(writer);
}
public override void VisitKeyValues(object value, IEnumerable<object> keys, Func<object, object> valueSelector, int depth)
{
string id = GetObjectId(value);
HtmlElement ul = new HtmlElement("ul");
ul.AddCssClass("typeEnumeration");
ul["id"] = id;
PushElement(ul);
base.VisitKeyValues(value, keys, valueSelector, depth);
PopElement();
Current.AppendChild(ul);
}
public override void VisitKeyValue(object key, object value, int depth)
{
HtmlElement keyElement = new HtmlElement("span");
PushElement(keyElement);
Visit(key, depth);
PopElement();
HtmlElement valueElement = new HtmlElement("span");
PushElement(valueElement);
Visit(value, depth);
PopElement();
// Append the elements to the li
HtmlElement li = new HtmlElement("li");
li.AppendChild(keyElement);
li.AppendChild(" = ");
li.AppendChild(valueElement);
Current.AppendChild(li);
}
public override void VisitEnumerable(IEnumerable enumerable, int depth)
{
string id = GetObjectId(enumerable);
HtmlElement ul = new HtmlElement("ul");
ul.AddCssClass("typeEnumeration");
ul["id"] = id;
PushElement(ul);
base.VisitEnumerable(enumerable, depth);
PopElement();
Current.AppendChild(ul);
}
public override void VisitIndexedEnumeratedValue(int index, object item, int depth)
{
HtmlElement li = new HtmlElement("li");
li.AppendChild(String.Format(CultureInfo.InvariantCulture, "[{0}] = ", index));
PushElement(li);
base.VisitIndexedEnumeratedValue(index, item, depth);
PopElement();
Current.AppendChild(li);
}
public override void VisitEnumeratedValue(object item, int depth)
{
HtmlElement li = new HtmlElement("li");
PushElement(li);
base.VisitEnumeratedValue(item, depth);
PopElement();
Current.AppendChild(li);
}
public override void VisitEnumeratonLimitExceeded()
{
HtmlElement li = new HtmlElement("li");
li.AppendChild("...");
Current.AppendChild(li);
}
public override void VisitMembers(IEnumerable<string> names, Func<string, Type> typeSelector, Func<string, object> valueSelector, int depth)
{
HtmlElement ul = new HtmlElement("ul");
ul.AddCssClass("typeProperties");
PushElement(ul);
base.VisitMembers(names, typeSelector, valueSelector, depth);
PopElement();
Current.AppendChild(ul);
}
public override void VisitMember(string name, Type type, object value, int depth)
{
HtmlElement li = new HtmlElement("li");
if (type != null)
{
li.AppendChild(CreateTypeNameSpan(type));
li.AppendChild(" ");
}
li.AppendChild(CreateNameSpan(name));
li.AppendChild(" = ");
PushElement(li);
_excludeTypeName = true;
base.VisitMember(name, type, value, depth);
_excludeTypeName = false;
PopElement();
Current.AppendChild(li);
}
public override void VisitComplexObject(object value, int depth)
{
string id = GetObjectId(value);
HtmlElement objectElement = new HtmlElement("div");
objectElement.AddCssClass("typeContainer");
objectElement["id"] = id;
PushElement(objectElement);
base.VisitComplexObject(value, depth);
PopElement();
if (objectElement.Children.Any())
{
Current.AppendChild(objectElement);
}
}
public override void VisitNull()
{
Current.AppendChild(_nullSpan);
}
public override void VisitStringValue(string stringValue)
{
// Convert the string escape sequences
stringValue = "\"" + ConvertEscapseSequences(stringValue) + "\"";
Current.AppendChild(CreateQuotedSpan(stringValue));
}
public override void VisitVisitedObject(string id, object value)
{
Current.AppendChild(CreateVisitedLink(id));
}
public override void Visit(object value, int depth)
{
if (value != null)
{
if (!_excludeTypeName)
{
Current.AppendChild(CreateTypeNameSpan(value.GetType()));
Current.AppendChild(" ");
}
_excludeTypeName = false;
}
base.Visit(value, depth);
}
public override void VisitObjectVisitorException(ObjectVisitorException exception)
{
Current.AppendChild(CreateExceptionSpan(exception));
}
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Making the value lowercase has nothing to do with normalization. It's used to show true or false instead of the Title case version")]
public override void VisitConvertedValue(object value, string convertedValue)
{
Type type = value.GetType();
if (type.Equals(typeof(bool)))
{
// Convert True or False to lowercase
convertedValue = convertedValue.ToLowerInvariant();
Current.AppendChild(CreateTypeSpan(convertedValue));
return;
}
if (type.Equals(typeof(char)))
{
string charValue = GetCharValue((char)value);
Current.AppendChild(CreateQuotedSpan("'" + charValue + "'"));
return;
}
// See if the value is a Type itself
Type valueAsType = value as Type;
if (valueAsType != null)
{
// For types we're going to generate elements that print typeof(TypeName)
Current.AppendChild(CreateParentSpan(CreateTypeSpan("typeof"),
CreateOperatorSpan("("),
CreateTypeNameSpan(valueAsType),
CreateOperatorSpan(")")));
}
else
{
Current.AppendChild(CreateValueSpan(convertedValue));
}
}
private static HtmlElement CreateParentSpan(params HtmlElement[] elements)
{
HtmlElement span = new HtmlElement("span");
foreach (var e in elements)
{
span.AppendChild(e);
}
return span;
}
private static HtmlElement CreateNameSpan(string name)
{
return HtmlElement.CreateSpan(name, "name");
}
private static HtmlElement CreateOperatorSpan(string @operator)
{
return HtmlElement.CreateSpan(@operator, "operator");
}
private static HtmlElement CreateValueSpan(string value)
{
return HtmlElement.CreateSpan(value, "value");
}
private static HtmlElement CreateExceptionSpan(ObjectVisitorException exception)
{
HtmlElement span = new HtmlElement("span");
span.AppendChild(HelpersResources.ObjectInfo_PropertyThrewException);
span.AppendChild(HtmlElement.CreateSpan(exception.InnerException.Message, "exception"));
return span;
}
private static HtmlElement CreateQuotedSpan(string value)
{
return HtmlElement.CreateSpan(value, "quote");
}
private static HtmlElement CreateLink(string href, string linkText, string cssClass = null)
{
HtmlElement a = new HtmlElement("a");
a.SetInnerText(linkText);
a["href"] = href;
if (!String.IsNullOrEmpty(cssClass))
{
a.AddCssClass(cssClass);
}
return a;
}
private static HtmlElement CreateVisitedLink(string id)
{
string text = String.Format(CultureInfo.InvariantCulture, "[{0}]", HelpersResources.ObjectInfo_PreviousDisplayed);
return CreateLink("#" + id, text);
}
private static HtmlElement CreateTypeSpan(string value)
{
return HtmlElement.CreateSpan(value, "type");
}
private static HtmlElement CreateTypeNameSpan(Type type)
{
string typeName = GetTypeName(type);
HtmlElement span = new HtmlElement("span");
StringBuilder sb = new StringBuilder();
// Convert the type name into html elements with different css classes
foreach (var ch in typeName)
{
if (IsOperator(ch))
{
if (sb.Length > 0)
{
span.AppendChild(CreateTypeSpan(sb.ToString()));
sb.Clear();
}
span.AppendChild(CreateOperatorSpan(ch.ToString()));
}
else
{
sb.Append(ch);
}
}
if (sb.Length > 0)
{
span.AppendChild(CreateTypeSpan(sb.ToString()));
}
return span;
}
private static bool IsOperator(char ch)
{
// These are the operators we expect to see within type names
return ch == '[' || ch == ']' || ch == '<' || ch == '>' || ch == '&' || ch == '*';
}
internal void PushElement(HtmlElement element)
{
_elementStack.Push(element);
}
internal HtmlElement PopElement()
{
Debug.Assert(_elementStack.Count > 0);
return _elementStack.Pop();
}
internal static string ConvertEscapseSequences(string value)
{
StringBuilder sb = new StringBuilder();
foreach (var ch in value)
{
sb.Append(GetCharValue(ch));
}
return sb.ToString();
}
private static string GetCharValue(char ch)
{
string value;
if (_printableEscapeChars.TryGetValue(ch, out value))
{
return value;
}
// REVIEW: Perf?
return ch.ToString();
}
}
}
|