phoenixframework / phoenix_live_view

Rich, real-time user experiences with server-rendered HTML
https://hex.pm/packages/phoenix_live_view
MIT License
6k stars 906 forks source link

.live_title keeps leading and trailing white space #2492

Closed aselder closed 1 year ago

aselder commented 1 year ago

Environment

Actual behavior

If you follow the Phoenix 1.6 - 1.7 upgrade guide for live_title

   <.live_title suffix=" · Phoenix Framework">
     <%= assigns[:page_title] || "MyApp" %>
   </.live_title>

You'll get the following html (assuming assigns[:page_title] is Home):

<title data-suffix=\" · Phoenix Framework\">\nHome\n · Phoenix Framework</title>

Expected behavior

I'd expect not to get whitespace added between the given content and the suffix and don't expect the newline at the start. In HTML titles, these characters really don't make sense

I'd expect to get: <title data-suffix=\" · Phoenix Framework\">Home · Phoenix Framework</title>

CoderDennis commented 1 year ago

The simple workaround would be to put it all on one line in your template.

<.live_title suffix=" · Phoenix Framework"><%= assigns[:page_title] || "MyApp" %></.live_title>

aiwaiwa commented 1 year ago

This won't last long, automatic formatting will eliminate this quite quickly.

Would one day it make sense to add a hint to <%= and %> that would tuck the content to the sibling preceding/following tags and erase any white space in between? Something like <%_= or <%|=. Something not ugly but still readable.

I know in Go template language I would write {{- assigns[:page_title] || "MyApp" -}} and that would survive any formatting errors.

CoderDennis commented 1 year ago

I tested this, and for me, when the :page_title was assigned it worked as expected without the whitespace. When it wasn't assigned, and the "MyApp" value was used, that's when I saw the extra whitespace and linebreaks. However, in both cases, the title appeared correctly in the browser. Whitespace isn't supposed to matter in HTML, so I don't think this is something to worry about.

chrismccord commented 1 year ago

@CoderDennis is correct you can inline the content and it will work as expected. If the extra space is not causing actual display differences then I wouldn't worry about it. If you are having display changes, you can phx-no-format on the <.live_title so this isn't an issue. Thanks!