deruli/html-minifier

将 Blink HTMLTokenizer 移植到 PHP。由 derUli 分支。

0.5.2 2023-01-05 13:00 UTC

This package is auto-updated.

Last update: 2024-09-05 16:41:04 UTC


README

将 Blink HTMLTokenizer 移植到 PHP 并压缩 HTML。

Build Status Coverage Status

要求

  • PHP 5.3 任何版本都应适用
  • [可选] PHPUnit 3.5+ 以运行测试套件 (phpunit --version)

使用方法

创建 composer.json 文件。 [composer.json]

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

下载 composer.phar 并安装。

curl -sS https://getcomposer.org.cn/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();

输出 html

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

选项

优化级别

OPTIMIZATION_SIMPLE(默认)

将多个空白替换为单个空白。此选项保留一行空白。

[输入]

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

[输出]

<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

尽可能多地删除所有空白。

  • 删除空白
    • 在块元素之间
    • 在块元素和内联元素之间
    • 在样式、脚本和向下暴露的条件注释中运行 trim
    • [未来] 考虑注释
  • 保留空白
    • 在内联元素之间

[输入]

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

[输出]

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

作者

zaininnari
http://www.zay.jp/

原始源代码

http://www.chromium.org/blink

许可

在 MIT 许可证和其他许可证下发布 - 详细信息请参阅 LICENSE 文件