Skip to content

Commit f5bf085

Browse files
Copilotdavidwengier
andcommitted
Fix: Handle RazorCommentBlockSyntax in TagHelper attribute processing
Added handling for Razor comments in TagHelperBlockRewriter to continue processing attributes after comments instead of breaking the loop. Co-authored-by: davidwengier <754264+davidwengier@users.noreply.github.com>
1 parent d8773eb commit f5bf085

14 files changed

+325
-0
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/TagHelperBlockRewriterTest.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,4 +2472,104 @@ public void Rewrites_MinimizedComponentDirectiveAttributes()
24722472
builder.AllowCSharpInMarkupAttributeArea = false;
24732473
});
24742474
}
2475+
2476+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/12261")]
2477+
public void TagHelper_AttributeAfterRazorComment()
2478+
{
2479+
// Arrange
2480+
var descriptors = ImmutableArray.Create(
2481+
TagHelperDescriptorBuilder.CreateTagHelper("PTagHelper", "TestAssembly")
2482+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"))
2483+
.BoundAttributeDescriptor(attribute => attribute
2484+
.Name("attribute-1")
2485+
.PropertyName("Attribute1")
2486+
.TypeName(typeof(string).FullName))
2487+
.BoundAttributeDescriptor(attribute => attribute
2488+
.Name("not-visible")
2489+
.PropertyName("NotVisible")
2490+
.TypeName(typeof(bool).FullName))
2491+
.Build());
2492+
2493+
// Act & Assert
2494+
EvaluateData(descriptors, """
2495+
<p
2496+
attribute-1="true"
2497+
@* visible *@
2498+
not-visible>
2499+
</p>
2500+
""");
2501+
}
2502+
2503+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/12261")]
2504+
public void TagHelper_MultipleAttributesAfterRazorComment()
2505+
{
2506+
// Arrange
2507+
var descriptors = ImmutableArray.Create(
2508+
TagHelperDescriptorBuilder.CreateTagHelper("PTagHelper", "TestAssembly")
2509+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"))
2510+
.BoundAttributeDescriptor(attribute => attribute
2511+
.Name("attr-1")
2512+
.PropertyName("Attr1")
2513+
.TypeName(typeof(string).FullName))
2514+
.BoundAttributeDescriptor(attribute => attribute
2515+
.Name("attr-2")
2516+
.PropertyName("Attr2")
2517+
.TypeName(typeof(string).FullName))
2518+
.BoundAttributeDescriptor(attribute => attribute
2519+
.Name("attr-3")
2520+
.PropertyName("Attr3")
2521+
.TypeName(typeof(string).FullName))
2522+
.Build());
2523+
2524+
// Act & Assert
2525+
EvaluateData(descriptors, """
2526+
<p attr-1="first" @* comment *@ attr-2="second" attr-3="third"></p>
2527+
""");
2528+
}
2529+
2530+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/12261")]
2531+
public void TagHelper_MultipleInterleavedRazorComments()
2532+
{
2533+
// Arrange
2534+
var descriptors = ImmutableArray.Create(
2535+
TagHelperDescriptorBuilder.CreateTagHelper("InputTagHelper", "TestAssembly")
2536+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("input"))
2537+
.BoundAttributeDescriptor(attribute => attribute
2538+
.Name("type")
2539+
.PropertyName("Type")
2540+
.TypeName(typeof(string).FullName))
2541+
.BoundAttributeDescriptor(attribute => attribute
2542+
.Name("value")
2543+
.PropertyName("Value")
2544+
.TypeName(typeof(string).FullName))
2545+
.Build());
2546+
2547+
// Act & Assert
2548+
EvaluateData(descriptors, """
2549+
<input @* comment1 *@ type="text" @* comment2 *@ value="test" @* comment3 *@ />
2550+
""");
2551+
}
2552+
2553+
[Fact, WorkItem("https://github.com/dotnet/razor/issues/12261")]
2554+
public void TagHelper_MinimizedAttributeAfterRazorComment()
2555+
{
2556+
// Arrange
2557+
var descriptors = ImmutableArray.Create(
2558+
TagHelperDescriptorBuilder.CreateTagHelper("InputTagHelper", "TestAssembly")
2559+
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("input"))
2560+
.BoundAttributeDescriptor(attribute => attribute
2561+
.Name("type")
2562+
.PropertyName("Type")
2563+
.TypeName(typeof(string).FullName))
2564+
.BoundAttributeDescriptor(attribute => attribute
2565+
.Name("checked")
2566+
.PropertyName("Checked")
2567+
.TypeName(typeof(bool).FullName))
2568+
.Build());
2569+
2570+
// Act & Assert
2571+
EvaluateData(descriptors, """
2572+
<input type="checkbox" @* comment *@ checked />
2573+
""");
2574+
}
24752575
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Markup span at (19:1,15 [4] ) - Parent: Tag block at (0:0,0 [63] )
2+
Markup span at (57:3,14 [2] ) - Parent: Tag block at (0:0,0 [63] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
RazorDocument - [0..63)::63 - [<pLF attribute-1="true"LF @* visible *@LF not-visible>LF</p>]
2+
MarkupBlock - [0..63)::63
3+
MarkupTagHelperElement - [0..63)::63 - p[StartTagAndEndTag] - PTagHelper
4+
MarkupTagHelperStartTag - [0..57)::57 - [<pLF attribute-1="true"LF @* visible *@LF not-visible>] - Gen<Markup>
5+
OpenAngle;[<];
6+
Text;[p];
7+
MarkupTagHelperAttribute - [2..24)::22 - attribute-1 - DoubleQuotes - Bound - [LF attribute-1="true"]
8+
MarkupTextLiteral - [2..6)::4 - [LF ] - Gen<Markup>
9+
NewLine;[LF];
10+
Whitespace;[ ];
11+
MarkupTextLiteral - [6..17)::11 - [attribute-1] - Gen<Markup>
12+
Text;[attribute-1];
13+
Equals;[=];
14+
MarkupTextLiteral - [18..19)::1 - ["] - Gen<Markup>
15+
DoubleQuote;["];
16+
MarkupTagHelperAttributeValue - [19..23)::4
17+
MarkupTextLiteral - [19..23)::4 - [true] - Gen<Markup>
18+
Text;[true];
19+
MarkupTextLiteral - [23..24)::1 - ["] - Gen<Markup>
20+
DoubleQuote;["];
21+
MarkupTextLiteral - [24..28)::4 - [LF ] - Gen<Markup>
22+
NewLine;[LF];
23+
Whitespace;[ ];
24+
RazorComment - [28..41)::13
25+
RazorCommentTransition;[@];
26+
RazorCommentStar;[*];
27+
RazorCommentLiteral;[ visible ];
28+
RazorCommentStar;[*];
29+
RazorCommentTransition;[@];
30+
MarkupEphemeralTextLiteral - [41..43)::2 - [LF] - Gen<None>
31+
NewLine;[LF];
32+
MarkupMinimizedAttributeBlock - [43..56)::13 - [ not-visible]
33+
MarkupTextLiteral - [43..45)::2 - [ ] - Gen<Markup>
34+
Whitespace;[ ];
35+
MarkupTextLiteral - [45..56)::11 - [not-visible] - Gen<Markup>
36+
Text;[not-visible];
37+
CloseAngle;[>];
38+
MarkupTextLiteral - [57..59)::2 - [LF] - Gen<Markup>
39+
NewLine;[LF];
40+
MarkupTagHelperEndTag - [59..63)::4 - [</p>]
41+
OpenAngle;[<];
42+
ForwardSlash;[/];
43+
Text;[p];
44+
CloseAngle;[>];
45+
EndOfFile;[];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TagHelper span at (0:0,0 [63] ) - PTagHelper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Markup span at (13:0,13 [8] ) - Parent: Tag block at (0:0,0 [47] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
RazorDocument - [0..47)::47 - [<input type="checkbox" @* comment *@ checked />]
2+
MarkupBlock - [0..47)::47
3+
MarkupTagHelperElement - [0..47)::47 - input[SelfClosing] - InputTagHelper
4+
MarkupTagHelperStartTag - [0..47)::47 - [<input type="checkbox" @* comment *@ checked />] - Gen<Markup>
5+
OpenAngle;[<];
6+
Text;[input];
7+
MarkupTagHelperAttribute - [6..22)::16 - type - DoubleQuotes - Bound - [ type="checkbox"]
8+
MarkupTextLiteral - [6..7)::1 - [ ] - Gen<Markup>
9+
Whitespace;[ ];
10+
MarkupTextLiteral - [7..11)::4 - [type] - Gen<Markup>
11+
Text;[type];
12+
Equals;[=];
13+
MarkupTextLiteral - [12..13)::1 - ["] - Gen<Markup>
14+
DoubleQuote;["];
15+
MarkupTagHelperAttributeValue - [13..21)::8
16+
MarkupTextLiteral - [13..21)::8 - [checkbox] - Gen<Markup>
17+
Text;[checkbox];
18+
MarkupTextLiteral - [21..22)::1 - ["] - Gen<Markup>
19+
DoubleQuote;["];
20+
MarkupTextLiteral - [22..23)::1 - [ ] - Gen<Markup>
21+
Whitespace;[ ];
22+
RazorComment - [23..36)::13
23+
RazorCommentTransition;[@];
24+
RazorCommentStar;[*];
25+
RazorCommentLiteral;[ comment ];
26+
RazorCommentStar;[*];
27+
RazorCommentTransition;[@];
28+
MarkupMinimizedAttributeBlock - [36..44)::8 - [ checked]
29+
MarkupTextLiteral - [36..37)::1 - [ ] - Gen<Markup>
30+
Whitespace;[ ];
31+
MarkupTextLiteral - [37..44)::7 - [checked] - Gen<Markup>
32+
Text;[checked];
33+
MarkupMiscAttributeContent - [44..45)::1
34+
MarkupTextLiteral - [44..45)::1 - [ ] - Gen<Markup>
35+
Whitespace;[ ];
36+
ForwardSlash;[/];
37+
CloseAngle;[>];
38+
EndOfFile;[];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TagHelper span at (0:0,0 [47] ) - InputTagHelper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Markup span at (11:0,11 [5] ) - Parent: Tag block at (0:0,0 [67] )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
RazorDocument - [0..67)::67 - [<p attr-1="first" @* comment *@ attr-2="second" attr-3="third"></p>]
2+
MarkupBlock - [0..67)::67
3+
MarkupTagHelperElement - [0..67)::67 - p[StartTagAndEndTag] - PTagHelper
4+
MarkupTagHelperStartTag - [0..63)::63 - [<p attr-1="first" @* comment *@ attr-2="second" attr-3="third">] - Gen<Markup>
5+
OpenAngle;[<];
6+
Text;[p];
7+
MarkupTagHelperAttribute - [2..17)::15 - attr-1 - DoubleQuotes - Bound - [ attr-1="first"]
8+
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup>
9+
Whitespace;[ ];
10+
MarkupTextLiteral - [3..9)::6 - [attr-1] - Gen<Markup>
11+
Text;[attr-1];
12+
Equals;[=];
13+
MarkupTextLiteral - [10..11)::1 - ["] - Gen<Markup>
14+
DoubleQuote;["];
15+
MarkupTagHelperAttributeValue - [11..16)::5
16+
MarkupTextLiteral - [11..16)::5 - [first] - Gen<Markup>
17+
Text;[first];
18+
MarkupTextLiteral - [16..17)::1 - ["] - Gen<Markup>
19+
DoubleQuote;["];
20+
MarkupTextLiteral - [17..18)::1 - [ ] - Gen<Markup>
21+
Whitespace;[ ];
22+
RazorComment - [18..31)::13
23+
RazorCommentTransition;[@];
24+
RazorCommentStar;[*];
25+
RazorCommentLiteral;[ comment ];
26+
RazorCommentStar;[*];
27+
RazorCommentTransition;[@];
28+
MarkupAttributeBlock - [31..47)::16 - [ attr-2="second"]
29+
MarkupTextLiteral - [31..32)::1 - [ ] - Gen<Markup>
30+
Whitespace;[ ];
31+
MarkupTextLiteral - [32..38)::6 - [attr-2] - Gen<Markup>
32+
Text;[attr-2];
33+
Equals;[=];
34+
MarkupTextLiteral - [39..40)::1 - ["] - Gen<Markup>
35+
DoubleQuote;["];
36+
GenericBlock - [40..46)::6
37+
MarkupLiteralAttributeValue - [40..46)::6 - [second]
38+
MarkupTextLiteral - [40..46)::6 - [second] - Gen<Markup>
39+
Text;[second];
40+
MarkupTextLiteral - [46..47)::1 - ["] - Gen<Markup>
41+
DoubleQuote;["];
42+
MarkupAttributeBlock - [47..62)::15 - [ attr-3="third"]
43+
MarkupTextLiteral - [47..48)::1 - [ ] - Gen<Markup>
44+
Whitespace;[ ];
45+
MarkupTextLiteral - [48..54)::6 - [attr-3] - Gen<Markup>
46+
Text;[attr-3];
47+
Equals;[=];
48+
MarkupTextLiteral - [55..56)::1 - ["] - Gen<Markup>
49+
DoubleQuote;["];
50+
GenericBlock - [56..61)::5
51+
MarkupLiteralAttributeValue - [56..61)::5 - [third]
52+
MarkupTextLiteral - [56..61)::5 - [third] - Gen<Markup>
53+
Text;[third];
54+
MarkupTextLiteral - [61..62)::1 - ["] - Gen<Markup>
55+
DoubleQuote;["];
56+
CloseAngle;[>];
57+
MarkupTagHelperEndTag - [63..67)::4 - [</p>]
58+
OpenAngle;[<];
59+
ForwardSlash;[/];
60+
Text;[p];
61+
CloseAngle;[>];
62+
EndOfFile;[];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TagHelper span at (0:0,0 [67] ) - PTagHelper

0 commit comments

Comments
 (0)