Move TagHelperBinding out of Legacy.

- Added additional properties to the class to make it more production ready.

#1092
This commit is contained in:
N. Taylor Mullen 2017-05-08 11:49:28 -07:00
parent 01ec2202b2
commit 9fe7c77d30
4 changed files with 81 additions and 31 deletions

View File

@ -1,25 +0,0 @@
// 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 System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language.Legacy
{
public sealed class TagHelperBinding
{
public IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRule>> _mappings;
internal TagHelperBinding(IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRule>> mappings)
{
_mappings = mappings;
Descriptors = _mappings.Keys;
}
public IEnumerable<TagHelperDescriptor> Descriptors { get; }
public IEnumerable<TagMatchingRule> GetBoundRules(TagHelperDescriptor descriptor)
{
return _mappings[descriptor];
}
}
}

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Legacy;
namespace Microsoft.AspNetCore.Razor.Language namespace Microsoft.AspNetCore.Razor.Language
{ {
@ -97,9 +96,14 @@ namespace Microsoft.AspNetCore.Razor.Language
return null; return null;
} }
var tagMappingResult = new TagHelperBinding(applicableDescriptorMappings); var tagHelperBinding = new TagHelperBinding(
tagName,
attributes,
parentTagName,
applicableDescriptorMappings,
_tagHelperPrefix);
return tagMappingResult; return tagHelperBinding;
} }
private void Register(TagHelperDescriptor descriptor) private void Register(TagHelperDescriptor descriptor)

View File

@ -0,0 +1,42 @@
// 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 System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language
{
public sealed class TagHelperBinding
{
private IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRule>> _mappings;
internal TagHelperBinding(
string tagName,
IEnumerable<KeyValuePair<string, string>> attributes,
string parentTagName,
IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRule>> mappings,
string tagHelperPrefix)
{
TagName = tagName;
Attributes = attributes;
ParentTagName = parentTagName;
TagHelperPrefix = tagHelperPrefix;
_mappings = mappings;
}
public IEnumerable<TagHelperDescriptor> Descriptors => _mappings.Keys;
public string TagName { get; }
public string ParentTagName { get; }
public IEnumerable<KeyValuePair<string, string>> Attributes { get; }
public string TagHelperPrefix { get; }
public IEnumerable<TagMatchingRule> GetBoundRules(TagHelperDescriptor descriptor)
{
return _mappings[descriptor];
}
}
}

View File

@ -10,6 +10,35 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
public class TagHelperBinderTest public class TagHelperBinderTest
{ {
[Fact]
public void GetBinding_ReturnsBindingWithInformation()
{
// Arrange
var divTagHelper = TagHelperDescriptorBuilder.Create("DivTagHelper", "SomeAssembly")
.TagMatchingRule(rule => rule.RequireTagName("div"))
.Build();
var expectedDescriptors = new[] { divTagHelper };
var expectedAttributes = new[]
{
new KeyValuePair<string, string>("class", "something")
};
var tagHelperBinder = new TagHelperBinder("th:", expectedDescriptors);
// Act
var bindingResult = tagHelperBinder.GetBinding(
tagName: "th:div",
attributes: expectedAttributes,
parentTagName: "body");
// Assert
Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.CaseSensitive);
Assert.Equal("th:div", bindingResult.TagName);
Assert.Equal("body", bindingResult.ParentTagName);
Assert.Equal(expectedAttributes, bindingResult.Attributes);
Assert.Equal("th:", bindingResult.TagHelperPrefix);
Assert.Equal(divTagHelper.TagMatchingRules, bindingResult.GetBoundRules(divTagHelper), TagMatchingRuleComparer.CaseSensitive);
}
public static TheoryData RequiredParentData public static TheoryData RequiredParentData
{ {
get get
@ -107,7 +136,7 @@ namespace Microsoft.AspNetCore.Razor.Language
.TagMatchingRule(rule => .TagMatchingRule(rule =>
rule rule
.RequireTagName("input") .RequireTagName("input")
.RequireAttribute(attribute => .RequireAttribute(attribute =>
attribute attribute
.Name("nodashprefix") .Name("nodashprefix")
.NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch))) .NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch)))
@ -129,7 +158,7 @@ namespace Microsoft.AspNetCore.Razor.Language
.TagMatchingRule(rule => .TagMatchingRule(rule =>
rule rule
.RequireTagName(TagHelperMatchingConventions.ElementCatchAllName) .RequireTagName(TagHelperMatchingConventions.ElementCatchAllName)
.RequireAttribute(attribute => .RequireAttribute(attribute =>
attribute attribute
.Name("prefix-") .Name("prefix-")
.NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch))) .NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch)))
@ -406,7 +435,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange // Arrange
var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly") var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
.TagMatchingRule(rule => rule.RequireTagName("div")) .TagMatchingRule(rule => rule.RequireTagName("div"))
.Build(); .Build();
var descriptors = new TagHelperDescriptor[] { divDescriptor, divDescriptor }; var descriptors = new TagHelperDescriptor[] { divDescriptor, divDescriptor };
var tagHelperBinder = new TagHelperBinder(null, descriptors); var tagHelperBinder = new TagHelperBinder(null, descriptors);