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

Commit

Permalink
Enable TagHelper directives to handle malformed text.
Browse files Browse the repository at this point in the history
- Prior to this change we'd try to substring a TagHelper directive with length 1 but our substring call would be for -1 (explosions).
- Added tests to validate that `@tagHelperPrefix`, `@removeTagHelper` and `@addTagHelper` all behave properly when they have malformed quotes.

#1242
  • Loading branch information
NTaylorMullen committed Apr 20, 2017
1 parent e856224 commit 9bde4e3
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ private TagHelperDirectiveDescriptor CreateDirective(
TagHelperDirectiveType directiveType)
{
directiveText = directiveText.Trim();
if (directiveText.StartsWith("\"", StringComparison.Ordinal) &&
if (directiveText.Length >= 2 &&
directiveText.StartsWith("\"", StringComparison.Ordinal) &&
directiveText.EndsWith("\"", StringComparison.Ordinal))
{
directiveText = directiveText.Substring(1, directiveText.Length - 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,132 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public class TagHelperBinderSyntaxTreePassTest
{
[Fact]
public void Execute_CanHandleSingleLengthAddTagHelperDirective()
{
// Arrange
var engine = RazorEngine.Create(builder =>
{
builder.AddTagHelpers(new TagHelperDescriptor[0]);
});

var pass = new TagHelperBinderSyntaxTreePass()
{
Engine = engine,
};
var expectedDiagnostics = new[]
{
RazorDiagnostic.Create(
new RazorError(
LegacyResources.ParseError_Unterminated_String_Literal,
new SourceLocation(16, 1, 14),
length: 1)),
RazorDiagnostic.Create(
new RazorError(
Resources.FormatInvalidTagHelperLookupText("\""),
new SourceLocation(16, 1, 14),
length: 1))
};


var content =
@"
@addTagHelper """;
var sourceDocument = TestRazorSourceDocument.Create(content, fileName: null);
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var originalTree = RazorSyntaxTree.Parse(sourceDocument);

// Act
var rewrittenTree = pass.Execute(codeDocument, originalTree);

// Assert
Assert.Equal(expectedDiagnostics, rewrittenTree.Diagnostics);
}

[Fact]
public void Execute_CanHandleSingleLengthRemoveTagHelperDirective()
{
// Arrange
var engine = RazorEngine.Create(builder =>
{
builder.AddTagHelpers(new TagHelperDescriptor[0]);
});

var pass = new TagHelperBinderSyntaxTreePass()
{
Engine = engine,
};
var expectedDiagnostics = new[]
{
RazorDiagnostic.Create(
new RazorError(
LegacyResources.ParseError_Unterminated_String_Literal,
new SourceLocation(19, 1, 17),
length: 1)),
RazorDiagnostic.Create(
new RazorError(
Resources.FormatInvalidTagHelperLookupText("\""),
new SourceLocation(19, 1, 17),
length: 1))
};


var content =
@"
@removeTagHelper """;
var sourceDocument = TestRazorSourceDocument.Create(content, fileName: null);
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var originalTree = RazorSyntaxTree.Parse(sourceDocument);

// Act
var rewrittenTree = pass.Execute(codeDocument, originalTree);

// Assert
Assert.Equal(expectedDiagnostics, rewrittenTree.Diagnostics);
}

[Fact]
public void Execute_CanHandleSingleLengthTagHelperPrefix()
{
// Arrange
var engine = RazorEngine.Create(builder =>
{
builder.AddTagHelpers(new TagHelperDescriptor[0]);
});

var pass = new TagHelperBinderSyntaxTreePass()
{
Engine = engine,
};
var expectedDiagnostics = new[]
{
RazorDiagnostic.Create(
new RazorError(
LegacyResources.ParseError_Unterminated_String_Literal,
new SourceLocation(19, 1, 17),
length: 1)),
RazorDiagnostic.Create(
new RazorError(
Resources.FormatInvalidTagHelperPrefixValue("tagHelperPrefix", "\"", "\""),
new SourceLocation(19, 1, 17),
length: 1))
};


var content =
@"
@tagHelperPrefix """;
var sourceDocument = TestRazorSourceDocument.Create(content, fileName: null);
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var originalTree = RazorSyntaxTree.Parse(sourceDocument);

// Act
var rewrittenTree = pass.Execute(codeDocument, originalTree);

// Assert
Assert.Equal(expectedDiagnostics, rewrittenTree.Diagnostics);
}

[Fact]
public void Execute_RewritesTagHelpers()
{
Expand Down

0 comments on commit 9bde4e3

Please sign in to comment.