// this loop processes character pairs (in case they are surrogates).
// there is an if block below to process single last character.
- for (int secondCharIndex = 1; secondCharIndex < valueLength; secondCharIndex++)
+ int secondCharIndex;
+ for (secondCharIndex = 1; secondCharIndex < valueLength; secondCharIndex++)
{
if (!wasSurrogatePair)
{
}
}
- if (!wasSurrogatePair)
+ if (!wasSurrogatePair || (secondCharIndex == valueLength))
{
firstChar = value[valueLength - 1];
int nextScalar = UnicodeHelpers.GetScalarValueFromUtf16(firstChar, null, out wasSurrogatePair);
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
{
public class HtmlEncoderTests
{
- [Fact]
- public void TestSurrogate()
+ [Theory]
+ [InlineData("💩", "\U0001f4a9")]
+ [InlineData("😂2", "\U0001F6022")]
+ [InlineData("😂 21", "\U0001F602 21")]
+ [InlineData("x😂y", "x\U0001F602y")]
+ [InlineData("😂x😂y", "\U0001F602x\U0001F602y")]
+ public void TestSurrogate(string expected, string actual)
{
- Assert.Equal("💩", System.Text.Encodings.Web.HtmlEncoder.Default.Encode("\U0001f4a9"));
+ Assert.Equal(expected, System.Text.Encodings.Web.HtmlEncoder.Default.Encode(actual));
using (var writer = new StringWriter())
{
- System.Text.Encodings.Web.HtmlEncoder.Default.Encode(writer, "\U0001f4a9");
- Assert.Equal("💩", writer.GetStringBuilder().ToString());
+ System.Text.Encodings.Web.HtmlEncoder.Default.Encode(writer, actual);
+ Assert.Equal(expected, writer.GetStringBuilder().ToString());
}
}
-
+
[Fact]
public void Ctor_WithTextEncoderSettings()
{