wikimedia/css-sanitizer

用于解析和清理CSS的类

v5.3.0 2024-04-18 20:06 UTC

This package is auto-updated.

Last update: 2024-09-17 14:02:46 UTC


README

Latest Stable Version License

Wikimedia CSS解析器 & 清理器

此库在PHP中实现了CSS分词器、解析器和语法匹配器。

用法

use Wikimedia\CSS\Parser\Parser;
use Wikimedia\CSS\Sanitizer\StylesheetSanitizer;

/** Parse a stylesheet from a string **/

$parser = Parser::newFromString( $cssText );
$stylesheet = $parser->parseStylesheet();

/** Report any parser errors **/

foreach ( $parser->getParseErrors() as list( $code, $line, $pos ) ) {
	// $code is a string that should be suitable as a key for an i18n library.
	// See errors.md for details.
	$error = lookupI18nMessage( "css-parse-error-$code" );
	echo "Parse error: $error at line $line character $pos\n";
}

/** Apply sanitization to the stylesheet **/

// If you need to customize the defaults, copy the code of this method and
// modify it.
$sanitizer = StylesheetSanitizer::newDefault();
$newStylesheet = $sanitizer->sanitize( $stylesheet );

/** Report any sanitizer errors **/

foreach ( $sanitizer->getSanitizationErrors() as list( $code, $line, $pos ) ) {
	// $code is a string that should be suitable as a key for an i18n library.
	// See errors.md for details.
	$error = lookupI18nMessage( "css-sanitization-error-$code" );
	echo "Sanitization error: $error at line $line character $pos\n";
}

/** Convert the sanitized stylesheet back to text **/

$newText = (string)$newStylesheet;

// Or if you'd rather have it minified too
$minifiedText = Wikimedia\CSS\Util::stringify( $newStylesheet, [ 'minify' => true ] );

一致性

该库遵循以下语法规范

清理器识别以下CSS模块

还有,

运行测试

composer install --prefer-dist
composer test

发布新版本

此包使用 wikimedia/update-history 及其约定。

有关详细信息,请参阅 https://www.mediawiki.org/wiki/UpdateHistory

历史

我们需要一个具有多个属性的CSS清理器

  • 严格按照现代标准进行解析。
  • 包括所有错误的行和字符位置。
  • 可配置以限制不安全的结构,例如外部URL引用。
  • 错误易于本地化。

我们没有找到符合这些要求的库,因此我们创建了一个。

额外的发布历史记录在 HISTORY.md 中。