Giter Club home page Giter Club logo

html-pretty-min's Introduction

HTML Pretty-Min

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

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

html-pretty-min's People

Contributors

peter279k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

html-pretty-min's Issues

DOMDocument::loadHTML(): Tag article invalid in Entity

Encountering unexpected error while parsing HTML5 tags:

DOMDocument::loadHTML(): Tag article invalid in Entity, line: 7
D:\...\Pasap\vendor\wa72\html-pretty-min\PrettyMin.php:76

Here is the code I'm trying to minify, https://validator.w3.org says it's valid:

<!DOCTYPE html><html lang="en">
    <head>
        <meta charset="UTF-8"/>
        <title>Document</title>
    </head>
    <body>
        <article class="News"> <!-- It blocks here -->
    <h1 class="title">We are Amazing</h1>
    <div class="about">
        <em>
            By Me,
            At 12-12-12     </em>
    </div>
    <div class="content">


                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                    Atque, consequatur cum iste laudantium libero magni maxime
                    minima mollitia quos. At earum nostrum pariatur. Amet, at
                    fugiat incidunt officia possimus vel?
                </p>
                </div>
</article>

    </body>
</html>

It seems to be a known problem: http://stackoverflow.com/questions/6090667/php-domdocument-errors-warnings-on-html5-tags

Rewrites Thai characters into gibberish

If any text node includes Thai characters, the script ends up rewriting them into HTML-escaped characters, but not the right ones.

Input:

<p>โหลด-เกม</p>

Output:

<p>&agrave;&sup1;&#130;&agrave;&cedil;&laquo;&agrave;&cedil;&yen;&agrave;&cedil;&#148; &agrave;&sup1;&#128;&agrave;&cedil;&#129;&agrave;&cedil;&iexcl;</p>

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.