akankov/html-min 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

akankov/html-min

最新稳定版本:v2.5.1

Composer 安装命令:

composer require akankov/html-min

包简介

HTML compressor and minifier for PHP 8.3+

README 文档

README

CI Latest Stable Version Monthly Downloads Dependents License

html-min

A fast HTML5 compressor and minifier for PHP. Strips redundant whitespace, comments, optional tags, and default attributes, then sorts what's left so your gzip layer has less work to do.

Built on native \DOMDocument — no third-party DOM dependencies.

Requirements

  • PHP 8.3, 8.4, or 8.5
  • ext-dom, ext-libxml, ext-mbstring

Installation

composer require akankov/html-min

Usage

use Akankov\HtmlMin\HtmlMin;

$html = <<<HTML
<html>
  <body>
    <ul style="">
      <li style="display: inline;" class="foo">One</li>
      <li class="foo" style="display: inline;">Two</li>
    </ul>
  </body>
</html>
HTML;

echo (new HtmlMin())->minify($html);
// <html><body><ul><li class=foo style="display: inline;">One<li class=foo style="display: inline;">Two</ul>

Wrap any block in <nocompress>…</nocompress> to keep its whitespace intact.

Configuration

Every option is a chainable setter. All defaults are shown — the example below reproduces the default configuration.

$htmlMin = (new HtmlMin())
    // Core
    ->doOptimizeViaHtmlDomParser(true)   // run the DOM-based pass (required for most of the flags below)
    ->doRemoveComments(true)             // drop HTML comments (conditional comments are preserved)
    ->doSumUpWhitespace(true)            // collapse runs of whitespace in text nodes
    ->doRemoveWhitespaceAroundTags(false)// aggressive: also trim whitespace adjacent to block tags
    ->doRemoveSpacesBetweenTags(false)   // aggressive: remove whitespace-only text nodes between elements

    // Attribute optimization
    ->doOptimizeAttributes(true)
    ->doSortHtmlAttributes(true)         // canonical attribute order → better gzip
    ->doSortCssClassNames(true)          // canonical class order → better gzip
    ->doRemoveOmittedQuotes(true)        // class="foo" → class=foo when safe
    ->doRemoveOmittedHtmlTags(true)      // <p>x</p> → <p>x where the closing tag is optional
    ->doRemoveEmptyAttributes(true)
    ->doRemoveValueFromEmptyInput(true)
    ->doRemoveDefaultAttributes(false)   // opt-in: drop defaults like form method=get

    // URL attribute trimming
    ->doRemoveHttpPrefixFromAttributes(false)
    ->doRemoveHttpsPrefixFromAttributes(false)
    ->doKeepHttpAndHttpsPrefixOnExternalAttributes(false)
    ->doMakeSameDomainsLinksRelative([])       // e.g. ['example.com'] → strip host from same-site links

    // Deprecated attribute cleanup
    ->doRemoveDeprecatedAnchorName(true)
    ->doRemoveDeprecatedScriptCharsetAttribute(true)
    ->doRemoveDeprecatedTypeFromScriptTag(true)
    ->doRemoveDeprecatedTypeFromStylesheetLink(true)
    ->doRemoveDeprecatedTypeFromStyleAndLinkTag(true)
    ->doRemoveDefaultMediaTypeFromStyleAndLinkTag(true)
    ->doRemoveDefaultTypeFromButton(false);

echo $htmlMin->minify($html);

Each setter returns $this, so you can configure and call minify() in one chain.

Extending

To run your own pass over every element during minification, implement Akankov\HtmlMin\Contract\DomObserver and register it:

use Akankov\HtmlMin\Contract\DomObserver;
use Akankov\HtmlMin\Contract\HtmlMinInterface;
use Akankov\HtmlMin\HtmlMin;

final class StripDataTestIds implements DomObserver
{
    public function domElementBeforeMinification(\DOMElement $element, HtmlMinInterface $htmlMin): void
    {
    }

    public function domElementAfterMinification(\DOMElement $element, HtmlMinInterface $htmlMin): void
    {
        if ($element->hasAttribute('data-testid')) {
            $element->removeAttribute('data-testid');
        }
    }
}

$htmlMin = new HtmlMin();
$htmlMin->attachObserverToTheDomLoop(new StripDataTestIds());
echo $htmlMin->minify($html);

Benchmarks

Measured against voku/html-min, wyrihaximus/html-compress, zaininnari/html-minifier, and abordage/html-min on a corpus of real-world HTML pages.

adapter median ms/op geomean ms/op parse failures avg gzipped ratio
akankov/html-min 2.1 1.9 0 / 11 90.9%
voku/html-min 3.0 3.2 0 / 11 90.9%
wyrihaximus/html-compress 5.4 7.1 0 / 11 86.8%
zaininnari/html-minifier 9.5 7.5 0 / 11 92.9%
abordage/html-min † 0.2 0.2 0 / 11 86.9%

The table above is regenerated by make bench from the latest run. See latest.md for the per-fixture detail (speed, peak memory, gzipped compression ratio, methodology, and non-claims). Reproduce with make bench-install && make bench (requires Docker).

Development

composer install
make md-check                 # markdown formatting (Docker)
vendor/bin/phpunit            # tests
vendor/bin/phpstan analyse    # static analysis (level max)
vendor/bin/php-cs-fixer fix   # code style

CI runs the full matrix (PHP 8.3 / 8.4 / 8.5) on every push and pull request.

License

MIT — see LICENSE.

Originally authored by Lars Moelleken; maintained in this fork by Alex Kankov.

统计信息

  • 总下载量: 20.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 4
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: HTML

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固