Skip to content

Instantly share code, notes, and snippets.

@analogrelay
Created November 8, 2012 02:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save analogrelay/4036121 to your computer and use it in GitHub Desktop.
Save analogrelay/4036121 to your computer and use it in GitHub Desktop.
Patched WebViewPage base class
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;
// This shouldn't be needed any more
//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, val.Value);
}
else
{
// Patch: Don't use valStr, use val.Value
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);
}
}
}
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;
// This shouldn't be needed any more
//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, val.Value);
}
else
{
// Patch: Don't use valStr, use val.Value
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);
}
}
}
<!-- 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>
<!-- 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