Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Update to use Razor parser to parse page directive
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Jun 5, 2017
1 parent 17a74a4 commit 20c04d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,38 @@

using System;
using System.IO;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Razor.Language;

namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
public static class PageDirectiveFeature
{
private static readonly RazorEngine PageDirectiveEngine = RazorEngine.Create(builder =>
{
for (var i = builder.Phases.Count - 1; i >= 0; i--)
{
var phase = builder.Phases[i];
builder.Phases.RemoveAt(i);
if (phase is IRazorDocumentClassifierPhase)
{
break;
}
}

RazorExtensions.Register(builder);
builder.Features.Add(new PageDirectiveParserOptionsFeature());
});

public static bool TryGetPageDirective(RazorProjectItem projectItem, out string template)
{
if (projectItem == null)
{
throw new ArgumentNullException(nameof(projectItem));
}

return TryGetPageDirective(projectItem.Read, out template);
var sourceDocument = RazorSourceDocument.ReadFrom(projectItem);
return TryGetPageDirective(sourceDocument, out template);
}

public static bool TryGetPageDirective(Func<Stream> streamFactory, out string template)
Expand All @@ -26,38 +44,36 @@ public static bool TryGetPageDirective(Func<Stream> streamFactory, out string te
throw new ArgumentNullException(nameof(streamFactory));
}

const string PageDirective = "@page";

string content;
using (var streamReader = new StreamReader(streamFactory()))
using (var stream = streamFactory())
{
do
{
content = streamReader.ReadLine();

} while (content != null && string.IsNullOrWhiteSpace(content));
content = content?.Trim();
var sourceDocument = RazorSourceDocument.ReadFrom(stream, fileName: "Parse.cshtml");
return TryGetPageDirective(sourceDocument, out template);
}
}

private static bool TryGetPageDirective(RazorSourceDocument sourceDocument, out string template)
{
var codeDocument = RazorCodeDocument.Create(sourceDocument);
PageDirectiveEngine.Process(codeDocument);

if (content == null || !content.StartsWith(PageDirective, StringComparison.Ordinal))
if (PageDirective.TryGetPageDirective(codeDocument.GetIRDocument(), out var pageDirective))
{
template = null;
return false;
template = pageDirective.RouteTemplate;
return true;
}

template = content.Substring(PageDirective.Length, content.Length - PageDirective.Length).TrimStart();
template = null;
return false;
}

if (template.StartsWith("\"") && template.EndsWith("\""))
{
template = template.Substring(1, template.Length - 2);
}
// If it's not in quotes it's not our template
else
private class PageDirectiveParserOptionsFeature : RazorEngineFeatureBase, IRazorParserOptionsFeature
{
public int Order { get; }

public void Configure(RazorParserOptionsBuilder options)
{
template = string.Empty;
options.ParseOnlyLeadingDirectives = true;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ public void TryGetPageDirective_JunkBeforeDirective()
Assert.Null(template);
}

[Fact]
public void TryGetPageDirective_MultipleQuotes()
{
// Arrange
string template;
var projectItem = new TestRazorProjectItem(@"@page """"template""""");

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Equal(@"""template""", template);
}

[Theory]
[InlineData(@"""Some/Path/{value}")]
[InlineData(@"Some/Path/{value}""")]
Expand All @@ -71,10 +59,9 @@ public void TryGetPageDirective_RequiresBothQuotes(string inTemplate)

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Equal(string.Empty, template);
Assert.Null(template);
}


[Fact]
public void TryGetPageDirective_NoQuotesAroundPath_IsNotTemplate()
{
Expand All @@ -84,7 +71,7 @@ public void TryGetPageDirective_NoQuotesAroundPath_IsNotTemplate()

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Equal(string.Empty, template);
Assert.Null(template);
}

[Fact]
Expand Down Expand Up @@ -126,7 +113,7 @@ public void TryGetPageDirective_WhitespaceBeforeDirective()
Assert.Equal("Some/Path/{value}", template);
}

[Fact(Skip = "Re-evaluate this scenario after we use Razor to parse this stuff")]
[Fact]
public void TryGetPageDirective_JunkBeforeNewline()
{
// Arrange
Expand All @@ -136,7 +123,7 @@ public void TryGetPageDirective_JunkBeforeNewline()

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Empty(template);
Assert.Equal("Some/Path/{value}", template);
}

[Fact]
Expand All @@ -148,7 +135,7 @@ public void TryGetPageDirective_Directive_WithoutPathOrContent()

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Empty(template);
Assert.Null(template);
}

[Fact]
Expand All @@ -161,7 +148,7 @@ public void TryGetPageDirective_DirectiveWithContent_WithoutPath()

// Act & Assert
Assert.True(PageDirectiveFeature.TryGetPageDirective(projectItem, out template));
Assert.Empty(template);
Assert.Null(template);
}

[Fact]
Expand Down Expand Up @@ -236,21 +223,9 @@ public override bool Exists
}
}

public override string Path
{
get
{
throw new NotImplementedException();
}
}
public override string Path => "Test.cshtml";

public override string PhysicalPath
{
get
{
throw new NotImplementedException();
}
}
public override string PhysicalPath => null;

public override Stream Read()
{
Expand Down

0 comments on commit 20c04d0

Please sign in to comment.