V14 Bugfix ensures correct line endings for partial view snippets (#15906)

* Created extension class so we can ensure native line endings

* Added usage of extension method for ensuring native line endings

* Added tests, to see if the snippets return the correct content

* Removed space
This commit is contained in:
Andreas Zerbst
2024-03-19 13:14:38 +01:00
committed by GitHub
parent 1e69695650
commit e7f40affac
3 changed files with 67 additions and 4 deletions

View File

@@ -0,0 +1,50 @@
using System.Runtime.InteropServices;
namespace Umbraco.Cms.Core.Extensions;
public static class LineEndingsExtensions
{
/// <summary>
/// Ensures Lf only everywhere.
/// </summary>
/// <param name="text">The text to filter.</param>
/// <returns>The filtered text.</returns>
private static string Lf(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
text = text.Replace("\r", string.Empty); // remove CR
return text;
}
/// <summary>
/// Ensures CrLf everywhere.
/// </summary>
/// <param name="text">The text to filter.</param>
/// <returns>The filtered text.</returns>
private static string CrLf(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
text = text.Replace("\r", string.Empty); // remove CR
text = text.Replace("\n", "\r\n"); // add CRLF everywhere
return text;
}
/// <summary>
/// Ensures native line endings.
/// </summary>
/// <param name="text">the text to ensure native line endings for.</param>
/// <returns>the text with native line endings.</returns>
public static string EnsureNativeLineEndings(this string text)
{
var useCrLf = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
return useCrLf ? CrLf(text) : Lf(text);
}
}

View File

@@ -2,6 +2,7 @@ using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Extensions;
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;
@@ -38,6 +39,7 @@ public partial class PartialViewSnippetCollectionBuilder : LazyCollectionBuilder
private string CleanUpSnippetContent(string content)
{
const string partialViewHeader = "@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage";
content = content.EnsureNativeLineEndings();
// Strip the @inherits if it's there
Regex headerMatch = HeaderRegex();