Added a copy constructor for tag helper descriptors. (#827)

This commit is contained in:
Crystal Qian 2016-08-31 15:38:35 -07:00 committed by GitHub
parent 0e1a54ef4f
commit d19845730f
3 changed files with 78 additions and 15 deletions

View File

@ -151,20 +151,10 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers
if (!string.IsNullOrEmpty(tagHelperPrefix))
{
return descriptors.Select(descriptor =>
new TagHelperDescriptor
{
Prefix = tagHelperPrefix,
TagName = descriptor.TagName,
TypeName = descriptor.TypeName,
AssemblyName = descriptor.AssemblyName,
Attributes = descriptor.Attributes,
RequiredAttributes = descriptor.RequiredAttributes,
AllowedChildren = descriptor.AllowedChildren,
RequiredParent = descriptor.RequiredParent,
TagStructure = descriptor.TagStructure,
DesignTimeDescriptor = descriptor.DesignTimeDescriptor
});
return descriptors.Select(descriptor => new TagHelperDescriptor(descriptor)
{
Prefix = tagHelperPrefix
});
}
return descriptors;

View File

@ -23,6 +23,36 @@ namespace Microsoft.AspNetCore.Razor.Compilation.TagHelpers
private IEnumerable<TagHelperRequiredAttributeDescriptor> _requiredAttributes =
Enumerable.Empty<TagHelperRequiredAttributeDescriptor>();
/// <summary>
/// Creates a new <see cref="TagHelperDescriptor"/>.
/// </summary>
public TagHelperDescriptor()
{
}
/// <summary>
/// Creates a shallow copy of the given <see cref="TagHelperDescriptor"/>.
/// </summary>
/// <param name="descriptor">The <see cref="TagHelperDescriptor"/> to copy.</param>
public TagHelperDescriptor (TagHelperDescriptor descriptor)
{
Prefix = descriptor.Prefix;
TagName = descriptor.TagName;
TypeName = descriptor.TypeName;
AssemblyName = descriptor.AssemblyName;
Attributes = descriptor.Attributes;
RequiredAttributes = descriptor.RequiredAttributes;
AllowedChildren = descriptor.AllowedChildren;
RequiredParent = descriptor.RequiredParent;
TagStructure = descriptor.TagStructure;
DesignTimeDescriptor = descriptor.DesignTimeDescriptor;
foreach (var property in descriptor.PropertyBag)
{
PropertyBag.Add(property.Key, property.Value);
}
}
/// <summary>
/// Text used as a required prefix when matching HTML start and end tags in the Razor source to available
/// tag helpers.
@ -219,4 +249,4 @@ namespace Microsoft.AspNetCore.Razor.Compilation.TagHelpers
}
}
}
}
}

View File

@ -12,6 +12,49 @@ namespace Microsoft.AspNetCore.Razor.Compilation.TagHelpers
{
public class TagHelperDescriptorTest
{
[Fact]
public void Constructor_CorrectlyCreatesCopy()
{
// Arrange
var descriptor = new TagHelperDescriptor
{
Prefix = "prefix",
TagName = "tag-name",
TypeName = "TypeName",
AssemblyName = "AsssemblyName",
Attributes = new List<TagHelperAttributeDescriptor>
{
new TagHelperAttributeDescriptor
{
Name = "test-attribute",
PropertyName = "TestAttribute",
TypeName = "string"
}
},
RequiredAttributes = new List<TagHelperRequiredAttributeDescriptor>
{
new TagHelperRequiredAttributeDescriptor
{
Name = "test-required-attribute"
}
},
AllowedChildren = new[] { "child" },
RequiredParent = "required parent",
TagStructure = TagStructure.NormalOrSelfClosing,
DesignTimeDescriptor = new TagHelperDesignTimeDescriptor()
};
descriptor.PropertyBag.Add("foo", "bar");
// Act
var copyDescriptor = new TagHelperDescriptor(descriptor);
// Assert
Assert.Equal(descriptor, copyDescriptor, CaseSensitiveTagHelperDescriptorComparer.Default);
Assert.Same(descriptor.Attributes, copyDescriptor.Attributes);
Assert.Same(descriptor.RequiredAttributes, copyDescriptor.RequiredAttributes);
}
[Fact]
public void TagHelperDescriptor_CanBeSerialized()
{