diff --git a/src/lib.rs b/src/lib.rs index 575cd8a..010ebe0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -296,7 +296,7 @@ pub fn from_path(p: &str, options: &Options) -> Result { .parse() .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?; - Css::from_stmts(stmts, false) + Css::from_stmts(stmts, false, options.allows_charset) .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))? .pretty_print(&map) .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages)) @@ -340,7 +340,7 @@ pub fn from_string(p: String, options: &Options) -> Result { .parse() .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?; - Css::from_stmts(stmts, false) + Css::from_stmts(stmts, false, options.allows_charset) .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))? .pretty_print(&map) .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages)) @@ -375,7 +375,7 @@ pub fn from_string(p: String, options: &'_ Options<'_>) -> std::result::Result std::io::Result<()> { .arg( Arg::with_name("NO_CHARSET") .long("no-charset") - .hidden(true) .help("Don't emit a @charset or BOM for CSS with non-ASCII characters."), ) .arg( @@ -190,7 +189,8 @@ fn main() -> std::io::Result<()> { let options = &Options::default() .load_paths(&vals) .quiet(matches.is_present("QUIET")) - .unicode_error_messages(!matches.is_present("NO_UNICODE")); + .unicode_error_messages(!matches.is_present("NO_UNICODE")) + .allows_charset(!matches.is_present("NO_CHARSET")); if let Some(name) = matches.value_of("INPUT") { if let Some(path) = matches.value_of("OUTPUT") { diff --git a/src/output.rs b/src/output.rs index 8f7d31f..0a14ada 100644 --- a/src/output.rs +++ b/src/output.rs @@ -95,18 +95,24 @@ impl Toplevel { pub(crate) struct Css { blocks: Vec, in_at_rule: bool, + allows_charset: bool, } impl Css { - pub const fn new(in_at_rule: bool) -> Self { + pub const fn new(in_at_rule: bool, allows_charset: bool) -> Self { Css { blocks: Vec::new(), in_at_rule, + allows_charset, } } - pub(crate) fn from_stmts(s: Vec, in_at_rule: bool) -> SassResult { - Css::new(in_at_rule).parse_stylesheet(s) + pub(crate) fn from_stmts( + s: Vec, + in_at_rule: bool, + allows_charset: bool, + ) -> SassResult { + Css::new(in_at_rule, allows_charset).parse_stylesheet(s) } fn parse_stmt(&mut self, stmt: Stmt) -> SassResult> { @@ -225,8 +231,9 @@ impl Css { pub fn pretty_print(self, map: &CodeMap) -> SassResult { let mut string = Vec::new(); + let allows_charset = self.allows_charset; self._inner_pretty_print(&mut string, map, 0)?; - if string.iter().any(|s| !s.is_ascii()) { + if allows_charset && string.iter().any(|s| !s.is_ascii()) { return Ok(format!("@charset \"UTF-8\";\n{}", unsafe { String::from_utf8_unchecked(string) })); @@ -309,7 +316,11 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true, self.allows_charset)?._inner_pretty_print( + buf, + map, + nesting + 1, + )?; writeln!(buf, "{}}}", padding)?; } Toplevel::Keyframes(k) => { @@ -332,7 +343,11 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true, self.allows_charset)?._inner_pretty_print( + buf, + map, + nesting + 1, + )?; writeln!(buf, "{}}}", padding)?; } Toplevel::Supports { params, body } => { @@ -354,7 +369,11 @@ impl Css { writeln!(buf, " {{")?; } - Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true, self.allows_charset)?._inner_pretty_print( + buf, + map, + nesting + 1, + )?; writeln!(buf, "{}}}", padding)?; } Toplevel::Media { query, body } => { @@ -363,7 +382,11 @@ impl Css { } writeln!(buf, "{}@media {} {{", padding, query)?; - Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?; + Css::from_stmts(body, true, self.allows_charset)?._inner_pretty_print( + buf, + map, + nesting + 1, + )?; writeln!(buf, "{}}}", padding)?; } Toplevel::Style(s) => {