shulyak/crossjoin-css

CSS阅读器和写入器,全面支持CSS3,已支持当前CSS4规范的大部分内容。支持媒体查询、注释、值优化等... 提供完整的Unicode支持,并能处理大型CSS源文件。需要PHP 5.4+。

1.0.0 2021-11-10 11:41 UTC

This package is auto-updated.

Last update: 2024-09-10 17:48:27 UTC


README

#CSS阅读器和写入器

##介绍 全面支持CSS3的CSS阅读器和写入器,已支持当前CSS4规范的大部分内容。支持媒体查询、注释、值优化等... 提供完整的Unicode支持,并能处理大型CSS源文件。

##安装 这是一个composer包。有关基本安装信息,请参阅composer网站

将以下行添加到您的composer.json文件中

{
    "require": {
        "shulyak/crossjoin-css": "1.0.*"
    }
}

###需求

  • PHP 5.4+
  • 多字节字符串(mbstring)扩展
  • DOM扩展(从HTML源中提取CSS)

##功能

  • 全面支持CSS3
  • 全面支持Unicode
  • 支持CSS4的大部分内容
  • 支持无限嵌套级别的at-rules
  • 支持CSS注释
  • 可处理大型、压缩的CSS文件
  • 集成检查无效CSS
  • ...

##基本使用

###读取CSS 您可以从不同的源读取CSS信息。

// Read CSS file
$cssFileName = "path/to/file.css";
$reader = new \Crossjoin\Css\Reader\CssFile($cssFileName);

// Read CSS string
$cssString = "body { background:red; }";
$reader = new \Crossjoin\Css\Reader\CssFile($cssString);

// Extract CSS from <style> tags in a HTML file
// (multiple tags are merges to one style sheet)
$htmlFileName = "path/to/file.html";
$reader = new \Crossjoin\Css\Reader\HtmlFile($htmlFileName);

// Extract CSS from <style> tags in a HTML string
// (multiple tags are merges to one style sheet)
$htmlString = "<html><head><style>body{color:red;}</style></head><body></body></html>";
$reader = new \Crossjoin\Css\Reader\HtmlFile($htmlString);

###字符集检测 CSS文件的字符集会自动检测(根据字节顺序标记或CSS中的字符集规则),默认为"UTF-8"。您可以通过设置环境编码来更改默认值。

// Sets the environment encoding of the referencing (!) document, e.g. if defined 
// in a link tag of an HTML page.
// This is used as a fall back value to determine the charset of the CSS file.
$reader->setEnvironmentEncoding("UTF-8");

###写入CSS 写入器可以创建不同格式的CSS。

// Gets the CSS content in compact format (no comments, no line breaks,...)
$writer = new \Crossjoin\Css\Writer\Compact($reader->getStyleSheet());
$cssContent = $writer->getContent();

// Gets the CSS content in prettified format
// (with comments, line breaks, indentations,...)
$writer = new \Crossjoin\Css\Writer\Pretty($reader->getStyleSheet());
$cssContent = $writer->getContent();

###错误处理 在解析CSS源时,会检查内容中的无效规则、选择器和声明。此外,阅读器和写入器还会进行额外的检查,例如检查规则的组成,例如无效的规则位置或不必要的空规则。

$cssString = 'body{color:black} @charset "UTF-8"; @media print{@page{margin:1cm;}}';
$reader = new \Crossjoin\Css\Reader\CssString($cssString);
$writer = new \Crossjoin\Css\Writer\Pretty($reader->getStyleSheet());
$content = $writer->getContent();
$errors = $writer->getErrors();

print_r($content);
// body { color: black; }

print_r($errors);
// Array (
// [0] => Ignored @charset rule, because at wrong position in style sheet.
// [1] => Rule instance of type 'Crossjoin\Css\Format\Rule\AtPage\PageRule'
//        not allowed in conditional group rule of type
//        'Crossjoin\Css\Format\Rule\AtMedia\MediaRule'.
// [2] => Empty media at-rule '@media print' ignored.
// )

##高级使用 TODO

##待办事项

  • 对有效CSS规则、属性和值进行额外检查
  • 对CSS属性和值进行额外优化
  • 优化无效CSS的自动纠正,遵循规范(示例
  • 支持CSS Paged Media Module Level 3(页面类型选择器、边距at-rules)
  • 支持import at-rule中的"supports"(尚未最终确定