3
3
4
4
using System . Collections . Generic ;
5
5
using System . ComponentModel ;
6
+ using System . Linq ;
6
7
using Umbraco . Cms . Core . Configuration . UmbracoSettings ;
7
8
using Umbraco . Extensions ;
8
9
@@ -16,33 +17,34 @@ public class RequestHandlerSettings
16
17
{
17
18
internal const bool StaticAddTrailingSlash = true ;
18
19
internal const string StaticConvertUrlsToAscii = "try" ;
20
+ internal const bool StaticEnableDefaultCharReplacements = true ;
19
21
20
- internal static readonly CharItem [ ] DefaultCharCollection =
22
+ internal static readonly CharacterReplacement [ ] DefaultCharCollection =
21
23
{
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 }
46
48
} ;
47
49
48
50
/// <summary>
@@ -67,41 +69,49 @@ public class RequestHandlerSettings
67
69
/// </summary>
68
70
public bool ShouldTryConvertUrlsToAscii => ConvertUrlsToAscii . InvariantEquals ( "try" ) ;
69
71
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 ;
88
77
89
78
/// <summary>
90
- /// Gets or sets a value for the default character collection for replacements.
79
+ /// Add additional character replacements, or override defaults
91
80
/// </summary>
92
- /// WB-TODO
93
- public IEnumerable < IChar > CharCollection { get ; set ; } = DefaultCharCollection ;
81
+ public CharacterReplacement [ ] CharCollection { get ; set ; }
94
82
95
83
/// <summary>
96
- /// Defines a character replacement.
84
+ /// Get concatenated user and default character replacements
85
+ /// taking into account <see cref="EnableDefaultCharReplacements"/>
97
86
/// </summary>
98
- public class CharItem : IChar
87
+ public IEnumerable < CharacterReplacement > GetCharReplacements ( )
99
88
{
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 ( ) ) ;
102
113
103
- /// <inheritdoc/>
104
- public string Replacement { get ; set ; }
114
+ return mergedCollections ;
105
115
}
106
116
}
107
117
}
0 commit comments