1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use super::CssWriter;
use std::{
    fmt::{Result, Write},
    str::from_utf8,
};
use swc_common::Span;

pub struct BasicCssWriterConfig<'a> {
    pub indent: &'a str,
}

pub struct BasicCssWriter<'a, W>
where
    W: Write,
{
    line: usize,
    col: usize,

    indent_level: usize,

    config: BasicCssWriterConfig<'a>,
    w: W,
}

impl<'a, W> BasicCssWriter<'a, W>
where
    W: Write,
{
    pub fn new(writer: W, config: BasicCssWriterConfig<'a>) -> Self {
        BasicCssWriter {
            config,
            w: writer,
            line: 0,
            col: 0,
            indent_level: 0,
        }
    }

    /// Applies indents if we are at the start of a line.
    fn apply_indent(&mut self) -> Result {
        if self.col == 0 {
            for _ in 0..self.indent_level {
                self.col += self.config.indent.len();
                self.w.write_str(self.config.indent)?;
            }
        }

        Ok(())
    }
}

impl<W> CssWriter for BasicCssWriter<'_, W>
where
    W: Write,
{
    fn write_punct(&mut self, _span: Option<Span>, punct: &str) -> Result {
        debug_assert!(
            !punct.contains('\n'),
            "punct should not contain newline characters"
        );

        self.apply_indent()?;
        self.col += punct.len();
        self.w.write_str(punct)?;

        Ok(())
    }

    fn write_space(&mut self) -> Result {
        self.w.write_char(' ')
    }

    fn write_str(&mut self, span: Option<Span>, text: &str) -> Result {
        let mut new_string = String::new();

        let mut dq = 0;
        let mut sq = 0;

        for char in text.chars() {
            match char {
                // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER (U+FFFD).
                '\0' => {
                    new_string.push('\u{FFFD}');
                }
                // If the character is in the range [\1-\1f] (U+0001 to U+001F) or is U+007F, the
                // character escaped as code point.
                '\x01'..='\x1F' | '\x7F' => {
                    static HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";

                    let b3;
                    let b4;
                    let char_as_u8 = char as u8;

                    let bytes = if char_as_u8 > 0x0F {
                        let high = (char_as_u8 >> 4) as usize;
                        let low = (char_as_u8 & 0x0F) as usize;

                        b4 = [b'\\', HEX_DIGITS[high], HEX_DIGITS[low], b' '];

                        &b4[..]
                    } else {
                        b3 = [b'\\', HEX_DIGITS[char as usize], b' '];

                        &b3[..]
                    };

                    new_string.push_str(from_utf8(bytes).unwrap());
                }
                // If the character is '"' (U+0022) or "\" (U+005C), the escaped character.
                // We avoid escaping `"` to better string compression - we count the quantity of
                // quotes to choose the best default quotes
                '\\' => {
                    new_string.push_str("\\\\");
                }
                '"' => {
                    dq += 1;

                    new_string.push(char);
                }
                '\'' => {
                    sq += 1;

                    new_string.push(char);
                }
                // Otherwise, the character itself.
                _ => {
                    new_string.push(char);
                }
            };
        }

        if dq > sq {
            self.write_raw_char(span, '\'')?;
            self.write_raw(span, &new_string.replace('\'', "\\'"))?;
            self.write_raw_char(span, '\'')?;
        } else {
            self.write_raw_char(span, '"')?;
            self.write_raw(span, &new_string.replace('"', "\\\""))?;
            self.write_raw_char(span, '"')?;
        }

        Ok(())
    }

    fn write_raw(&mut self, span: Option<Span>, text: &str) -> Result {
        for char in text.chars() {
            self.write_raw_char(span, char)?;
        }

        Ok(())
    }

    fn write_raw_char(&mut self, _span: Option<Span>, c: char) -> Result {
        self.col += c.len_utf8();
        self.w.write_char(c)?;

        Ok(())
    }

    fn write_newline(&mut self) -> Result {
        self.line += 1;
        self.col = 0;

        self.w.write_char('\n')?;

        Ok(())
    }

    fn increase_indent(&mut self) {
        self.indent_level += 1;
    }

    fn decrease_indent(&mut self) {
        self.indent_level -= 1;
    }
}