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

TagHelper attributes with no value cannot be used in TagHelpers #220

Closed
NTaylorMullen opened this issue Nov 4, 2014 · 17 comments
Closed
Assignees
Milestone

Comments

@NTaylorMullen
Copy link
Member

Razor does not correctly parse HTML attributes that have no value.

After playing around with the parse tree a bit I noticed that if you were to create HTML elements such as:

<input checked>
<input checked value>

The Razor parser does not generate an accurate syntax tree. This in turn results in the generated code not calling WriteAttribute and furthermore with TagHelpers does not allow us to pull the attributes from an HTML element easily.

Once this has been added we should also support this attribute syntax in Tag Helpers.

@dougbu
Copy link
Member

dougbu commented Nov 4, 2014

supporting this would go beyond the XHTML restrictions elsewhere in the system. the only special case I'm aware of is <input @checked/> and that seems sufficient.

@NTaylorMullen
Copy link
Member Author

@dougbu this is supported in HTML 5: http://www.w3.org/TR/html-markup/syntax.html#syntax-attr-empty

@dougbu
Copy link
Member

dougbu commented Nov 4, 2014

Agreed. However we restrict ourselves to generating and accepting only the XHTML subset of HTML 5.

@NTaylorMullen
Copy link
Member Author

Hmm? Where do we restrict ourselves to generating and accepting only the XHTML subset of HTML5?

@dougbu
Copy link
Member

dougbu commented Nov 5, 2014

This bug is just one example where Razor doesn't support non-XHTML Another is tag helpers not handling an empty element unless it's written as a self-closing tag.

On the generation side, RazorPage.WriteAttributeTo() special-cases bool values and generates long-form attributes -- either nothing (for false) or attribute-name='attribute-name'. TagBuilder.AppendAttributes() and TagHelperOutput.GenerateStartTag() also generate only long-form attributes and their containing classes never leave off end tags.

Side question: Why did you add the "bug" tag for this issue? Seems more like an enhancement to support additional syntax for already-supported HTML features. Even more like a part of a larger "HTML short-form" feature that would touch a lot of the Razor parser as well as the HTML and tag helper infrastructure.

@NTaylorMullen
Copy link
Member Author

It's marked as a bug because we're making the move to support HTML5. That was also why we modified TagBuiler/TagHelperOutput to have case-insensitive attributes and marked those similarly as bugs.

@dougbu
Copy link
Member

dougbu commented Nov 5, 2014

Low importance: The analogy doesn't hold because a .cshtml file containing attributes using various cases can still generate a valid-looking XHTML document. But perhaps you're right and we should normalize the case of all attributes we write, regardless of the user's input.

@yishaigalatzer
Copy link
Contributor

The key issue here is this:

In razor 2 or 3 when the user wrote

<input required />

We rendered

<input required />

But with tag helpers (input tag helper for example)

<input asp-for="FieldName" required />

We will trigger a parsing error and you will have to type

<input asp-for="FieldName" required="true" />

@danroth27 danroth27 modified the milestones: 4.0.0-beta2, 4.0.0-rc1 Jan 9, 2015
@yishaigalatzer yishaigalatzer changed the title Razor does not correctly parse HTML attributes that have no value. [TagHelpers] attributes with no value cannot be used in TagHelpers Jan 13, 2015
@yishaigalatzer yishaigalatzer modified the milestones: 4.0.0-rc1, 4.0.0-beta3 Jan 13, 2015
@dougbu
Copy link
Member

dougbu commented Jan 22, 2015

when this bug is fixed, will need to modify the expectations in https://github.com/aspnet/Mvc/tree/dev/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources files as well as one HTML helper test file (see https://github.com/aspnet/Mvc/blob/dev/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/OrderUsingHtmlHelpers.cshtml#L72) that mimics current tag helper results

@dougbu dougbu closed this as completed Jan 22, 2015
@dougbu dougbu reopened this Jan 22, 2015
@NTaylorMullen NTaylorMullen changed the title [TagHelpers] attributes with no value cannot be used in TagHelpers TagHelper attributes with no value cannot be used in TagHelpers Jan 23, 2015
@NTaylorMullen
Copy link
Member Author

With issue #289 all TagHelper bound attributes that are not typed System.String without a value will be invalid and cause a parse error. Do we want to do something special with say bool typed attributes, or any other types?

/cc @DamianEdwards

@DamianEdwards
Copy link
Member

I think we just shouldn't support bound properties that don't have a value specified, i.e. parser error.

@danroth27 danroth27 modified the milestones: Backlog, 4.0.0-rc1 Feb 23, 2015
@NTaylorMullen
Copy link
Member Author

@ChrisKlug Can completely understand the frustration. Razor actually as whole today doesn't parse attributes correctly even when not using TagHelpers. For instance: #123. Hoping to get these issues all resolved to have less frustration 😄

@DamianEdwards
Copy link
Member

Just to be clear, the Tag Helper will be able to see the attribute once we fix other things we're currently working on. You'll be able to see the attribute from TagHelperContext.AllAttributes. This bug is specifically about having Tag Helper bound properties (i.e. properties on your Tag Helper class) be populated from attributes with no value.

Also, you'll be able to target a Tag Helper based on the existence of such a property using the TargetElement attribute.

@ChrisKlug
Copy link

Ok, great! It just caused me a heap of headscratching when I couldn't get my TagHelper to run when the "required" attribute didn't have a value. After a bit of looking, it seemed as it could be solved by changing some not too complicated things in the attribute parsing, but I don't know if that would affect something else... But fixing that seemed like a small thing that could be fixed by a quick pull request, but if it is bigger I will stay out of your way...

@NTaylorMullen
Copy link
Member Author

@ChrisKlug The parsing logic change isn't much. Writing the tests for it will be the time sink, prob 20% feature 80% test time for this one 😄

@NTaylorMullen NTaylorMullen modified the milestones: 4.0.0-beta5, Backlog Apr 24, 2015
@NTaylorMullen NTaylorMullen self-assigned this Apr 24, 2015
NTaylorMullen added a commit that referenced this issue May 1, 2015
- Updated the Razor parser to understand minimized attributes instead of just treating them like plain text. This just involved encompassing minimized attributes in their own blocks just like the other attributes found on the HTML tag.
- Updated TagHelperParseTreeRewriter to only accept minimized attributes for unbound attributes.
- Updated IReadOnlyTagHelperAttribute/TagHelperAttribute to have a Minimized property to indicate that an attribute was minimized.
- Updated parser level block structures to represent minimized attributes as null syntax tree nodes.
- Updated chunk level structures to represent minimized attributes as null chunks.

#220
NTaylorMullen added a commit that referenced this issue May 1, 2015
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes.
- Updated existing parser tests to understand minimized attributes.
- Added codegen test to validate understanding of minimized attributes.
- Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests.

#220
NTaylorMullen added a commit that referenced this issue May 1, 2015
- Updated the Razor parser to understand minimized attributes instead of just treating them like plain text. This just involved encompassing minimized attributes in their own blocks just like the other attributes found on the HTML tag.
- Updated TagHelperParseTreeRewriter to only accept minimized attributes for unbound attributes.
- Updated IReadOnlyTagHelperAttribute/TagHelperAttribute to have a Minimized property to indicate that an attribute was minimized.
- Updated parser level block structures to represent minimized attributes as null syntax tree nodes.
- Updated chunk level structures to represent minimized attributes as null chunks.

#220
NTaylorMullen added a commit that referenced this issue May 1, 2015
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes.
- Updated existing parser tests to understand minimized attributes.
- Added codegen test to validate understanding of minimized attributes.
- Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests.
- Refactored part of the TagHelperParseTreeRewriterTest file into a base class file so we can make better rewriting tests.

#220
NTaylorMullen added a commit that referenced this issue May 6, 2015
- Updated the Razor parser to understand minimized attributes instead of just treating them like plain text. This just involved encompassing minimized attributes in their own blocks just like the other attributes found on the HTML tag.
- Updated TagHelperParseTreeRewriter to only accept minimized attributes for unbound attributes.
- Updated IReadOnlyTagHelperAttribute/TagHelperAttribute to have a Minimized property to indicate that an attribute was minimized.
- Updated parser level block structures to represent minimized attributes as null syntax tree nodes.
- Updated chunk level structures to represent minimized attributes as null chunks.

#220
NTaylorMullen added a commit that referenced this issue May 6, 2015
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes.
- Updated existing parser tests to understand minimized attributes.
- Added codegen test to validate understanding of minimized attributes.
- Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests.
- Refactored part of the TagHelperParseTreeRewriterTest file into a base class file so we can make better rewriting tests.

#220
NTaylorMullen added a commit that referenced this issue May 11, 2015
- Updated the Razor parser to understand minimized attributes instead of just treating them like plain text. This just involved encompassing minimized attributes in their own blocks just like the other attributes found on the HTML tag.
- Updated TagHelperParseTreeRewriter to only accept minimized attributes for unbound attributes.
- Updated IReadOnlyTagHelperAttribute/TagHelperAttribute to have a Minimized property to indicate that an attribute was minimized.
- Updated parser level block structures to represent minimized attributes as null syntax tree nodes.
- Updated chunk level structures to represent minimized attributes as null chunks.

#220
NTaylorMullen added a commit that referenced this issue May 11, 2015
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes.
- Updated existing parser tests to understand minimized attributes.
- Added codegen test to validate understanding of minimized attributes.
- Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests.
- Refactored part of the TagHelperParseTreeRewriterTest file into a base class file so we can make better rewriting tests.

#220
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue May 13, 2015
- Added the ability for the WriteTagHelperAsync in RazorPage.cs to understand minimized attributes.
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue May 13, 2015
- Added unit and functional test.
NTaylorMullen added a commit that referenced this issue May 15, 2015
- Updated the Razor parser to understand minimized attributes instead of just treating them like plain text. This just involved encompassing minimized attributes in their own blocks just like the other attributes found on the HTML tag.
- Updated TagHelperParseTreeRewriter to only accept minimized attributes for unbound attributes.
- Updated IReadOnlyTagHelperAttribute/TagHelperAttribute to have a Minimized property to indicate that an attribute was minimized.
- Updated parser level block structures to represent minimized attributes as null syntax tree nodes.
- Updated chunk level structures to represent minimized attributes as null chunks.

#220
NTaylorMullen added a commit that referenced this issue May 15, 2015
- Added parse level rewriting tests to validate new TagHelper rewritten structures for minimized attributes.
- Updated existing parser tests to understand minimized attributes.
- Added codegen test to validate understanding of minimized attributes.
- Added TagHelperExecutionContext tests to validate maintaining of runtime TagHelperOutput tests.
- Refactored part of the TagHelperParseTreeRewriterTest file into a base class file so we can make better rewriting tests.

#220
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue May 15, 2015
- Added the ability for the WriteTagHelperAsync in RazorPage.cs to understand minimized attributes.
NTaylorMullen added a commit to aspnet/Mvc that referenced this issue May 15, 2015
- Added unit and functional test.
@NTaylorMullen
Copy link
Member Author

6fa3e40
0882ff4

dougbu added a commit that referenced this issue May 16, 2015
…rt II

- relates to #89 because that changes `string` property checks and needs this refactor
- determine `string`-ness when creating `TagHelperAttributeDescriptor`s
 - add `TagHelperAttributeDescriptor.IsStringProperty` (set in constructor)
 - avoid repeated `string` comparisons and be more explicit
- change `TagHelperBlockRewriter` to centralize more of the `string`-ness determination
 - also add `TryParseResult` DTO, avoiding multiple `out` parameters
- refactor `CSharpTagHelperCodeRenderer` to allow reuse of core attribute value rendering
- test all of it
 - add `TagHelperDescriptorTest` to confirm serialization / deserialization

minor:
- fix `TagHelperBlockRewriter.TryParseBlock()` end quote removal when tag is malformed

nits:
- remove dangling mention of fixed bug #220
- make recently-added `TagHelperBlockRewriterTest` tests realistic
 - multiple `TagHelperDescriptor`s for same tag helper have identical `Attributes`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants