Create new way of suppressing unique Id generation

Adds a way of suppressing generation of guid ids that isn't based on a
global static.
This commit is contained in:
Ryan Nowak 2017-01-17 11:01:57 -08:00
parent 1b0379126b
commit d7527d2e0a
4 changed files with 20 additions and 17 deletions

View File

@ -12,18 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
{ {
internal class DefaultRazorRuntimeCSharpLoweringPhase : RazorCSharpLoweringPhaseBase internal class DefaultRazorRuntimeCSharpLoweringPhase : RazorCSharpLoweringPhaseBase
{ {
private static string _tagHelperId; internal static readonly object SuppressUniqueIds = "SuppressUniqueIds";
internal static string GenerateUniqueTagHelperId {
get
{
return _tagHelperId ?? Guid.NewGuid().ToString("N");
}
set
{
_tagHelperId = value;
}
}
protected override void ExecuteCore(RazorCodeDocument codeDocument) protected override void ExecuteCore(RazorCodeDocument codeDocument)
{ {
@ -39,6 +28,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution
SourceDocument = codeDocument.Source, SourceDocument = codeDocument.Source,
Options = syntaxTree.Options, Options = syntaxTree.Options,
}; };
var idValue = codeDocument.Items[SuppressUniqueIds];
if (idValue != null)
{
renderingContext.IdGenerator = () => idValue.ToString();
}
var visitor = new CSharpRenderer(renderingContext); var visitor = new CSharpRenderer(renderingContext);
visitor.VisitDocument(irDocument); visitor.VisitDocument(irDocument);
var csharpDocument = new RazorCSharpDocument() var csharpDocument = new RazorCSharpDocument()
@ -300,7 +296,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
.Write(".") .Write(".")
.Write(node.TagMode.ToString()) .Write(node.TagMode.ToString())
.WriteParameterSeparator() .WriteParameterSeparator()
.WriteStringLiteral(GenerateUniqueTagHelperId) .WriteStringLiteral(Context.IdGenerator())
.WriteParameterSeparator(); .WriteParameterSeparator();
// We remove and redirect writers so TagHelper authors can retrieve content. // We remove and redirect writers so TagHelper authors can retrieve content.

View File

@ -189,6 +189,8 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public ICollection<DirectiveDescriptor> Directives { get; set; } public ICollection<DirectiveDescriptor> Directives { get; set; }
public Func<string> IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N");
public List<LineMapping> LineMappings { get; } = new List<LineMapping>(); public List<LineMapping> LineMappings { get; } = new List<LineMapping>();
public CSharpCodeWriter Writer { get; set; } public CSharpCodeWriter Writer { get; set; }

View File

@ -1408,7 +1408,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
throw new XunitException($"The resource {sourceFilename} was not found."); throw new XunitException($"The resource {sourceFilename} was not found.");
} }
return RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename));
// This will ensure that we're not putting any randomly generated data in a baseline.
codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test";
return codeDocument;
} }
private void RunRuntimeTagHelpersTest(IEnumerable<TagHelperDescriptor> descriptors) private void RunRuntimeTagHelpersTest(IEnumerable<TagHelperDescriptor> descriptors)

View File

@ -88,10 +88,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
throw new XunitException($"The resource {sourceFilename} was not found."); throw new XunitException($"The resource {sourceFilename} was not found.");
} }
// This will ensure that we're not putting any randomly generated data in a baseline. var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename));
DefaultRazorRuntimeCSharpLoweringPhase.GenerateUniqueTagHelperId = "test";
return RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename)); // This will ensure that we're not putting any randomly generated data in a baseline.
codeDocument.Items[DefaultRazorRuntimeCSharpLoweringPhase.SuppressUniqueIds] = "test";
return codeDocument;
} }
protected void AssertIRMatchesBaseline(DocumentIRNode document) protected void AssertIRMatchesBaseline(DocumentIRNode document)