public string Format(string format, object arg, IFormatProvider formatProvider) { // This is the case we need to special case. We trust the IHtmlContent instance to do the // right thing with encoding. var htmlContent = arg as IHtmlContent; if (htmlContent != null) { using (var writer = new StringWriter()) { htmlContent.WriteTo(writer, _encoder); return(writer.ToString()); } } // If we get here then 'arg' is not an IHtmlContent, and we want to handle it the way a normal // string.Format would work, but then HTML encode the result. // // First check for an ICustomFormatter - if the IFormatProvider is a CultureInfo, then it's likely // that ICustomFormatter will be null. var customFormatter = (ICustomFormatter)_formatProvider.GetFormat(typeof(ICustomFormatter)); if (customFormatter != null) { var result = customFormatter.Format(format, arg, _formatProvider); if (result != null) { return(_encoder.HtmlEncode(result)); } } // Next check if 'arg' is an IFormattable (DateTime is an example). // // An IFormattable will likely call back into the IFormatterProvider and ask for more information // about how to format itself. This is the typical case when IFormatterProvider is a CultureInfo. var formattable = arg as IFormattable; if (formattable != null) { var result = formattable.ToString(format, _formatProvider); if (result != null) { return(_encoder.HtmlEncode(result)); } } // If we get here then there's nothing really smart left to try. if (arg != null) { var result = arg.ToString(); if (result != null) { return(_encoder.HtmlEncode(result)); } } return(string.Empty); }
private static void WriteTo(TextWriter writer, IHtmlEncoder encoder, string value) { if (!string.IsNullOrEmpty(value)) { encoder.HtmlEncode(value, writer); } }
private void AppendToOutputBuffer(object[] arguments, StringBuilder tokenBuffer, StringBuilder outputBuffer) { if (tokenBuffer != null && tokenBuffer.Length > 0) { outputBuffer.Append(_encoder.HtmlEncode(string.Format(tokenBuffer.ToString(), arguments))); } }
/// <inheritdoc /> public void WriteTo(TextWriter writer, IHtmlEncoder encoder) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } foreach (var entry in Entries) { if (entry == null) { continue; } var entryAsString = entry as string; if (entryAsString != null) { encoder.HtmlEncode(entryAsString, writer); } else { // Only string, IHtmlContent values can be added to the buffer. ((IHtmlContent)entry).WriteTo(writer, encoder); } } }
private TimeSpan EncodeHtml(IHtmlEncoder encoder, string text, Stopwatch timer, int iterations) { timer.Restart(); for (int iteration = 0; iteration < iterations; iteration++) { Ignore(encoder.HtmlEncode(text)); } return(timer.Elapsed); }
/// <summary> /// HTML-encodes a string and writes the result to the supplied output. /// </summary> /// <remarks> /// The encoded value is also safe for inclusion inside an HTML attribute /// as long as the attribute value is surrounded by single or double quotes. /// </remarks> public static void HtmlEncode(this IHtmlEncoder htmlEncoder, string value, TextWriter output) { if (htmlEncoder == null) { throw new ArgumentNullException(nameof(htmlEncoder)); } if (!String.IsNullOrEmpty(value)) { htmlEncoder.HtmlEncode(value, 0, value.Length, output); } }
/// <inheritdoc /> public void WriteTo(TextWriter writer, IHtmlEncoder encoder) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (encoder == null) { throw new ArgumentNullException(nameof(encoder)); } encoder.HtmlEncode(_input, writer); }
private static TimeSpan EncodeHtmlToTextWriter(IHtmlEncoder encoder, string text, Stopwatch timer, int iterations) { var defaultEncoder = System.Text.Encodings.Web.HtmlEncoder.Default; var stream = new MemoryStream(text.Length * 2 * defaultEncoder.MaxOutputCharactersPerInputCharacter); var writer = new StreamWriter(stream); timer.Restart(); for (int iteration = 0; iteration < iterations; iteration++) { encoder.HtmlEncode(text, writer); } writer.Flush(); writer.Dispose(); return(timer.Elapsed); }
private void AppendAttributes(TextWriter writer, IHtmlEncoder encoder) { foreach (var attribute in Attributes) { var key = attribute.Key; if (string.Equals(key, "id", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(attribute.Value)) { continue; } writer.Write(" "); writer.Write(key); writer.Write("=\""); encoder.HtmlEncode(attribute.Value, writer); writer.Write("\""); } }
protected object[] EncodeArguments(object[] arguments) { object[] encodedArguments = new object[arguments.Length]; for (var index = 0; index != arguments.Length; ++index) { var argument = arguments[index]; if (argument.GetType().GetTypeInfo().IsPrimitive || argument is HtmlString || argument is DateTime || argument is DateTimeOffset) { encodedArguments[index] = argument; } else { encodedArguments[index] = _encoder.HtmlEncode(argument.ToString()); } } return(encodedArguments); }
private void AppendAttributes(TextWriter writer, IHtmlEncoder encoder) { // Perf: Avoid allocating enumerator for `_attributes` if possible if (_attributes != null && _attributes.Count > 0) { foreach (var attribute in Attributes) { var key = attribute.Key; if (string.Equals(key, "id", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(attribute.Value)) { continue; } writer.Write(" "); writer.Write(key); writer.Write("=\""); encoder.HtmlEncode(attribute.Value, writer); writer.Write("\""); } } }
/// <inheritdoc /> public void WriteTo([NotNull] TextWriter writer, [NotNull] IHtmlEncoder encoder) { encoder.HtmlEncode(_input, writer); }
/// <inheritdoc /> public string Encode(string value) { return(!string.IsNullOrEmpty(value) ? _htmlEncoder.HtmlEncode(value) : string.Empty); }
public void WriteTo(TextWriter writer, IHtmlEncoder encoder) { encoder.HtmlEncode(Value, writer); }
private static string HtmlEncode(string body) { return(_htmlEncoder.HtmlEncode(body)); }