Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit a852352

Browse files
Medeni Baykaldougbu
authored andcommitted
Fix for #4903. (#4907)
Fix for #4903. - `HtmlHelperTextAreaTest` added - fix up `HtmlHelperTextBoxTest`
1 parent e51a118 commit a852352

File tree

3 files changed

+88
-13
lines changed

3 files changed

+88
-13
lines changed

src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewFeatures/DefaultHtmlGenerator.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ public virtual TagBuilder GenerateTextArea(
662662
}
663663

664664
tagBuilder.MergeAttribute("name", fullName, true);
665+
666+
AddPlaceholderAttribute(viewContext.ViewData, tagBuilder, modelExplorer, expression);
665667
AddValidationAttributes(viewContext, tagBuilder, modelExplorer, expression);
666668

667669
// If there are any errors for a named field, we add this CSS attribute.
@@ -1170,12 +1172,7 @@ protected virtual TagBuilder GenerateInput(
11701172
var suppliedTypeString = tagBuilder.Attributes["type"];
11711173
if (_placeholderInputTypes.Contains(suppliedTypeString))
11721174
{
1173-
modelExplorer = modelExplorer ?? ExpressionMetadataProvider.FromStringExpression(expression, viewContext.ViewData, _metadataProvider);
1174-
var placeholder = modelExplorer.Metadata.Placeholder;
1175-
if (!string.IsNullOrEmpty(placeholder))
1176-
{
1177-
tagBuilder.MergeAttribute("placeholder", placeholder);
1178-
}
1175+
AddPlaceholderAttribute(viewContext.ViewData, tagBuilder, modelExplorer, expression);
11791176
}
11801177

11811178
var valueParameter = FormatValue(value, format);
@@ -1288,6 +1285,27 @@ protected virtual TagBuilder GenerateLink(
12881285
return tagBuilder;
12891286
}
12901287

1288+
/// <summary>
1289+
/// Adds a placeholder attribute to the <paramref name="tagBuilder" />.
1290+
/// </summary>
1291+
/// <param name="viewData">A <see cref="ViewDataDictionary"/> instance for the current scope.</param>
1292+
/// <param name="tagBuilder">A <see cref="TagBuilder"/> instance.</param>
1293+
/// <param name="modelExplorer">The <see cref="ModelExplorer"/> for the <paramref name="expression"/>.</param>
1294+
/// <param name="expression">Expression name, relative to the current model.</param>
1295+
protected virtual void AddPlaceholderAttribute(
1296+
ViewDataDictionary viewData,
1297+
TagBuilder tagBuilder,
1298+
ModelExplorer modelExplorer,
1299+
string expression)
1300+
{
1301+
modelExplorer = modelExplorer ?? ExpressionMetadataProvider.FromStringExpression(expression, viewData, _metadataProvider);
1302+
var placeholder = modelExplorer.Metadata.Placeholder;
1303+
if (!string.IsNullOrEmpty(placeholder))
1304+
{
1305+
tagBuilder.MergeAttribute("placeholder", placeholder);
1306+
}
1307+
}
1308+
12911309
/// <summary>
12921310
/// Adds validation attributes to the <paramref name="tagBuilder" /> if client validation
12931311
/// is enabled.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.ComponentModel.DataAnnotations;
6+
using Microsoft.AspNetCore.Mvc.TestCommon;
7+
using Xunit;
8+
9+
namespace Microsoft.AspNetCore.Mvc.Rendering
10+
{
11+
public class HtmlHelperTextBoxAreaTest
12+
{
13+
[Fact]
14+
public void TextAreaFor_GeneratesPlaceholderAttribute_WhenDisplayAttributePromptIsSetAndTypeIsValid()
15+
{
16+
// Arrange
17+
var model = new TextAreaModelWithAPlaceholder();
18+
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
19+
20+
// Act
21+
var textArea = helper.TextAreaFor(m => m.Property1);
22+
23+
// Assert
24+
var result = HtmlContentUtilities.HtmlContentToString(textArea);
25+
Assert.Contains(@"placeholder=""HtmlEncode[[placeholder]]""", result, StringComparison.Ordinal);
26+
}
27+
28+
[Fact]
29+
public void TextAreaFor_DoesNotGeneratePlaceholderAttribute_WhenNoPlaceholderPresentInModel()
30+
{
31+
// Arrange
32+
var model = new TextAreaModelWithoutAPlaceholder();
33+
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
34+
35+
// Act
36+
var textArea = helper.TextAreaFor(m => m.Property1);
37+
38+
// Assert
39+
var result = HtmlContentUtilities.HtmlContentToString(textArea);
40+
Assert.DoesNotContain(@"placeholder=""HtmlEncode[[placeholder]]""", result, StringComparison.Ordinal);
41+
}
42+
43+
private class TextAreaModelWithAPlaceholder
44+
{
45+
[Display(Prompt = "placeholder")]
46+
public string Property1 { get; set; }
47+
}
48+
49+
private class TextAreaModelWithoutAPlaceholder
50+
{
51+
public string Property1 { get; set; }
52+
}
53+
}
54+
}

test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextBoxTest.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Microsoft.AspNetCore.Mvc.TestCommon;
4+
using System;
55
using System.ComponentModel.DataAnnotations;
6+
using Microsoft.AspNetCore.Mvc.TestCommon;
67
using Xunit;
78

89
namespace Microsoft.AspNetCore.Mvc.Rendering
@@ -23,10 +24,11 @@ public void TextBoxFor_GeneratesPlaceholderAttribute_WhenDisplayAttributePromptI
2324
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
2425

2526
// Act
26-
var result = HtmlContentUtilities.HtmlContentToString(helper.TextBoxFor(m => m.Property1, new { type }));
27+
var textBox = helper.TextBoxFor(m => m.Property1, new { type });
2728

28-
// Assert
29-
Assert.True(result.Contains(@"placeholder=""HtmlEncode[[placeholder]]"""));
29+
// Assert
30+
var result = HtmlContentUtilities.HtmlContentToString(textBox);
31+
Assert.Contains(@"placeholder=""HtmlEncode[[placeholder]]""", result, StringComparison.Ordinal);
3032
}
3133

3234
[Theory]
@@ -49,10 +51,11 @@ public void TextBoxFor_DoesNotGeneratePlaceholderAttribute_WhenDisplayAttributeP
4951
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
5052

5153
// Act
52-
var result = HtmlContentUtilities.HtmlContentToString(helper.TextBoxFor(m => m.Property1, new { type }));
54+
var textBox = helper.TextBoxFor(m => m.Property1, new { type });
5355

54-
// Assert
55-
Assert.False(result.Contains(@"placeholder=""HtmlEncode[[placeholder]]"""));
56+
// Assert
57+
var result = HtmlContentUtilities.HtmlContentToString(textBox);
58+
Assert.DoesNotContain(@"placeholder=""HtmlEncode[[placeholder]]""", result, StringComparison.Ordinal);
5659
}
5760

5861
private class TextBoxModel

0 commit comments

Comments
 (0)