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

Commit

Permalink
Add ContentBehaviorAttribute for TagHelpers.
Browse files Browse the repository at this point in the history
- Added detection of custom ContentBehaviors via the ContentBehaviorAttribute in the TagHelperDescriptorFactory.
- Updated some comments in the ContentBehavior enum.
- Add tests to validate custom content behavior resolution.

#122
  • Loading branch information
NTaylorMullen committed Sep 29, 2014
1 parent b7749f9 commit 8f15a16
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 System;
using Microsoft.AspNet.Razor.TagHelpers;

namespace Microsoft.AspNet.Razor.Runtime.TagHelpers
{
/// <summary>
/// Used to override <see cref="TagHelper"/>s functionality for when its
/// <see cref="TagHelper.Process(TagHelperOutput, TagHelperContext)"/> or
/// <see cref="TagHelper.ProcessAsync(TagHelperOutput, TagHelperContext)"/> is invoked with a
/// <see cref="TagHelperOutput"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class ContentBehaviorAttribute : Attribute
{
/// <summary>
/// Instantiates a new instance of the <see cref="ContentBehaviorAttribute"/> class.
/// </summary>
/// <param name="contentBehavior">The <see cref="Razor.TagHelpers.ContentBehavior"/> for the
/// <see cref="TagHelper"/> to use.</param>
public ContentBehaviorAttribute(ContentBehavior contentBehavior)
{
ContentBehavior = contentBehavior;
}

/// <summary>
/// A <see cref="Razor.TagHelpers.ContentBehavior"/> for the <see cref="TagHelper"/> to follow.
/// </summary>
public ContentBehavior ContentBehavior { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ private static TagHelperAttributeDescriptor ToTagHelperAttributeDescriptor(Prope
return new TagHelperAttributeDescriptor(property.Name, property);
}

// TODO: Make the content behavior pull from a ContentBehaviorAttribute: https://github.com/aspnet/Razor/issues/122
private static ContentBehavior GetContentBehavior(Type type)
{
return DefaultContentBehavior;
var typeInfo = type.GetTypeInfo();
var contentBehaviorAttribute = typeInfo.GetCustomAttribute<ContentBehaviorAttribute>(inherit: false);

return contentBehaviorAttribute?.ContentBehavior ?? DefaultContentBehavior;
}

private static bool IsValidTagHelperProperty(PropertyInfo property)
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.AspNet.Razor.Runtime/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
},
"frameworks": {
"net45": { },
"aspnetcore50": { }
"aspnetcore50": {
"dependencies": {
"System.Reflection.Extensions": "4.0.0.0"
}
}
}
}
14 changes: 7 additions & 7 deletions src/Microsoft.AspNet.Razor/TagHelpers/ContentBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Microsoft.AspNet.Razor.TagHelpers
public enum ContentBehavior
{
/// <summary>
/// Indicates that the tag helper will not modify its inner HTML in any way. This is the default
/// Indicates that a tag helper will not modify its content in any way. This is the default
/// <see cref="ContentBehavior"/>.
/// </summary>
/// <remarks>Children of the current tag helper will execute after the current tag helper.</remarks>
None,

/// <summary>
/// Indicates that the tag helper wants anything within its tag builder's inner HTML to be
/// appended to content its children generate.
/// Indicates that the tag helper wants anything within its outputs content to be appended to what its children
/// generate.
/// </summary>
/// <remarks>Children of the current tag helper will execute before the current tag helper.</remarks>
Append,
Expand All @@ -30,15 +30,15 @@ public enum ContentBehavior
Modify,

/// <summary>
/// Indicates that the tag helper wants anything within its tag builder's inner HTML to be
/// prepended to the content its children generate.
/// Indicates that the tag helper wants anything within its outputs content to be prepended to what its
/// children generate.
/// </summary>
/// <remarks>Children of the current tag helper will execute after the current tag helper.</remarks>
Prepend,

/// <summary>
/// Indicates that the tag helper wants anything within its tag builder's inner HTML to
/// replace any HTML inside of it.
/// Indicates that the tag helper wants anything within its outputs content to replace the HTML its children
/// generate.
/// </summary>
/// <remarks>Children of the current tag helper will not execute.</remarks>
Replace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,48 @@ public void DescriptorFactory_OnlyUnderstandsPropertiesWithPublicGetAndSet()
// Assert
Assert.Equal(descriptor, expectedDescriptor, DefaultDescriptorComparer);
}


[Fact]
public void DescriptorResolver_ResolvesCustomContentBehaviors()
{
// Arrange
var expectedDescriptor = new TagHelperDescriptor(
"CustomContentBehavior",
typeof(CustomContentBehaviorTagHelper).FullName,
ContentBehavior.Append);

// Act
var descriptor = TagHelperDescriptorFactory.CreateDescriptor(typeof(CustomContentBehaviorTagHelper));

// Assert
Assert.Equal(descriptor, expectedDescriptor, DefaultDescriptorComparer);
}

[Fact]
public void DescriptorResolver_DoesntResolveInheritedCustomContentBehaviors()
{
// Arrange
var expectedDescriptor = new TagHelperDescriptor(
"InheritedCustomContentBehavior",
typeof(InheritedCustomContentBehaviorTagHelper).FullName,
ContentBehavior.None);

// Act
var descriptor = TagHelperDescriptorFactory.CreateDescriptor(
typeof(InheritedCustomContentBehaviorTagHelper));

// Assert
Assert.Equal(descriptor, expectedDescriptor, DefaultDescriptorComparer);
}

[ContentBehavior(ContentBehavior.Append)]
private class CustomContentBehaviorTagHelper
{
}

private class InheritedCustomContentBehaviorTagHelper : CustomContentBehaviorTagHelper
{
}
}
}

0 comments on commit 8f15a16

Please sign in to comment.