Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attribute parsing recovery #10620

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public static TheoryData GetAttributeNameValuePairsData
return new TheoryData<string, IEnumerable<KeyValuePair<string, string>>>
{
{ "<a>", empty },
{ "<a @{ } href='~/home'>", empty },
{ "<a @{ } href='~/home'>", new[] { kvp("href", "~/home") } },
{ "<a href=\"@true\">", new[] { kvp("href", csharp) } },
{ "<a href=\"prefix @true suffix\">", new[] { kvp("href", $"prefix{csharp} suffix") } },
{ "<a href=~/home>", new[] { kvp("href", "~/home") } },
{ "<a href=~/home @{ } nothing='something'>", new[] { kvp("href", "~/home") } },
{ "<a href=~/home @{ } nothing='something'>", new[] { kvp("href", "~/home"), kvp("nothing", "something") } },
{
"<a href=\"@DateTime.Now::0\" class='btn btn-success' random>",
new[] { kvp("href", $"{csharp}::0"), kvp("class", "btn btn-success"), kvp("random", "") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
MarkupStartTag - [0..26)::26 - [<text @* razor comment *@>] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[text];
MarkupMiscAttributeContent - [5..25)::20
MarkupTextLiteral - [5..6)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
RazorComment - [6..25)::19
RazorCommentTransition;[@];
RazorCommentStar;[*];
RazorCommentLiteral;[ razor comment ];
RazorCommentStar;[*];
RazorCommentTransition;[@];
MarkupTextLiteral - [5..6)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
RazorComment - [6..25)::19
RazorCommentTransition;[@];
RazorCommentStar;[*];
RazorCommentLiteral;[ razor comment ];
RazorCommentStar;[*];
RazorCommentTransition;[@];
CloseAngle;[>];
MarkupEndTag - [26..33)::7 - [</text>] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
MarkupStartTag - [2..14)::12 - [<foo @bar />] - Gen<Markup> - SpanEditHandler;Accepts:None
OpenAngle;[<];
Text;[foo];
MarkupMiscAttributeContent - [6..12)::6
MarkupMiscAttributeContent - [6..11)::5
MarkupTextLiteral - [6..7)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [7..11)::4
Expand All @@ -26,6 +26,7 @@
CSharpCodeBlock - [8..11)::3
CSharpExpressionLiteral - [8..11)::3 - [bar] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:NonWhitespace;ImplicitExpression[RTD];K14
Identifier;[bar];
MarkupMiscAttributeContent - [11..12)::1
MarkupTextLiteral - [11..12)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
ForwardSlash;[/];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MarkupStartTag - [0..25)::25 - [<input @onclick="@test"/>] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[input];
MarkupMiscAttributeContent - [6..23)::17
MarkupMiscAttributeContent - [6..15)::9
MarkupTextLiteral - [6..7)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [7..15)::8
Expand All @@ -15,6 +15,7 @@
CSharpCodeBlock - [8..15)::7
CSharpExpressionLiteral - [8..15)::7 - [onclick] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:NonWhitespace;ImplicitExpression[RTD];K14
Identifier;[onclick];
MarkupMiscAttributeContent - [15..23)::8
MarkupTextLiteral - [15..17)::2 - [="] - Gen<Markup> - SpanEditHandler;Accepts:Any
Equals;[=];
DoubleQuote;["];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MarkupStartTag - [0..24)::24 - [<input @onclick="@test">] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[input];
MarkupMiscAttributeContent - [6..23)::17
MarkupMiscAttributeContent - [6..15)::9
MarkupTextLiteral - [6..7)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [7..15)::8
Expand All @@ -15,6 +15,7 @@
CSharpCodeBlock - [8..15)::7
CSharpExpressionLiteral - [8..15)::7 - [onclick] - Gen<Expr> - ImplicitExpressionEditHandler;Accepts:NonWhitespace;ImplicitExpression[RTD];K14
Identifier;[onclick];
MarkupMiscAttributeContent - [15..23)::8
MarkupTextLiteral - [15..17)::2 - [="] - Gen<Markup> - SpanEditHandler;Accepts:Any
Equals;[=];
DoubleQuote;["];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MarkupTagHelperStartTag - [0..29)::29 - [<p @DateTime.Now class="btn">] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[p];
MarkupMiscAttributeContent - [2..28)::26
MarkupMiscAttributeContent - [2..16)::14
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [3..16)::13
Expand All @@ -17,12 +17,19 @@
Identifier;[DateTime];
Dot;[.];
Identifier;[Now];
MarkupTextLiteral - [16..28)::12 - [ class="btn"] - Gen<Markup> - SpanEditHandler;Accepts:Any
MarkupAttributeBlock - [16..28)::12 - [ class="btn"]
MarkupTextLiteral - [16..17)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
MarkupTextLiteral - [17..22)::5 - [class] - Gen<Markup> - SpanEditHandler;Accepts:Any
Text;[class];
Equals;[=];
Equals;[=];
MarkupTextLiteral - [23..24)::1 - ["] - Gen<Markup> - SpanEditHandler;Accepts:Any
DoubleQuote;["];
Text;[btn];
GenericBlock - [24..27)::3
MarkupLiteralAttributeValue - [24..27)::3 - [btn]
MarkupTextLiteral - [24..27)::3 - [btn] - Gen<Markup> - SpanEditHandler;Accepts:Any
Text;[btn];
MarkupTextLiteral - [27..28)::1 - ["] - Gen<Markup> - SpanEditHandler;Accepts:Any
DoubleQuote;["];
CloseAngle;[>];
MarkupTagHelperEndTag - [29..33)::4 - [</p>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
MarkupTagHelperStartTag - [0..23)::23 - [<p @DateTime.Now="btn">] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[p];
MarkupMiscAttributeContent - [2..22)::20
MarkupMiscAttributeContent - [2..16)::14
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [3..16)::13
Expand All @@ -17,6 +17,7 @@
Identifier;[DateTime];
Dot;[.];
Identifier;[Now];
MarkupMiscAttributeContent - [16..22)::6
MarkupTextLiteral - [16..22)::6 - [="btn"] - Gen<Markup> - SpanEditHandler;Accepts:Any
Equals;[=];
DoubleQuote;["];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
MarkupStartTag - [3..20)::17 - [< @DateTime.Now >] - Gen<Markup> - SpanEditHandler;Accepts:Any
OpenAngle;[<];
Text;[<Missing>];
MarkupMiscAttributeContent - [4..19)::15
MarkupMiscAttributeContent - [4..18)::14
MarkupTextLiteral - [4..5)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CSharpCodeBlock - [5..18)::13
Expand All @@ -22,6 +22,7 @@
Identifier;[DateTime];
Dot;[.];
Identifier;[Now];
MarkupMiscAttributeContent - [18..19)::1
MarkupTextLiteral - [18..19)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
CloseAngle;[>];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ public void CSharp8()
[IntegrationTestFact]
public void ConditionalAttributes() => RunTest();

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttributes2()
{
if (designTime)
{
// An error scenario: tag helper + C# dynamic content (a razor error is reported,
// so it is fine there is a missing mapping for the C# dynamic content).
ExpectedMissingSourceMappings = new()
{
{ new(base.GetTestFileName() + ".cshtml", 328, 11, 8), "s" }
};
}

RunTest();
}

[IntegrationTestFact]
public void CodeBlockWithTextElement() => RunTest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Linq;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Language.Legacy;
Expand Down Expand Up @@ -299,6 +300,78 @@ public void ConditionalAttributesWithWeirdSpacingAreDisabledForDataAttributesInD
ParseDocumentTest("@{<span data-foo=@foo ></span>}");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_DynamicContentAfter()
{
ParseDocumentTest("""<p class="@c" @x />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_DynamicContentBefore()
{
ParseDocumentTest("""<p @x class="@c" />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_DynamicContentBefore_02()
{
ParseDocumentTest("""<p @(x + y) class="@c" />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_DynamicContentBefore_03()
{
ParseDocumentTest("""<p @{if (x) { @x }} class="@c" />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_DynamicContentBefore_04()
{
ParseDocumentTest("""<p @@x class="@c" />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_InvalidContentBefore()
{
ParseDocumentTest("""<p "ab" class="@c" />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_CommentAfter()
{
ParseDocumentTest("""<p class="@c" @* comment *@ />""");
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/10586")]
public void ConditionalAttribute_CommentBefore()
{
ParseDocumentTest("""<p @* comment *@ class="@c" />""");
}

[Fact]
public void EscapedAttributeName_WithValue()
{
ParseDocumentTest("""<p @@attr="value" />""");
}

[Fact]
public void EscapedAttributeName_Minimized()
{
ParseDocumentTest("""<p @@attr />""");
}

[Fact]
public void EscapedAttributeName_Eof()
{
ParseDocumentTest("""<p @@""");
}

[Fact]
public void EscapedAttributeName_InvalidName()
{
ParseDocumentTest("""<p @@"invalid" />""");
}

[Fact]
public void ComponentFileKind_ParsesDirectiveAttributesAsMarkup()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public static TheoryData GetAttributeNameValuePairsData
return new TheoryData<string, IEnumerable<KeyValuePair<string, string>>>
{
{ "<a>", empty },
{ "<a @{ } href='~/home'>", empty },
{ "<a @{ } href='~/home'>", new[] { kvp("href", "~/home") } },
{ "<a href=\"@true\">", new[] { kvp("href", csharp) } },
{ "<a href=\"prefix @true suffix\">", new[] { kvp("href", $"prefix{csharp} suffix") } },
{ "<a href=~/home>", new[] { kvp("href", "~/home") } },
{ "<a href=~/home @{ } nothing='something'>", new[] { kvp("href", "~/home") } },
{ "<a href=~/home @{ } nothing='something'>", new[] { kvp("href", "~/home"), kvp("nothing", "something") } },
{
"<a href=\"@DateTime.Now::0\" class='btn btn-success' random>",
new[] { kvp("href", $"{csharp}::0"), kvp("class", "btn btn-success"), kvp("random", "") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
}
<p class="@null" />
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@{
var ch = true;
var cls = "bar";
var s = "str";
<a @s href="x" />
<p @s class="@cls" />
<p @s class="x @cls" />
<p @s class="@cls x" />
<input type="checkbox" @s checked="@ch" />
<input type="checkbox" @s checked="x @ch" />
<p @s class="@if(cls != null) { @cls }" />
<a @s href="~/x" />
<script @s src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<p @s class="@null" />
<p class="@null" @s />

<p @* comment *@ @s style="@null">x</p>

<p @(s + s) style="@null">x</p>
<p @{if (s.Length != 0) { @s }} style="@null">x</p>
<p @@s style="@null">x</p>
}
Loading
Loading