diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/RazorCodeDocumentExtensions.cs b/src/Microsoft.AspNetCore.Razor.Evolution/RazorCodeDocumentExtensions.cs
index ea2e4dce74..f12dc9a9ad 100644
--- a/src/Microsoft.AspNetCore.Razor.Evolution/RazorCodeDocumentExtensions.cs
+++ b/src/Microsoft.AspNetCore.Razor.Evolution/RazorCodeDocumentExtensions.cs
@@ -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)
diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperBinderSyntaxTreePass.cs b/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperBinderSyntaxTreePass.cs
index be0c732154..649b2dba4c 100644
--- a/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperBinderSyntaxTreePass.cs
+++ b/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperBinderSyntaxTreePass.cs
@@ -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)
diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperDocumentContext.cs b/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperDocumentContext.cs
new file mode 100644
index 0000000000..526e6e3222
--- /dev/null
+++ b/src/Microsoft.AspNetCore.Razor.Evolution/TagHelperDocumentContext.cs
@@ -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
+{
+ ///
+ /// The binding information for Tag Helpers resulted to a . Represents the
+ /// Tag Helper information after processing by directives.
+ ///
+ public abstract class TagHelperDocumentContext
+ {
+ public static TagHelperDocumentContext Create(string prefix, IEnumerable tagHelpers)
+ {
+ if (tagHelpers == null)
+ {
+ throw new ArgumentNullException(nameof(tagHelpers));
+ }
+
+ return new DefaultTagHelperDocumentContext(prefix, tagHelpers.ToArray());
+ }
+
+ public abstract string Prefix { get; }
+
+ public abstract IReadOnlyList 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 TagHelpers => _tagHelpers;
+ }
+ }
+}
diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/RazorCodeDocumentExtensionsTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/RazorCodeDocumentExtensionsTest.cs
index bf8aa8ae87..555ca10d2c 100644
--- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/RazorCodeDocumentExtensionsTest.cs
+++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/RazorCodeDocumentExtensionsTest.cs
@@ -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)]);
+ }
}
}
diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TagHelperBinderSyntaxTreePassTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TagHelperBinderSyntaxTreePassTest.cs
index 4edbc0f227..5299ca4b40 100644
--- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/TagHelperBinderSyntaxTreePassTest.cs
+++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/TagHelperBinderSyntaxTreePassTest.cs
@@ -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()
{