forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the use of the Prompt property of DisplayAttribute for the plac…
…eholder attribute of input fields (addresses aspnet#3723)
- Loading branch information
Showing
11 changed files
with
165 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Rendering/HtmlHelperTextBoxTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Mvc.TestCommon; | ||
using System.ComponentModel.DataAnnotations; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Rendering | ||
{ | ||
public class HtmlHelperTextBoxTest | ||
{ | ||
private const string InputWithPlaceholderAttribute = @"<input id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" placeholder=""HtmlEncode[[placeholder]]"" type=""HtmlEncode[[{0}]]"" value="""" />"; | ||
private const string InputWithoutPlaceholderAttribute = @"<input id=""HtmlEncode[[Property1]]"" name=""HtmlEncode[[Property1]]"" type=""HtmlEncode[[{0}]]"" value="""" />"; | ||
|
||
[Theory] | ||
[InlineData("text")] | ||
[InlineData("search")] | ||
[InlineData("url")] | ||
[InlineData("tel")] | ||
[InlineData("email")] | ||
[InlineData("number")] | ||
public void TextBoxFor_GeneratesPlaceholderAttribute_WhenDisplayAttributePromptIsSetAndTypeIsValid(string type) | ||
{ | ||
// Arrange | ||
var model = new TextBoxModel(); | ||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model); | ||
|
||
// Act | ||
var result = helper.TextBoxFor(m => m.Property1, new { type }); | ||
|
||
// Assert | ||
Assert.Equal(string.Format(InputWithPlaceholderAttribute, type), HtmlContentUtilities.HtmlContentToString(result)); | ||
} | ||
|
||
[Theory] | ||
[InlineData("hidden")] | ||
[InlineData("date")] | ||
[InlineData("time")] | ||
[InlineData("range")] | ||
[InlineData("color")] | ||
[InlineData("checkbox")] | ||
[InlineData("radio")] | ||
[InlineData("submit")] | ||
[InlineData("reset")] | ||
[InlineData("button")] | ||
// Skipping these two because they won't match the constant string, | ||
// only because their 'value' attribute also does not get set. | ||
[InlineData("file")] | ||
[InlineData("image")] | ||
public void TextBoxFor_DoesNotGeneratePlaceholderAttribute_WhenDisplayAttributePromptIsSetAndTypeIsInvalid(string type) | ||
{ | ||
// Arrange | ||
var model = new TextBoxModel(); | ||
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model); | ||
|
||
// Act | ||
var result = helper.TextBoxFor(m => m.Property1, new { type }); | ||
|
||
// Assert | ||
Assert.Equal(string.Format(InputWithoutPlaceholderAttribute, type), HtmlContentUtilities.HtmlContentToString(result)); | ||
} | ||
|
||
private class TextBoxModel | ||
{ | ||
[Display(Prompt = "placeholder")] | ||
public string Property1 { get; set; } | ||
} | ||
} | ||
} |