2022-06-14 10:05:35 +03:00
|
|
|
#![deny(warnings)]
|
2022-01-16 08:33:06 +03:00
|
|
|
#![allow(clippy::needless_update)]
|
|
|
|
|
2023-10-10 14:42:20 +03:00
|
|
|
use std::{path::PathBuf, rc::Rc};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2023-10-10 14:42:20 +03:00
|
|
|
use swc_common::{
|
|
|
|
comments::SingleThreadedComments, errors::Handler, input::SourceFileInput, Span, Spanned,
|
|
|
|
};
|
2021-08-20 10:48:08 +03:00
|
|
|
use swc_css_ast::*;
|
2021-08-19 08:16:32 +03:00
|
|
|
use swc_css_parser::{
|
|
|
|
lexer::Lexer,
|
2022-11-10 05:10:23 +03:00
|
|
|
parse_input,
|
2022-10-13 07:15:29 +03:00
|
|
|
parser::{
|
2022-11-10 05:10:23 +03:00
|
|
|
input::{InputType, ParserInput, Tokens},
|
2022-10-13 07:15:29 +03:00
|
|
|
PResult, Parser, ParserConfig,
|
|
|
|
},
|
2021-08-19 08:16:32 +03:00
|
|
|
};
|
2021-11-28 04:12:02 +03:00
|
|
|
use swc_css_visit::{Visit, VisitWith};
|
2021-08-19 08:16:32 +03:00
|
|
|
use testing::NormalizedOutput;
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
fn stylesheet_test(input: PathBuf, config: ParserConfig) {
|
|
|
|
let ref_json_path = input.parent().unwrap().join("output.json");
|
2021-12-01 20:07:49 +03:00
|
|
|
|
2021-09-28 12:58:56 +03:00
|
|
|
testing::run_test2(false, |cm, handler| {
|
2023-10-10 14:42:20 +03:00
|
|
|
let comments = SingleThreadedComments::default();
|
|
|
|
|
2021-09-08 04:19:14 +03:00
|
|
|
let fm = cm.load_file(&input).unwrap();
|
2023-10-10 14:42:20 +03:00
|
|
|
let lexer = Lexer::new(SourceFileInput::from(&*fm), Some(&comments), config);
|
2022-06-13 23:27:30 +03:00
|
|
|
let mut parser = Parser::new(lexer, config);
|
|
|
|
let stylesheet = parser.parse_all();
|
|
|
|
let errors = parser.take_errors();
|
|
|
|
|
|
|
|
for err in &errors {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
match stylesheet {
|
|
|
|
Ok(stylesheet) => {
|
|
|
|
let actual_json = serde_json::to_string_pretty(&stylesheet)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize stylesheet");
|
|
|
|
|
2022-06-14 10:05:35 +03:00
|
|
|
actual_json.compare_to_file(&ref_json_path).unwrap();
|
2022-06-13 23:27:30 +03:00
|
|
|
|
2023-10-10 14:42:20 +03:00
|
|
|
let (leading, trailing) = comments.take_all();
|
|
|
|
let leading = Rc::try_unwrap(leading).unwrap().into_inner();
|
|
|
|
let trailing = Rc::try_unwrap(trailing).unwrap().into_inner();
|
|
|
|
|
|
|
|
serde_json::to_string_pretty(&leading)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize comments")
|
|
|
|
.compare_to_file(input.parent().unwrap().join("leading-comments.json"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
serde_json::to_string_pretty(&trailing)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize comments")
|
|
|
|
.compare_to_file(input.parent().unwrap().join("trailing-comments.json"))
|
|
|
|
.unwrap();
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut d = err.to_diagnostics(&handler);
|
|
|
|
|
|
|
|
d.note(&format!("current token = {}", parser.dump_cur()));
|
|
|
|
d.emit();
|
2021-09-08 04:19:14 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stylesheet_test_tokens(input: PathBuf, config: ParserConfig) {
|
|
|
|
let ref_json_path = input.parent().unwrap().join("output.json");
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
let mut errors = vec![];
|
2021-09-08 04:19:14 +03:00
|
|
|
let tokens = {
|
2023-10-10 14:42:20 +03:00
|
|
|
let mut lexer = Lexer::new(SourceFileInput::from(&*fm), None, Default::default());
|
2021-09-08 04:19:14 +03:00
|
|
|
let mut tokens = vec![];
|
|
|
|
|
2022-06-13 18:52:15 +03:00
|
|
|
for token_and_span in lexer.by_ref() {
|
2022-06-12 06:28:23 +03:00
|
|
|
tokens.push(token_and_span);
|
2021-09-08 04:19:14 +03:00
|
|
|
}
|
2022-06-12 06:28:23 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
errors.extend(lexer.take_errors());
|
|
|
|
|
2021-09-08 04:19:14 +03:00
|
|
|
Tokens {
|
|
|
|
span: Span::new(fm.start_pos, fm.end_pos, Default::default()),
|
|
|
|
tokens,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-10 05:10:23 +03:00
|
|
|
let stylesheet: PResult<Stylesheet> =
|
|
|
|
parse_input(InputType::Tokens(&tokens), config, &mut errors);
|
2022-06-13 23:27:30 +03:00
|
|
|
|
|
|
|
for err in &errors {
|
2021-09-28 12:58:56 +03:00
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
2021-09-08 04:19:14 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
if !errors.is_empty() {
|
2021-09-28 12:58:56 +03:00
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
match stylesheet {
|
|
|
|
Ok(stylesheet) => {
|
|
|
|
let actual_json = serde_json::to_string_pretty(&stylesheet)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize stylesheet");
|
|
|
|
|
2022-06-14 10:05:35 +03:00
|
|
|
actual_json.compare_to_file(&ref_json_path).unwrap();
|
2022-06-13 23:27:30 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut d = err.to_diagnostics(&handler);
|
|
|
|
|
|
|
|
d.emit();
|
|
|
|
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 04:19:14 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
fn stylesheet_recovery_test(input: PathBuf, config: ParserConfig) {
|
|
|
|
let stderr_path = input.parent().unwrap().join("output.swc-stderr");
|
|
|
|
let ref_json_path = input.parent().unwrap().join("output.json");
|
|
|
|
|
|
|
|
let mut recovered = false;
|
|
|
|
|
|
|
|
let stderr = testing::run_test2(false, |cm, handler| {
|
|
|
|
if false {
|
|
|
|
// For type inference
|
|
|
|
return Ok(());
|
|
|
|
}
|
2021-08-19 08:16:32 +03:00
|
|
|
|
2023-10-10 14:42:20 +03:00
|
|
|
let comments = SingleThreadedComments::default();
|
|
|
|
|
2021-08-19 08:16:32 +03:00
|
|
|
let fm = cm.load_file(&input).unwrap();
|
2023-10-10 14:42:20 +03:00
|
|
|
let lexer = Lexer::new(SourceFileInput::from(&*fm), Some(&comments), config);
|
2021-10-15 17:21:33 +03:00
|
|
|
let mut parser = Parser::new(lexer, config);
|
2021-08-19 08:16:32 +03:00
|
|
|
let stylesheet = parser.parse_all();
|
2022-06-23 06:56:13 +03:00
|
|
|
let mut errors = parser.take_errors();
|
|
|
|
|
|
|
|
errors.sort_by(|a, b| a.message().cmp(&b.message()));
|
2022-06-13 23:27:30 +03:00
|
|
|
|
|
|
|
for err in &errors {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
recovered = true;
|
|
|
|
}
|
2021-08-19 08:16:32 +03:00
|
|
|
|
|
|
|
match stylesheet {
|
|
|
|
Ok(stylesheet) => {
|
|
|
|
let actual_json = serde_json::to_string_pretty(&stylesheet)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize stylesheet");
|
|
|
|
|
2022-06-14 10:05:35 +03:00
|
|
|
actual_json.compare_to_file(&ref_json_path).unwrap();
|
2021-08-25 05:46:17 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
Err(())
|
2021-08-19 08:16:32 +03:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut d = err.to_diagnostics(&handler);
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
d.note(&format!("current token = {}", parser.dump_cur()));
|
2021-08-19 08:16:32 +03:00
|
|
|
d.emit();
|
|
|
|
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-06-13 23:27:30 +03:00
|
|
|
.unwrap_err();
|
2021-08-19 08:16:32 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
if !recovered {
|
|
|
|
panic!(
|
|
|
|
"Parser should emit errors (recover mode), but parser parsed everything successfully \
|
|
|
|
{}",
|
|
|
|
stderr
|
|
|
|
);
|
|
|
|
}
|
2021-10-15 17:21:33 +03:00
|
|
|
|
2023-03-14 07:56:21 +03:00
|
|
|
stderr.compare_to_file(stderr_path).unwrap();
|
2021-10-15 17:21:33 +03:00
|
|
|
}
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
fn stylesheet_recovery_test_tokens(input: PathBuf, config: ParserConfig) {
|
2021-09-29 08:38:49 +03:00
|
|
|
let stderr_path = input.parent().unwrap().join("output.swc-stderr");
|
2022-06-13 23:27:30 +03:00
|
|
|
let ref_json_path = input.parent().unwrap().join("output.json");
|
2021-09-29 08:38:49 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
let mut recovered = false;
|
2021-09-29 08:38:49 +03:00
|
|
|
|
|
|
|
let stderr = testing::run_test2(false, |cm, handler| {
|
|
|
|
if false {
|
|
|
|
// For type inference
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
let fm = cm.load_file(&input).unwrap();
|
2022-06-23 06:56:13 +03:00
|
|
|
let mut lexer_errors = vec![];
|
2022-06-13 23:27:30 +03:00
|
|
|
let tokens = {
|
2023-10-10 14:42:20 +03:00
|
|
|
let mut lexer = Lexer::new(SourceFileInput::from(&*fm), None, Default::default());
|
2022-06-13 23:27:30 +03:00
|
|
|
let mut tokens = vec![];
|
2021-09-29 08:38:49 +03:00
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
for token_and_span in lexer.by_ref() {
|
|
|
|
tokens.push(token_and_span);
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:56:13 +03:00
|
|
|
lexer_errors.extend(lexer.take_errors());
|
2022-06-13 23:27:30 +03:00
|
|
|
|
|
|
|
Tokens {
|
|
|
|
span: Span::new(fm.start_pos, fm.end_pos, Default::default()),
|
|
|
|
tokens,
|
|
|
|
}
|
2021-10-15 17:21:33 +03:00
|
|
|
};
|
2021-09-29 08:38:49 +03:00
|
|
|
|
2022-06-23 06:56:13 +03:00
|
|
|
let mut parser_errors = vec![];
|
2022-06-13 23:27:30 +03:00
|
|
|
|
2022-11-10 05:10:23 +03:00
|
|
|
let stylesheet: PResult<Stylesheet> =
|
|
|
|
parse_input(InputType::Tokens(&tokens), config, &mut parser_errors);
|
2022-06-23 06:56:13 +03:00
|
|
|
|
|
|
|
parser_errors.extend(lexer_errors);
|
|
|
|
parser_errors.sort_by(|a, b| a.message().cmp(&b.message()));
|
|
|
|
|
|
|
|
for err in &parser_errors {
|
2022-06-13 23:27:30 +03:00
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:56:13 +03:00
|
|
|
if !parser_errors.is_empty() {
|
2022-06-13 23:27:30 +03:00
|
|
|
recovered = true;
|
|
|
|
}
|
2021-09-29 08:38:49 +03:00
|
|
|
|
|
|
|
match stylesheet {
|
|
|
|
Ok(stylesheet) => {
|
|
|
|
let actual_json = serde_json::to_string_pretty(&stylesheet)
|
|
|
|
.map(NormalizedOutput::from)
|
|
|
|
.expect("failed to serialize stylesheet");
|
|
|
|
|
2022-06-14 10:05:35 +03:00
|
|
|
actual_json.compare_to_file(&ref_json_path).unwrap();
|
2021-09-29 08:38:49 +03:00
|
|
|
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut d = err.to_diagnostics(&handler);
|
|
|
|
|
|
|
|
d.emit();
|
|
|
|
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_err();
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
if !recovered {
|
|
|
|
panic!(
|
|
|
|
"Parser should emit errors (recover mode), but parser parsed everything successfully \
|
|
|
|
{}",
|
|
|
|
stderr
|
|
|
|
);
|
2021-09-29 08:38:49 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 07:56:21 +03:00
|
|
|
stderr.compare_to_file(stderr_path).unwrap();
|
2021-09-29 08:38:49 +03:00
|
|
|
}
|
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
struct SpanVisualizer<'a> {
|
|
|
|
handler: &'a Handler,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! mtd {
|
|
|
|
($T:ty,$name:ident) => {
|
2021-12-07 08:34:16 +03:00
|
|
|
fn $name(&mut self, n: &$T) {
|
2021-08-20 10:48:08 +03:00
|
|
|
self.handler
|
|
|
|
.struct_span_err(n.span(), stringify!($T))
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
n.visit_children_with(self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit for SpanVisualizer<'_> {
|
2022-04-01 14:53:23 +03:00
|
|
|
mtd!(Stylesheet, visit_stylesheet);
|
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(AtRule, visit_at_rule);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-04-01 14:53:23 +03:00
|
|
|
mtd!(AtRuleName, visit_at_rule_name);
|
|
|
|
|
|
|
|
mtd!(QualifiedRule, visit_qualified_rule);
|
|
|
|
|
|
|
|
mtd!(StyleBlock, visit_style_block);
|
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(SelectorList, visit_selector_list);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-08-29 18:25:48 +03:00
|
|
|
mtd!(ForgivingSelectorList, visit_forgiving_selector_list);
|
|
|
|
|
2022-02-25 05:05:12 +03:00
|
|
|
mtd!(CompoundSelectorList, visit_compound_selector_list);
|
|
|
|
|
|
|
|
mtd!(RelativeSelectorList, visit_relative_selector_list);
|
|
|
|
|
2022-08-29 18:25:48 +03:00
|
|
|
mtd!(
|
|
|
|
ForgivingRelativeSelectorList,
|
|
|
|
visit_forgiving_relative_selector_list
|
|
|
|
);
|
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(ComplexSelector, visit_complex_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-11-07 18:29:25 +03:00
|
|
|
mtd!(Combinator, visit_combinator);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-25 05:05:12 +03:00
|
|
|
mtd!(RelativeSelector, visit_relative_selector);
|
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(CompoundSelector, visit_compound_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(TypeSelector, visit_type_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(TagNameSelector, visit_tag_name_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-10-12 05:57:43 +03:00
|
|
|
mtd!(NamespacePrefix, visit_namespace_prefix);
|
|
|
|
|
|
|
|
mtd!(Namespace, visit_namespace);
|
|
|
|
|
|
|
|
mtd!(NamedNamespace, visit_named_namespace);
|
|
|
|
|
|
|
|
mtd!(AnyNamespace, visit_any_namespace);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(WqName, visit_wq_name);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(UniversalSelector, visit_universal_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(IdSelector, visit_id_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(ClassSelector, visit_class_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(AttributeSelector, visit_attribute_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(AttributeSelectorMatcher, visit_attribute_selector_matcher);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(AttributeSelectorValue, visit_attribute_selector_value);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(AttributeSelectorModifier, visit_attribute_selector_modifier);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(SubclassSelector, visit_subclass_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(NestingSelector, visit_nesting_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(PseudoClassSelector, visit_pseudo_class_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-25 05:05:12 +03:00
|
|
|
mtd!(
|
|
|
|
PseudoClassSelectorChildren,
|
|
|
|
visit_pseudo_class_selector_children
|
|
|
|
);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(AnPlusB, visit_an_plus_b);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-23 19:20:40 +03:00
|
|
|
mtd!(AnPlusBNotation, visit_an_plus_b_notation);
|
|
|
|
|
2022-02-25 05:05:12 +03:00
|
|
|
mtd!(PseudoElementSelector, visit_pseudo_element_selector);
|
|
|
|
|
|
|
|
mtd!(
|
|
|
|
PseudoElementSelectorChildren,
|
|
|
|
visit_pseudo_element_selector_children
|
|
|
|
);
|
|
|
|
|
2022-02-06 07:24:51 +03:00
|
|
|
mtd!(Delimiter, visit_delimiter);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-22 17:10:25 +03:00
|
|
|
mtd!(SimpleBlock, visit_simple_block);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-03-01 18:26:17 +03:00
|
|
|
mtd!(ComponentValue, visit_component_value);
|
|
|
|
|
2021-12-01 20:07:49 +03:00
|
|
|
mtd!(Function, visit_function);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-22 22:45:33 +03:00
|
|
|
mtd!(Color, visit_color);
|
|
|
|
|
2022-01-31 06:10:37 +03:00
|
|
|
mtd!(HexColor, visit_hex_color);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-03-04 00:24:22 +03:00
|
|
|
mtd!(AlphaValue, visit_alpha_value);
|
|
|
|
|
|
|
|
mtd!(Hue, visit_hue);
|
|
|
|
|
|
|
|
mtd!(CmykComponent, visit_cmyk_component);
|
|
|
|
|
2022-02-26 16:51:59 +03:00
|
|
|
mtd!(Integer, visit_integer);
|
|
|
|
|
2022-01-24 17:27:10 +03:00
|
|
|
mtd!(Number, visit_number);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-24 17:27:10 +03:00
|
|
|
mtd!(Ratio, visit_ratio);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-09 13:43:15 +03:00
|
|
|
mtd!(Percentage, visit_percentage);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-10-26 21:38:05 +03:00
|
|
|
mtd!(Declaration, visit_declaration);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-05 17:22:47 +03:00
|
|
|
mtd!(DeclarationName, visit_declaration_name);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-05 17:22:47 +03:00
|
|
|
mtd!(ImportantFlag, visit_important_flag);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(Rule, visit_rule);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(Str, visit_str);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-11-12 13:09:52 +03:00
|
|
|
mtd!(Ident, visit_ident);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 04:55:49 +03:00
|
|
|
mtd!(CustomIdent, visit_custom_ident);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-26 14:35:49 +03:00
|
|
|
mtd!(DashedIdent, visit_dashed_ident);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-30 15:15:05 +03:00
|
|
|
mtd!(Dimension, visit_dimension);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Length, visit_length);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Angle, visit_angle);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Time, visit_time);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Frequency, visit_frequency);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Resolution, visit_resolution);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(Flex, visit_flex);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-08 14:57:57 +03:00
|
|
|
mtd!(UnknownDimension, visit_unknown_dimension);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-25 16:50:23 +03:00
|
|
|
mtd!(Url, visit_url);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(UrlValue, visit_url_value);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-25 16:50:23 +03:00
|
|
|
mtd!(UrlValueRaw, visit_url_value_raw);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-25 16:50:23 +03:00
|
|
|
mtd!(UrlModifier, visit_url_modifier);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-27 08:16:22 +03:00
|
|
|
mtd!(UnicodeRange, visit_unicode_range);
|
2022-02-09 15:23:14 +03:00
|
|
|
|
2022-02-14 09:19:02 +03:00
|
|
|
mtd!(CalcSum, visit_calc_sum);
|
|
|
|
|
|
|
|
mtd!(CalcProductOrOperator, visit_calc_product_or_operator);
|
|
|
|
|
|
|
|
mtd!(CalcProduct, visit_calc_product);
|
|
|
|
|
|
|
|
mtd!(CalcOperator, visit_calc_operator);
|
|
|
|
|
|
|
|
mtd!(CalcValueOrOperator, visit_calc_value_or_operator);
|
|
|
|
|
|
|
|
mtd!(CalcValue, visit_calc_value);
|
|
|
|
|
2022-01-14 14:21:41 +03:00
|
|
|
mtd!(LayerName, visit_layer_name);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-14 14:21:41 +03:00
|
|
|
mtd!(LayerNameList, visit_layer_name_list);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-14 14:21:41 +03:00
|
|
|
mtd!(LayerPrelude, visit_layer_prelude);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaQueryList, visit_media_query_list);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaQuery, visit_media_query);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-08-30 06:18:43 +03:00
|
|
|
mtd!(MediaType, visit_media_type);
|
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaCondition, visit_media_condition);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaConditionWithoutOr, visit_media_condition_without_or);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaConditionAllType, visit_media_condition_all_type);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(
|
|
|
|
MediaConditionWithoutOrType,
|
|
|
|
visit_media_condition_without_or_type
|
|
|
|
);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaNot, visit_media_not);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaAnd, visit_media_and);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaOr, visit_media_or);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaInParens, visit_media_in_parens);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeatureName, visit_media_feature_name);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeatureValue, visit_media_feature_value);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeature, visit_media_feature);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeaturePlain, visit_media_feature_plain);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeatureBoolean, visit_media_feature_boolean);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(MediaFeatureRange, visit_media_feature_range);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-20 05:57:24 +03:00
|
|
|
mtd!(
|
|
|
|
MediaFeatureRangeInterval,
|
|
|
|
visit_media_feature_range_interval
|
|
|
|
);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsCondition, visit_supports_condition);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsConditionType, visit_supports_condition_type);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsNot, visit_supports_not);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsAnd, visit_supports_and);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsOr, visit_supports_or);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsInParens, visit_supports_in_parens);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-01-21 03:57:29 +03:00
|
|
|
mtd!(SupportsFeature, visit_supports_feature);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 23:22:30 +03:00
|
|
|
mtd!(PageSelectorList, visit_page_selector_list);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
mtd!(PageSelector, visit_page_selector);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 23:22:30 +03:00
|
|
|
mtd!(PageSelectorType, visit_page_selector_type);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-02-06 23:22:30 +03:00
|
|
|
mtd!(PageSelectorPseudo, visit_page_selector_pseudo);
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2021-12-07 08:34:16 +03:00
|
|
|
fn visit_token_and_span(&mut self, n: &TokenAndSpan) {
|
2021-08-20 10:48:08 +03:00
|
|
|
self.handler
|
|
|
|
.struct_span_err(n.span, &format!("{:?}", n.token))
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:15:29 +03:00
|
|
|
fn stylesheet_span_visualizer(input: PathBuf, config: Option<ParserConfig>) {
|
2021-08-20 10:48:08 +03:00
|
|
|
let dir = input.parent().unwrap().to_path_buf();
|
|
|
|
|
|
|
|
let output = testing::run_test2(false, |cm, handler| {
|
2021-10-15 17:21:33 +03:00
|
|
|
// Type annotation
|
2021-08-20 10:48:08 +03:00
|
|
|
if false {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:15:29 +03:00
|
|
|
let config = match config {
|
|
|
|
Some(config) => config,
|
|
|
|
_ => ParserConfig {
|
2023-03-27 09:02:18 +03:00
|
|
|
legacy_ie: true,
|
2022-10-13 07:15:29 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-10-15 17:21:33 +03:00
|
|
|
};
|
|
|
|
|
2021-08-20 10:48:08 +03:00
|
|
|
let fm = cm.load_file(&input).unwrap();
|
2023-10-10 14:42:20 +03:00
|
|
|
let lexer = Lexer::new(SourceFileInput::from(&*fm), None, config);
|
2021-10-15 17:21:33 +03:00
|
|
|
let mut parser = Parser::new(lexer, config);
|
2021-08-20 10:48:08 +03:00
|
|
|
|
|
|
|
let stylesheet = parser.parse_all();
|
|
|
|
|
|
|
|
match stylesheet {
|
|
|
|
Ok(stylesheet) => {
|
2021-12-07 08:34:16 +03:00
|
|
|
stylesheet.visit_with(&mut SpanVisualizer { handler: &handler });
|
2021-08-20 10:48:08 +03:00
|
|
|
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let mut d = err.to_diagnostics(&handler);
|
|
|
|
d.note(&format!("current token = {}", parser.dump_cur()));
|
|
|
|
|
|
|
|
d.emit();
|
|
|
|
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_err();
|
|
|
|
|
2023-03-14 07:56:21 +03:00
|
|
|
output.compare_to_file(dir.join("span.swc-stderr")).unwrap();
|
2021-08-20 10:48:08 +03:00
|
|
|
}
|
2022-06-13 23:27:30 +03:00
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.css")]
|
|
|
|
fn pass(input: PathBuf) {
|
2023-03-27 09:02:18 +03:00
|
|
|
stylesheet_test(
|
|
|
|
input.clone(),
|
|
|
|
ParserConfig {
|
|
|
|
legacy_ie: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
stylesheet_test_tokens(
|
|
|
|
input,
|
|
|
|
ParserConfig {
|
|
|
|
legacy_ie: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2022-06-13 23:27:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[testing::fixture("tests/line-comment/**/input.css")]
|
|
|
|
fn line_comments_pass(input: PathBuf) {
|
|
|
|
stylesheet_test(
|
|
|
|
input,
|
|
|
|
ParserConfig {
|
|
|
|
allow_wrong_line_comments: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-13 07:15:29 +03:00
|
|
|
#[testing::fixture("tests/line-comment/**/input.css")]
|
|
|
|
fn span_visualizer_line_comment(input: PathBuf) {
|
|
|
|
stylesheet_span_visualizer(
|
|
|
|
input,
|
|
|
|
Some(ParserConfig {
|
|
|
|
allow_wrong_line_comments: true,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-13 23:27:30 +03:00
|
|
|
// TODO fix exclude
|
|
|
|
#[testing::fixture(
|
|
|
|
"tests/recovery/**/input.css",
|
|
|
|
exclude(
|
|
|
|
"at-rule/page/invalid-nesting/input.css",
|
|
|
|
"at-rule/page/without-page/input.css",
|
|
|
|
"function/calc/division/input.css",
|
|
|
|
"function/var/input.css",
|
|
|
|
"whitespaces/input.css",
|
|
|
|
)
|
|
|
|
)]
|
|
|
|
fn recovery(input: PathBuf) {
|
|
|
|
stylesheet_recovery_test(input.clone(), Default::default());
|
|
|
|
stylesheet_recovery_test_tokens(input, Default::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[testing::fixture("tests/fixture/**/input.css")]
|
|
|
|
#[testing::fixture("tests/recovery/**/input.css")]
|
|
|
|
fn span_visualizer(input: PathBuf) {
|
2022-10-13 07:15:29 +03:00
|
|
|
stylesheet_span_visualizer(input, None)
|
2022-06-13 23:27:30 +03:00
|
|
|
}
|