diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorDirectiveFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorDirectiveFeature.cs index 3e93506c72..9738743079 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorDirectiveFeature.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorDirectiveFeature.cs @@ -6,13 +6,13 @@ using System.Collections.Generic; namespace Microsoft.AspNetCore.Razor.Language { - internal class DefaultRazorDirectiveFeature : RazorEngineFeatureBase, IRazorDirectiveFeature, IRazorParserOptionsFeature + internal class DefaultRazorDirectiveFeature : RazorEngineFeatureBase, IRazorDirectiveFeature, IConfigureRazorParserOptionsFeature { public ICollection Directives { get; } = new List(); public int Order => 100; - void IRazorParserOptionsFeature.Configure(RazorParserOptionsBuilder options) + void IConfigureRazorParserOptionsFeature.Configure(RazorParserOptionsBuilder options) { if (options == null) { diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsBuilder.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsBuilder.cs index 6fd2a00f07..3b193030ee 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsBuilder.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsBuilder.cs @@ -8,7 +8,12 @@ namespace Microsoft.AspNetCore.Razor.Language { internal class DefaultRazorParserOptionsBuilder : RazorParserOptionsBuilder { - public override bool DesignTime { get; set; } + public DefaultRazorParserOptionsBuilder(bool designTime) + { + DesignTime = designTime; + } + + public override bool DesignTime { get; } public override ICollection Directives { get; } = new List(); diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsFeature.cs new file mode 100644 index 0000000000..c5df47dbd6 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParserOptionsFeature.cs @@ -0,0 +1,36 @@ +// 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.Linq; + +namespace Microsoft.AspNetCore.Razor.Language +{ + internal class DefaultRazorParserOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature + { + private readonly bool _designTime; + private IConfigureRazorParserOptionsFeature[] _configureOptions; + + public DefaultRazorParserOptionsFeature(bool designTime) + { + _designTime = designTime; + } + + protected override void OnInitialized() + { + _configureOptions = Engine.Features.OfType().ToArray(); + } + + public RazorParserOptions GetOptions() + { + var builder = new DefaultRazorParserOptionsBuilder(_designTime); + for (var i = 0; i < _configureOptions.Length; i++) + { + _configureOptions[i].Configure(builder); + } + + var options = builder.Build(); + + return options; + } + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParsingPhase.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParsingPhase.cs index 6f66cc538d..5a13e68760 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParsingPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorParsingPhase.cs @@ -1,29 +1,20 @@ // 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.Linq; - namespace Microsoft.AspNetCore.Razor.Language { internal class DefaultRazorParsingPhase : RazorEnginePhaseBase, IRazorParsingPhase { - private IRazorParserOptionsFeature[] _parserOptionsCallbacks; + private IRazorParserOptionsFeature _optionsFeature; protected override void OnIntialized() { - _parserOptionsCallbacks = Engine.Features.OfType().ToArray(); + _optionsFeature = GetRequiredFeature(); } protected override void ExecuteCore(RazorCodeDocument codeDocument) { - var builder = new DefaultRazorParserOptionsBuilder(); - for (var i = 0; i < _parserOptionsCallbacks.Length; i++) - { - _parserOptionsCallbacks[i].Configure(builder); - } - - var options = builder.Build(); - + var options = _optionsFeature.GetOptions(); var syntaxTree = RazorSyntaxTree.Parse(codeDocument.Source, options); codeDocument.SetSyntaxTree(syntaxTree); diff --git a/src/Microsoft.AspNetCore.Razor.Language/DesignTimeOptionsFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/DesignTimeOptionsFeature.cs index bdcbd3978b..2c989d8aba 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/DesignTimeOptionsFeature.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/DesignTimeOptionsFeature.cs @@ -5,20 +5,10 @@ using System; namespace Microsoft.AspNetCore.Razor.Language { - internal class DesignTimeOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature, IRazorCodeGenerationOptionsFeature + internal class DesignTimeOptionsFeature : RazorEngineFeatureBase, IRazorCodeGenerationOptionsFeature { public int Order { get; set; } - public void Configure(RazorParserOptionsBuilder options) - { - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } - - options.DesignTime = true; - } - public void Configure(RazorCodeGenerationOptionsBuilder options) { if (options == null) diff --git a/src/Microsoft.AspNetCore.Razor.Language/IConfigureRazorParserOptionsFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/IConfigureRazorParserOptionsFeature.cs new file mode 100644 index 0000000000..541243ca06 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/IConfigureRazorParserOptionsFeature.cs @@ -0,0 +1,12 @@ +// 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. + +namespace Microsoft.AspNetCore.Razor.Language +{ + public interface IConfigureRazorParserOptionsFeature : IRazorEngineFeature + { + int Order { get; } + + void Configure(RazorParserOptionsBuilder options); + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/IRazorParserOptionsFeature.cs b/src/Microsoft.AspNetCore.Razor.Language/IRazorParserOptionsFeature.cs index f96fdfd518..ec4b3f1ab1 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/IRazorParserOptionsFeature.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/IRazorParserOptionsFeature.cs @@ -5,8 +5,6 @@ namespace Microsoft.AspNetCore.Razor.Language { public interface IRazorParserOptionsFeature : IRazorEngineFeature { - int Order { get; } - - void Configure(RazorParserOptionsBuilder options); + RazorParserOptions GetOptions(); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorEngine.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorEngine.cs index a22e6ff02d..3e1f233120 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/RazorEngine.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorEngine.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Extensions; namespace Microsoft.AspNetCore.Razor.Language @@ -105,6 +104,9 @@ namespace Microsoft.AspNetCore.Razor.Language internal static void AddRuntimeDefaults(IRazorEngineBuilder builder) { + // Configure options + builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false)); + // Intermediate Node Passes builder.Features.Add(new PreallocatedTagHelperAttributeOptimizationPass()); @@ -116,6 +118,7 @@ namespace Microsoft.AspNetCore.Razor.Language internal static void AddDesignTimeDefaults(IRazorEngineBuilder builder) { // Configure options + builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: true)); builder.Features.Add(new DesignTimeOptionsFeature()); // Intermediate Node Passes diff --git a/src/Microsoft.AspNetCore.Razor.Language/RazorParserOptionsBuilder.cs b/src/Microsoft.AspNetCore.Razor.Language/RazorParserOptionsBuilder.cs index 183284c2dc..fc119ec2af 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/RazorParserOptionsBuilder.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/RazorParserOptionsBuilder.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Razor.Language { public abstract class RazorParserOptionsBuilder { - public abstract bool DesignTime { get; set; } + public abstract bool DesignTime { get; } public abstract ICollection Directives { get; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/DefaultRazorParsingPhaseTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/DefaultRazorParsingPhaseTest.cs index 7dbbfde435..0fc887fc06 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/DefaultRazorParsingPhaseTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/DefaultRazorParsingPhaseTest.cs @@ -12,7 +12,11 @@ namespace Microsoft.AspNetCore.Razor.Language { // Arrange var phase = new DefaultRazorParsingPhase(); - var engine = RazorEngine.CreateEmpty(b => b.Phases.Add(phase)); + var engine = RazorEngine.CreateEmpty(builder => + { + builder.Phases.Add(phase); + builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false)); + }); var codeDocument = TestRazorCodeDocument.CreateEmpty(); @@ -28,10 +32,11 @@ namespace Microsoft.AspNetCore.Razor.Language { // Arrange var phase = new DefaultRazorParsingPhase(); - var engine = RazorEngine.CreateEmpty((b) => + var engine = RazorEngine.CreateEmpty((builder) => { - b.Phases.Add(phase); - b.Features.Add(new MyParserOptionsFeature()); + builder.Phases.Add(phase); + builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false)); + builder.Features.Add(new MyParserOptionsFeature()); }); var codeDocument = TestRazorCodeDocument.CreateEmpty(); @@ -50,10 +55,12 @@ namespace Microsoft.AspNetCore.Razor.Language { // Arrange var phase = new DefaultRazorParsingPhase(); - var engine = RazorEngine.CreateEmpty((b) => + var engine = RazorEngine.CreateEmpty((builder) => { - b.Phases.Add(phase); - b.Features.Add(new MyParserOptionsFeature()); + builder.Phases.Add(phase); + builder.Features.Add(new DefaultRazorParserOptionsFeature(designTime: false)); + builder.Features.Add(new MyParserOptionsFeature()); + }); var imports = new[] @@ -74,7 +81,7 @@ namespace Microsoft.AspNetCore.Razor.Language t => { Assert.Same(t.Source, imports[1]); Assert.Equal("test", Assert.Single(t.Options.Directives).Directive); }); } - private class MyParserOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature + private class MyParserOptionsFeature : RazorEngineFeatureBase, IConfigureRazorParserOptionsFeature { public int Order { get; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorEngineTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorEngineTest.cs index 174715bdce..a7c1dbe787 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorEngineTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorEngineTest.cs @@ -160,6 +160,7 @@ namespace Microsoft.AspNetCore.Razor.Language feature => Assert.IsType(feature), feature => Assert.IsType(feature), feature => Assert.IsType(feature), + feature => Assert.IsType(feature), feature => Assert.IsType(feature)); } @@ -200,6 +201,7 @@ namespace Microsoft.AspNetCore.Razor.Language feature => Assert.IsType(feature), feature => Assert.IsType(feature), feature => Assert.IsType(feature), + feature => Assert.IsType(feature), feature => Assert.IsType(feature), feature => Assert.IsType(feature)); }