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

Commit

Permalink
Move TagHelperBinding out of Legacy.
Browse files Browse the repository at this point in the history
- Added additional properties to the class to make it more production ready.

#1092
  • Loading branch information
NTaylorMullen committed May 9, 2017
1 parent fd1f650 commit 9d9fca5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
25 changes: 0 additions & 25 deletions src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperBinding.cs

This file was deleted.

8 changes: 6 additions & 2 deletions src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Legacy;

namespace Microsoft.AspNetCore.Razor.Language
{
Expand Down Expand Up @@ -97,7 +96,12 @@ public TagHelperBinding GetBinding(
return null;
}

var tagMappingResult = new TagHelperBinding(applicableDescriptorMappings);
var tagMappingResult = new TagHelperBinding(
tagName,
attributes,
parentTagName,
applicableDescriptorMappings,
_tagHelperPrefix);

return tagMappingResult;
}
Expand Down
43 changes: 43 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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;
Descriptors = _mappings.Keys;
}

public IEnumerable<TagHelperDescriptor> Descriptors { get; }

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];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ namespace Microsoft.AspNetCore.Razor.Language
{
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
{
get
Expand Down Expand Up @@ -107,7 +136,7 @@ public static TheoryData RequiredAttributeData
.TagMatchingRule(rule =>
rule
.RequireTagName("input")
.RequireAttribute(attribute =>
.RequireAttribute(attribute =>
attribute
.Name("nodashprefix")
.NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch)))
Expand All @@ -129,7 +158,7 @@ public static TheoryData RequiredAttributeData
.TagMatchingRule(rule =>
rule
.RequireTagName(TagHelperMatchingConventions.ElementCatchAllName)
.RequireAttribute(attribute =>
.RequireAttribute(attribute =>
attribute
.Name("prefix-")
.NameComparisonMode(RequiredAttributeDescriptor.NameComparisonMode.PrefixMatch)))
Expand Down Expand Up @@ -406,7 +435,7 @@ public void GetDescriptors_DuplicateDescriptorsAreNotPartOfTagHelperDescriptorPo
// Arrange
var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
.TagMatchingRule(rule => rule.RequireTagName("div"))
.Build();
.Build();
var descriptors = new TagHelperDescriptor[] { divDescriptor, divDescriptor };
var tagHelperBinder = new TagHelperBinder(null, descriptors);

Expand Down

0 comments on commit 9d9fca5

Please sign in to comment.