Created
November 8, 2012 02:09
-
-
Save analogrelay/4036111 to your computer and use it in GitHub Desktop.
Patched WebViewPage base class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.WebPages; | |
using System.Web.WebPages.Instrumentation; | |
namespace MvcApplication1.Patches | |
{ | |
public abstract class PatchedWebViewPage : WebViewPage | |
{ | |
protected override void WriteAttributeTo(string pageVirtualPath, TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values) | |
{ | |
bool first = true; | |
bool wroteSomething = false; | |
if (values.Length == 0) | |
{ | |
// Explicitly empty attribute, so write the prefix and suffix | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
else | |
{ | |
for (int i = 0; i < values.Length; i++) | |
{ | |
AttributeValue attrVal = values[i]; | |
PositionTagged<object> val = attrVal.Value; | |
PositionTagged<string> next = i == values.Length - 1 ? | |
suffix : // End of the list, grab the suffix | |
values[i + 1].Prefix; // Still in the list, grab the next prefix | |
bool? boolVal = null; | |
if (val.Value is bool) | |
{ | |
boolVal = (bool)val.Value; | |
} | |
if (val.Value != null && (boolVal == null || boolVal.Value)) | |
{ | |
string valStr = val.Value as string; | |
if (valStr == null) | |
{ | |
valStr = val.Value.ToString(); | |
} | |
if (boolVal != null) | |
{ | |
valStr = name; | |
} | |
if (first) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
first = false; | |
} | |
else | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, attrVal.Prefix); | |
} | |
// Calculate length of the source span by the position of the next value (or suffix) | |
int sourceLength = next.Position - attrVal.Value.Position; | |
BeginContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
if (attrVal.Literal) | |
{ | |
WriteLiteralTo(writer, valStr); | |
} | |
else | |
{ | |
WriteTo(writer, val.Value); // Write value | |
} | |
EndContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
wroteSomething = true; | |
} | |
} | |
if (wroteSomething) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
} | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, string value, int position) | |
{ | |
BeginContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
WriteLiteralTo(writer, value); | |
EndContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, PositionTagged<string> value) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, value.Value, value.Position); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.WebPages; | |
using System.Web.WebPages.Instrumentation; | |
namespace MvcApplication1.Patches | |
{ | |
public abstract class PatchedWebViewPage<T> : WebViewPage<T> | |
{ | |
protected override void WriteAttributeTo(string pageVirtualPath, TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values) | |
{ | |
bool first = true; | |
bool wroteSomething = false; | |
if (values.Length == 0) | |
{ | |
// Explicitly empty attribute, so write the prefix and suffix | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
else | |
{ | |
for (int i = 0; i < values.Length; i++) | |
{ | |
AttributeValue attrVal = values[i]; | |
PositionTagged<object> val = attrVal.Value; | |
PositionTagged<string> next = i == values.Length - 1 ? | |
suffix : // End of the list, grab the suffix | |
values[i + 1].Prefix; // Still in the list, grab the next prefix | |
bool? boolVal = null; | |
if (val.Value is bool) | |
{ | |
boolVal = (bool)val.Value; | |
} | |
if (val.Value != null && (boolVal == null || boolVal.Value)) | |
{ | |
string valStr = val.Value as string; | |
if (valStr == null) | |
{ | |
valStr = val.Value.ToString(); | |
} | |
if (boolVal != null) | |
{ | |
valStr = name; | |
} | |
if (first) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
first = false; | |
} | |
else | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, attrVal.Prefix); | |
} | |
// Calculate length of the source span by the position of the next value (or suffix) | |
int sourceLength = next.Position - attrVal.Value.Position; | |
BeginContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
if (attrVal.Literal) | |
{ | |
WriteLiteralTo(writer, valStr); | |
} | |
else | |
{ | |
WriteTo(writer, val.Value); // Write value | |
} | |
EndContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
wroteSomething = true; | |
} | |
} | |
if (wroteSomething) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
} | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, string value, int position) | |
{ | |
BeginContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
WriteLiteralTo(writer, value); | |
EndContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, PositionTagged<string> value) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, value.Value, value.Position); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.WebPages; | |
using System.Web.WebPages.Instrumentation; | |
namespace MvcApplication1.Patches | |
{ | |
public abstract class PatchedWebViewPage<T> : WebViewPage<T> | |
{ | |
protected override void WriteAttributeTo(string pageVirtualPath, TextWriter writer, string name, PositionTagged<string> prefix, PositionTagged<string> suffix, params AttributeValue[] values) | |
{ | |
bool first = true; | |
bool wroteSomething = false; | |
if (values.Length == 0) | |
{ | |
// Explicitly empty attribute, so write the prefix and suffix | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
else | |
{ | |
for (int i = 0; i < values.Length; i++) | |
{ | |
AttributeValue attrVal = values[i]; | |
PositionTagged<object> val = attrVal.Value; | |
PositionTagged<string> next = i == values.Length - 1 ? | |
suffix : // End of the list, grab the suffix | |
values[i + 1].Prefix; // Still in the list, grab the next prefix | |
bool? boolVal = null; | |
if (val.Value is bool) | |
{ | |
boolVal = (bool)val.Value; | |
} | |
if (val.Value != null && (boolVal == null || boolVal.Value)) | |
{ | |
string valStr = val.Value as string; | |
if (valStr == null) | |
{ | |
valStr = val.Value.ToString(); | |
} | |
if (boolVal != null) | |
{ | |
valStr = name; | |
} | |
if (first) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, prefix); | |
first = false; | |
} | |
else | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, attrVal.Prefix); | |
} | |
// Calculate length of the source span by the position of the next value (or suffix) | |
int sourceLength = next.Position - attrVal.Value.Position; | |
BeginContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
if (attrVal.Literal) | |
{ | |
WriteLiteralTo(writer, valStr); | |
} | |
else | |
{ | |
WriteTo(writer, val.Value); // Write value | |
} | |
EndContext(writer, pageVirtualPath, attrVal.Value.Position, sourceLength, isLiteral: attrVal.Literal); | |
wroteSomething = true; | |
} | |
} | |
if (wroteSomething) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, suffix); | |
} | |
} | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, string value, int position) | |
{ | |
BeginContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
WriteLiteralTo(writer, value); | |
EndContext(writer, pageVirtualPath, position, value.Length, isLiteral: true); | |
} | |
private void WritePositionTaggedLiteral(TextWriter writer, string pageVirtualPath, PositionTagged<string> value) | |
{ | |
WritePositionTaggedLiteral(writer, pageVirtualPath, value.Value, value.Position); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- All need to change is the pageBaseType on the <pages> element. Change it to point at your patched page base class. MVC will automatically pick the generic version if @model is used. --> | |
<configuration> | |
<system.web.webPages.razor> | |
<pages pageBaseType="MvcApplication1.Patches.PatchedWebViewPage"> | |
</pages> | |
</system.web.webPages.razor> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- !!!IMPORTANT!!! This goes in Views\web.config --> | |
<!-- All need to change is the pageBaseType on the <pages> element. Change it to point at your patched page base class. MVC will automatically pick the generic version if @model is used. --> | |
<configuration> | |
<system.web.webPages.razor> | |
<pages pageBaseType="MvcApplication1.Patches.PatchedWebViewPage"> | |
</pages> | |
</system.web.webPages.razor> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment