mallardduck/html-formatter
Composer 安装命令:
composer require mallardduck/html-formatter
包简介
HTML beautifier and minifier library for development and testing.
README 文档
README
1. What Is It
HTML Formatter will beautify or minify your HTML string for development and testing. Dedicated for those who suffer from reading a template engine produced markup.
2. What Is It Not
HTML Formatter will not sanitize or otherwise manipulate your output beyond indentation and whitespace replacement. HTML Formatter will only add indentation, without otherwise affecting the markup.
If you are looking to remove malicious code or make sure that your document is standards compliant, consider the following alternatives:
If you need to format your code in the development environment, beware that earlier mentioned libraries will attempt to fix your markup.
3. Installation
This package can be installed through Composer.
composer require mallardduck/html-formatter
4. Usage
use MallardDuck\HtmlFormatter\Formatter; $input = 'This is your HTML code.'; $formatter = new Formatter(); $output = $formatter->beautify($input);
5. Configuration
Formatter is using Simple config to adjust its configuration settings.
| Name | Type | Description |
|---|---|---|
tab |
string | Character(s) used for indentation. Defaults to 4 spaces. |
self-closing.tag |
array | Here you can add more self-closing tags. see below |
inline.tag |
array | Here you can add more inline tags, or change any block tags and make them inline. see below |
formatted.tag |
array | Here you can add more tags to be exluded of formatting. see below |
attributes.trim |
boolean | Remove leading and trailing whitespace in the attribute values. |
attribute.cleanup |
boolean | Replace all whitespaces with a single space in the attribute values. |
cdata.cleanup |
boolean | Replace all whitespaces with a single space in CDATA. |
cdata.trim |
boolean | Remove leading and trailing whitespace in CDATA. |
5.1. Inline and block elements
HTML elements are either "inline" elements or "block-level" elements.
An inline element occupies only the space bounded by the tags that define the inline element. The following example demonstrates the inline element's influence:
<p>This is an <span>inline</span> element within a block element.</p>
A block-level element occupies the entire space of its parent element (container), thereby creating a "block." Browsers typically display the block-level element with a new line both before and after the element. The following example demonstrates the block-level element's influence:
<div> <p>This is a block element within a block element.</p> </div>
HTML Formatter identifies the following elements as "inline":
a,abbr,acronym,b,bdo,big,br,button,cite,code,dfn,em,i,img,kbd,label,samp,small,span,strong,sub,sup,tt,var
This is a subset of the inline elements defined in the MDN. All other elements are treated as block.
You can set additional inline elements by adding them to the inline.tag option.
use MallardDuck\HtmlFormatter\Formatter; $formatter = new Formatter(); $config = $formatter->getConfig(); $config->append('inline.tag', 'foo')->subtract('inline.tag', ['bar', 'baz']); $formatter->setConfig($config);
5.2. Self-closing (empty) elements
An empty element is an element from HTML, SVG, or MathML that cannot have any child nodes (i.e., nested elements or text nodes).
HTML Formatter identifies the following elements as "inline":
area,base,br,col,command,embed,hr,img,input,keygen,link,menuitem,meta,param,source,track,wbr,animate,stop,path,circle,line,polyline,rect,use
This is a subset of the empty elements defined in the MDN. All other elements require closing tag.
You can set additional self-closing elements by adding them to the self-closing.tag option.
use MallardDuck\HtmlFormatter\Formatter; $formatter = new Formatter(); $formatter->setConfig($formatter->getConfig()->append('self-closing.tag', ['foo', 'bar']));
5.3. Preformatted elements
Specific element will be not touched by the formatter. The built in preformatted elements are
script,pre,textarea.
You can exclude additional elements by adding them to the formatted.tag option.
There settings for all the formatted tags are the following:
| Setting | Type | Description |
|---|---|---|
formatted.tag.cleanup-empty |
boolean | Removes the inner content if it has only whitespace. |
formatted.tag.opening-break |
boolean | Inserts a line break after the opening tag. |
formatted.tag.closing-break |
boolean | Inserts a line break before the closing tag. |
formatted.tag.trim |
boolean | Removes the leading and trailing whitespace of the content. |
You can also change the settings for a specific tag. For example, disabling the cleanup-empty setting for the script tag looks like this:
use MallardDuck\HtmlFormatter\Formatter; $formatter = new Formatter(); $config = $formatter->getConfig(); $config->set('formatted.tag.script.cleanup-empty', false); $formatter->setConfig($config);
5.4. Attributes
Additional settings for formatted tags are the following:
| Setting | Type | Description |
|---|---|---|
cleanup-empty |
boolean | Removes the inner content if it has only whitespace. |
opening-break |
boolean | Inserts a line break after the opening tag. |
closing-break |
boolean | Inserts a line break before the closing tag. |
trim |
boolean | Removes the leading and trailing whitespace of the content. |
6. Methods
| Name | Attributes | Description | Example |
|---|---|---|---|
constructor |
Config|array|null | Creates a Formatter class instance |
$f = new Formatter($config); |
getConfig |
- | Configuration getter | $config = $f->getConfig(); |
getConfigArray |
- | Configuration getter | $config = $f->getConfigArray(); |
setConfig |
Config|array|null | Configuration setter | $f->setConfig($config); |
beautify |
string | Beautifies the input string | $output = $f->beautify($html); |
minify |
string | Minifies the input string | $output = $f->minify($html); |
7. Credits
Thanks to Gajus Kuizinas for originally creating gajus/dindent and all the other developers who are tirelessly working on it. HTML Formatter was inspired by Dindent.
mallardduck/html-formatter 适用场景与选型建议
mallardduck/html-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.81k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「minify」 「format」 「beautify」 「indent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mallardduck/html-formatter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mallardduck/html-formatter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mallardduck/html-formatter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Provides a simple decimal/roman number converter
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
Softonic OpenApi validation formats extension
Tools to convert between .NET and PHP date formats
Caching and compression for Twig assets (JavaScript and CSS).
统计信息
- 总下载量: 5.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-27