added activation tests for tag helpers
This commit is contained in:
parent
63940bee87
commit
191af4a5a9
|
|
@ -172,5 +172,28 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
Assert.Equal(typeof(InvalidOperationException).FullName, exception.ExceptionType);
|
||||
Assert.Equal(expectedMessage, exception.ExceptionMessage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TagHelperActivation_ActivateHtmlHelper_RendersProperly()
|
||||
{
|
||||
// Arrange
|
||||
var server = TestServer.Create(_provider, _app);
|
||||
var client = server.CreateClient();
|
||||
var expected = "<body><h2>Activation Test</h2>" +
|
||||
Environment.NewLine +
|
||||
"<div>FakeFakeFake</div>" +
|
||||
Environment.NewLine +
|
||||
"<span>" +
|
||||
"<input id=\"foo\" name=\"foo\" type=\"hidden\" value=\"test content\" />" +
|
||||
"</span>" +
|
||||
Environment.NewLine +
|
||||
"</body>";
|
||||
|
||||
// Act
|
||||
var body = await client.GetStringAsync("http://localhost/View/UseTagHelper");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, body.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using ActivatorWebSite.Models;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
|
||||
namespace ActivatorWebSite
|
||||
|
|
@ -44,5 +45,14 @@ namespace ActivatorWebSite
|
|||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public ViewResult UseTagHelper()
|
||||
{
|
||||
var item = new Item
|
||||
{
|
||||
Name = "Fake"
|
||||
};
|
||||
return View(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace ActivatorWebSite.Models
|
||||
{
|
||||
public class Item
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Razor.TagHelpers;
|
||||
|
||||
namespace ActivatorWebSite.TagHelpers
|
||||
{
|
||||
[HtmlElementName("span")]
|
||||
[ContentBehavior(ContentBehavior.Modify)]
|
||||
public class HiddenTagHelper : TagHelper
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
[Activate]
|
||||
public IHtmlHelper HtmlHelper { get; set; }
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
output.Content = HtmlHelper.Hidden(Name, output.Content).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Razor.TagHelpers;
|
||||
|
||||
namespace ActivatorWebSite.TagHelpers
|
||||
{
|
||||
[HtmlElementName("div")]
|
||||
[ContentBehavior(ContentBehavior.Modify)]
|
||||
public class RepeatContentTagHelper : TagHelper
|
||||
{
|
||||
public int RepeatContent { get; set; }
|
||||
|
||||
public ModelExpression Expression { get; set; }
|
||||
|
||||
[Activate]
|
||||
public IHtmlHelper HtmlHelper { get; set; }
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var repeatContent = HtmlHelper.Encode(Expression.Metadata.Model.ToString());
|
||||
|
||||
if (string.IsNullOrEmpty(repeatContent))
|
||||
{
|
||||
repeatContent = output.Content;
|
||||
output.Content = string.Empty;
|
||||
}
|
||||
|
||||
for (int i = 0; i < RepeatContent; i++)
|
||||
{
|
||||
output.Content += repeatContent;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
using Microsoft.AspNet.Razor.TagHelpers;
|
||||
|
||||
namespace ActivatorWebSite.TagHelpers
|
||||
{
|
||||
[HtmlElementName("body")]
|
||||
[ContentBehavior(ContentBehavior.Prepend)]
|
||||
public class TitleTagHelper : TagHelper
|
||||
{
|
||||
[Activate]
|
||||
public IHtmlHelper HtmlHelper { get; set; }
|
||||
|
||||
[Activate]
|
||||
public ViewContext ViewContext { get; set; }
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var builder = new TagBuilder("h2");
|
||||
var title = ViewContext.ViewBag.Title;
|
||||
builder.InnerHtml = HtmlHelper.Encode(title);
|
||||
output.Content = builder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
@using ActivatorWebSite.Models
|
||||
@model Item
|
||||
|
||||
@addtaghelper "ActivatorWebSite"
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Activation Test";
|
||||
}
|
||||
|
||||
<body>
|
||||
<div expression="Name" repeatcontent="3"></div>
|
||||
<span name="foo">test content</span>
|
||||
</body>
|
||||
Loading…
Reference in New Issue