-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
proposal: html/template: auto trim whitespace #20879
Comments
/cc @golang/proposal-review |
@odeke-em, no need to cc the proposal-review group. We go through various search queries to find stuff. The Proposal label is enough. Thanks. |
Gotcha, thanks @bradfitz! |
Even with the global option you still have to go through and add Does it make sense to adjust this proposal to be about exposing some kind of HTML compaction in html/template, a little like json.Compact? |
@rsc I would LOVE that, but I assumed that it wasn't done for reasons. I was kind of surprised that the x/net/html isn't vendored in and used by html/template. It seems like x/net/html would be in a good position to do such minification. |
OK, so html/template certainly understands HTML well enough to know what tags it is inside. What/where are the rules for HTML minification? I know <pre> content can't be trimmed. Anything else? |
This issue somewhat covers this: #3164. Minification is useful, but this issue is more concerned with trimming, which minification can't always do. For example,
Isn't the same as
A minifier can't produce the latter because it would be a behavior change. The whitespace will show up in the rendered page. The |
@bep what do you think about this? Would this feature help? |
@spf13 It would not solve any real problem that I have experienced; the I notice that this issue is targeting |
One alternative would be to redefine this partially: Make an option to trim inter-element whitespace. For example:
But
This would alleviate the need for the |
Per discussion with @spf13 and @golang/proposal-review:
Can anyone point at a spec for when we can safely remove spaces between elements vs when one must be preserved? Does the general approach (opt-in "as small as possible without changing meaning" and then reuse the existing syntax for pulling out the remaining spaces) seem OK? |
Does anyone know of a spec for when we can safely remove spaces between elements vs when one must be reserved? Or are there CSS properties that can turn an element (say, div) from whitespace-ignoring to whitespace-caring (like pre)? |
@spf13 found it: css "white-space: pre" makes writing a spec basically impossible. |
Wrong button. |
@rsc I think you are bloating this proposal. We won't be able to implement it. Looking at the html5 spec for parsing, there is a formally defined parsing standard for doing this. But, it's complicated, and only applies to html5, and will be much much harder to do. I don't think you want that in the standard library. What I don't understand is why you are mixing two issues into one? The distinct problems at hand are template clarity and control, not really conserving html output characters. Even if minification was done perfectly in Go, it still wouldn't solve this issue. Edit: There is a WHATWG link with references to all the places whitespace collapsing and stripping happens: https://html.spec.whatwg.org/multipage/infrastructure.html#dependencies I don't know if this is exhaustive. |
Fair enough, that's why the proposal is still open, because we now know that minifying can't be done / isn't enough. But it seems clear that we don't want to have to write templates that look like:
Writing |
@rsc
The reason for I don't
because it would squash the tag into |
To be clear, what you have to write today is:
And your proposal is to make the {{- ... -}} the default (under a given mode), where you have to say + to opt out of the default, so that the template would shorten to:
where the {{" "}} could be instead {{+ ""}} or {{"" +}} depending on whether they had the right spaces on either side to reuse. It's not obvious to me this is a net win. The shortening of the main control flow elements seems to me clearer in the original. Worse, when you get down to small items, I think the default ends up hurting and causing difficult-to-notice bugs. For example, the original template said:
but the auto-trimmed one that you posted said:
See the bug? The space in " deleted" just got eaten. You'd need to write something like:
Honestly I'd rather have the - signs in the outer structure than deal with subtle bugs like this in the inner structure. I don't see a compelling win here for flipping the default, and I do see new complexity. |
I see your point about the subtle bug. CSS classes have to be space separated which forces that. Stepping back, do you even agree with me that there is a problem in the template code today? Specifically, do you agree that the top two examples in the OP are ugly and/or could benefit from a cleaner template? If not, then I guess there is nothing more to do here. |
After all the back and forth, I don't see a viable path forward here. Systems that know the entire context (CSS in effect and so on) might be able to do something, but it's not clear that we can. Post-processing with any other kind of HTML minifier (outside this package) seems like a better solution than adding complexity to the API here. Going to mark this closed, as suggested in previous comment. |
In Go 1.6, text/template was modified to be able to automatically trim whitespace if the
-
character was added to the beginning or end of a template directive. This is really useful, but using it can make the template more verbose than it could be.For example, when templating html, precise whitespace control is necessary (html rendering is whitespace dependent). Option 1 is to litter html with
<!-- -->
comments, to trim whitespace between elements. This greatly obscures the original logic and bracing:Additionally, it doesn't work for wrapped html elements like the
img
tag.Option two is use whitespace consuming template directives:
This is cleaner than the html based comment since there is no line wrapping comments, and works with wrapped elements. However, it still includes a lot of
-
characters.Proposal
To this end, I would like to raise the idea of adding a complementary form of templates directives which use '+' delimiters, and introduce a
template.Option
stringtrim
which will implicitly make all directives act like {{- pipeline -}}. For example the code above would become:The comment directives would still be necessary to drop whitespace between lines, but all the
-
dash characters can then be removed. This makes it easier to see the layout and structure of the template without worrying about whitespace.The text was updated successfully, but these errors were encountered: