2022-01-27 15:44:38 +03:00
|
|
|
use std::{
|
|
|
|
mem::take,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-08-20 15:38:15 +03:00
|
|
|
use swc_common::{FileName, Span};
|
2022-02-17 07:36:50 +03:00
|
|
|
use swc_css_ast::{
|
2022-02-18 19:27:32 +03:00
|
|
|
AnPlusB, HexColor, ImportantFlag, Number, Str, Stylesheet, Token, TokenAndSpan, UrlValueRaw,
|
2022-02-17 07:36:50 +03:00
|
|
|
};
|
2021-08-20 15:38:15 +03:00
|
|
|
use swc_css_codegen::{
|
|
|
|
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
|
|
|
|
CodeGenerator, CodegenConfig, Emit,
|
|
|
|
};
|
|
|
|
use swc_css_parser::{parse_file, parser::ParserConfig};
|
|
|
|
use swc_css_visit::{VisitMut, VisitMutWith};
|
2022-01-27 15:44:38 +03:00
|
|
|
use testing::{assert_eq, run_test2, NormalizedOutput};
|
2021-11-09 09:48:38 +03:00
|
|
|
|
2022-01-27 15:44:38 +03:00
|
|
|
fn run(input: &Path, minify: bool) {
|
2021-11-09 09:48:38 +03:00
|
|
|
let dir = input.parent().unwrap();
|
2022-01-27 15:44:38 +03:00
|
|
|
let output = if minify {
|
|
|
|
dir.join(format!(
|
|
|
|
"output.min.{}",
|
|
|
|
input.extension().unwrap().to_string_lossy()
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
dir.join(format!(
|
|
|
|
"output.{}",
|
|
|
|
input.extension().unwrap().to_string_lossy()
|
|
|
|
))
|
|
|
|
};
|
2021-11-09 09:48:38 +03:00
|
|
|
|
2022-01-27 15:44:38 +03:00
|
|
|
run_test2(false, |cm, handler| {
|
|
|
|
let fm = cm.load_file(input).unwrap();
|
2021-11-09 09:48:38 +03:00
|
|
|
|
|
|
|
eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);
|
|
|
|
|
|
|
|
let mut errors = vec![];
|
2022-02-03 06:50:13 +03:00
|
|
|
let mut stylesheet: Stylesheet = parse_file(
|
2021-11-09 09:48:38 +03:00
|
|
|
&fm,
|
|
|
|
ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&mut errors,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
for err in take(&mut errors) {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut css_str = String::new();
|
2022-01-27 15:44:38 +03:00
|
|
|
|
2021-11-09 09:48:38 +03:00
|
|
|
{
|
2022-01-27 15:44:38 +03:00
|
|
|
let wr = BasicCssWriter::new(
|
|
|
|
&mut css_str,
|
|
|
|
BasicCssWriterConfig {
|
|
|
|
indent: if minify { "" } else { "\t" },
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut gen = CodeGenerator::new(wr, CodegenConfig { minify });
|
2021-11-09 09:48:38 +03:00
|
|
|
|
|
|
|
gen.emit(&stylesheet).unwrap();
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:50:13 +03:00
|
|
|
let fm_output = cm.load_file(&output).unwrap();
|
|
|
|
|
2021-11-09 09:48:38 +03:00
|
|
|
NormalizedOutput::from(css_str)
|
2022-01-27 15:44:38 +03:00
|
|
|
.compare_to_file(output)
|
2021-11-09 09:48:38 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2022-02-03 06:50:13 +03:00
|
|
|
let mut errors = vec![];
|
|
|
|
let mut stylesheet_output: Stylesheet = parse_file(
|
|
|
|
&fm_output,
|
|
|
|
ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&mut errors,
|
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
})?;
|
|
|
|
|
|
|
|
for err in take(&mut errors) {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
stylesheet.visit_mut_with(&mut NormalizeTest);
|
|
|
|
stylesheet_output.visit_mut_with(&mut NormalizeTest);
|
|
|
|
|
|
|
|
assert_eq!(stylesheet, stylesheet_output);
|
|
|
|
|
2021-11-09 09:48:38 +03:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
2021-08-20 15:38:15 +03:00
|
|
|
|
2022-02-03 06:50:13 +03:00
|
|
|
struct NormalizeTest;
|
|
|
|
|
|
|
|
impl VisitMut for NormalizeTest {
|
2022-02-09 09:33:32 +03:00
|
|
|
fn visit_mut_hex_color(&mut self, n: &mut HexColor) {
|
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
n.value = "fff".into();
|
|
|
|
n.raw = "fff".into();
|
2022-02-03 06:50:13 +03:00
|
|
|
}
|
|
|
|
|
2022-02-05 17:22:47 +03:00
|
|
|
fn visit_mut_important_flag(&mut self, n: &mut ImportantFlag) {
|
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
n.value.value = n.value.value.to_lowercase().into();
|
|
|
|
n.value.raw = n.value.raw.to_lowercase().into();
|
|
|
|
}
|
|
|
|
|
2022-02-09 09:33:32 +03:00
|
|
|
fn visit_mut_number(&mut self, n: &mut Number) {
|
2022-02-04 07:05:44 +03:00
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
n.raw = "".into();
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:50:13 +03:00
|
|
|
fn visit_mut_str(&mut self, n: &mut Str) {
|
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
n.raw = "".into();
|
|
|
|
}
|
2022-02-05 08:02:10 +03:00
|
|
|
|
2022-02-09 09:33:32 +03:00
|
|
|
fn visit_mut_url_value_raw(&mut self, n: &mut UrlValueRaw) {
|
2022-02-05 08:02:10 +03:00
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
2022-02-09 09:33:32 +03:00
|
|
|
n.before = "".into();
|
|
|
|
n.after = "".into();
|
|
|
|
n.raw = "".into();
|
2022-02-05 08:02:10 +03:00
|
|
|
}
|
2022-02-17 07:36:50 +03:00
|
|
|
|
2022-02-18 19:27:32 +03:00
|
|
|
fn visit_mut_an_plus_b(&mut self, n: &mut AnPlusB) {
|
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
if n.a_raw.is_some() {
|
|
|
|
n.a_raw = Some("".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.b_raw.is_some() {
|
|
|
|
n.b_raw = Some("".into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:36:50 +03:00
|
|
|
fn visit_mut_token_and_span(&mut self, n: &mut TokenAndSpan) {
|
|
|
|
n.visit_mut_children_with(self);
|
|
|
|
|
|
|
|
if let Token::WhiteSpace { .. } = &n.token {
|
|
|
|
n.token = Token::WhiteSpace { value: "".into() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mut_span(&mut self, n: &mut Span) {
|
|
|
|
*n = Default::default()
|
|
|
|
}
|
2022-02-03 06:50:13 +03:00
|
|
|
}
|
|
|
|
|
2022-01-27 15:44:38 +03:00
|
|
|
#[testing::fixture("tests/fixture/**/input.css")]
|
|
|
|
fn css(input: PathBuf) {
|
|
|
|
run(&input, false);
|
|
|
|
run(&input, true);
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:37:05 +03:00
|
|
|
#[testing::fixture("../swc_css_parser/tests/fixture/**/input.css")]
|
2021-08-20 15:38:15 +03:00
|
|
|
fn parse_again(input: PathBuf) {
|
|
|
|
eprintln!("{}", input.display());
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
|
|
|
|
eprintln!("==== ==== Input ==== ====\n{}\n", fm.src);
|
|
|
|
|
2021-09-28 12:58:56 +03:00
|
|
|
let mut errors = vec![];
|
2021-10-15 17:21:33 +03:00
|
|
|
let mut stylesheet: Stylesheet = parse_file(
|
|
|
|
&fm,
|
|
|
|
ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&mut errors,
|
|
|
|
)
|
2022-02-03 06:50:13 +03:00
|
|
|
.map_err(|err| {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
})?;
|
2021-09-28 12:58:56 +03:00
|
|
|
|
|
|
|
for err in take(&mut errors) {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
2021-08-20 15:38:15 +03:00
|
|
|
|
|
|
|
let mut css_str = String::new();
|
|
|
|
{
|
|
|
|
let wr = BasicCssWriter::new(&mut css_str, BasicCssWriterConfig { indent: "\t" });
|
|
|
|
let mut gen = CodeGenerator::new(wr, CodegenConfig { minify: false });
|
|
|
|
|
|
|
|
gen.emit(&stylesheet).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
eprintln!("==== ==== Codegen ==== ====\n{}\n", css_str);
|
|
|
|
|
|
|
|
let new_fm = cm.new_source_file(FileName::Anon, css_str);
|
2022-02-03 06:50:13 +03:00
|
|
|
let mut parsed_errors = vec![];
|
2021-10-15 17:21:33 +03:00
|
|
|
let mut parsed: Stylesheet = parse_file(
|
|
|
|
&new_fm,
|
|
|
|
ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-02-03 06:50:13 +03:00
|
|
|
&mut parsed_errors,
|
2021-10-15 17:21:33 +03:00
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
})?;
|
2021-09-28 12:58:56 +03:00
|
|
|
|
2022-02-03 06:50:13 +03:00
|
|
|
for err in parsed_errors {
|
2021-09-28 12:58:56 +03:00
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
2021-08-20 15:38:15 +03:00
|
|
|
|
|
|
|
stylesheet.visit_mut_with(&mut DropSpan);
|
|
|
|
parsed.visit_mut_with(&mut DropSpan);
|
|
|
|
|
|
|
|
assert_eq!(stylesheet, parsed);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DropSpan;
|
|
|
|
|
|
|
|
impl VisitMut for DropSpan {
|
|
|
|
fn visit_mut_span(&mut self, n: &mut Span) {
|
|
|
|
*n = Default::default()
|
|
|
|
}
|
|
|
|
}
|