Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.CodeDom;
using System.Web.Razor;
using System.Web.Razor.Generator;
namespace System.Web.Mvc.Razor
{
internal class MvcCSharpRazorCodeGenerator : CSharpRazorCodeGenerator
{
private const string DefaultModelTypeName = "dynamic";
public MvcCSharpRazorCodeGenerator(string className, string rootNamespaceName, string sourceFileName, RazorEngineHost host)
: base(className, rootNamespaceName, sourceFileName, host)
{
var mvcHost = host as MvcWebPageRazorHost;
if (mvcHost != null && !mvcHost.IsSpecialPage)
{
// set the default model type to "dynamic" (Dev10 bug 935656)
// don't set it for "special" pages (such as "_viewStart.cshtml")
SetBaseType(DefaultModelTypeName);
}
}
private void SetBaseType(string modelTypeName)
{
var baseType = new CodeTypeReference(Context.Host.DefaultBaseClass + "<" + modelTypeName + ">");
Context.GeneratedClass.BaseTypes.Clear();
Context.GeneratedClass.BaseTypes.Add(baseType);
}
}
}

View File

@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Web.Mvc.Properties;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser;
using System.Web.Razor.Text;
namespace System.Web.Mvc.Razor
{
public class MvcCSharpRazorCodeParser : CSharpCodeParser
{
private const string ModelKeyword = "model";
private const string GenericTypeFormatString = "{0}<{1}>";
private SourceLocation? _endInheritsLocation;
private bool _modelStatementFound;
public MvcCSharpRazorCodeParser()
{
MapDirectives(ModelDirective, ModelKeyword);
}
protected override void InheritsDirective()
{
// Verify we're on the right keyword and accept
AssertDirective(SyntaxConstants.CSharp.InheritsKeyword);
AcceptAndMoveNext();
_endInheritsLocation = CurrentLocation;
InheritsDirectiveCore();
CheckForInheritsAndModelStatements();
}
private void CheckForInheritsAndModelStatements()
{
if (_modelStatementFound && _endInheritsLocation.HasValue)
{
Context.OnError(_endInheritsLocation.Value, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_CannotHaveModelAndInheritsKeyword, ModelKeyword));
}
}
protected virtual void ModelDirective()
{
// Verify we're on the right keyword and accept
AssertDirective(ModelKeyword);
AcceptAndMoveNext();
SourceLocation endModelLocation = CurrentLocation;
BaseTypeDirective(
String.Format(CultureInfo.CurrentCulture,
MvcResources.MvcRazorCodeParser_ModelKeywordMustBeFollowedByTypeName, ModelKeyword),
CreateModelCodeGenerator);
if (_modelStatementFound)
{
Context.OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_OnlyOneModelStatementIsAllowed, ModelKeyword));
}
_modelStatementFound = true;
CheckForInheritsAndModelStatements();
}
private SpanCodeGenerator CreateModelCodeGenerator(string model)
{
return new SetModelTypeCodeGenerator(model, GenericTypeFormatString);
}
}
}

View File

@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Linq;
using System.Web.Mvc.Properties;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer.Symbols;
namespace System.Web.Mvc.Razor
{
public class MvcVBRazorCodeParser : VBCodeParser
{
internal const string ModelTypeKeyword = "ModelType";
private const string GenericTypeFormatString = "{0}(Of {1})";
private SourceLocation? _endInheritsLocation;
private bool _modelStatementFound;
public MvcVBRazorCodeParser()
{
MapDirective(ModelTypeKeyword, ModelTypeDirective);
}
protected override bool InheritsStatement()
{
// Verify we're on the right keyword and accept
Assert(VBKeyword.Inherits);
VBSymbol inherits = CurrentSymbol;
NextToken();
_endInheritsLocation = CurrentLocation;
PutCurrentBack();
PutBack(inherits);
EnsureCurrent();
bool result = base.InheritsStatement();
CheckForInheritsAndModelStatements();
return result;
}
private void CheckForInheritsAndModelStatements()
{
if (_modelStatementFound && _endInheritsLocation.HasValue)
{
Context.OnError(_endInheritsLocation.Value, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_CannotHaveModelAndInheritsKeyword, ModelTypeKeyword));
}
}
protected virtual bool ModelTypeDirective()
{
AssertDirective(ModelTypeKeyword);
Span.CodeGenerator = SpanCodeGenerator.Null;
Context.CurrentBlock.Type = BlockType.Directive;
AcceptAndMoveNext();
SourceLocation endModelLocation = CurrentLocation;
if (At(VBSymbolType.WhiteSpace))
{
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
}
AcceptWhile(VBSymbolType.WhiteSpace);
Output(SpanKind.MetaCode);
if (_modelStatementFound)
{
Context.OnError(endModelLocation, String.Format(CultureInfo.CurrentCulture, MvcResources.MvcRazorCodeParser_OnlyOneModelStatementIsAllowed, ModelTypeKeyword));
}
_modelStatementFound = true;
if (EndOfFile || At(VBSymbolType.WhiteSpace) || At(VBSymbolType.NewLine))
{
Context.OnError(endModelLocation, MvcResources.MvcRazorCodeParser_ModelKeywordMustBeFollowedByTypeName, ModelTypeKeyword);
}
// Just accept to a newline
AcceptUntil(VBSymbolType.NewLine);
if (!Context.DesignTimeMode)
{
// We want the newline to be treated as code, but it causes issues at design-time.
Optional(VBSymbolType.NewLine);
}
string baseType = String.Concat(Span.Symbols.Select(s => s.Content)).Trim();
Span.CodeGenerator = new SetModelTypeCodeGenerator(baseType, GenericTypeFormatString);
CheckForInheritsAndModelStatements();
Output(SpanKind.Code);
return false;
}
}
}

View File

@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser;
using System.Web.WebPages.Razor;
namespace System.Web.Mvc.Razor
{
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "WebPage", Justification = "The class name is derived from the name of the base type")]
public class MvcWebPageRazorHost : WebPageRazorHost
{
[SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification = "The NamespaceImports property should not be virtual. This is a temporary fix.")]
public MvcWebPageRazorHost(string virtualPath, string physicalPath)
: base(virtualPath, physicalPath)
{
RegisterSpecialFile(RazorViewEngine.ViewStartFileName, typeof(ViewStartPage));
DefaultPageBaseClass = typeof(WebViewPage).FullName;
// REVIEW get rid of the namespace import to not force additional references in default MVC projects
GetRidOfNamespace("System.Web.WebPages.Html");
}
public override RazorCodeGenerator DecorateCodeGenerator(RazorCodeGenerator incomingCodeGenerator)
{
if (incomingCodeGenerator is CSharpRazorCodeGenerator)
{
return new MvcCSharpRazorCodeGenerator(incomingCodeGenerator.ClassName,
incomingCodeGenerator.RootNamespaceName,
incomingCodeGenerator.SourceFileName,
incomingCodeGenerator.Host);
}
else
{
return base.DecorateCodeGenerator(incomingCodeGenerator);
}
}
public override ParserBase DecorateCodeParser(ParserBase incomingCodeParser)
{
if (incomingCodeParser is CSharpCodeParser)
{
return new MvcCSharpRazorCodeParser();
}
else if (incomingCodeParser is VBCodeParser)
{
return new MvcVBRazorCodeParser();
}
else
{
return base.DecorateCodeParser(incomingCodeParser);
}
}
private void GetRidOfNamespace(string ns)
{
Debug.Assert(NamespaceImports.Contains(ns), ns + " is not a default namespace anymore");
if (NamespaceImports.Contains(ns))
{
NamespaceImports.Remove(ns);
}
}
}
}

View File

@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Web.Mvc.ExpressionUtil;
using System.Web.Razor.Generator;
namespace System.Web.Mvc.Razor
{
internal class SetModelTypeCodeGenerator : SetBaseTypeCodeGenerator
{
private string _genericTypeFormat;
public SetModelTypeCodeGenerator(string modelType, string genericTypeFormat)
: base(modelType)
{
_genericTypeFormat = genericTypeFormat;
}
protected override string ResolveType(CodeGeneratorContext context, string baseType)
{
return String.Format(
CultureInfo.InvariantCulture,
_genericTypeFormat,
context.Host.DefaultBaseClass,
baseType);
}
public override bool Equals(object obj)
{
SetModelTypeCodeGenerator other = obj as SetModelTypeCodeGenerator;
return other != null &&
base.Equals(obj) &&
String.Equals(_genericTypeFormat, other._genericTypeFormat, StringComparison.Ordinal);
}
public override int GetHashCode()
{
var combiner = new HashCodeCombiner();
combiner.AddInt32(base.GetHashCode());
combiner.AddObject(_genericTypeFormat);
return combiner.CombinedHash;
}
public override string ToString()
{
return "Model:" + BaseType;
}
}
}

View File

@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Web.WebPages;
namespace System.Web.Mvc.Razor
{
internal delegate WebPageRenderingBase StartPageLookupDelegate(WebPageRenderingBase page, string fileName, IEnumerable<string> supportedExtensions);
}