Remove ugly CharCollection hack
By adding a new configuration and mapping the old CharCollection to that, we can get around having to return null the first time, by obsoleting the old one and redirecting to the new GetCharReplacements method
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Umbraco.Cms.Core.Configuration.UmbracoSettings;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
@@ -75,63 +75,15 @@ namespace Umbraco.Cms.Core.Configuration.Models
|
||||
[DefaultValue(StaticEnableDefaultCharReplacements)]
|
||||
public bool EnableDefaultCharReplacements { get; set; } = StaticEnableDefaultCharReplacements;
|
||||
|
||||
private IEnumerable<CharItem> _charCollection;
|
||||
/// <summary>
|
||||
/// Add additional character replacements, or override defaults
|
||||
/// </summary>
|
||||
[Obsolete("Use the GetCharReplacements extension method in the Umbraco.Extensions namespace instead. Scheduled for removal in V11")]
|
||||
public IEnumerable<IChar> CharCollection { get; set; } = DefaultCharCollection;
|
||||
|
||||
/// <summary>
|
||||
/// Add additional character replacements, or override defaults
|
||||
/// </summary>
|
||||
public IEnumerable<CharItem> CharCollection
|
||||
{
|
||||
get
|
||||
{
|
||||
// This is pretty ugly, but necessary.
|
||||
// Essentially the config binding will only run properly if we return null the first time this is invoked.
|
||||
// Otherwise whatever we return will just be used and user specific bindings won't overwrite the existing ones.
|
||||
// However the next time this get is invoked, for instance in DefaultShortStringHelper we want to actually run the GetCharReplacements
|
||||
// To make sure that the default bindings is used if configured to do so.
|
||||
// Therefore we set _charCollection to be something, and still return null, to trick dotnet to actually read the config.
|
||||
if (_charCollection is null)
|
||||
{
|
||||
_charCollection = Enumerable.Empty<CharItem>();
|
||||
return null;
|
||||
}
|
||||
|
||||
return GetCharReplacements();
|
||||
}
|
||||
|
||||
set => _charCollection = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get concatenated user and default character replacements
|
||||
/// taking into account <see cref="EnableDefaultCharReplacements"/>
|
||||
/// </summary>
|
||||
private IEnumerable<CharItem> GetCharReplacements()
|
||||
{
|
||||
if (!EnableDefaultCharReplacements)
|
||||
{
|
||||
return _charCollection ?? Enumerable.Empty<CharItem>();
|
||||
}
|
||||
|
||||
if (_charCollection == null || !_charCollection.Any())
|
||||
{
|
||||
return DefaultCharCollection;
|
||||
}
|
||||
|
||||
foreach (CharItem defaultReplacement in DefaultCharCollection)
|
||||
{
|
||||
foreach (CharItem userReplacement in _charCollection)
|
||||
{
|
||||
if (userReplacement.Char == defaultReplacement.Char)
|
||||
{
|
||||
defaultReplacement.Replacement = userReplacement.Replacement;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerable<CharItem> mergedCollections = DefaultCharCollection.Union<CharItem>(_charCollection, new CharacterReplacementEqualityComparer());
|
||||
|
||||
return mergedCollections;
|
||||
}
|
||||
public IEnumerable<CharItem> UserDefinedCharCollection { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user