Fixing that the technical names of widgets couldn't contain hyphens, see #4981

This commit is contained in:
Lombiq
2015-05-11 16:52:54 +02:00
committed by Zoltán Lehóczky
parent 73336e19ba
commit 91e040e684
2 changed files with 29 additions and 1 deletions

View File

@@ -53,7 +53,7 @@ namespace Orchard.Widgets.Drivers {
// if there is a name, ensure it's unique
if(!string.IsNullOrWhiteSpace(widgetPart.Name)) {
widgetPart.Name = widgetPart.Name.ToSafeName();
widgetPart.Name = widgetPart.Name.ToHtmlName();
var widgets = _contentManager.Query<WidgetPart, WidgetPartRecord>().Where(x => x.Name == widgetPart.Name && x.Id != widgetPart.Id).Count();
if(widgets > 0) {

View File

@@ -191,6 +191,34 @@ namespace Orchard.Utility.Extensions {
return name;
}
/// <summary>
/// Generates a valid Html name.
/// </summary>
/// <remarks>
/// Uses a white list set of chars.
/// </remarks>
public static string ToHtmlName(this string name) {
if (String.IsNullOrWhiteSpace(name))
return String.Empty;
name = RemoveDiacritics(name);
name = name.Strip(c =>
c != '-'
&& c != '_'
&& !c.IsLetter()
&& !Char.IsDigit(c)
);
name = name.Trim();
// don't allow non A-Z chars as first letter, as they are not allowed in prefixes
while (name.Length > 0 && !IsLetter(name[0])) {
name = name.Substring(1);
}
return name;
}
/// <summary>
/// Whether the char is a letter between A and Z or not
/// </summary>