Skip to content

Commit d3a479c

Browse files
author
Nate McMaster
committed
Merge the source code for Microsoft.Extensions.WebEncoders
\n\nCommit migrated from dotnet/extensions@ca683f3
2 parents 8d17a5b + aa26d51 commit d3a479c

10 files changed

+1126
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Text.Encodings.Web;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
using Microsoft.Extensions.Options;
8+
using Microsoft.Extensions.WebEncoders;
9+
10+
namespace Microsoft.Extensions.DependencyInjection
11+
{
12+
/// <summary>
13+
/// Extension methods for setting up web encoding services in an <see cref="IServiceCollection" />.
14+
/// </summary>
15+
public static class EncoderServiceCollectionExtensions
16+
{
17+
/// <summary>
18+
/// Adds <see cref="HtmlEncoder"/>, <see cref="JavaScriptEncoder"/> and <see cref="UrlEncoder"/>
19+
/// to the specified <paramref name="services" />.
20+
/// </summary>
21+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
22+
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
23+
public static IServiceCollection AddWebEncoders(this IServiceCollection services)
24+
{
25+
if (services == null)
26+
{
27+
throw new ArgumentNullException(nameof(services));
28+
}
29+
30+
services.AddOptions();
31+
32+
// Register the default encoders
33+
// We want to call the 'Default' property getters lazily since they perform static caching
34+
services.TryAddSingleton(
35+
CreateFactory(() => HtmlEncoder.Default, settings => HtmlEncoder.Create(settings)));
36+
services.TryAddSingleton(
37+
CreateFactory(() => JavaScriptEncoder.Default, settings => JavaScriptEncoder.Create(settings)));
38+
services.TryAddSingleton(
39+
CreateFactory(() => UrlEncoder.Default, settings => UrlEncoder.Create(settings)));
40+
41+
return services;
42+
}
43+
44+
/// <summary>
45+
/// Adds <see cref="HtmlEncoder"/>, <see cref="JavaScriptEncoder"/> and <see cref="UrlEncoder"/>
46+
/// to the specified <paramref name="services" />.
47+
/// </summary>
48+
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
49+
/// <param name="setupAction">An <see cref="Action{WebEncoderOptions}"/> to configure the provided <see cref="WebEncoderOptions"/>.</param>
50+
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
51+
public static IServiceCollection AddWebEncoders(this IServiceCollection services, Action<WebEncoderOptions> setupAction)
52+
{
53+
if (services == null)
54+
{
55+
throw new ArgumentNullException(nameof(services));
56+
}
57+
58+
if (setupAction == null)
59+
{
60+
throw new ArgumentNullException(nameof(setupAction));
61+
}
62+
63+
services.AddWebEncoders();
64+
services.Configure(setupAction);
65+
66+
return services;
67+
}
68+
69+
private static Func<IServiceProvider, TService> CreateFactory<TService>(
70+
Func<TService> defaultFactory,
71+
Func<TextEncoderSettings, TService> customSettingsFactory)
72+
{
73+
return serviceProvider =>
74+
{
75+
var settings = serviceProvider
76+
?.GetService<IOptions<WebEncoderOptions>>()
77+
?.Value
78+
?.TextEncoderSettings;
79+
return (settings != null) ? customSettingsFactory(settings) : defaultFactory();
80+
};
81+
}
82+
}
83+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>Contains registration and configuration APIs to add the core framework encoders to a dependency injection container.</Description>
5+
<TargetFramework>netstandard2.0</TargetFramework>
6+
<NoWarn>$(NoWarn);CS1591</NoWarn>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9+
<PackageTags>aspnetcore</PackageTags>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
14+
<Reference Include="Microsoft.Extensions.Options" />
15+
<Reference Include="System.Text.Encodings.Web" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Text.Encodings.Web;
7+
8+
namespace Microsoft.Extensions.WebEncoders.Testing
9+
{
10+
/// <summary>
11+
/// Encoder used for unit testing.
12+
/// </summary>
13+
public sealed class HtmlTestEncoder : HtmlEncoder
14+
{
15+
public override int MaxOutputCharactersPerInputCharacter
16+
{
17+
get { return 1; }
18+
}
19+
20+
public override string Encode(string value)
21+
{
22+
if (value == null)
23+
{
24+
throw new ArgumentNullException(nameof(value));
25+
}
26+
27+
if (value.Length == 0)
28+
{
29+
return string.Empty;
30+
}
31+
32+
return $"HtmlEncode[[{value}]]";
33+
}
34+
35+
public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount)
36+
{
37+
if (output == null)
38+
{
39+
throw new ArgumentNullException(nameof(output));
40+
}
41+
42+
if (value == null)
43+
{
44+
throw new ArgumentNullException(nameof(value));
45+
}
46+
47+
if (characterCount == 0)
48+
{
49+
return;
50+
}
51+
52+
output.Write("HtmlEncode[[");
53+
output.Write(value, startIndex, characterCount);
54+
output.Write("]]");
55+
}
56+
57+
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
58+
{
59+
if (output == null)
60+
{
61+
throw new ArgumentNullException(nameof(output));
62+
}
63+
64+
if (value == null)
65+
{
66+
throw new ArgumentNullException(nameof(value));
67+
}
68+
69+
if (characterCount == 0)
70+
{
71+
return;
72+
}
73+
74+
output.Write("HtmlEncode[[");
75+
output.Write(value.Substring(startIndex, characterCount));
76+
output.Write("]]");
77+
}
78+
79+
public override bool WillEncode(int unicodeScalar)
80+
{
81+
return false;
82+
}
83+
84+
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
85+
{
86+
return -1;
87+
}
88+
89+
public override unsafe bool TryEncodeUnicodeScalar(
90+
int unicodeScalar,
91+
char* buffer,
92+
int bufferLength,
93+
out int numberOfCharactersWritten)
94+
{
95+
if (buffer == null)
96+
{
97+
throw new ArgumentNullException(nameof(buffer));
98+
}
99+
100+
numberOfCharactersWritten = 0;
101+
return false;
102+
}
103+
}
104+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
using System.Text.Encodings.Web;
7+
8+
namespace Microsoft.Extensions.WebEncoders.Testing
9+
{
10+
/// <summary>
11+
/// Encoder used for unit testing.
12+
/// </summary>
13+
public class JavaScriptTestEncoder : JavaScriptEncoder
14+
{
15+
public override int MaxOutputCharactersPerInputCharacter
16+
{
17+
get { return 1; }
18+
}
19+
20+
public override string Encode(string value)
21+
{
22+
if (value == null)
23+
{
24+
throw new ArgumentNullException(nameof(value));
25+
}
26+
27+
if (value.Length == 0)
28+
{
29+
return string.Empty;
30+
}
31+
32+
return $"JavaScriptEncode[[{value}]]";
33+
}
34+
35+
public override void Encode(TextWriter output, char[] value, int startIndex, int characterCount)
36+
{
37+
if (output == null)
38+
{
39+
throw new ArgumentNullException(nameof(output));
40+
}
41+
42+
if (value == null)
43+
{
44+
throw new ArgumentNullException(nameof(value));
45+
}
46+
47+
if (characterCount == 0)
48+
{
49+
return;
50+
}
51+
52+
output.Write("JavaScriptEncode[[");
53+
output.Write(value, startIndex, characterCount);
54+
output.Write("]]");
55+
}
56+
57+
public override void Encode(TextWriter output, string value, int startIndex, int characterCount)
58+
{
59+
if (output == null)
60+
{
61+
throw new ArgumentNullException(nameof(output));
62+
}
63+
64+
if (value == null)
65+
{
66+
throw new ArgumentNullException(nameof(value));
67+
}
68+
69+
if (characterCount == 0)
70+
{
71+
return;
72+
}
73+
74+
output.Write("JavaScriptEncode[[");
75+
output.Write(value.Substring(startIndex, characterCount));
76+
output.Write("]]");
77+
}
78+
79+
public override bool WillEncode(int unicodeScalar)
80+
{
81+
return false;
82+
}
83+
84+
public override unsafe int FindFirstCharacterToEncode(char* text, int textLength)
85+
{
86+
return -1;
87+
}
88+
89+
public override unsafe bool TryEncodeUnicodeScalar(
90+
int unicodeScalar,
91+
char* buffer,
92+
int bufferLength,
93+
out int numberOfCharactersWritten)
94+
{
95+
if (buffer == null)
96+
{
97+
throw new ArgumentNullException(nameof(buffer));
98+
}
99+
100+
numberOfCharactersWritten = 0;
101+
return false;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)