/// <summary>
        /// Encodes a <c>string[]</c> for safe use as a JavaScript array literal in many contexts, including
        /// inline in an HTML file.
        /// </summary>
        public static string Encode(JavaScriptEncoder encoder, string[] values)
        {
            var writer = new StringWriter();
            writer.Write('[');

            // Perf: Avoid allocating enumerator
            var firstAdded = false;
            for (var i = 0; i < values.Length; i++)
            {
                if (firstAdded)
                {
                    writer.Write(',');
                }

                writer.Write('"');
                encoder.Encode(writer, values[i]);
                writer.Write('"');
                firstAdded = true;
            }

            writer.Write(']');

            return writer.ToString();
        }
Exemplo n.º 2
0
 public JavaScriptStringEncoder(TextEncoderSettings filter)
 {
     _encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(filter);
 }
Exemplo n.º 3
0
 public JavaScriptStringEncoder()
 {
     _encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new <see cref="ScriptTagHelper"/>.
 /// </summary>
 /// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
 /// <param name="cache">The <see cref="IMemoryCache"/>.</param>
 /// <param name="htmlEncoder">The <see cref="HtmlEncoder"/>.</param>
 /// <param name="javaScriptEncoder">The <see cref="JavaScriptEncoder"/>.</param>
 /// <param name="urlHelperFactory">The <see cref="IUrlHelperFactory"/>.</param>
 public ScriptTagHelper(
     IHostingEnvironment hostingEnvironment,
     IMemoryCache cache,
     HtmlEncoder htmlEncoder,
     JavaScriptEncoder javaScriptEncoder,
     IUrlHelperFactory urlHelperFactory)
     : base(urlHelperFactory, htmlEncoder)
 {
     HostingEnvironment = hostingEnvironment;
     Cache = cache;
     JavaScriptEncoder = javaScriptEncoder;
 }
Exemplo n.º 5
0
 public JavaScriptStringEncoder(bool relaxed = false)
 {
     _encoder = relaxed ? JavaScriptEncoder.UnsafeRelaxedJsonEscaping : JavaScriptEncoder.Default;
 }