GithubHelp home page GithubHelp logo

nddery / minimize-handlebars-attribs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 168 KB

A minimize plugin to handle Handlebars template in element attributes

License: MIT License

JavaScript 100.00%

minimize-handlebars-attribs's Introduction

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

minimize-handlebars-attribs's People

Contributors

nddery avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.