Giter Club home page Giter Club logo

html-minifier's Introduction

html-minifier

The Blink HTMLTokenizer ported to PHP and minify HTML.

Build Status Coverage Status

Requirements

  • Any flavor of PHP 5.3 should do
  • [optional] PHPUnit 3.5+ to execute the test suite (phpunit --version)

Use

Create composer.json. [composer.json]

{
    "require": {
        "zaininnari/html-minifier": "*"
    }
}

Download composer.phar and install.

curl -sS https://getcomposer.org/installer | php
php composer.phar install
<?php
require 'vendor/autoload.php';

use zz\Html\HTMLMinify;

$html = '<div>
    <p>
        text
    </p> 
</div>';

// shortcut. retrun minify html
$minify = HTMLMinify::minify($html);

// detail
$HTMLMinify = new HTMLMinify($html);
$minify = $HTMLMinify->process();

output html

<div>
<p>
text
</p> 
</div>

Option

optimizationLevel

OPTIMIZATION_SIMPLE(default)

Replace many whitespace to a single whitespace. This option leave a new line of one.

[input]

<section>
    <h1>Example of paragraphs</h1>
    This is the <em>first</em> paragraph in this example.
    <p>This is the second.</p>
    <!-- This is not a paragraph. -->
    <!--[if expression]> HTML <![endif]-->
    <![if expression]> HTML <![endif]>
</section>

[output]

<section>
<h1>Example of paragraphs</h1>
This is the <em>first</em> paragraph in this example.
<p>This is the second.</p>
<!--[if expression]> HTML <![endif]-->
<![if expression]> HTML <![endif]>
</section>

OPTIMIZATION_ADVANCED

Remove the whitespace of all as much as possible.

  • Remove whitespace
    • between block element and block element
    • between block element and inline element
    • run trim in style, script and downlevel-revealed conditional comment
    • [future] consider comments
  • Preserve whitespace
    • between inline element and inline element

[input]

<section>
    <h1>Example of paragraphs</h1>
    This is the <em>first</em> paragraph in this example.
    <p>This is the second.</p>
    <!-- This is not a paragraph. -->
    <!--[if expression]> HTML <![endif]-->
    <![if expression]> HTML <![endif]>
</section>

[output]

<section><h1>Example of paragraphs</h1>This is the<em>first</em>paragraph in this example.<p>This is the second.</p><!--[if expression]> HTML <![endif]--><![if expression]>HTML<![endif]></section>

Author

zaininnari
http://www.zay.jp/

Original source

http://www.chromium.org/blink

License

Licensed under the MIT License and other License - see the LICENSE file for details

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.