Update request handler settings

`CharCollection` didn't map correctly from the config, updated to an array so that it does
Add logic to concatenate user and default replacements, replacing defaults with user defined if present.
Added additional option to disable the default replacements
This commit is contained in:
Matthew Care
2021-10-15 23:22:01 +01:00
parent 5c25567599
commit c2ed6a629b

View File

@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Umbraco.Cms.Core.Configuration.UmbracoSettings;
using Umbraco.Extensions;
@@ -16,33 +17,34 @@ namespace Umbraco.Cms.Core.Configuration.Models
{
internal const bool StaticAddTrailingSlash = true;
internal const string StaticConvertUrlsToAscii = "try";
internal const bool StaticEnableDefaultCharReplacements = true;
internal static readonly CharItem[] DefaultCharCollection =
internal static readonly CharacterReplacement[] DefaultCharCollection =
{
new CharItem { Char = " ", Replacement = "-" },
new CharItem { Char = "\"", Replacement = string.Empty },
new CharItem { Char = "'", Replacement = string.Empty },
new CharItem { Char = "%", Replacement = string.Empty },
new CharItem { Char = ".", Replacement = string.Empty },
new CharItem { Char = ";", Replacement = string.Empty },
new CharItem { Char = "/", Replacement = string.Empty },
new CharItem { Char = "\\", Replacement = string.Empty },
new CharItem { Char = ":", Replacement = string.Empty },
new CharItem { Char = "#", Replacement = string.Empty },
new CharItem { Char = "+", Replacement = "plus" },
new CharItem { Char = "*", Replacement = "star" },
new CharItem { Char = "&", Replacement = string.Empty },
new CharItem { Char = "?", Replacement = string.Empty },
new CharItem { Char = "æ", Replacement = "ae" },
new CharItem { Char = "ä", Replacement = "ae" },
new CharItem { Char = "ø", Replacement = "oe" },
new CharItem { Char = "ö", Replacement = "oe" },
new CharItem { Char = "å", Replacement = "aa" },
new CharItem { Char = "ü", Replacement = "ue" },
new CharItem { Char = "ß", Replacement = "ss" },
new CharItem { Char = "|", Replacement = "-" },
new CharItem { Char = "<", Replacement = string.Empty },
new CharItem { Char = ">", Replacement = string.Empty }
new () { Char = " ", Replacement = "-" },
new () { Char = "\"", Replacement = string.Empty },
new () { Char = "'", Replacement = string.Empty },
new () { Char = "%", Replacement = string.Empty },
new () { Char = ".", Replacement = string.Empty },
new () { Char = ";", Replacement = string.Empty },
new () { Char = "/", Replacement = string.Empty },
new () { Char = "\\", Replacement = string.Empty },
new () { Char = ":", Replacement = string.Empty },
new () { Char = "#", Replacement = string.Empty },
new () { Char = "+", Replacement = "plus" },
new () { Char = "*", Replacement = "star" },
new () { Char = "&", Replacement = string.Empty },
new () { Char = "?", Replacement = string.Empty },
new () { Char = "æ", Replacement = "ae" },
new () { Char = "ä", Replacement = "ae" },
new () { Char = "ø", Replacement = "oe" },
new () { Char = "ö", Replacement = "oe" },
new () { Char = "å", Replacement = "aa" },
new () { Char = "ü", Replacement = "ue" },
new () { Char = "ß", Replacement = "ss" },
new () { Char = "|", Replacement = "-" },
new () { Char = "<", Replacement = string.Empty },
new () { Char = ">", Replacement = string.Empty }
};
/// <summary>
@@ -67,41 +69,49 @@ namespace Umbraco.Cms.Core.Configuration.Models
/// </summary>
public bool ShouldTryConvertUrlsToAscii => ConvertUrlsToAscii.InvariantEquals("try");
// We need to special handle ":", as this character is special in keys
// TODO: implement from configuration
//// var collection = _configuration.GetSection(Prefix + "CharCollection").GetChildren()
//// .Select(x => new CharItem()
//// {
//// Char = x.GetValue<string>("Char"),
//// Replacement = x.GetValue<string>("Replacement"),
//// }).ToArray();
//// if (collection.Any() || _configuration.GetSection("Prefix").GetChildren().Any(x =>
//// x.Key.Equals("CharCollection", StringComparison.OrdinalIgnoreCase)))
//// {
//// return collection;
//// }
//// return DefaultCharCollection;
/// <summary>
/// Disable all default character replacements
/// </summary>
[DefaultValue(StaticEnableDefaultCharReplacements)]
public bool EnableDefaultCharReplacements { get; set; } = StaticEnableDefaultCharReplacements;
/// <summary>
/// Gets or sets a value for the default character collection for replacements.
/// Add additional character replacements, or override defaults
/// </summary>
/// WB-TODO
public IEnumerable<IChar> CharCollection { get; set; } = DefaultCharCollection;
public CharacterReplacement[] CharCollection { get; set; }
/// <summary>
/// Defines a character replacement.
/// Get concatenated user and default character replacements
/// taking into account <see cref="EnableDefaultCharReplacements"/>
/// </summary>
public class CharItem : IChar
public IEnumerable<CharacterReplacement> GetCharReplacements()
{
/// <inheritdoc/>
public string Char { get; set; }
// TODO We need to special handle ":", as this character is special in keys
/// <inheritdoc/>
public string Replacement { get; set; }
if (!EnableDefaultCharReplacements)
{
return CharCollection;
}
if (CharCollection == null || !CharCollection.Any())
{
return DefaultCharCollection;
}
foreach (var defaultReplacement in DefaultCharCollection)
{
foreach (var userReplacement in CharCollection)
{
if (userReplacement.Char == defaultReplacement.Char)
{
defaultReplacement.Replacement = userReplacement.Replacement;
}
}
}
var mergedCollections = DefaultCharCollection.Union(CharCollection, new CharacterReplacementEqualityComparer());
return mergedCollections;
}
}
}