2017 © Pedro Peláez
 

library html-pretty-min

HTML minifier and indenter that works on the DOM tree

image

wa72/html-pretty-min

HTML minifier and indenter that works on the DOM tree

  • Friday, May 11, 2018
  • by wasinger
  • Repository
  • 1 Watchers
  • 8 Stars
  • 9,091 Installations
  • PHP
  • 3 Dependents
  • 2 Suggesters
  • 2 Forks
  • 2 Open issues
  • 7 Versions
  • 17 % Grown

The README.md

HTML Pretty-Min

tests Latest Version, (*1)

HTML Pretty-Min is a PHP library for minifying and prettyprinting (indenting) HTML documents that works directly on the DOM tree of an HTML document., (*2)

Currently, it has the following features:, (*3)

  • Prettyprint:, (*4)

    • Indent Block-level elements, do not indent inline elements
  • Minify:, (*5)

    • Remove whitespace and newlines
    • Compress embedded Javascript using mrclay/jsmin-php
    • Compress embedded CSS using tubalmartin/cssmin
    • Remove some attributes when their value is empty (by default "style" and "class" attributes)
    • Remove comments, except those matching some given regular expressions (by default, IE conditional comments are kept)

Installation

HTML Pretty-Min is listed on Packagist., (*6)

composer require wa72/html-pretty-min, (*7)

Usage

<?php
use Wa72\HtmlPrettymin\PrettyMin;

$pm = new PrettyMin();

$output = $pm
    ->load($html)   // $html may be a \DOMDocument, a string containing an HTML code, 
                    // or an \SplFileInfo pointing to an HTML document
    ->minify()
    ->saveHtml();

For prettyprinting, call the indent() method instead of minify()., (*8)

The PrettyMin() constructor can be given an associative options array. Here are the possible option keys and their default values:, (*9)

    'minify_js' => true,
    'minify_css' => true,
    'remove_comments' => true,
    'remove_comments_exeptions' => ['/^\[if /'],
    'keep_whitespace_around' => [
        // keep whitespace around all inline elements
        'b', 'big', 'i', 'small', 'tt',
        'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var',
        'a', 'bdo', 'br', 'img', 'map', 'object', 'q', 'span', 'sub', 'sup',
        'button', 'input', 'label', 'select', 'textarea'
    ],
    'keep_whitespace_in' => ['script', 'style', 'pre'],
    'remove_empty_attributes' => ['style', 'class'],
    'indent_characters' => "\t"

Attention: Because the formatting is done directly on the DOM tree, a DOMDocument object given to the load() method will be modified:, (*10)

$dom_document = new \DOMDocument('1.0', 'UTF-8');
$dom_document->loadHTML('<html>...some html code...</html>');

$pm->load($dom_document)->minify();

echo $dom_document->saveHTML(); // Will output the minified, not the original, document

The Versions

11/05 2018

dev-master

9999999-dev

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

The Development Requires

08/03 2018

v0.2.0

0.2.0.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

The Development Requires

22/03 2016

v0.1.4

0.1.4.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

10/12 2015

v0.1.3

0.1.3.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

09/11 2015

v0.1.2

0.1.2.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

05/11 2015

0.1.1

0.1.1.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires

 

05/11 2015

0.1.0

0.1.0.0

HTML minifier and indenter that works on the DOM tree

  Sources   Download

MIT

The Requires