diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs index 630d19e9ab..4c93aa29b1 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/DefaultTagHelperContent.cs @@ -133,137 +133,6 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers return this; } - /// - public override TagHelperContent AppendFormat(string format, object arg0) - { - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(format, arg0)); - return this; - } - - /// - public override TagHelperContent AppendFormat(string format, object arg0, object arg1) - { - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(format, arg0, arg1)); - return this; - } - - /// - public override TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2) - { - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(format, arg0, arg1, arg2)); - return this; - } - - /// - public override TagHelperContent AppendFormat(string format, params object[] args) - { - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(format, args)); - return this; - } - - /// - public override TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - object arg0) - { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } - - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(provider, format, arg0)); - return this; - } - - /// - public override TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - object arg0, - object arg1) - { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } - - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(provider, format, arg0, arg1)); - return this; - } - - /// - public override TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - object arg0, - object arg1, - object arg2) - { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } - - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(provider, format, arg0, arg1, arg2)); - return this; - } - - /// - public override TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - params object[] args) - { - if (provider == null) - { - throw new ArgumentNullException(nameof(provider)); - } - - if (format == null) - { - throw new ArgumentNullException(nameof(format)); - } - - Buffer.Append(string.Format(provider, format, args)); - return this; - } - /// public override TagHelperContent Append(IHtmlContent htmlContent) { diff --git a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs index ee274b6a6a..4f86f4bd99 100644 --- a/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs +++ b/src/Microsoft.AspNet.Razor.Runtime/TagHelpers/TagHelperContent.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// Abstract class used to buffer content returned by s. /// - public abstract class TagHelperContent : IHtmlContent + public abstract class TagHelperContent : IHtmlContentBuilder { /// /// Gets a value indicating whether the content was modifed. @@ -31,24 +31,39 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// Sets the content. /// - /// The that replaces the content. + /// The that replaces the content. /// A reference to this instance after the set operation has completed. - public TagHelperContent SetContent(string value) + public TagHelperContent SetContent(IHtmlContent htmlContent) { - Clear(); - Append(value); + HtmlContentBuilderExtensions.SetContent(this, htmlContent); return this; } /// /// Sets the content. /// - /// The that replaces the content. + /// + /// The that replaces the content. The value is assume to be unencoded + /// as-provided and will be HTML encoded before being written. + /// /// A reference to this instance after the set operation has completed. - public TagHelperContent SetContent(IHtmlContent htmlContent) + public TagHelperContent SetContent(string unencoded) { - Clear(); - Append(htmlContent); + HtmlContentBuilderExtensions.SetContent(this, unencoded); + return this; + } + + /// + /// Sets the content. + /// + /// + /// The that replaces the content. The value is assume to be HTML encoded + /// as-provided and no further encoding will be performed. + /// + /// A reference to this instance after the set operation has completed. + public TagHelperContent SetContentEncoded(string encoded) + { + HtmlContentBuilderExtensions.SetContentEncoded(this, encoded); return this; } @@ -59,6 +74,13 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// A reference to this instance after the append operation has completed. public abstract TagHelperContent Append(string value); + /// + /// Appends to the existing content. + /// + /// The to be appended. + /// A reference to this instance after the append operation has completed. + public abstract TagHelperContent Append(IHtmlContent htmlContent); + /// /// Appends to the existing content. is assumed /// to be an HTML encoded and no further encoding will be performed. @@ -69,46 +91,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// Appends the specified to the existing content after - /// replacing the format item with the representation of the - /// . - /// - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(string format, object arg0); - - /// - /// Appends the specified to the existing content after - /// replacing each format item with the representation of the - /// and . - /// - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1); - - /// - /// Appends the specified to the existing content after - /// replacing each format item with the representation of the - /// , and . - /// - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// The object to format. - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(string format, object arg0, object arg1, object arg2); - - /// - /// Appends the specified to the existing content after - /// replacing each format item with the representation of the + /// replacing each format item with the HTML encoded representation of the /// corresponding item in the array. /// /// @@ -116,63 +99,15 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The object array to format. /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(string format, params object[] args); + public TagHelperContent AppendFormat(string format, params object[] args) + { + HtmlContentBuilderExtensions.AppendFormat(this, null, format, args); + return this; + } /// /// Appends the specified to the existing content with information from the - /// after replacing the format item with the - /// representation of the corresponding item in . - /// - /// An object that supplies culture-specific formatting information. - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, object arg0); - - /// - /// Appends the specified to the existing content with information from the - /// after replacing each format item with the - /// representation of the corresponding item in and . - /// - /// An object that supplies culture-specific formatting information. - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - object arg0, - object arg1); - - /// - /// Appends the specified to the existing content with information from the - /// after replacing each format item with the - /// representation of the corresponding item in , - /// and . - /// - /// An object that supplies culture-specific formatting information. - /// - /// The composite format (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx). - /// - /// The object to format. - /// The object to format. - /// The object to format. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat( - IFormatProvider provider, - string format, - object arg0, - object arg1, - object arg2); - - /// - /// Appends the specified to the existing content with information from the - /// after replacing each format item with the + /// after replacing each format item with the HTML encoded /// representation of the corresponding item in the array. /// /// An object that supplies culture-specific formatting information. @@ -181,14 +116,11 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// /// The object array to format. /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args); - - /// - /// Appends to the existing content. - /// - /// The to be appended. - /// A reference to this instance after the append operation has completed. - public abstract TagHelperContent Append(IHtmlContent htmlContent); + public TagHelperContent AppendFormat(IFormatProvider provider, string format, params object[] args) + { + HtmlContentBuilderExtensions.AppendFormat(this, provider, format, args); + return this; + } /// /// Clears the content. @@ -211,5 +143,29 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers /// public abstract void WriteTo(TextWriter writer, IHtmlEncoder encoder); + + /// + IHtmlContentBuilder IHtmlContentBuilder.Append(IHtmlContent content) + { + return Append(content); + } + + /// + IHtmlContentBuilder IHtmlContentBuilder.Append(string unencoded) + { + return Append(unencoded); + } + + /// + IHtmlContentBuilder IHtmlContentBuilder.AppendEncoded(string encoded) + { + return AppendEncoded(encoded); + } + + /// + IHtmlContentBuilder IHtmlContentBuilder.Clear() + { + return Clear(); + } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs index 30e2f074fa..bdf2a1f00d 100644 --- a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs +++ b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/DefaultTagHelperContentTest.cs @@ -88,7 +88,6 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder())); } - // Overload with args array is called. [Fact] public void CanAppendFormatContent() { @@ -100,84 +99,10 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers // Assert Assert.Equal( - "HtmlEncode[[First Second Third Fourth!]]", + "HtmlEncode[[First]] HtmlEncode[[Second]] HtmlEncode[[Third]] HtmlEncode[[Fourth]]!", tagHelperContent.GetContent(new CommonTestEncoder())); } - - [Fact] - public void CanAppendFormatContent_With1Argument() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat("0x{0, -5:X} - hex equivalent for 50.", 50); - - // Assert - Assert.Equal( - "HtmlEncode[[0x32 - hex equivalent for 50.]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormatContent_With2Arguments() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat("0x{0, -5:X} - hex equivalent for {1}.", 50, 50); - - // Assert - Assert.Equal( - "HtmlEncode[[0x32 - hex equivalent for 50.]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormatContent_With3Arguments() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat("0x{0, -5:X} - {1} equivalent for {2}.", 50, "hex", 50); - - // Assert - Assert.Equal( - "HtmlEncode[[0x32 - hex equivalent for 50.]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormat_WithAlignmentComponent() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat("{0, -10} World!", "Hello"); - - // Assert - Assert.Equal( - "HtmlEncode[[Hello World!]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormat_WithFormatStringComponent() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat("0x{0:X}", 50); - - // Assert - Assert.Equal("HtmlEncode[[0x32]]", tagHelperContent.GetContent(new CommonTestEncoder())); - } - - // Overload with args array is called. + [Fact] public void CanAppendFormat_WithCulture() { @@ -187,7 +112,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers // Act tagHelperContent.AppendFormat( CultureInfo.InvariantCulture, - "Numbers in InvariantCulture - {0, -5:N} {1} {2} {3}!", + "Numbers in InvariantCulture - {0:N} {1} {2} {3}!", 1.1, 2.98, 145.82, @@ -195,107 +120,19 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers // Assert Assert.Equal( - "HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98 145.82 32.86!]]", + "Numbers in InvariantCulture - HtmlEncode[[1.10]] HtmlEncode[[2.98]] " + + "HtmlEncode[[145.82]] HtmlEncode[[32.86]]!", tagHelperContent.GetContent(new CommonTestEncoder())); } [Fact] - public void CanAppendFormat_WithCulture_1Argument() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat( - CultureInfo.InvariantCulture, - "Numbers in InvariantCulture - {0, -5:N}!", - 1.1); - - // Assert - Assert.Equal( - "HtmlEncode[[Numbers in InvariantCulture - 1.10 !]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormat_WithCulture_2Arguments() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat( - CultureInfo.InvariantCulture, - "Numbers in InvariantCulture - {0, -5:N} {1}!", - 1.1, - 2.98); - - // Assert - Assert.Equal( - "HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98!]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormat_WithCulture_3Arguments() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat( - CultureInfo.InvariantCulture, - "Numbers in InvariantCulture - {0, -5:N} {1} {2}!", - 1.1, - 2.98, - 3.12); - - // Assert - Assert.Equal( - "HtmlEncode[[Numbers in InvariantCulture - 1.10 2.98 3.12!]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - public void CanAppendFormat_WithDifferentCulture() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - var culture = new CultureInfo("fr-FR"); - - // Act - tagHelperContent.AppendFormat(culture, "{0} in french!", 1.21); - - // Assert - Assert.Equal( - "HtmlEncode[[1,21 in french!]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact] - [ReplaceCulture] - public void CanAppendFormat_WithDifferentCurrentCulture() - { - // Arrange - var tagHelperContent = new DefaultTagHelperContent(); - - // Act - tagHelperContent.AppendFormat(CultureInfo.CurrentCulture, "{0:D}", DateTime.Parse("01/02/2015")); - - // Assert - Assert.Equal( - "HtmlEncode[[01 February 2015]]", - tagHelperContent.GetContent(new CommonTestEncoder())); - } - - [Fact(Skip = "Blocked by #526")] public void CanAppendDefaultTagHelperContent() { // Arrange var tagHelperContent = new DefaultTagHelperContent(); var helloWorldContent = new DefaultTagHelperContent(); helloWorldContent.SetContent("HelloWorld"); - var expected = "HtmlEncode[[Content was HelloWorld]]"; + var expected = "Content was HtmlEncode[[HelloWorld]]"; // Act tagHelperContent.AppendFormat("Content was {0}", helloWorldContent); @@ -549,7 +386,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { // Arrange var tagHelperContent = new DefaultTagHelperContent(); - var expected = "HtmlEncode[[First ]]HtmlEncode[[Second Third]]"; + var expected = "HtmlEncode[[First ]]HtmlEncode[[Second]] Third"; // Act tagHelperContent.SetContent("First ").AppendFormat("{0} Third", "Second"); @@ -563,7 +400,7 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers { // Arrange var tagHelperContent = new DefaultTagHelperContent(); - var expected = "HtmlEncode[[First ]]HtmlEncode[[Second Third ]]HtmlEncode[[Fourth]]"; + var expected = "HtmlEncode[[First ]]HtmlEncode[[Second]] Third HtmlEncode[[Fourth]]"; // Act tagHelperContent