-
Notifications
You must be signed in to change notification settings - Fork 215
Fix: TagHelperContext.AllAttributes missing attributes after Razor comment #12322
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
base: main
Are you sure you want to change the base?
Conversation
Added handling for Razor comments in TagHelperBlockRewriter to continue processing attributes after comments instead of breaking the loop. Co-authored-by: davidwengier <[email protected]>
Added test to verify the HTML parser correctly handles attributes after Razor comments. Co-authored-by: davidwengier <[email protected]>
@copilot Looks great so far, but would be good to see some tests that have the generated C# baselines too. Please add a test for the scenario of having a comment between two bound tag helper attributes in |
…ssing Added handling for whitespace-only MarkupTextLiteral nodes between attributes to prevent early termination of attribute processing. Also added CodeGenerationIntegrationTest for attributes with Razor comments. Co-authored-by: davidwengier <[email protected]>
Added Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Fix: TagHelperContext.AllAttributes missing attributes after Razor comment
Problem Analysis
When a Razor comment (
@* *@
) appears in an element's attribute list, any attributes after it are missing fromTagHelperContext.AllAttributes
.The root cause was in
TagHelperBlockRewriter.cs
:startTag.Attributes
, whitespace between attributes is represented as standaloneMarkupTextLiteralSyntax
nodesresult = null
RazorCommentBlockSyntax
nodes also didn't match, causing the same issueSolution
Added explicit handling for:
RazorCommentBlockSyntax
- preserved in syntax tree while continuing to process subsequent attributesMarkupTextLiteralSyntax
with whitespace-only content - preserved while continuing processingTesting
Added comprehensive test coverage:
All existing tests pass with no regressions (3,871 compiler tests pass).
Impact
TagHelpers now correctly receive all attributes in their execution context, regardless of Razor comment or whitespace placement. Both bound and unbound attributes after comments are properly processed and included in the generated code.
Original prompt
Issue: #12261
Summary
TagHelperContext.AllAttributes is missing attributes that appear after a Razor comment (@* *@) inside an element's attribute list. When a Razor (server-side) comment is present between attributes, any subsequent attributes are omitted from the TagHelper execution context, leading to incorrect TagHelper behavior.
Reproduction
Given a TagHelper targeting the
tag (or any element processed by a TagHelper):
Input Razor:
In the TagHelper's Process/ProcessAsync method, examining context.AllAttributes results in a collection containing only attribute-1, excluding not-visible.
Expected Behavior
All valid attributes appearing after a Razor comment inside a tag's attribute list should be present in TagHelperContext.AllAttributes. Razor comments should be ignored by the attribute collection logic rather than terminating or truncating collection.
Actual Behavior
Attributes that appear after a Razor comment block inside the start tag are missing from TagHelperContext.AllAttributes.
Impact
This breaks TagHelpers that rely on full attribute enumeration (e.g., diagnostic, conditional logic, passthrough attribute processing). It creates surprising behavior for developers who use Razor comments to temporarily disable or annotate attributes.
Suspected Cause
During the parsing / rewriting phase that builds the intermediate node structure for TagHelper execution, encountering a RazorComment (RazorCommentBlockSyntax) inside the attribute list likely causes:
Likely Areas to Inspect (filenames approximate; adjust to actual paths):
Proposed Fix
Testing Strategy
Add at least the following tests (adjust namespaces to existing test layout):
a. Multiple attributes after a comment.
b. Multiple comments interleaved.
c. Self-closing tag scenario: <p attribute-1="true" @* c *@ attribute-2 />
d. Minimized attributes (boolean) after a comment.
Acceptance Criteria
Non-Goals
Implementation Notes
If the parser currently builds a single token list for the start tag, ensure the enumerator that converts tokens to attributes skips RazorComment tokens rather than resetting or bailing. If attribute rewriter uses state machine, add a branch for comment start that preserves the current attribute parsing state and resumes afterwards.
Documentation / Changelog
Add a short entry (if project maintains one) noting: Fixed an issue where attributes appearing after a Razor comment inside a start tag were not included in TagHelperContext.AllAttributes.
Follow Project Contribution Guidelines
Apply coding style, run code formatter, and ensure added tests follow naming conventions. Reference the linked issue in the commit message (e.g., Fixes #12261).
Deliverables
This pull request was created as a result of the following prompt from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.