This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ViewBase.cs
195 lines (172 loc) · 6.82 KB
/
ViewBase.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
using System;
using PortableRazor.Web;
using PortableRazor.Web.Mvc;
namespace PortableRazor
{
//This was copied from the generated .cs for one of the razor views
public abstract class ViewBase
{
private static string scheme;
public static string UrlScheme {
get { return scheme ?? "hybrid:"; }
set { scheme = value; }
}
public HtmlHelper Html { get; private set; }
public UrlHelper Url { get; private set; }
// This field is OPTIONAL, but used by the default implementation of Generate, Write, WriteAttribute and WriteLiteral
//
System.IO.TextWriter __razor_writer;
// This method is OPTIONAL
//
/// <summary>Executes the template and returns the output as a string.</summary>
/// <returns>The template output.</returns>
public string GenerateString ()
{
using (var sw = new System.IO.StringWriter ()) {
Generate (sw);
return sw.ToString ();
}
}
// This method is OPTIONAL, you may choose to implement Write and WriteLiteral without use of __razor_writer
// and provide another means of invoking Execute.
//
/// <summary>Executes the template, writing to the provided text writer.</summary>
/// <param name=""writer"">The TextWriter to which to write the template output.</param>
public void Generate (System.IO.TextWriter writer)
{
Html = new HtmlHelper (writer);
Url = new UrlHelper ();
__razor_writer = writer;
Execute ();
__razor_writer = null;
}
// This method is REQUIRED, but you may choose to implement it differently
//
/// <summary>Writes a literal value to the template output without HTML escaping it.</summary>
/// <param name=""value"">The literal value.</param>
protected void WriteLiteral (string value)
{
__razor_writer.Write (value);
}
// This method is REQUIRED if the template contains any Razor helpers, but you may choose to implement it differently
//
/// <summary>Writes a literal value to the TextWriter without HTML escaping it.</summary>
/// <param name=""writer"">The TextWriter to which to write the literal.</param>
/// <param name=""value"">The literal value.</param>
protected static void WriteLiteralTo (System.IO.TextWriter writer, string value)
{
writer.Write (value);
}
// This method is REQUIRED, but you may choose to implement it differently
//
/// <summary>Writes a value to the template output, HTML escaping it if necessary.</summary>
/// <param name=""value"">The value.</param>
/// <remarks>The value may be a Action<System.IO.TextWriter>, as returned by Razor helpers.</remarks>
protected void Write (object value)
{
WriteTo (__razor_writer, value);
}
// This method is REQUIRED if the template contains any Razor helpers, but you may choose to implement it differently
//
/// <summary>Writes an object value to the TextWriter, HTML escaping it if necessary.</summary>
/// <param name=""writer"">The TextWriter to which to write the value.</param>
/// <param name=""value"">The value.</param>
/// <remarks>The value may be a Action<System.IO.TextWriter>, as returned by Razor helpers.</remarks>
protected static void WriteTo (System.IO.TextWriter writer, object value)
{
if (value == null)
return;
var write = value as Action<System.IO.TextWriter>;
if (write != null) {
write (writer);
return;
}
if (value is IHtmlString)
writer.Write (value);
else
writer.Write(System.Net.WebUtility.HtmlEncode (value.ToString ()));
}
// This method is REQUIRED, but you may choose to implement it differently
//
/// <summary>
/// Conditionally writes an attribute to the template output.
/// </summary>
/// <param name=""name"">The name of the attribute.</param>
/// <param name=""prefix"">The prefix of the attribute.</param>
/// <param name=""suffix"">The suffix of the attribute.</param>
/// <param name=""values"">Attribute values, each specifying a prefix, value and whether it's a literal.</param>
protected void WriteAttribute (string name, string prefix, string suffix, params Tuple<string,object,bool>[] values)
{
WriteAttributeTo (__razor_writer, name, prefix, suffix, values);
}
// This method is REQUIRED if the template contains any Razor helpers, but you may choose to implement it differently
//
/// <summary>
/// Conditionally writes an attribute to a TextWriter.
/// </summary>
/// <param name=""writer"">The TextWriter to which to write the attribute.</param>
/// <param name=""name"">The name of the attribute.</param>
/// <param name=""prefix"">The prefix of the attribute.</param>
/// <param name=""suffix"">The suffix of the attribute.</param>
/// <param name=""values"">Attribute values, each specifying a prefix, value and whether it's a literal.</param>
///<remarks>Used by Razor helpers to write attributes.</remarks>
protected static void WriteAttributeTo (System.IO.TextWriter writer, string name, string prefix, string suffix, params Tuple<string,object,bool>[] values)
{
// this is based on System.Web.WebPages.WebPageExecutingBase
// Copyright (c) Microsoft Open Technologies, Inc.
// Licensed under the Apache License, Version 2.0
if (values.Length == 0) {
// Explicitly empty attribute, so write the prefix and suffix
writer.Write (prefix);
writer.Write (suffix);
return;
}
bool first = true;
bool wroteSomething = false;
for (int i = 0; i < values.Length; i++) {
Tuple<string,object,bool> attrVal = values [i];
string attPrefix = attrVal.Item1;
object value = attrVal.Item2;
bool isLiteral = attrVal.Item3;
if (value == null) {
// Nothing to write
continue;
}
// The special cases here are that the value we're writing might already be a string, or that the
// value might be a bool. If the value is the bool 'true' we want to write the attribute name instead
// of the string 'true'. If the value is the bool 'false' we don't want to write anything.
//
// Otherwise the value is another object (perhaps an IHtmlString), and we'll ask it to format itself.
string stringValue;
bool? boolValue = value as bool?;
if (boolValue == true) {
stringValue = name;
} else if (boolValue == false) {
continue;
} else {
stringValue = value as string;
}
if (first) {
writer.Write (prefix);
first = false;
} else {
writer.Write (attPrefix);
}
if (isLiteral) {
writer.Write (stringValue ?? value);
} else {
WriteTo (writer, stringValue ?? value);
}
wroteSomething = true;
}
if (wroteSomething) {
writer.Write (suffix);
}
}
// This method is REQUIRED. The generated Razor subclass will override it with the generated code.
//
///<summary>Executes the template, writing output to the Write and WriteLiteral methods.</summary>.
///<remarks>Not intended to be called directly. Call the Generate method instead.</remarks>
public abstract void Execute ();
}
}