Add ability for wildcards in @addtaghelper and @removetaghelper.

- @addtaghelper and @removetaghelper can now utilize the '*' wild card to represent 0 or more characters.
- Restricted the @addtaghelper to need the TypeName. @addtaghelper "MyAssemblyName" => @addtaghelper "*, MyAssemblyName".

#285
This commit is contained in:
N. Taylor Mullen 2015-01-29 17:36:33 -08:00
parent 7afd78b36a
commit a78ed663d0
3 changed files with 17 additions and 27 deletions

View File

@ -11,9 +11,7 @@ namespace Microsoft.AspNet.Razor.Runtime
= new ResourceManager("Microsoft.AspNet.Razor.Runtime.Resources", typeof(Resources).GetTypeInfo().Assembly);
/// <summary>
/// Invalid tag helper directive look up text '{0}'. The correct look up text formats are:
/// "assemblyName"
/// "typeName, assemblyName"
/// Invalid tag helper directive look up text '{0}'. The correct look up text format is: "typeName, assemblyName".
/// </summary>
internal static string TagHelperDescriptorResolver_InvalidTagHelperLookupText
{
@ -21,9 +19,7 @@ namespace Microsoft.AspNet.Razor.Runtime
}
/// <summary>
/// Invalid tag helper directive look up text '{0}'. The correct look up text formats are:
/// "assemblyName"
/// "typeName, assemblyName"
/// Invalid tag helper directive look up text '{0}'. The correct look up text format is: "typeName, assemblyName".
/// </summary>
internal static string FormatTagHelperDescriptorResolver_InvalidTagHelperLookupText(object p0)
{

View File

@ -118,9 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="TagHelperDescriptorResolver_InvalidTagHelperLookupText" xml:space="preserve">
<value>Invalid tag helper directive look up text '{0}'. The correct look up text formats are:
"assemblyName"
"typeName, assemblyName"</value>
<value>Invalid tag helper directive look up text '{0}'. The correct look up text format is: "typeName, assemblyName".</value>
</data>
<data name="TagHelperTypeResolver_CannotResolveTagHelperAssembly" xml:space="preserve">
<value>Cannot resolve TagHelper containing assembly '{0}'. Error: {1}</value>

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.TagHelpers;
using Microsoft.AspNet.Razor.Text;
@ -113,8 +114,15 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
return false;
}
return string.IsNullOrEmpty(lookupInfo.TypeName) ||
string.Equals(descriptor.TypeName, lookupInfo.TypeName, StringComparison.Ordinal);
// We need to escape the TypePattern so we can choose to only allow specific regex.
var escaped = Regex.Escape(lookupInfo.TypePattern);
// We surround the escaped with ^ and $ in order ot ensure a regex match matches the entire
// string. We also replace any '*' or '?' characters with regex to match appropriate content.
var pattern = "^" + escaped.Replace(@"\?", ".?").Replace(@"\*", ".*?") + "$";
var regex = new Regex(pattern, RegexOptions.Singleline);
return regex.IsMatch(descriptor.TypeName);
}
private static LookupInfo GetLookupInfo(TagHelperDirectiveDescriptor directiveDescriptor,
@ -128,7 +136,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
// "typeName, assemblyName"
if (lookupStrings == null ||
lookupStrings.Any(string.IsNullOrWhiteSpace) ||
(lookupStrings.Length != 1 && lookupStrings.Length != 2))
lookupStrings.Length != 2)
{
errorSink.OnError(
directiveDescriptor.Location,
@ -137,22 +145,10 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
return null;
}
// Grab the assembly name from the lookup text strings. Due to our supported lookupText formats it will
// always be the last element provided.
var assemblyName = lookupStrings.Last().Trim();
string typeName = null;
// Check if the lookupText specifies a type to search for.
if (lookupStrings.Length == 2)
{
// The user provided a type name. Retrieve it so we can prune our descriptors.
typeName = lookupStrings[0].Trim();
}
return new LookupInfo
{
AssemblyName = assemblyName,
TypeName = typeName
TypePattern = lookupStrings[0].Trim(),
AssemblyName = lookupStrings[1].Trim()
};
}
@ -160,7 +156,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
{
public string AssemblyName { get; set; }
public string TypeName { get; set; }
public string TypePattern { get; set; }
}
}
}