perfectpanel / extlib-crossjoin-css
CSS读取器和写入器,支持完整的CSS3,同时已支持当前CSS4规范的绝大部分。支持媒体查询、注释、值优化等功能... 提供完整的Unicode支持,并可以处理大型CSS源文件。需要PHP 5.4+。
v1.0
2022-11-24 07:36 UTC
Requires
- php: >=5.4.0
- ext-dom: *
- ext-mbstring: *
This package is not auto-updated.
Last update: 2024-09-27 13:53:52 UTC
README
#CSS读取器和写入器
##简介 支持完整CSS3的CSS读取器和写入器,已支持当前CSS4规范的绝大部分。支持媒体查询、注释、值优化等功能... 提供完整的Unicode支持,并可以处理大型CSS源文件。
##安装 这是一个composer包。有关基本安装信息,请参阅composer网站。
将以下行添加到您的composer.json
文件中
{ "require": { "perfectpanel/extlib-crossjoin-css": "1.0.*" } }
###要求
- PHP 5.4+
- 多字节字符串(mbstring)扩展
- DOM扩展(从HTML源中提取CSS)
##特性
- 完整CSS3支持
- 完整Unicode支持
- 支持CSS4的大部分功能
- 支持无限嵌套级别的at规则
- 支持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规则)
- 支持导入at规则中的"supports"(未最终确定)