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
use crate::output::CssBuf;
use std::cmp::Ordering;

/// A comment in a css file.
#[derive(Clone, Debug)]
pub struct Comment(String);

impl<T: Into<String>> From<T> for Comment {
    fn from(t: T) -> Self {
        Self(t.into())
    }
}

impl Comment {
    /// Write this comment to a css output buffer.
    pub(crate) fn write(&self, buf: &mut CssBuf) {
        if self.0.starts_with('#') {
            buf.add_one("\n", "");
            return;
        }
        let indent = buf.indent_level();
        let existing = self
            .0
            .lines()
            .skip(1)
            .map(|s| {
                let i = s.bytes().take_while(|b| *b == b' ').count();
                if s.as_bytes().get(i).unwrap_or(&b'*') == &b'*' {
                    i
                } else {
                    i.saturating_sub(2)
                }
            })
            .min()
            .unwrap_or(indent);

        buf.do_indent_no_nl();
        buf.add_str("/*");
        match indent.cmp(&existing) {
            Ordering::Greater => {
                let start = buf.format().get_indent(indent - existing);
                buf.add_str(&self.0.replace('\n', start));
            }
            Ordering::Less => {
                let start = buf.format().get_indent(existing - indent - 1);
                buf.add_str(&self.0.replace(start, "\n"));
            }
            Ordering::Equal => {
                buf.add_str(&self.0);
            }
        }
        buf.add_one("*/\n", "*/");
    }
}