Skip to content

wasinger/html-pretty-min

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTML Pretty-Min

tests Latest Version

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.

Currently, it has the following features:

  • Prettyprint:

    • Indent Block-level elements, do not indent inline elements
  • Minify:

    • 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.

composer require wa72/html-pretty-min

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().

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

    '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:

$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

About

PHP library for minifying and indenting HTML documents that works on the DOM tree

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy