From 91e040e684a874bd410a2259f45070b0b7f55945 Mon Sep 17 00:00:00 2001 From: Lombiq Date: Mon, 11 May 2015 16:52:54 +0200 Subject: [PATCH] Fixing that the technical names of widgets couldn't contain hyphens, see #4981 --- .../Drivers/WidgetPartDriver.cs | 2 +- .../Utility/Extensions/StringExtensions.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Orchard.Web/Modules/Orchard.Widgets/Drivers/WidgetPartDriver.cs b/src/Orchard.Web/Modules/Orchard.Widgets/Drivers/WidgetPartDriver.cs index 9a3fba25c..4016c9f17 100644 --- a/src/Orchard.Web/Modules/Orchard.Widgets/Drivers/WidgetPartDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Widgets/Drivers/WidgetPartDriver.cs @@ -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().Where(x => x.Name == widgetPart.Name && x.Id != widgetPart.Id).Count(); if(widgets > 0) { diff --git a/src/Orchard/Utility/Extensions/StringExtensions.cs b/src/Orchard/Utility/Extensions/StringExtensions.cs index 65a6c73e9..d1290b794 100644 --- a/src/Orchard/Utility/Extensions/StringExtensions.cs +++ b/src/Orchard/Utility/Extensions/StringExtensions.cs @@ -191,6 +191,34 @@ namespace Orchard.Utility.Extensions { return name; } + /// + /// Generates a valid Html name. + /// + /// + /// Uses a white list set of chars. + /// + 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; + } + /// /// Whether the char is a letter between A and Z or not ///