Move TagHelperBinding out of Legacy.
- Added additional properties to the class to make it more production ready. #1092
This commit is contained in:
parent
01ec2202b2
commit
9fe7c77d30
|
|
@ -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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue