此包已被放弃,不再维护。未建议替代包。

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

v1.0.3 2015-09-29 15:38 UTC

This package is not auto-updated.

Last update: 2020-02-07 07:39:08 UTC


README

#CSS 读取器和写入器

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

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

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

{
    "require": {
        "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 规则)
  • 支持 import at 规则中的 "supports"(非最终版本