use crate::{
CssContainerRule, CssCounterStyleRule, CssDeclaration, CssFontFaceRule, CssImportRule,
CssKeyframeRule, CssKeyframesRule, CssLayerBlockRule, CssLayerStatementRule, CssMediaRule,
CssNamespaceRule, CssPageRule, CssPropertyRule, CssRule, CssScopeRule, CssStyleDeclaration,
CssStyleRule, CssStyleSheet, CssSupportsRule, OtherRule,
};
use muskitty_css::parser::{BlockKind, ComponentValue, Function, SimpleBlock};
use muskitty_css::tokenizer::{HashType, Numeric, Token};
pub trait ToCss {
fn to_css_string(&self) -> String;
}
fn escape_char(c: char) -> String {
format!("\\{}", c)
}
fn escape_as_code_point(c: char) -> String {
format!("\\{:x} ", c as u32)
}
pub fn serialize_identifier(name: &str) -> String {
let mut out = String::new();
for (i, c) in name.chars().enumerate() {
if c == '\0' {
out.push('\u{FFFD}');
} else if (c as u32) <= 0x1f
|| c == '\u{7f}'
|| (i == 0 && c.is_ascii_digit())
|| (i == 1 && c.is_ascii_digit() && name.starts_with('-'))
{
out.push_str(&escape_as_code_point(c));
} else if i == 0 && c == '-' && name.len() == 1 {
out.push_str(&escape_char(c));
} else if (c as u32) >= 0x80 || c == '-' || c == '_' || c.is_ascii_alphanumeric() {
out.push(c);
} else {
out.push_str(&escape_char(c));
}
}
out
}
pub fn serialize_string(s: &str) -> String {
let mut out = String::from("\"");
for c in s.chars() {
if c == '\0' {
out.push('\u{FFFD}');
} else if (c as u32) <= 0x1f || c == '\u{7f}' {
out.push_str(&escape_as_code_point(c));
} else if c == '"' || c == '\\' {
out.push_str(&escape_char(c));
} else {
out.push(c);
}
}
out.push('"');
out
}
pub fn serialize_url(url: &str) -> String {
format!("url({})", serialize_string(url))
}
fn format_number(n: f64) -> String {
if n == n.trunc() && n.abs() < 1e16 {
format!("{}", n as i64)
} else {
format!("{}", n)
}
}
fn serialize_numeric(n: &Numeric) -> String {
format_number(n.value)
}
fn serialize_token(token: &Token) -> String {
match token {
Token::Ident(s) => serialize_identifier(s),
Token::Function(s) => format!("{}(", serialize_identifier(s)),
Token::AtKeyword(s) => format!("@{}", serialize_identifier(s)),
Token::Hash(s, HashType::Id) => format!("#{}", serialize_identifier(s)),
Token::Hash(s, HashType::Unrestricted) => format!("#{}", s),
Token::String(s) => serialize_string(s),
Token::BadString => String::new(),
Token::Url(s) => serialize_url(s),
Token::BadUrl => String::new(),
Token::Delim(c) => c.to_string(),
Token::Number(n) => serialize_numeric(n),
Token::Percentage(n) => format!("{}%", serialize_numeric(n)),
Token::Dimension(n, unit) => {
format!("{}{}", serialize_numeric(n), serialize_identifier(unit))
}
Token::UnicodeRange(start, end) => serialize_unicode_range(*start, *end),
Token::Whitespace => " ".to_string(),
Token::Comment(s) => format!("/*{}*/", s),
Token::Colon => ":".to_string(),
Token::Semicolon => ";".to_string(),
Token::Comma => ",".to_string(),
Token::OpenBracket => "[".to_string(),
Token::CloseBracket => "]".to_string(),
Token::OpenParen => "(".to_string(),
Token::CloseParen => ")".to_string(),
Token::OpenBrace => "{".to_string(),
Token::CloseBrace => "}".to_string(),
Token::Cdo => "<!--".to_string(),
Token::Cdc => "-->".to_string(),
Token::Eof => String::new(),
}
}
fn serialize_unicode_range(start: Option<u32>, end: Option<u32>) -> String {
match (start, end) {
(Some(s), Some(e)) if s == e => format!("U+{:04X}", s),
(Some(s), Some(e)) => format!("U+{:04X}-{:04X}", s, e),
(Some(s), None) => format!("U+{:04X}", s),
_ => "U+0".to_string(),
}
}
pub fn serialize_component_values(cvs: &[ComponentValue]) -> String {
cvs.iter().map(serialize_component_value).collect()
}
pub fn serialize_component_value(cv: &ComponentValue) -> String {
match cv {
ComponentValue::PreservedToken(t) => serialize_token(t),
ComponentValue::Function(f) => serialize_function(f),
ComponentValue::SimpleBlock(b) => serialize_simple_block(b),
}
}
fn serialize_function(f: &Function) -> String {
format!(
"{}({})",
serialize_identifier(&f.name),
serialize_component_values(&f.value)
)
}
fn serialize_simple_block(b: &SimpleBlock) -> String {
let (open, close) = match b.kind {
BlockKind::Curly => ("{", "}"),
BlockKind::Square => ("[", "]"),
BlockKind::Paren => ("(", ")"),
};
format!("{}{}{}", open, serialize_component_values(&b.value), close)
}
impl ToCss for CssDeclaration {
fn to_css_string(&self) -> String {
let mut s = format!("{}: {}", self.name, serialize_component_values(&self.value));
if self.important {
s.push_str(" !important");
}
s
}
}
impl ToCss for CssStyleDeclaration {
fn to_css_string(&self) -> String {
let mut s = String::new();
for (i, d) in self.declarations.iter().enumerate() {
if i > 0 {
s.push(' ');
}
s.push_str(&d.to_css_string());
s.push(';');
}
s
}
}
impl ToCss for CssRule {
fn to_css_string(&self) -> String {
match self {
CssRule::Style(r) => r.to_css_string(),
CssRule::Import(r) => r.to_css_string(),
CssRule::Media(r) => r.to_css_string(),
CssRule::FontFace(r) => r.to_css_string(),
CssRule::Page(r) => r.to_css_string(),
CssRule::Keyframes(r) => r.to_css_string(),
CssRule::Keyframe(r) => r.to_css_string(),
CssRule::Namespace(r) => r.to_css_string(),
CssRule::CounterStyle(r) => r.to_css_string(),
CssRule::Supports(r) => r.to_css_string(),
CssRule::LayerBlock(r) => r.to_css_string(),
CssRule::LayerStatement(r) => r.to_css_string(),
CssRule::Container(r) => r.to_css_string(),
CssRule::Property(r) => r.to_css_string(),
CssRule::Scope(r) => r.to_css_string(),
CssRule::Other(r) => r.to_css_string(),
}
}
}
impl ToCss for CssStyleRule {
fn to_css_string(&self) -> String {
let selectors = serialize_component_values(&self.selectors);
let mut s = String::new();
s.push_str(&selectors);
let s_trimmed = s.trim_end();
let mut result = format!("{} {{ ", s_trimmed);
if !self.style.is_empty() {
result.push_str(&self.style.to_css_string());
}
for child in &self.css_rules {
if !self.style.is_empty() || !result.ends_with("{ ") {
result.push(' ');
}
result.push_str(&child.to_css_string());
}
result.push_str(" }");
result
}
}
impl ToCss for CssImportRule {
fn to_css_string(&self) -> String {
let mut s = format!("@import {}", serialize_url(&self.href));
if !self.media.is_empty() {
s.push(' ');
s.push_str(&serialize_component_values(&self.media));
}
s.push(';');
s
}
}
impl ToCss for CssMediaRule {
fn to_css_string(&self) -> String {
let condition = serialize_component_values(&self.condition);
serialize_block_at_rule("media", &condition, &self.css_rules)
}
}
impl ToCss for CssNamespaceRule {
fn to_css_string(&self) -> String {
let mut s = String::from("@namespace");
if let Some(p) = &self.prefix {
s.push(' ');
s.push_str(&serialize_identifier(p));
}
s.push(' ');
s.push_str(&serialize_url(&self.namespace_uri));
s.push(';');
s
}
}
impl ToCss for CssSupportsRule {
fn to_css_string(&self) -> String {
let condition = serialize_component_values(&self.condition);
serialize_block_at_rule("supports", &condition, &self.css_rules)
}
}
impl ToCss for CssLayerBlockRule {
fn to_css_string(&self) -> String {
let prelude = match &self.name {
Some(n) => serialize_identifier(n),
None => String::new(),
};
serialize_block_at_rule("layer", &prelude, &self.css_rules)
}
}
impl ToCss for CssLayerStatementRule {
fn to_css_string(&self) -> String {
let mut s = String::from("@layer ");
for (i, n) in self.names.iter().enumerate() {
if i > 0 {
s.push_str(", ");
}
s.push_str(&serialize_identifier(n));
}
s.push(';');
s
}
}
impl ToCss for CssContainerRule {
fn to_css_string(&self) -> String {
let condition = serialize_component_values(&self.condition);
serialize_block_at_rule("container", &condition, &self.css_rules)
}
}
impl ToCss for CssFontFaceRule {
fn to_css_string(&self) -> String {
serialize_descriptor_at_rule("font-face", "", &self.style)
}
}
impl ToCss for CssPageRule {
fn to_css_string(&self) -> String {
let prelude = serialize_component_values(&self.selectors);
serialize_descriptor_at_rule("page", &prelude, &self.style)
}
}
impl ToCss for CssKeyframesRule {
fn to_css_string(&self) -> String {
let name = self.name.as_deref().unwrap_or("");
let mut s = format!("@keyframes {}", serialize_identifier(name));
s.push_str(" {");
for kf in &self.keyframes {
s.push(' ');
s.push_str(&kf.to_css_string());
}
s.push_str(" }");
s
}
}
impl ToCss for CssKeyframeRule {
fn to_css_string(&self) -> String {
let key_text = serialize_component_values(&self.key_text);
let mut s = format!("{} {{", key_text.trim());
if !self.style.is_empty() {
s.push(' ');
s.push_str(&self.style.to_css_string());
}
s.push_str(" }");
s
}
}
impl ToCss for CssCounterStyleRule {
fn to_css_string(&self) -> String {
serialize_descriptor_at_rule(
"counter-style",
&serialize_identifier(&self.name),
&self.style,
)
}
}
impl ToCss for CssPropertyRule {
fn to_css_string(&self) -> String {
serialize_descriptor_at_rule("property", &serialize_identifier(&self.name), &self.style)
}
}
impl ToCss for CssScopeRule {
fn to_css_string(&self) -> String {
let prelude = serialize_component_values(&self.prelude);
serialize_block_at_rule("scope", &prelude, &self.css_rules)
}
}
impl ToCss for OtherRule {
fn to_css_string(&self) -> String {
let prelude = serialize_component_values(&self.prelude);
if self.child_rules.is_empty() && self.declarations.is_none() {
let mut s = format!("@{}", serialize_identifier(&self.name));
if !prelude.trim().is_empty() {
s.push(' ');
s.push_str(&prelude);
}
s.push(';');
s
} else {
let mut s = format!("@{}", serialize_identifier(&self.name));
if !prelude.trim().is_empty() {
s.push(' ');
s.push_str(&prelude);
}
s.push_str(" {");
if let Some(decls) = &self.declarations {
for d in decls {
s.push(' ');
s.push_str(&d.to_css_string());
s.push(';');
}
}
for r in &self.child_rules {
s.push(' ');
s.push_str(&r.to_css_string());
}
s.push_str(" }");
s
}
}
}
impl ToCss for CssStyleSheet {
fn to_css_string(&self) -> String {
let mut s = String::new();
for (i, r) in self.css_rules.iter().enumerate() {
if i > 0 {
s.push('\n');
}
s.push_str(&r.to_css_string());
}
s
}
}
fn serialize_descriptor_at_rule(name: &str, prelude: &str, style: &CssStyleDeclaration) -> String {
let mut s = format!("@{}", serialize_identifier(name));
if !prelude.trim().is_empty() {
s.push(' ');
s.push_str(prelude);
}
s.push_str(" {");
if !style.is_empty() {
s.push(' ');
s.push_str(&style.to_css_string());
}
s.push_str(" }");
s
}
fn serialize_block_at_rule(name: &str, prelude: &str, css_rules: &[CssRule]) -> String {
let mut s = format!("@{}", serialize_identifier(name));
if !prelude.trim().is_empty() {
s.push(' ');
s.push_str(prelude);
}
s.push_str(" {");
for r in css_rules {
s.push(' ');
s.push_str(&r.to_css_string());
}
s.push_str(" }");
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn serialize_simple_identifier() {
assert_eq!(serialize_identifier("color"), "color");
assert_eq!(serialize_identifier("font-size"), "font-size");
}
#[test]
fn serialize_identifier_with_escape() {
assert_eq!(serialize_identifier("1foo"), "\\31 foo");
assert_eq!(serialize_identifier("-"), "\\-");
}
#[test]
fn serialize_string_basic() {
assert_eq!(serialize_string("hello"), "\"hello\"");
}
#[test]
fn serialize_string_with_quotes() {
assert_eq!(serialize_string("he said \"hi\""), "\"he said \\\"hi\\\"\"");
}
#[test]
fn serialize_string_with_backslash() {
assert_eq!(serialize_string("a\\b"), "\"a\\\\b\"");
}
#[test]
fn serialize_url_basic() {
assert_eq!(serialize_url("style.css"), "url(\"style.css\")");
}
#[test]
fn format_number_integer() {
assert_eq!(format_number(10.0), "10");
assert_eq!(format_number(-5.0), "-5");
assert_eq!(format_number(0.0), "0");
}
#[test]
fn format_number_float() {
assert_eq!(format_number(2.5), "2.5");
assert_eq!(format_number(0.5), "0.5");
}
#[test]
fn declaration_without_important() {
let d = CssDeclaration::new("color", Vec::new(), false);
assert_eq!(d.to_css_string(), "color: ");
}
#[test]
fn declaration_with_important() {
let d = CssDeclaration::new("color", Vec::new(), true);
assert_eq!(d.to_css_string(), "color: !important");
}
#[test]
fn style_declaration_serialization() {
let mut block = CssStyleDeclaration::new();
block.push(CssDeclaration::new("a", Vec::new(), false));
block.push(CssDeclaration::new("b", Vec::new(), false));
assert_eq!(block.to_css_string(), "a: ; b: ;");
}
#[test]
fn layer_statement_serialization() {
let r = CssLayerStatementRule {
names: vec!["base".to_string(), "theme".to_string()],
};
assert_eq!(r.to_css_string(), "@layer base, theme;");
}
#[test]
fn empty_stylesheet_serializes_to_empty() {
let ss = CssStyleSheet::new();
assert_eq!(ss.to_css_string(), "");
}
}