implement configuration: allows_charset

This commit is contained in:
Connor Skees 2020-07-16 00:02:42 -04:00
parent 2ea48b4445
commit d5d527ab70
3 changed files with 36 additions and 13 deletions

View File

@ -296,7 +296,7 @@ pub fn from_path(p: &str, options: &Options) -> Result<String> {
.parse() .parse()
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?; .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))? .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?
.pretty_print(&map) .pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages)) .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<String> {
.parse() .parse()
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?; .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))? .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages))?
.pretty_print(&map) .pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages)) .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<S
.parse() .parse()
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())?; .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())?;
Ok(Css::from_stmts(stmts, false) Ok(Css::from_stmts(stmts, false, options.allows_charset)
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())? .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())?
.pretty_print(&map) .pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())?) .map_err(|e| raw_to_parse_error(&map, *e, options.unicode_error_messages).to_string())?)

View File

@ -72,7 +72,6 @@ fn main() -> std::io::Result<()> {
.arg( .arg(
Arg::with_name("NO_CHARSET") Arg::with_name("NO_CHARSET")
.long("no-charset") .long("no-charset")
.hidden(true)
.help("Don't emit a @charset or BOM for CSS with non-ASCII characters."), .help("Don't emit a @charset or BOM for CSS with non-ASCII characters."),
) )
.arg( .arg(
@ -190,7 +189,8 @@ fn main() -> std::io::Result<()> {
let options = &Options::default() let options = &Options::default()
.load_paths(&vals) .load_paths(&vals)
.quiet(matches.is_present("QUIET")) .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(name) = matches.value_of("INPUT") {
if let Some(path) = matches.value_of("OUTPUT") { if let Some(path) = matches.value_of("OUTPUT") {

View File

@ -95,18 +95,24 @@ impl Toplevel {
pub(crate) struct Css { pub(crate) struct Css {
blocks: Vec<Toplevel>, blocks: Vec<Toplevel>,
in_at_rule: bool, in_at_rule: bool,
allows_charset: bool,
} }
impl Css { impl Css {
pub const fn new(in_at_rule: bool) -> Self { pub const fn new(in_at_rule: bool, allows_charset: bool) -> Self {
Css { Css {
blocks: Vec::new(), blocks: Vec::new(),
in_at_rule, in_at_rule,
allows_charset,
} }
} }
pub(crate) fn from_stmts(s: Vec<Stmt>, in_at_rule: bool) -> SassResult<Self> { pub(crate) fn from_stmts(
Css::new(in_at_rule).parse_stylesheet(s) s: Vec<Stmt>,
in_at_rule: bool,
allows_charset: bool,
) -> SassResult<Self> {
Css::new(in_at_rule, allows_charset).parse_stylesheet(s)
} }
fn parse_stmt(&mut self, stmt: Stmt) -> SassResult<Vec<Toplevel>> { fn parse_stmt(&mut self, stmt: Stmt) -> SassResult<Vec<Toplevel>> {
@ -225,8 +231,9 @@ impl Css {
pub fn pretty_print(self, map: &CodeMap) -> SassResult<String> { pub fn pretty_print(self, map: &CodeMap) -> SassResult<String> {
let mut string = Vec::new(); let mut string = Vec::new();
let allows_charset = self.allows_charset;
self._inner_pretty_print(&mut string, map, 0)?; 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 { return Ok(format!("@charset \"UTF-8\";\n{}", unsafe {
String::from_utf8_unchecked(string) String::from_utf8_unchecked(string)
})); }));
@ -309,7 +316,11 @@ impl Css {
writeln!(buf, " {{")?; 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)?; writeln!(buf, "{}}}", padding)?;
} }
Toplevel::Keyframes(k) => { Toplevel::Keyframes(k) => {
@ -332,7 +343,11 @@ impl Css {
writeln!(buf, " {{")?; 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)?; writeln!(buf, "{}}}", padding)?;
} }
Toplevel::Supports { params, body } => { Toplevel::Supports { params, body } => {
@ -354,7 +369,11 @@ impl Css {
writeln!(buf, " {{")?; 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)?; writeln!(buf, "{}}}", padding)?;
} }
Toplevel::Media { query, body } => { Toplevel::Media { query, body } => {
@ -363,7 +382,11 @@ impl Css {
} }
writeln!(buf, "{}@media {} {{", padding, query)?; 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)?; writeln!(buf, "{}}}", padding)?;
} }
Toplevel::Style(s) => { Toplevel::Style(s) => {