Add TagHelper directive and match info to CodeDocument
This commit is contained in:
parent
feb5f395d2
commit
bbd08f0cc9
|
|
@ -11,6 +11,26 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
{
|
||||
private static object TagHelperPrefixKey = new object();
|
||||
|
||||
public static TagHelperDocumentContext GetTagHelperContext(this RazorCodeDocument document)
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
|
||||
return (TagHelperDocumentContext)document.Items[typeof(TagHelperDocumentContext)];
|
||||
}
|
||||
|
||||
public static void SetTagHelperContext(this RazorCodeDocument document, TagHelperDocumentContext context)
|
||||
{
|
||||
if (document == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
|
||||
document.Items[typeof(TagHelperDocumentContext)] = context;
|
||||
}
|
||||
|
||||
public static string GetTagHelperPrefix(this RazorCodeDocument document)
|
||||
{
|
||||
if (document == null)
|
||||
|
|
|
|||
|
|
@ -55,6 +55,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
var tagHelperPrefix = ProcessTagHelperPrefix(directives, codeDocument, errorSink);
|
||||
var root = syntaxTree.Root;
|
||||
|
||||
var context = TagHelperDocumentContext.Create(tagHelperPrefix, descriptors);
|
||||
codeDocument.SetTagHelperContext(context);
|
||||
|
||||
if (descriptors.Count == 0)
|
||||
{
|
||||
if (errorSink.Errors.Count == 0 && errorList.Count == 0)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Evolution
|
||||
{
|
||||
/// <summary>
|
||||
/// The binding information for Tag Helpers resulted to a <see cref="RazorCodeDocument"/>. Represents the
|
||||
/// Tag Helper information after processing by directives.
|
||||
/// </summary>
|
||||
public abstract class TagHelperDocumentContext
|
||||
{
|
||||
public static TagHelperDocumentContext Create(string prefix, IEnumerable<TagHelperDescriptor> tagHelpers)
|
||||
{
|
||||
if (tagHelpers == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tagHelpers));
|
||||
}
|
||||
|
||||
return new DefaultTagHelperDocumentContext(prefix, tagHelpers.ToArray());
|
||||
}
|
||||
|
||||
public abstract string Prefix { get; }
|
||||
|
||||
public abstract IReadOnlyList<TagHelperDescriptor> TagHelpers { get; }
|
||||
|
||||
private class DefaultTagHelperDocumentContext : TagHelperDocumentContext
|
||||
{
|
||||
private readonly string _prefix;
|
||||
private readonly TagHelperDescriptor[] _tagHelpers;
|
||||
|
||||
public DefaultTagHelperDocumentContext(string prefix, TagHelperDescriptor[] tagHelpers)
|
||||
{
|
||||
_prefix = prefix;
|
||||
_tagHelpers = tagHelpers;
|
||||
}
|
||||
|
||||
public override string Prefix => _prefix;
|
||||
|
||||
public override IReadOnlyList<TagHelperDescriptor> TagHelpers => _tagHelpers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -116,5 +116,36 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
// Assert
|
||||
Assert.Same(expected, codeDocument.Items[typeof(RazorCSharpDocument)]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetTagHelperContext_ReturnsTagHelperContext()
|
||||
{
|
||||
// Arrange
|
||||
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
||||
|
||||
var expected = TagHelperDocumentContext.Create(null, new TagHelperDescriptor[0]);
|
||||
codeDocument.Items[typeof(TagHelperDocumentContext)] = expected;
|
||||
|
||||
// Act
|
||||
var actual = codeDocument.GetTagHelperContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(expected, actual);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetTagHelperContext_SetsTagHelperContext()
|
||||
{
|
||||
// Arrange
|
||||
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
||||
|
||||
var expected = TagHelperDocumentContext.Create(null, new TagHelperDescriptor[0]);
|
||||
|
||||
// Act
|
||||
codeDocument.SetTagHelperContext(expected);
|
||||
|
||||
// Assert
|
||||
Assert.Same(expected, codeDocument.Items[typeof(TagHelperDocumentContext)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -228,6 +228,34 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
Assert.Same(originalTree, outputTree);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Execute_SetsTagHelperDocumentContext()
|
||||
{
|
||||
// Arrange
|
||||
var engine = RazorEngine.Create(builder =>
|
||||
{
|
||||
builder.Features.Add(new TestTagHelperFeature());
|
||||
});
|
||||
|
||||
var pass = new TagHelperBinderSyntaxTreePass()
|
||||
{
|
||||
Engine = engine,
|
||||
};
|
||||
|
||||
// No taghelper directives here so nothing is resolved.
|
||||
var sourceDocument = TestRazorSourceDocument.Create("Hello, world");
|
||||
var codeDocument = RazorCodeDocument.Create(sourceDocument);
|
||||
var originalTree = RazorSyntaxTree.Parse(sourceDocument);
|
||||
|
||||
// Act
|
||||
var outputTree = pass.Execute(codeDocument, originalTree);
|
||||
|
||||
// Assert
|
||||
var context = codeDocument.GetTagHelperContext();
|
||||
Assert.Null(context.Prefix);
|
||||
Assert.Empty(context.TagHelpers);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Execute_AddsErrorWhenNoTagHelpersAreFoundInAssembly()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue