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,78 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Web.WebPages.Resources;
namespace System.Web.Mvc
{
[Serializable]
[TypeForwardedFrom("System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public sealed class HttpAntiForgeryException : HttpException
{
public HttpAntiForgeryException()
{
}
private HttpAntiForgeryException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public HttpAntiForgeryException(string message)
: base(message)
{
}
private HttpAntiForgeryException(string message, params object[] args)
: this(String.Format(CultureInfo.CurrentCulture, message, args))
{
}
public HttpAntiForgeryException(string message, Exception innerException)
: base(message, innerException)
{
}
internal static HttpAntiForgeryException CreateAdditionalDataCheckFailedException()
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_AdditionalDataCheckFailed);
}
internal static HttpAntiForgeryException CreateClaimUidMismatchException()
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_ClaimUidMismatch);
}
internal static HttpAntiForgeryException CreateCookieMissingException(string cookieName)
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_CookieMissing, cookieName);
}
internal static HttpAntiForgeryException CreateDeserializationFailedException()
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_DeserializationFailed);
}
internal static HttpAntiForgeryException CreateFormFieldMissingException(string formFieldName)
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_FormFieldMissing, formFieldName);
}
internal static HttpAntiForgeryException CreateSecurityTokenMismatchException()
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_SecurityTokenMismatch);
}
internal static HttpAntiForgeryException CreateTokensSwappedException(string cookieName, string formFieldName)
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_TokensSwapped, cookieName, formFieldName);
}
internal static HttpAntiForgeryException CreateUsernameMismatchException(string usernameInToken, string currentUsername)
{
return new HttpAntiForgeryException(WebPageResources.AntiForgeryToken_UsernameMismatch, usernameInToken, currentUsername);
}
}
}

View File

@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationEqualToRule : ModelClientValidationRule
{
public ModelClientValidationEqualToRule(string errorMessage, object other)
{
ErrorMessage = errorMessage;
ValidationType = "equalto";
ValidationParameters["other"] = other;
}
}
}

View File

@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationRangeRule : ModelClientValidationRule
{
public ModelClientValidationRangeRule(string errorMessage, object minValue, object maxValue)
{
ErrorMessage = errorMessage;
ValidationType = "range";
ValidationParameters["min"] = minValue;
ValidationParameters["max"] = maxValue;
}
}
}

View File

@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationRegexRule : ModelClientValidationRule
{
public ModelClientValidationRegexRule(string errorMessage, string pattern)
{
ErrorMessage = errorMessage;
ValidationType = "regex";
ValidationParameters.Add("pattern", pattern);
}
}
}

View File

@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationRemoteRule : ModelClientValidationRule
{
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "The value is a not a regular URL since it may contain ~/ ASP.NET-specific characters")]
public ModelClientValidationRemoteRule(string errorMessage, string url, string httpMethod, string additionalFields)
{
ErrorMessage = errorMessage;
ValidationType = "remote";
ValidationParameters["url"] = url;
if (!String.IsNullOrEmpty(httpMethod))
{
ValidationParameters["type"] = httpMethod;
}
ValidationParameters["additionalfields"] = additionalFields;
}
}
}

View File

@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationRequiredRule : ModelClientValidationRule
{
public ModelClientValidationRequiredRule(string errorMessage)
{
ErrorMessage = errorMessage;
ValidationType = "required";
}
}
}

View File

@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationRule
{
private readonly Dictionary<string, object> _validationParameters = new Dictionary<string, object>();
private string _validationType;
public string ErrorMessage { get; set; }
public IDictionary<string, object> ValidationParameters
{
get { return _validationParameters; }
}
public string ValidationType
{
get { return _validationType ?? String.Empty; }
set { _validationType = value; }
}
}
}

View File

@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ModelClientValidationStringLengthRule : ModelClientValidationRule
{
public ModelClientValidationStringLengthRule(string errorMessage, int minimumLength, int maximumLength)
{
ErrorMessage = errorMessage;
ValidationType = "length";
if (minimumLength != 0)
{
ValidationParameters["min"] = minimumLength;
}
if (maximumLength != Int32.MaxValue)
{
ValidationParameters["max"] = maximumLength;
}
}
}
}

View File

@@ -0,0 +1,261 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.WebPages.Html;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class TagBuilder
{
private string _idAttributeDotReplacement;
private string _innerHtml;
public TagBuilder(string tagName)
{
if (String.IsNullOrEmpty(tagName))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "tagName");
}
TagName = tagName;
Attributes = new SortedDictionary<string, string>(StringComparer.Ordinal);
}
public IDictionary<string, string> Attributes { get; private set; }
public string IdAttributeDotReplacement
{
get
{
if (String.IsNullOrEmpty(_idAttributeDotReplacement))
{
_idAttributeDotReplacement = HtmlHelper.IdAttributeDotReplacement;
}
return _idAttributeDotReplacement;
}
set { _idAttributeDotReplacement = value; }
}
public string InnerHtml
{
get { return _innerHtml ?? String.Empty; }
set { _innerHtml = value; }
}
public string TagName { get; private set; }
public void AddCssClass(string value)
{
string currentValue;
if (Attributes.TryGetValue("class", out currentValue))
{
Attributes["class"] = value + " " + currentValue;
}
else
{
Attributes["class"] = value;
}
}
public static string CreateSanitizedId(string originalId)
{
return CreateSanitizedId(originalId, HtmlHelper.IdAttributeDotReplacement);
}
public static string CreateSanitizedId(string originalId, string invalidCharReplacement)
{
if (String.IsNullOrEmpty(originalId))
{
return null;
}
if (invalidCharReplacement == null)
{
throw new ArgumentNullException("invalidCharReplacement");
}
char firstChar = originalId[0];
if (!Html401IdUtil.IsLetter(firstChar))
{
// the first character must be a letter
return null;
}
StringBuilder sb = new StringBuilder(originalId.Length);
sb.Append(firstChar);
for (int i = 1; i < originalId.Length; i++)
{
char thisChar = originalId[i];
if (Html401IdUtil.IsValidIdCharacter(thisChar))
{
sb.Append(thisChar);
}
else
{
sb.Append(invalidCharReplacement);
}
}
return sb.ToString();
}
public void GenerateId(string name)
{
if (!Attributes.ContainsKey("id"))
{
string sanitizedId = CreateSanitizedId(name, IdAttributeDotReplacement);
if (!String.IsNullOrEmpty(sanitizedId))
{
Attributes["id"] = sanitizedId;
}
}
}
private void AppendAttributes(StringBuilder sb)
{
foreach (var attribute in Attributes)
{
string key = attribute.Key;
if (String.Equals(key, "id", StringComparison.Ordinal /* case-sensitive */) && String.IsNullOrEmpty(attribute.Value))
{
continue; // DevDiv Bugs #227595: don't output empty IDs
}
string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
sb.Append(' ')
.Append(key)
.Append("=\"")
.Append(value)
.Append('"');
}
}
public void MergeAttribute(string key, string value)
{
MergeAttribute(key, value, replaceExisting: false);
}
public void MergeAttribute(string key, string value, bool replaceExisting)
{
if (String.IsNullOrEmpty(key))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "key");
}
if (replaceExisting || !Attributes.ContainsKey(key))
{
Attributes[key] = value;
}
}
public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes)
{
MergeAttributes(attributes, replaceExisting: false);
}
public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes, bool replaceExisting)
{
if (attributes != null)
{
foreach (var entry in attributes)
{
string key = Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
string value = Convert.ToString(entry.Value, CultureInfo.InvariantCulture);
MergeAttribute(key, value, replaceExisting);
}
}
}
public void SetInnerText(string innerText)
{
InnerHtml = HttpUtility.HtmlEncode(innerText);
}
internal HtmlString ToHtmlString(TagRenderMode renderMode)
{
return new HtmlString(ToString(renderMode));
}
public override string ToString()
{
return ToString(TagRenderMode.Normal);
}
public string ToString(TagRenderMode renderMode)
{
StringBuilder sb = new StringBuilder();
switch (renderMode)
{
case TagRenderMode.StartTag:
sb.Append('<')
.Append(TagName);
AppendAttributes(sb);
sb.Append('>');
break;
case TagRenderMode.EndTag:
sb.Append("</")
.Append(TagName)
.Append('>');
break;
case TagRenderMode.SelfClosing:
sb.Append('<')
.Append(TagName);
AppendAttributes(sb);
sb.Append(" />");
break;
default:
sb.Append('<')
.Append(TagName);
AppendAttributes(sb);
sb.Append('>')
.Append(InnerHtml)
.Append("</")
.Append(TagName)
.Append('>');
break;
}
return sb.ToString();
}
// Valid IDs are defined in http://www.w3.org/TR/html401/types.html#type-id
private static class Html401IdUtil
{
private static bool IsAllowableSpecialCharacter(char c)
{
switch (c)
{
case '-':
case '_':
case ':':
// note that we're specifically excluding the '.' character
return true;
default:
return false;
}
}
private static bool IsDigit(char c)
{
return ('0' <= c && c <= '9');
}
public static bool IsLetter(char c)
{
return (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'));
}
public static bool IsValidIdCharacter(char c)
{
return (IsLetter(c) || IsDigit(c) || IsAllowableSpecialCharacter(c));
}
}
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
namespace System.Web.Mvc
{
[TypeForwardedFrom("System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public enum TagRenderMode
{
Normal,
StartTag,
EndTag,
SelfClosing
}
}

View File

@@ -0,0 +1,98 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.WebPages.Resources;
namespace System.Web.Mvc
{
public static class UnobtrusiveValidationAttributesGenerator
{
public static void GetValidationAttributes(IEnumerable<ModelClientValidationRule> clientRules, IDictionary<string, object> results)
{
if (clientRules == null)
{
throw new ArgumentNullException("clientRules");
}
if (results == null)
{
throw new ArgumentNullException("results");
}
bool renderedRules = false;
foreach (ModelClientValidationRule rule in clientRules)
{
renderedRules = true;
string ruleName = "data-val-" + rule.ValidationType;
ValidateUnobtrusiveValidationRule(rule, results, ruleName);
results.Add(ruleName, HttpUtility.HtmlEncode(rule.ErrorMessage ?? String.Empty));
ruleName += "-";
foreach (var kvp in rule.ValidationParameters)
{
results.Add(ruleName + kvp.Key, HttpUtility.HtmlEncode(kvp.Value ?? String.Empty));
}
}
if (renderedRules)
{
results.Add("data-val", "true");
}
}
private static void ValidateUnobtrusiveValidationRule(ModelClientValidationRule rule, IDictionary<string, object> resultsDictionary, string dictionaryKey)
{
if (String.IsNullOrWhiteSpace(rule.ValidationType))
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
WebPageResources.UnobtrusiveJavascript_ValidationTypeCannotBeEmpty,
rule.GetType().FullName));
}
if (resultsDictionary.ContainsKey(dictionaryKey))
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
WebPageResources.UnobtrusiveJavascript_ValidationTypeMustBeUnique,
rule.ValidationType));
}
if (rule.ValidationType.Any(c => !Char.IsLower(c)))
{
throw new InvalidOperationException(
String.Format(CultureInfo.CurrentCulture, WebPageResources.UnobtrusiveJavascript_ValidationTypeMustBeLegal,
rule.ValidationType,
rule.GetType().FullName));
}
foreach (var key in rule.ValidationParameters.Keys)
{
if (String.IsNullOrWhiteSpace(key))
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
WebPageResources.UnobtrusiveJavascript_ValidationParameterCannotBeEmpty,
rule.GetType().FullName));
}
if (!Char.IsLower(key.First()) || key.Any(c => !Char.IsLower(c) && !Char.IsDigit(c)))
{
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
WebPageResources.UnobtrusiveJavascript_ValidationParameterMustBeLegal,
key,
rule.GetType().FullName));
}
}
}
}
}