Skip to content

Commit

Permalink
Move HTML-encoding code together
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Nov 29, 2023
1 parent c36c33e commit 25be9cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
46 changes: 45 additions & 1 deletion src/RazorBlade.Library/HtmlHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;

namespace RazorBlade;
Expand Down Expand Up @@ -60,7 +61,7 @@ public string Encode(object? value)
'>' => ">",
'"' => """,
'\'' => "'",
var c => c.ToString() // Won't happen
var c => c.ToString() // Unreachable
});

valueSpan = valueSpan[(idx + 1)..];
Expand All @@ -79,6 +80,49 @@ public string Encode(object? value)
.Replace(">", ">")
.Replace("\"", """)
.Replace("\'", "'");
#endif
}

/// <summary>
/// HTML-encodes the provided value to the writer.
/// </summary>
/// <param name="value">Value to HTML-encode.</param>
/// <param name="writer">Destination writer.</param>
internal static void Encode(object? value, TextWriter writer)
{
var valueString = value?.ToString();
if (valueString is null or "")
return;

#if NET6_0_OR_GREATER
var valueSpan = valueString.AsSpan();

while (true)
{
var idx = valueSpan.IndexOfAny(_charsToEscape);
if (idx < 0)
break;

if (idx != 0)
writer.Write(valueSpan[..idx]);

writer.Write(valueSpan[idx] switch
{
'&' => "&amp;",
'<' => "&lt;",
'>' => "&gt;",
'"' => "&quot;",
'\'' => "&#x27;",
var c => c.ToString() // Unreachable
});

valueSpan = valueSpan[(idx + 1)..];
}

if (valueSpan.Length != 0)
writer.Write(valueSpan);
#else
writer.Write(Instance.Encode(valueString));
#endif
}
}
52 changes: 2 additions & 50 deletions src/RazorBlade.Library/HtmlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ namespace RazorBlade;
/// </remarks>
public abstract class HtmlTemplate : RazorTemplate
{
#if NET8_0_OR_GREATER
private static readonly System.Buffers.SearchValues<char> _charsToEscape = System.Buffers.SearchValues.Create("&<>\"\'");
#elif NET6_0_OR_GREATER
private const string _charsToEscape = "&<>\"\'";
#endif

private AttributeInfo _currentAttribute;

// ReSharper disable once RedundantDisableWarningComment
Expand All @@ -39,51 +33,9 @@ protected internal HtmlString Raw(object? value)
protected internal override void Write(object? value)
{
if (value is IEncodedContent encodedContent)
{
encodedContent.WriteTo(Output);
return;
}

var valueString = value?.ToString();
if (valueString is null or "")
return;

#if NET6_0_OR_GREATER
var valueSpan = valueString.AsSpan();

while (true)
{
var idx = valueSpan.IndexOfAny(_charsToEscape);
if (idx < 0)
break;

if (idx != 0)
Output.Write(valueSpan[..idx]);

Output.Write(valueSpan[idx] switch
{
'&' => "&amp;",
'<' => "&lt;",
'>' => "&gt;",
'"' => "&quot;",
'\'' => "&#x27;",
var c => c.ToString() // Won't happen
});

valueSpan = valueSpan[(idx + 1)..];
}

if (valueSpan.Length != 0)
Output.Write(valueSpan);
#else
Output.Write(
valueString.Replace("&", "&amp;")
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\"", "&quot;")
.Replace("\'", "&#x27;")
);
#endif
else
HtmlHelper.Encode(value, Output);
}

/// <inheritdoc />
Expand Down

0 comments on commit 25be9cf

Please sign in to comment.