Swaagie / minimize

Minimize HTML
MIT License
163 stars 18 forks source link

Handlebars helper in <option> tag for selected attribute #52

Closed nddery closed 8 years ago

nddery commented 9 years ago

I'm having an issue minimizing an Handlebars template.

<option {{#ifCond this.key ../model.preset}}selected{{/ifCond}}>
  {{this.key}}
</option>

get's minimized as:

<option {{#ifcond="" this.key="" ..="" model.preset}}selected{{="" ifcond}}="">{{this.key}}</option>

It seems like minimize is trying to minimize my Handlebars template into xHTML compliant markup. If I put my Handlebars helper directly in an element attribute (between the quotes), it will get minimized just fine, see example below.

Source HTML

<!-- this works just fine! -->
<div class="{{#ifCond model.is_available false}}disabled{{/ifCond}}"></div>

<!-- bad! minimize hates on badly formatted HTML I guess -->
<div {{#ifCond model.is_available false}}class="disabled"{{/ifCond}}></div>

Minimized HTML (formatted for readability):

<!-- good! -->
<div class="{{#ifCond model.is_available false}}disabled{{/ifCond}}"></div>

<!-- bad -->
<div {{#ifcond="" model.is_available="" false}}class="disabled" {{="" ifcond}}=""></div>

It is easy to fix the example just above, however, I am not sure how to deal with the selected attribute. Is there a way to indicate to minimize not to parse some HTML ?

Thanks!

nddery commented 9 years ago

Just re-read the README and saw that

Minimize does not correctly parse inline PHP or raw template files. Simply because this is not valid HTML and never will be either. The output of the templaters should be parsed and minified.

I imagine Handlebars template are considered raw template file ? Is there an option to not parse the HTML (or some of it) and simply minimize it instead ?

I'm looking into writing (or finding) a plugin that could skip over Handlebars expressions and helpers.

Swaagie commented 9 years ago

Yup handlebars are also template files. I'm not sure if a plugin would be helpful here. The problem lies in the fact that htmlparser2 transforms the HTML to a tree. And it usually just parses the assignments inside template directives as html attributes. There is little that can be done about this. Your welcome to try writing a plugin, but I highly doubt you'll receive normal input in the iterator. At that point the the template directives are scrambled already. The better option is to first parse the template and then process the outputted HTML with minimize.

nddery commented 9 years ago

I was just looking at the output of htmlparser2 and was unfortunately going to the same conclusion.. Unfortunately, we are processing the template on the browser so this won't be possible for us (at least for now, apparently support for that is on it's way). However, I'm not sure I see the benefit of minifying on the client, as you've already sent the extra bytes you'd gain from minification ?

Swaagie commented 8 years ago

Solved by plugin