needs_separator_when_before omits CDC, so serialized tokens re-parse wrong

未关闭 适合新手
#434 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
rust
领域
tooling

调研方向

从 src/serializer.rs 第 519 行附近开始阅读 needs_separator_when_before,尤其关注 Number、DelimHash、DelimAt 和 DelimMinus 对应的行。根据 issue 的复现用例和现有的 serializer 覆盖情况,验证四种 CDC 组合。完成标准是每种组合都会请求一个分隔符,并且序列化后的 token 重新解析后得到原始 token 序列。

由索引模型根据 Issue 内容生成。

描述

TokenSerializationType::needs_separator_when_before returns false when a Number, DelimHash, DelimAt, or DelimMinus is followed by CDC. Concatenating the two serializations produces text that tokenizes into different tokens than the input.

Reproducer

cssparser 0.37.0, default features.

use cssparser::{Parser, ParserInput, ToCss};

fn main() {
    let mut pi = ParserInput::new("5 -->");
    let mut p = Parser::new(&mut pi);
    let a = p.next().unwrap().clone(); // Number { value: 5.0, int_value: Some(5) }
    let b = p.next().unwrap().clone(); // CDC

    // no separator requested
    assert!(!a
        .serialization_type()
        .needs_separator_when_before(b.serialization_type()));

    let mut s = String::new();
    a.to_css(&mut s).unwrap();
    b.to_css(&mut s).unwrap();
    assert_eq!(s, "5-->");
}

Observed vs expected

Observed: the call returns false and serialization yields 5-->. Re-parsing 5--> gives Dimension { value: 5.0, unit: "--" } followed by Delim('>').

Expected: true, which signals that an empty comment is needed so the output round-trips. The doc comment on needs_separator_when_before says it returns true if "an empty comment /**/ needs to be inserted between them so that they are not re-parsed as a single token". CSS Syntax Level 3 §Serialization requires the serialized form to round-trip.

Root cause

src/serializer.rs:519. The Ident and AtKeywordOrHash | Dimension rows list CDC. The rows covering Number, DelimHash, DelimAt, and DelimMinus do not.

Scope

Four pairs return false: Number then CDC gives 5-->, re-parsing as Dimension{5,"--"} + Delim('>'). DelimHash then CDC gives #-->, re-parsing as IDHash("--") + Delim('>'). DelimAt then CDC gives @-->, re-parsing as AtKeyword("--") + Delim('>'). DelimMinus then CDC gives --->, re-parsing as Ident("---") + Delim('>').

Any consumer using this API to re-emit CSS is affected. Stylo calls it for custom-property and var() substitution serialization. Present on main as of the 2026-07-21 commit.

主要语言
Rust
星标
869
派生
152
平均合并
15 小时 8 分钟
30 天内合并 PR
12

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

servo/rust-cssparser 的其他 Issue

查看 servo/rust-cssparser 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。