Skip to content

Commit c2ed6a6

Browse files
committed
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
1 parent 5c25567 commit c2ed6a6

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.ComponentModel;
6+
using System.Linq;
67
using Umbraco.Cms.Core.Configuration.UmbracoSettings;
78
using Umbraco.Extensions;
89

@@ -16,33 +17,34 @@ public class RequestHandlerSettings
1617
{
1718
internal const bool StaticAddTrailingSlash = true;
1819
internal const string StaticConvertUrlsToAscii = "try";
20+
internal const bool StaticEnableDefaultCharReplacements = true;
1921

20-
internal static readonly CharItem[] DefaultCharCollection =
22+
internal static readonly CharacterReplacement[] DefaultCharCollection =
2123
{
22-
new CharItem { Char = " ", Replacement = "-" },
23-
new CharItem { Char = "\"", Replacement = string.Empty },
24-
new CharItem { Char = "'", Replacement = string.Empty },
25-
new CharItem { Char = "%", Replacement = string.Empty },
26-
new CharItem { Char = ".", Replacement = string.Empty },
27-
new CharItem { Char = ";", Replacement = string.Empty },
28-
new CharItem { Char = "/", Replacement = string.Empty },
29-
new CharItem { Char = "\\", Replacement = string.Empty },
30-
new CharItem { Char = ":", Replacement = string.Empty },
31-
new CharItem { Char = "#", Replacement = string.Empty },
32-
new CharItem { Char = "+", Replacement = "plus" },
33-
new CharItem { Char = "*", Replacement = "star" },
34-
new CharItem { Char = "&", Replacement = string.Empty },
35-
new CharItem { Char = "?", Replacement = string.Empty },
36-
new CharItem { Char = "æ", Replacement = "ae" },
37-
new CharItem { Char = "ä", Replacement = "ae" },
38-
new CharItem { Char = "ø", Replacement = "oe" },
39-
new CharItem { Char = "ö", Replacement = "oe" },
40-
new CharItem { Char = "å", Replacement = "aa" },
41-
new CharItem { Char = "ü", Replacement = "ue" },
42-
new CharItem { Char = "ß", Replacement = "ss" },
43-
new CharItem { Char = "|", Replacement = "-" },
44-
new CharItem { Char = "<", Replacement = string.Empty },
45-
new CharItem { Char = ">", Replacement = string.Empty }
24+
new () { Char = " ", Replacement = "-" },
25+
new () { Char = "\"", Replacement = string.Empty },
26+
new () { Char = "'", Replacement = string.Empty },
27+
new () { Char = "%", Replacement = string.Empty },
28+
new () { Char = ".", Replacement = string.Empty },
29+
new () { Char = ";", Replacement = string.Empty },
30+
new () { Char = "/", Replacement = string.Empty },
31+
new () { Char = "\\", Replacement = string.Empty },
32+
new () { Char = ":", Replacement = string.Empty },
33+
new () { Char = "#", Replacement = string.Empty },
34+
new () { Char = "+", Replacement = "plus" },
35+
new () { Char = "*", Replacement = "star" },
36+
new () { Char = "&", Replacement = string.Empty },
37+
new () { Char = "?", Replacement = string.Empty },
38+
new () { Char = "æ", Replacement = "ae" },
39+
new () { Char = "ä", Replacement = "ae" },
40+
new () { Char = "ø", Replacement = "oe" },
41+
new () { Char = "ö", Replacement = "oe" },
42+
new () { Char = "å", Replacement = "aa" },
43+
new () { Char = "ü", Replacement = "ue" },
44+
new () { Char = "ß", Replacement = "ss" },
45+
new () { Char = "|", Replacement = "-" },
46+
new () { Char = "<", Replacement = string.Empty },
47+
new () { Char = ">", Replacement = string.Empty }
4648
};
4749

4850
/// <summary>
@@ -67,41 +69,49 @@ public class RequestHandlerSettings
6769
/// </summary>
6870
public bool ShouldTryConvertUrlsToAscii => ConvertUrlsToAscii.InvariantEquals("try");
6971

70-
// We need to special handle ":", as this character is special in keys
71-
72-
// TODO: implement from configuration
73-
74-
//// var collection = _configuration.GetSection(Prefix + "CharCollection").GetChildren()
75-
//// .Select(x => new CharItem()
76-
//// {
77-
//// Char = x.GetValue<string>("Char"),
78-
//// Replacement = x.GetValue<string>("Replacement"),
79-
//// }).ToArray();
80-
81-
//// if (collection.Any() || _configuration.GetSection("Prefix").GetChildren().Any(x =>
82-
//// x.Key.Equals("CharCollection", StringComparison.OrdinalIgnoreCase)))
83-
//// {
84-
//// return collection;
85-
//// }
86-
87-
//// return DefaultCharCollection;
72+
/// <summary>
73+
/// Disable all default character replacements
74+
/// </summary>
75+
[DefaultValue(StaticEnableDefaultCharReplacements)]
76+
public bool EnableDefaultCharReplacements { get; set; } = StaticEnableDefaultCharReplacements;
8877

8978
/// <summary>
90-
/// Gets or sets a value for the default character collection for replacements.
79+
/// Add additional character replacements, or override defaults
9180
/// </summary>
92-
/// WB-TODO
93-
public IEnumerable<IChar> CharCollection { get; set; } = DefaultCharCollection;
81+
public CharacterReplacement[] CharCollection { get; set; }
9482

9583
/// <summary>
96-
/// Defines a character replacement.
84+
/// Get concatenated user and default character replacements
85+
/// taking into account <see cref="EnableDefaultCharReplacements"/>
9786
/// </summary>
98-
public class CharItem : IChar
87+
public IEnumerable<CharacterReplacement> GetCharReplacements()
9988
{
100-
/// <inheritdoc/>
101-
public string Char { get; set; }
89+
// TODO We need to special handle ":", as this character is special in keys
90+
91+
if (!EnableDefaultCharReplacements)
92+
{
93+
return CharCollection;
94+
}
95+
96+
if (CharCollection == null || !CharCollection.Any())
97+
{
98+
return DefaultCharCollection;
99+
}
100+
101+
foreach (var defaultReplacement in DefaultCharCollection)
102+
{
103+
foreach (var userReplacement in CharCollection)
104+
{
105+
if (userReplacement.Char == defaultReplacement.Char)
106+
{
107+
defaultReplacement.Replacement = userReplacement.Replacement;
108+
}
109+
}
110+
}
111+
112+
var mergedCollections = DefaultCharCollection.Union(CharCollection, new CharacterReplacementEqualityComparer());
102113

103-
/// <inheritdoc/>
104-
public string Replacement { get; set; }
114+
return mergedCollections;
105115
}
106116
}
107117
}

0 commit comments

Comments
 (0)