nddery / minimize-handlebars-attribs

A minimize plugin to handle Handlebars template in element attributes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minimize Handlebars attributes plugin Build Status

This is experimental, use in production is not recommended.

A minimize plugin to handle Handlebars template in element attributes

Usage

Note: This plugin is not compatible with the spare option.

var Minimize  = require('minimize'),
    hbattribs = require('minimize-handlebars-attribs'),
    minimize  = new Minimize({
      spare: false,
      plugins: [ hbattribs ]
    });

var html = '<div id="divID" class="my-class your-class" {{#ifCond clause ../../isTrue}}selected{{/ifCond}}>A div</div>';

minimize.parse(html, function(error, data) {
  // data output: <div id=divID class="my-class your-class" {{#ifcond clause ../../istrue}}selected{{/ifcond}}>A div</div>
});

Note that everything has been lowercase'd.

Why ?

Minifing HTML with minize will give you valid HTML. But what if when your HTML contains some Handlebars template ? What if you HTML looked like this:

<select name="preset">
  {{#forEach presets}}
    <option {{#ifCond this.key ../model.preset}}selected{{/ifCond}}>
      {{this.key}}
    </option>
  {{/forEach}}
</select>

Although this will produce valid HTML once it's parsed by Handlebars, it is not valid HTML at the time of minifying. Here's the output from running this through minimize:

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

Notice how the Handlebars template is all split up ?

This plugin attemps to mitigate the issue by re-structuring the tree output from htmlparser2, which is used by minimize. A downside is that everything will be lowercase'd.

Credits

minimize of Swaagie is used to parse and minify the tree rendered by node-htmlparser of fb55.

Running tests

npm test
npm run watch

About

A minimize plugin to handle Handlebars template in element attributes

License:MIT License


Languages

Language:JavaScript 100.0%