webignition / html-document-type
用于解析、验证和创建HTML文档类型字符串
0.1.1
2018-03-14 15:21 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ~5.0
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-09-05 19:55:17 UTC
README
已测试PHP 5.6、7、7.1和7.2。
你是否曾想过
- 如果我能以编程方式创建文档类型字符串,我的生活会更轻松
- 如果能将文档类型字符串解析为其组成部分,我会更快乐
- 如何从一个HTML文档中获取文档类型字符串?
- 我眼前这个文档类型字符串有效吗?
- 我真的需要从这个文档字符串中获取FPI和URI,但我就是不知道怎么办!
这对你适用吗?如果是这样,那你真是太幸运了。
用法
创建
<?php use webignition\HtmlDocumentType\Factory; use webignition\HtmlDocumentType\HtmlDocumentType; use webignition\HtmlDocumentType\HtmlDocumentTypeInterface; $html5DocType = new HtmlDocumentType(); echo $html5DocType; // <!DOCTYPE html> $html401StrictDocType = new HtmlDocumentType( '-//W3C//DTD HTML 4.01//EN', 'http://www.w3.org/TR/html4/strict.dtd', HtmlDocumentTypeInterface::PUBLIC_SYSTEM_KEYWORD_SYSTEM ); echo html401StrictDocType; // <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
解析
如果你已经知道文档类型的组成部分,你不妨直接创建一个字符串。
更有用的通常是取一个现有的文档类型字符串并将其解析为其组成部分。
<?php use webignition\HtmlDocumentType\HtmlDocumentTypeInterface; use webignition\HtmlDocumentType\Parser\Parser; use webignition\HtmlDocumentType\Parser\ParserInterface; $parser = new Parser(); $html5DocTypeParts = $parser->parse('<!DOCTYPE html>'); var_dump($html5DocTypeParts); // array(3) { // 'public-system' => NULL // 'fpi' => NULL // 'uri' => NULL // } $html401StrictDocTypeParts = $parser->parse('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'); var_dump($html401StrictDocTypeParts); // array(3) { // 'public-system' => string(6) "PUBLIC" // 'fpi' => string(25) "-//W3C//DTD HTML 4.01//EN" // 'uri' => string(37) "http://www.w3.org/TR/html4/strict.dtd" // }
从HTML文档中获取文档类型字符串
解析文档类型字符串需要文档类型字符串。最常见的地方之一是在HTML文档中找到文档类型字符串。
<?php use webignition\HtmlDocumentType\Extractor; $html5DocTypeString = Extractor::extract('<!DOCTYPE html><html>...</html>'); echo $html5DocTypeString; // <!DOCTYPE html>
从文档类型字符串创建HtmlDocumentType
对象
你可以从HTML文档中提取文档类型字符串,但你接下来怎么办?一个选择是创建一个HtmlDocumentType
对象。
use webignition\HtmlDocumentType\Factory; // Create from a known doctype string $html5DocumentType = Factory::createFromDocTypeString('<!DOCTYPE html>'); echo $html5DocType; // <!DOCTYPE html> // Create from a HTML document $html5DocumentType = Factory::createFromHtmlDocument('<!DOCTYPE html><html>...</html>'); echo $html5DocType; // <!DOCTYPE html>
验证
文档类型的两个组成部分尤其与验证相关:fpi
和uri
。存在一组已知的有效fpi
值。对于每个有效的fpi
,都有一个已知的有效uri
值集。
需要检查您的文档类型的fpi
是否有效,以及相应的uri
对于该fpi
是否有效?
use webignition\HtmlDocumentType\Factory; use webignition\HtmlDocumentType\Validator; $validator = new Validator(); $html5DocumentType = Factory::createFromDocTypeString('<!DOCTYPE html>'); $validator->isValid($html5DocumentType); // true $html401StrictDocumentType = Factory::createFromDocTypeString('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'); $validator->isValid($html401StrictDocumentType); // true $invalidDocumentType = new HtmlDocumentType('invalid FPI'); $validator->isValid($invalidDocumentType); // false // If the uri doesn't match what is expected for the fpi, the doctype is considered invalid $laxHtml401StrictDocumentType = Factory::createFromDocTypeString('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "foo">'); $validator->isValid($laxHtml401StrictDocumentType); // false // You can relax the strictness of validity. This is useful in cases where a doctype string uses the uri of a draft version of a HTML spec (the doctype string was once valid but is no longer) $validator->setMode(Validator::MODE_IGNORE_FPI_URI_VALIDITY); $validator->isValid($laxHtml401StrictDocumentType); // true // Relaxing validity strictness has no impact on invalid fpi values $validator->setMode(Validator::MODE_IGNORE_FPI_URI_VALIDITY); $validator->isValid($invalidDocumentType); // false
开发
克隆
您需要git
来克隆和composer
来检索依赖项。我假设它们都在您的路径中。按需调整。
git clone git@github.com:webignition/html-document-type.git
cd html-document-type
composer install
测试
该项目使用phpcs
检查PSR2编码标准,使用phpunit
运行测试。这两个都是通过composer作为开发依赖项安装的。
Composer脚本可用于运行CS检查、运行测试或同时运行两者。
# Coding standards checks composer cs > ./vendor/bin/phpcs src tests --colors --standard=PSR2 # Tests composer test > ./vendor/bin/phpunit --colors=always PHPUnit 5.7.27 by Sebastian Bergmann and contributors. ................................ (and so on) # Coding standards checks then tests composer ci > ./vendor/bin/phpcs src tests --colors --standard=PSR2 > ./vendor/bin/phpunit --colors=always PHPUnit 5.7.27 by Sebastian Bergmann and contributors. .................................... (and so on)