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
{
private static string _tagHelperId;
internal static string GenerateUniqueTagHelperId {
get
{
return _tagHelperId ?? Guid.NewGuid().ToString("N");
}
set
{
_tagHelperId = value;
}
}
internal static readonly object SuppressUniqueIds = "SuppressUniqueIds";
protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
@ -39,6 +28,13 @@ namespace Microsoft.AspNetCore.Razor.Evolution
SourceDocument = codeDocument.Source,
Options = syntaxTree.Options,
};
var idValue = codeDocument.Items[SuppressUniqueIds];
if (idValue != null)
{
renderingContext.IdGenerator = () => idValue.ToString();
}
var visitor = new CSharpRenderer(renderingContext);
visitor.VisitDocument(irDocument);
var csharpDocument = new RazorCSharpDocument()
@ -300,7 +296,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
.Write(".")
.Write(node.TagMode.ToString())
.WriteParameterSeparator()
.WriteStringLiteral(GenerateUniqueTagHelperId)
.WriteStringLiteral(Context.IdGenerator())
.WriteParameterSeparator();
// 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 Func<string> IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N");
public List<LineMapping> LineMappings { get; } = new List<LineMapping>();
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.");
}
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)

View File

@ -88,10 +88,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
throw new XunitException($"The resource {sourceFilename} was not found.");
}
// This will ensure that we're not putting any randomly generated data in a baseline.
DefaultRazorRuntimeCSharpLoweringPhase.GenerateUniqueTagHelperId = "test";
var codeDocument = RazorCodeDocument.Create(TestRazorSourceDocument.CreateResource(sourceFilename));
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)