drupol/htmltag

这是一个用于生成HTML标签、属性和内容的快速且可扩展的帮助库。

维护者

详细信息

github.com/drupol/htmltag

源代码

问题

资助包维护!
drupol

2.2.3 2019-08-13 14:13 UTC

This package is auto-updated.

Last update: 2024-09-17 04:28:02 UTC


README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Donate! Donate!

HTMLTag

描述

这是一个PHP库,用于处理HTML标签、属性和内容。

重点是安全性、速度和可用性。

要求

  • PHP >= 7.1.3

安装

composer require drupol/htmltag

使用

<?php

include 'vendor/autoload.php';

// Meta object.
$meta = \drupol\htmltag\HtmlTag::tag('meta', ['name' => 'author']);
$meta->attr('content', 'pol dellaiera');

// Title object.
$title = \drupol\htmltag\HtmlTag::tag('h1', ['class' => 'title'], 'Welcome to HTMLTag');

// Paragraph object.
$paragraph = \drupol\htmltag\HtmlTag::tag('p', ['class' => 'section']);
$paragraph->attr('class')->append('paragraph');
$paragraph->content('This library helps you create HTML.');

// Simple footer
$footer = \drupol\htmltag\HtmlTag::tag('footer', [], 'Thanks for using it!');

// Body tag.
// Add content that can be transformed into strings.
$body = \drupol\htmltag\HtmlTag::tag('body', [], [$title, $paragraph, $footer]);

// Fix something that was already added.
$paragraph->attr('class')->remove('section')->replace('paragraph', 'description');

// Alter the values of a specific attributes.
$meta->attr('content')->alter(
    function ($values) {
        return array_map('strtoupper', $values);
    }
);

echo $meta . $body;

将打印

<meta content="POL DELLAIERA" name="author"/>
<body>
  <h1 class="title">Welcome to HTMLTag</h1>
  <p class="description">This library helps you create HTML.</p>
  <footer>Thanks for using it!</footer>
</body>

HTML Builder

该库包含一个HTML Builder类,允许您快速创建HTML内容。

<?php 

include 'vendor/autoload.php';

$builder = new \drupol\htmltag\HtmlBuilder();

$html = $builder
    ->c(' Comment 1 ') // Add a comment
    ->p(['class' => ['paragraph']], 'some content')
    ->div(['class' => 'container'], 'this is a simple div')
    ->_() // End tag <div>
    ->c(' Comment 2 ')
    ->region([], 'region with <unsafe> tags')
    ->_()
    ->c(' Comment 3 ')
    ->a()
    ->c(' Comment 4 ')
    ->span(['class' => 'link'], 'Link content')
    ->_()
    ->div(['class' => 'Unsecure "classes"'], 'Unsecure <a href="#">content</a>')
    ->_()
    ->c(' Comment 5 ');

echo $html;

这将生成

<!-- Comment 1 -->
<p class="paragraph">
  some content
  <div class="container">
    this is a simple div
  </div>
</p>
<!-- Comment 2 -->
<region>
  region with &lt;unsafe&gt; tags
</region>
<!-- Comment 3 -->
<a>
  <!-- Comment 4 -->
  <span class="link">
    Link content
  </span>
</a>
<div class="Unsecure &quot;classes&quot;">
  Unsecure &lt;a href=&quot;#&quot;&gt;content&lt;/a&gt;
</div>
<!-- Comment 5 -->

技术说明

标签分析

 The tag name              An attribute                 The content
  |                              |                           |
 ++-+                      +-----+-----+                +----+-----+
 |  |                      |           |                |          |
 
<body class="content node" id="node-123" data-clickable>Hello world!</body>

      |                                               |
      +-----------------------+-----------------------+
                              |
                        The attributes

该库围绕3个对象构建。

  • 处理属性、标签名称和内容的Tag对象
  • 处理属性的Attributes对象
  • 处理由名称及其值组成的属性的Attribute对象

Tag对象使用Attributes对象,它基本上是Attribute对象的存储。您可以单独使用这些对象中的任何一个。

所有方法都通过接口进行了文档记录,您的IDE应在需要时能够自动完成。

大多数方法参数都是可变参数,可以接受无限嵌套的值或值的数组。您还可以链式调用大多数方法。

允许的值类型几乎可以是任何东西。如果是一个对象,它必须实现__toString()方法。

示例

方法链

<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag
    ->attr('class', ['FRONT', ['NODE', ['sidebra']], 'node', '  a', '  b  ', [' c']])
    ->replace('sidebra', 'sidebar')
    ->alter(
        function ($values) {
            $values = array_map('strtolower', $values);
            $values = array_unique($values);
            $values = array_map('trim', $values);
            natcasesort($values);

            return $values;
        }
    );
$tag->content('Hello world');

echo $tag; // <body class="a b c front node sidebar">Hello world</body>

以下所有示例都将生成相同的HTML。

<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag->attr('class', ['front', ['node', ['sidebar']]]);
$tag->content('Hello world');

echo $tag; // <body class="front node sidebar">Hello world</body>
<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag->attr('class', 'front', 'node', 'sidebar');
$tag->content('Hello world');

echo $tag; // <body class="front node sidebar">Hello world</body>
<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag->attr('class', ['front', 'node', 'sidebar']);
$tag->content('Hello world');

echo $tag; // <body class="front node sidebar">Hello world</body>
<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag->attr('class', 'front node sidebar');
$tag->content('Hello world');

echo $tag; // <body class="front node sidebar">Hello world</body>

Tag对象

<?php 

include 'vendor/autoload.php';

$tag = \drupol\htmltag\HtmlTag::tag('body');
$tag->attr('class', 'front');
$tag->content('Hello world');

echo $tag; // <body class="front">Hello world</body>

Attributes对象

<?php 

include 'vendor/autoload.php';

$attributes = \drupol\htmltag\HtmlTag::attributes();
$attributes->append('class', 'a', 'b', 'c');
$attributes->append('id', 'htmltag');

// Hence the trailing space before the class attribute.
echo $attributes; //  class="a b c" id="htmltag"

Attribute对象

<?php 

include 'vendor/autoload.php';

$attribute = \drupol\htmltag\HtmlTag::attribute('class', 'section');

echo $attribute; // class="section"

依赖注入和扩展

感谢库中提供的工厂,可以使用不同的类替换默认类。

例如:您希望对“class”属性进行特殊处理。

<?php 

include 'vendor/autoload.php';

class MyCustomAttributeClass extends \drupol\htmltag\Attribute\Attribute {

    protected function preprocess(array $values, array $context = []) {
        // Remove duplicated values.
        $values = array_unique($values);

        // Trim values.
        $values = array_map('trim', $values);

        // Convert to lower case
        $values = array_map('strtolower', $values);

        // Sort values.
        natcasesort($values);

        return $values;
    }
}

\drupol\htmltag\Attribute\AttributeFactory::$registry['class'] = MyCustomAttributeClass::class;

$tag = HtmlTag::tag('p');

// Add a class attribute and some values.
$tag->attr('class', 'E', 'C', ['A', 'B'], 'D', 'A', ' F ');
// Add a random attribute and the same values.
$tag->attr('data-original', 'e', 'c', ['a', 'b'], 'd', 'a', ' f ');

echo $tag; // <p class="a b c d e f" data-original="e c a b d a  f "/>

Tag类也是如此。

安全

为了避免安全问题,所有打印的对象都被转义。

如果使用对象作为输入并且它们实现了__toString()方法,它们将被转换为字符串。确保它们打印的是不安全的输出,这样就不会被转义两次,这是用户的责任。

代码质量、测试和基准测试

每次向库中引入更改时,Travis CI都会运行测试和基准测试。

该库使用PHPSpec编写了测试。您可以在spec目录中查看它们。运行composer phpspec以触发测试。

PHPBench用于基准测试库,要运行基准测试:composer bench

PHPInfection用于确保您的代码得到了适当的测试,运行composer infection以测试您的代码。

贡献

请随时通过发送Github pull请求来为此库做出贡献。我非常积极: