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

Conditional attribute rendering: null value attributes are sometimes unexpectedly rendered #10586

Closed
tormnator opened this issue Jul 4, 2024 · 1 comment · Fixed by #10620
Assignees
Labels
area-compiler Umbrella for all compiler issues

Comments

@tormnator
Copy link

tormnator commented Jul 4, 2024

This issue is about conditional attribute rendering where sometimes null value attributes are unexpectedly rendered. I stumbled upon this today during work in a asp.net core mvc 7 application (and VS 2022), and it took me a while to nail down the behavior. I haven't found any documentation or issues stating whether this is a bug, as designed, or otherwise.

When rendering something like <div @(something) style="@null">, then you would expect the style attribute not to be output in the final html. But in my testing that is not the case. But, if you place the @(something) behind the style attribute, then the style attribute is omitted as expected. Below is a more thorough test:

@{
  var aString = new HtmlString("string");
  var aStringClassEq = "class=\"string\"";
  var anHtmlString = new HtmlString("HtmlString");
  var anHtmlStringClassEq = new HtmlString("class=\"HtmlString\"");

  // 1. Regular strings:
  <div name="works-1" class="@aString" style="@null">Styles</div>
  <div name="works-1b" class="@Html.Raw(aString)" style="@null">Styles</div>
  <div name="fails-1a" @aStringClassEq style="@null">Styles</div>
  <div name="fails-1b" @Html.Raw(aStringClassEq) style="@null">Styles</div>

  // 2. HtmlString:
  <div name="works-2a" class="@anHtmlString" style="@null">Styles</div>
  <div name="works-2b" class="@Html.Raw(anHtmlString)" style="@null">Styles</div>
  <div name="fails-2a" @anHtmlStringClassEq style="@null">Styles</div>
  <div name="fails-2b" @Html.Raw(anHtmlStringClassEq) style="@null">Styles</div>

  <div name="works-3" htmlString style="@null">Styles</div>
  <div name="fails-3b" @aString style="@null">Styles</div>
  <div name="fails-3a" @anHtmlString style="@null">Styles</div>

  @* Placing an attribute with null value *first* always works: *@
  <div name="works-4a" style="@null" @aStringClassEq>Styles</div>
  <div name="fails-4b" style="@null" @Html.Raw(aStringClassEq)>Styles</div>
  <div name="fails-4c" style="@null" @anHtmlStringClassEq>Styles</div>
  <div name="fails-4d" style="@null" @Html.Raw(anHtmlStringClassEq)>Styles</div>
  <div name="works-4e" style="@null" @aString>Styles</div>
  <div name="works-4f" style="@null" @anHtmlString>Styles</div>
}

I expected all the div elements with name="fails-*" to not output the empty styles attribute, but here's the actual output:

  <div name="works-1" class="string">Styles</div>
  <div name="works-1b" class="string">Styles</div>
  <div name="fails-1a" class=&quot;string&quot; style="">Styles</div>
  <div name="fails-1b" class="string" style="">Styles</div>
  <div name="works-2a" class="HtmlString">Styles</div>
  <div name="works-2b" class="HtmlString">Styles</div>
  <div name="fails-2a" class="HtmlString" style="">Styles</div>
  <div name="fails-2b" class="HtmlString" style="">Styles</div>
  <div name="works-3" htmlString>Styles</div>
  <div name="fails-3b" string style="">Styles</div>
  <div name="fails-3a" HtmlString style="">Styles</div>
  <div name="works-4a" class=&quot;string&quot;>Styles</div>
  <div name="works-4b" class="string">Styles</div>
  <div name="works-4c" class="HtmlString">Styles</div>
  <div name="works-4d" class="HtmlString">Styles</div>
  <div name="works-4e" string>Styles</div>
  <div name="works-4f" HtmlString>Styles</div>
@davidwengier davidwengier added the area-compiler Umbrella for all compiler issues label Jul 4, 2024
@chsienki chsienki added this to the 17.12 Planning milestone Jul 11, 2024
@chsienki
Copy link
Contributor

@jjonescz Please take a look, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants