girgias/css3-parser

用 PHP 编写的 CSS 解析器

0.1.2 2021-08-03 19:51 UTC

This package is not auto-updated.

Last update: 2024-09-26 11:33:49 UTC


README

pipeline status coverage report Codeac.io report

一个遵循 CSS 语法模块 3 W3C 候选推荐规范(修订日期:2019-07-16)的 PHP 编写的 CSS 3 词法和解析器(https://www.w3.org/TR/2019/CR-css-syntax-3-20190716/

当前 CSS 语法修订版位于:https://www.w3.org/TR/css-syntax-3/

安装

composer require girgias/css3-parser

特性

标记

所有由词法分析器返回的标记都是

namespace Girgias\CSSParser\Tokens;

interface Token
{
    public function getValue(): string;
}

哈希标记具有一个额外的方法 `public function getType(): int`,它提供有关哈希是否不受限制(`Girgias\CSSParser\Tokens\Hash::UNRESTRICTED`) 或 ID(Girgias\CSSParser\Tokens\Hash::ID`)的信息。

IntegerNumber 和 FloatNumber 标记公开了两个额外的函数: `public function getRawValue(): float|intpublic function getSign(): string`,以访问标记的原始值和数字的符号(如果已明确给出)。

Dimension 标记也公开了两个额外的函数: `public function getNumberToken(): TNumberpublic function getUnit(): string`,以访问尺寸的数字标记和单位值。

符合 CSS 规范的输入流

new Girgias\CSSParser\SpecificationCompliantInputStream (string $input)

它将输入字符串分割成 UTF-8 代码点,并按规范预处理输入流,如果在解析过程中出现解析错误,则也抛出 `Girgias\CSSParser\Exception\ParseError`

输入流装饰器

new Girgias\CSSParser\LaxInputStream (InputStream $inputStream)

它会抑制由提供的 InputStream 发出的任何解析错误。

词法分析器/标记化器接口

namespace Girgias\CSSParser;

use Girgias\CSSParser\Tokens\Token;

interface Lexer
{
    public function readNext(): Token;
}

一个完整的 CSS 词法分析器/标记化器

new Girgias\CSSParser\CompleteLexer implements Girgias\CSSParser\Lexer (InputStream $inputStream)

核心词法分析器将输入流标记化成标记流。

它还可以返回非官方的 `Girgias\Tokens\Comment` 标记,这些标记可能需要用于代码检查器。

它还具有以下方法 `public function consumeComments(): void`,允许从传递给 CompleteLexer` 的输入流中删除任何注释。

符合 CSS 规范的词法分析器/标记化器

new Girgias\CSSParser\SpecificationCompliantLexer (CompleteLexer $inputStream)

一个装饰器类,它自动调用 `CompleteLexer->consumeComments()` 来根据 CSS 规范第 3.2 节删除输入流中的注释。

高级用法

输入流接口

namespace Girgias\CSSParser;

interface InputStream
{
    /**
     * Fetch the next code point from the input stream.
     */
    public function next(): string;

    /**
     * Peek at the next code point from the input stream.
     *
     * @param int $lookAhead used to skip $lookAhead code points from the input stream before peeking.
     */
    public function peek(int $lookAhead = 0): string;

    /**
     * Inform if the next code point is the end of the stream.
     */
    public function isEndOfStream(): bool;

    /**
     * Emit a parse error.
     */
    public function error(string $message): void;
}

以实现自定义 InputStream。

路线图

贡献

欢迎贡献,有关更多信息,请参阅专门的 CONTRIBUTING 页面。

链接

许可

本项目中使用的代码受 MIT 许可证许可。