定制 schleuse/dindent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

schleuse/dindent

Composer 安装命令:

composer require schleuse/dindent

包简介

Fork of gajus/dindent with various fixes from the community

README 文档

README

Coverage Status Latest Stable Version License

Dindent (aka., "HTML beautifier") will indent HTML for development and testing. Dedicated for those who suffer from reading a template engine produced markup.

Abuse Case

Dindent will not sanitize or otherwise manipulate your output beyond indentation.

If you are looking to remove malicious code or make sure that your document is standards compliant, consider the following alternatives:

If you only want to indent your code, beware that earlier mentioned libraries will attempt to fix your markup (that's their primary purpose; indentation is a by-product).

Regex

There is a good reason not to use regular expression to parse HTML. However, DOM parser will rebuild the whole HTML document. It will add missing tags, close open block tags, or remove anything that's not a valid HTML. This is what Tidy does, DOM, etc. This behavior is undesirable when debugging HTML output. Regex based parser will not rebuild the document. Dindent will only add indentation, without otherwise affecting the markup.

The above is also the reason why Chrome DevTools is not a direct replacement for Dindent.

Installation

The recommended way to use Dindent is through Composer.

composer req schleuse/dindent:^3.0

Use

$indenter = new \Gajus\Dindent\Indenter();
$indenter->indent('[..]');
In the above example, `[..]` is a placeholder for:
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>console.log('te> <st')</script>
    <script>
    function test () {
        test; <!-- <a> -->
    }
    </script>
    <div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <div><table border="1" style="background-color: red;"><tr><td>A cell    test!</td>
<td colspan="2" rowspan="2"><table border="1" style="background-color: green;"><tr> <td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr>
        <td><input><input><input></td></tr><tr><td>Cell</td><td>Cell</td><td>Ce
            ll</td></tr></table></td></tr><tr><td>Test <span>Ce       ll</span></td><td>Test <span>Ce ll</span> </td><td>Test <span>Ce </span> ll</td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div>
    <area>xx</area><xyz/><wbr><hr><hr/>
</body>
</html>
Dindent will convert it to:
<!DOCTYPE html>
<html>
    <head></head>
    <body>
        <script>console.log('te> <st')</script>
        <script>
    function test () {
        test; <!-- <a> -->
    }
        </script>
        <div>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
            <div>
                <table border="1" style="background-color: red;">
                    <tr>
                        <td>A cell test!</td>
                        <td colspan="2" rowspan="2">
                            <table border="1" style="background-color: green;">
                                <tr>
                                    <td>Cell</td>
                                    <td colspan="2" rowspan="2"></td>
                                </tr>
                                <tr>
                                    <td>
                                        <input>
                                        <input>
                                        <input>
                                    </td>
                                </tr>
                                <tr>
                                    <td>Cell</td>
                                    <td>Cell</td>
                                    <td>Ce ll</td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td>Test <span>Ce ll</span></td>
                        <td>Test <span>Ce ll</span></td>
                        <td>Test <span>Ce</span> ll</td>
                    </tr>
                    <tr>
                        <td>Cell</td>
                        <td>Cell</td>
                        <td>Cell</td>
                    </tr>
                </table>
            </div>
        </div>
        <area>xx</area>
        <xyz/>
        <wbr>
        <hr>
        <hr/>
    </body>
</html>

Options

Indenter constructor accepts the following options that control indentation:

Name Description Default
indentation_character Character(s) used for indentation. [ info; null removes all line-breaks ] 4 space-char
logging Keep Track of every transformation and verify. May be memory intensive. false

Set element type

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>

Dindent identifies the following elements as "inline":

dindent/src/Indenter.php

Lines 36 to 39 in 296a640

'a', 'abbr', 'b', 'bdi', 'bdo', 'big', 'cite',
'code', 'data', 'dfn', 'em', 'i', 'kbd', 'mark',
'q', 's', 'samp', 'small', 'span', 'strong',
'sub', 'sup', 'time', 'u', 'var', 'acronym','tt'

This is a subset of the inline elements ignoring void elements.

All other elements are treated as block.

You can set element type to either block or inline using setElementType method:

$indenter = new \Gajus\Dindent\Indenter();
/**
 * @param string $element_name Element name, e.g. "b".
 * @param ELEMENT_TYPE_BLOCK|ELEMENT_TYPE_INLINE $type
 * @return null
 */
$indenter->setElementType('foo', \Gajus\Dindent\Indenter::ELEMENT_TYPE_BLOCK);
$indenter->setElementType('bar', \Gajus\Dindent\Indenter::ELEMENT_TYPE_INLINE);

CLI

Dindent can be used via the CLI script;

php bin/dindent.php

dindent/bin/dindent.php

Lines 14 to 24 in 2ffc154

Options:
--input=./input_file.html
Input file
--indentation_character=" "
Character(s) used for indentation. Defaults to 4 whitespace characters.
--inline=""
A list of comma separated "inline" element names.
--block=""
A list of comma separated "block" element names.

Known issues

  • Does not treat comments nicely (ToCheck)

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 32
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-10-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固