2020-01-04 22:55:04 -05:00
|
|
|
#![warn(
|
|
|
|
clippy::all,
|
|
|
|
clippy::restriction,
|
|
|
|
clippy::pedantic,
|
|
|
|
clippy::nursery,
|
2020-01-18 19:00:49 -05:00
|
|
|
clippy::cargo
|
2020-01-04 22:55:04 -05:00
|
|
|
)]
|
|
|
|
#![deny(missing_debug_implementations)]
|
|
|
|
#![allow(
|
2020-01-18 19:44:34 -05:00
|
|
|
// explicit return makes some things look ugly
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::implicit_return,
|
2020-01-18 19:44:34 -05:00
|
|
|
// Self { .. } is less explicit than Foo { .. }
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::use_self,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this is way too pedantic -- some things don't need docs!
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::missing_docs_in_private_items,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this crate is too new to deny todo!()
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::todo,
|
2020-01-18 19:44:34 -05:00
|
|
|
// unreachable!() has many valid use cases
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::unreachable,
|
2020-01-18 19:44:34 -05:00
|
|
|
// _ => {} has many valid use cases
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::wildcard_enum_match_arm,
|
2020-01-18 19:44:34 -05:00
|
|
|
// .expect() has many valid use cases, like when we know a value is `Some(..)`
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::option_expect_used,
|
2020-01-18 19:44:34 -05:00
|
|
|
// for now, panic() is an acceptable solution
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::panic,
|
2020-01-18 19:44:34 -05:00
|
|
|
// for now, some functions require a lot of lines
|
|
|
|
// future refactoring should make functions small and make
|
|
|
|
// this lint less annoying
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::too_many_lines,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this is too pedantic -- we are allowed to add numbers!
|
2020-01-04 22:55:04 -05:00
|
|
|
clippy::integer_arithmetic,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this is too pedantic for now -- the library is changing too quickly for
|
|
|
|
// good docs to be written
|
2020-01-11 14:51:31 -05:00
|
|
|
clippy::missing_errors_doc,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this incorrectly results in errors for types that derive `Debug`
|
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/4980
|
2020-01-11 16:12:23 -05:00
|
|
|
clippy::let_underscore_must_use,
|
2020-01-18 19:44:34 -05:00
|
|
|
// this is too pedantic -- it results in some names being less explicit
|
|
|
|
// than they should
|
2020-01-11 16:12:23 -05:00
|
|
|
clippy::module_name_repetitions
|
2020-01-04 22:55:04 -05:00
|
|
|
)]
|
2020-01-18 09:42:25 -05:00
|
|
|
#![feature(track_caller)]
|
2020-01-12 10:54:46 -05:00
|
|
|
// todo! handle erroring on styles at the toplevel
|
2020-01-06 17:06:37 -05:00
|
|
|
use std::fmt::{self, Display};
|
2020-01-04 22:55:04 -05:00
|
|
|
use std::fs;
|
2020-01-18 19:00:49 -05:00
|
|
|
use std::io::{self, stdout, BufWriter, Write};
|
2020-01-06 17:06:37 -05:00
|
|
|
use std::iter::{Iterator, Peekable};
|
2020-01-07 19:58:38 -05:00
|
|
|
use std::path::Path;
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-01-17 14:44:55 -05:00
|
|
|
use crate::common::{AtRule, Keyword, Op, Pos, Printer, Scope, Symbol, Whitespace};
|
2020-01-05 12:45:51 -05:00
|
|
|
use crate::css::Css;
|
2020-01-06 18:55:51 -05:00
|
|
|
use crate::error::SassError;
|
2020-01-04 22:55:04 -05:00
|
|
|
use crate::format::PrettyPrinter;
|
2020-01-18 15:47:51 -05:00
|
|
|
use crate::imports::import;
|
2020-01-04 22:55:04 -05:00
|
|
|
use crate::lexer::Lexer;
|
2020-01-18 23:55:11 -05:00
|
|
|
use crate::mixin::{eat_include, Mixin};
|
2020-01-11 14:51:31 -05:00
|
|
|
use crate::selector::{Attribute, Selector};
|
2020-01-06 16:50:16 -05:00
|
|
|
use crate::style::Style;
|
2020-01-04 22:55:04 -05:00
|
|
|
use crate::units::Unit;
|
2020-01-14 19:34:13 -05:00
|
|
|
use crate::utils::{devour_whitespace, eat_variable_value, IsWhitespace};
|
2020-01-04 22:55:04 -05:00
|
|
|
|
|
|
|
mod color;
|
|
|
|
mod common;
|
2020-01-05 12:45:51 -05:00
|
|
|
mod css;
|
2020-01-04 22:55:04 -05:00
|
|
|
mod error;
|
|
|
|
mod format;
|
2020-01-17 16:23:21 -05:00
|
|
|
mod function;
|
2020-01-11 18:42:42 -05:00
|
|
|
mod imports;
|
2020-01-04 22:55:04 -05:00
|
|
|
mod lexer;
|
2020-01-12 17:44:49 -05:00
|
|
|
mod mixin;
|
2020-01-04 22:55:04 -05:00
|
|
|
mod selector;
|
2020-01-06 16:50:16 -05:00
|
|
|
mod style;
|
2020-01-04 22:55:04 -05:00
|
|
|
mod units;
|
2020-01-12 19:56:58 -05:00
|
|
|
mod utils;
|
2020-01-04 22:55:04 -05:00
|
|
|
|
2020-01-06 18:55:51 -05:00
|
|
|
type SassResult<T> = Result<T, SassError>;
|
|
|
|
|
2020-01-08 20:39:05 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct Token {
|
|
|
|
pos: Pos,
|
|
|
|
pub kind: TokenKind,
|
|
|
|
}
|
|
|
|
|
2020-01-12 20:15:27 -05:00
|
|
|
impl IsWhitespace for Token {
|
|
|
|
fn is_whitespace(&self) -> bool {
|
|
|
|
if let TokenKind::Whitespace(_) = self.kind {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IsWhitespace for &Token {
|
|
|
|
fn is_whitespace(&self) -> bool {
|
|
|
|
if let TokenKind::Whitespace(_) = self.kind {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:55:04 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum TokenKind {
|
|
|
|
Ident(String),
|
|
|
|
Symbol(Symbol),
|
2020-01-18 15:47:51 -05:00
|
|
|
String(String),
|
2020-01-07 18:37:28 -05:00
|
|
|
AtRule(AtRule),
|
2020-01-04 22:55:04 -05:00
|
|
|
Keyword(Keyword),
|
|
|
|
Number(String),
|
|
|
|
Unit(Unit),
|
|
|
|
Whitespace(Whitespace),
|
|
|
|
Variable(String),
|
2020-01-11 14:51:31 -05:00
|
|
|
Attribute(Attribute),
|
2020-01-06 00:40:29 -05:00
|
|
|
Op(Op),
|
2020-01-05 19:21:58 -05:00
|
|
|
MultilineComment(String),
|
2020-01-12 10:54:46 -05:00
|
|
|
Interpolation,
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for TokenKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2020-01-07 18:37:28 -05:00
|
|
|
TokenKind::Ident(s) | TokenKind::Number(s) => write!(f, "{}", s),
|
2020-01-05 19:21:58 -05:00
|
|
|
TokenKind::Symbol(s) => write!(f, "{}", s),
|
2020-01-18 15:47:51 -05:00
|
|
|
TokenKind::String(s) => write!(f, "\"{}\"", s),
|
2020-01-07 18:37:28 -05:00
|
|
|
TokenKind::AtRule(s) => write!(f, "{}", s),
|
2020-01-06 00:40:29 -05:00
|
|
|
TokenKind::Op(s) => write!(f, "{}", s),
|
2020-01-05 19:21:58 -05:00
|
|
|
TokenKind::Unit(s) => write!(f, "{}", s),
|
|
|
|
TokenKind::Whitespace(s) => write!(f, "{}", s),
|
2020-01-11 14:51:31 -05:00
|
|
|
TokenKind::Attribute(s) => write!(f, "{}", s),
|
2020-01-05 19:21:58 -05:00
|
|
|
TokenKind::Keyword(kw) => write!(f, "{}", kw),
|
|
|
|
TokenKind::MultilineComment(s) => write!(f, "/*{}*/", s),
|
|
|
|
TokenKind::Variable(s) => write!(f, "${}", s),
|
2020-01-12 10:54:46 -05:00
|
|
|
TokenKind::Interpolation => {
|
|
|
|
panic!("we don't want to format TokenKind::Interpolation using Display")
|
|
|
|
}
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Represents a parsed SASS stylesheet with nesting
|
2020-01-04 22:55:04 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-01-18 14:57:56 -05:00
|
|
|
pub struct StyleSheet(Vec<Stmt>);
|
2020-01-04 22:55:04 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub enum Stmt {
|
2020-01-09 20:56:09 -05:00
|
|
|
/// A [`Style`](/grass/style/struct.Style)
|
2020-01-04 22:55:04 -05:00
|
|
|
Style(Style),
|
2020-01-09 20:56:09 -05:00
|
|
|
/// A [`RuleSet`](/grass/struct.RuleSet.html)
|
2020-01-04 22:55:04 -05:00
|
|
|
RuleSet(RuleSet),
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A multiline comment: `/* foo bar */`
|
2020-01-08 20:39:05 -05:00
|
|
|
MultilineComment(String),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Represents a single rule set. Rule sets can contain other rule sets
|
2020-01-11 14:51:31 -05:00
|
|
|
///
|
2020-01-12 17:44:49 -05:00
|
|
|
/// ```scss
|
2020-01-08 20:58:02 -05:00
|
|
|
/// a {
|
|
|
|
/// color: blue;
|
|
|
|
/// b {
|
|
|
|
/// color: red;
|
|
|
|
/// }
|
|
|
|
/// }
|
2020-01-12 17:44:49 -05:00
|
|
|
/// ```
|
2020-01-04 22:55:04 -05:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct RuleSet {
|
|
|
|
selector: Selector,
|
|
|
|
rules: Vec<Stmt>,
|
|
|
|
// potential optimization: we don't *need* to own the selector
|
2020-01-05 12:37:50 -05:00
|
|
|
super_selector: Selector,
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// An intermediate representation of what are essentially single lines
|
|
|
|
/// todo! rename this
|
2020-01-17 08:14:10 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2020-01-04 22:55:04 -05:00
|
|
|
enum Expr {
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A style: `color: red`
|
2020-01-04 22:55:04 -05:00
|
|
|
Style(Style),
|
2020-01-12 19:56:58 -05:00
|
|
|
/// A collection of styles, from a mixin or function
|
2020-01-17 08:14:10 -05:00
|
|
|
// Styles(Vec<Style>),
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A full selector `a > h1`
|
2020-01-04 22:55:04 -05:00
|
|
|
Selector(Selector),
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A variable declaration `$var: 1px`
|
2020-01-05 19:21:58 -05:00
|
|
|
VariableDecl(String, Vec<Token>),
|
2020-01-17 08:14:10 -05:00
|
|
|
/// A mixin declaration `@mixin foo {}`
|
|
|
|
MixinDecl(String, Mixin),
|
|
|
|
/// An include statement `@include foo;`
|
|
|
|
Include(Vec<Stmt>),
|
2020-01-08 20:58:02 -05:00
|
|
|
/// A multiline comment: `/* foobar */`
|
2020-01-08 20:39:05 -05:00
|
|
|
MultilineComment(String),
|
2020-01-19 00:10:37 -05:00
|
|
|
Debug(Pos, String),
|
|
|
|
Warn(Pos, String),
|
2020-01-12 10:54:46 -05:00
|
|
|
// /// Function call: `calc(10vw - 1px)`
|
|
|
|
// FuncCall(String, Vec<Token>),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StyleSheet {
|
2020-01-06 18:55:51 -05:00
|
|
|
pub fn new(input: &str) -> SassResult<StyleSheet> {
|
2020-01-18 14:57:56 -05:00
|
|
|
Ok(StyleSheet(
|
|
|
|
StyleSheetParser {
|
|
|
|
global_scope: Scope::new(),
|
|
|
|
lexer: Lexer::new(input).peekable(),
|
|
|
|
rules: Vec::new(),
|
|
|
|
scope: 0,
|
|
|
|
file: String::from("stdin"),
|
|
|
|
}
|
|
|
|
.parse_toplevel()?
|
|
|
|
.0,
|
|
|
|
))
|
2020-01-07 19:24:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_path<P: AsRef<Path> + Into<String>>(p: P) -> SassResult<StyleSheet> {
|
2020-01-18 14:57:56 -05:00
|
|
|
Ok(StyleSheet(
|
|
|
|
StyleSheetParser {
|
|
|
|
global_scope: Scope::new(),
|
|
|
|
lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(),
|
|
|
|
rules: Vec::new(),
|
|
|
|
scope: 0,
|
|
|
|
file: p.into(),
|
|
|
|
}
|
|
|
|
.parse_toplevel()?
|
|
|
|
.0,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn export_from_path<P: AsRef<Path> + Into<String>>(
|
|
|
|
p: P,
|
|
|
|
) -> SassResult<(Vec<Stmt>, Scope)> {
|
|
|
|
Ok(StyleSheetParser {
|
2020-01-12 17:44:49 -05:00
|
|
|
global_scope: Scope::new(),
|
2020-01-07 19:24:37 -05:00
|
|
|
lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(),
|
|
|
|
rules: Vec::new(),
|
|
|
|
scope: 0,
|
|
|
|
file: p.into(),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-01-18 14:57:56 -05:00
|
|
|
.parse_toplevel()?)
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Print the internal representation of a parsed stylesheet
|
2020-01-11 14:51:31 -05:00
|
|
|
///
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Very closely resembles the origin SASS, but contains only things translatable
|
|
|
|
/// to pure CSS
|
2020-01-11 14:51:31 -05:00
|
|
|
///
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Used mainly in debugging, but can at times be useful
|
2020-01-18 18:36:00 -05:00
|
|
|
pub fn pretty_print<W: Write>(&self, buf: W) -> io::Result<()> {
|
2020-01-04 22:55:04 -05:00
|
|
|
PrettyPrinter::new(buf).pretty_print(self)
|
|
|
|
}
|
2020-01-05 12:37:02 -05:00
|
|
|
|
2020-01-18 19:44:34 -05:00
|
|
|
#[allow(dead_code)]
|
2020-01-18 18:36:00 -05:00
|
|
|
fn pretty_print_selectors<W: Write>(&self, buf: W) -> io::Result<()> {
|
2020-01-05 12:37:02 -05:00
|
|
|
PrettyPrinter::new(buf).pretty_print_preserve_super_selectors(self)
|
|
|
|
}
|
|
|
|
|
2020-01-08 20:58:02 -05:00
|
|
|
/// Write the internal representation as CSS to `buf`
|
2020-01-18 18:36:00 -05:00
|
|
|
pub fn print_as_css<W: Write>(self, buf: &mut W) -> io::Result<()> {
|
2020-01-05 12:37:02 -05:00
|
|
|
Css::from_stylesheet(self).pretty_print(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 22:55:04 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct StyleSheetParser<'a> {
|
2020-01-12 17:44:49 -05:00
|
|
|
global_scope: Scope,
|
2020-01-04 22:55:04 -05:00
|
|
|
lexer: Peekable<Lexer<'a>>,
|
|
|
|
rules: Vec<Stmt>,
|
2020-01-06 18:55:51 -05:00
|
|
|
scope: u32,
|
2020-01-07 19:24:37 -05:00
|
|
|
file: String,
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StyleSheetParser<'a> {
|
2020-01-18 14:57:56 -05:00
|
|
|
fn parse_toplevel(mut self) -> SassResult<(Vec<Stmt>, Scope)> {
|
2020-01-17 21:42:51 -05:00
|
|
|
let mut rules: Vec<Stmt> = Vec::new();
|
2020-01-07 18:37:28 -05:00
|
|
|
while let Some(Token { kind, .. }) = self.lexer.peek() {
|
2020-01-18 20:24:28 -05:00
|
|
|
match kind {
|
2020-01-04 22:55:04 -05:00
|
|
|
TokenKind::Ident(_)
|
2020-01-11 14:51:31 -05:00
|
|
|
| TokenKind::Attribute(_)
|
2020-01-12 10:54:46 -05:00
|
|
|
| TokenKind::Interpolation
|
2020-01-04 22:55:04 -05:00
|
|
|
| TokenKind::Symbol(Symbol::Hash)
|
|
|
|
| TokenKind::Symbol(Symbol::Colon)
|
|
|
|
| TokenKind::Symbol(Symbol::Mul)
|
2020-01-12 19:56:58 -05:00
|
|
|
| TokenKind::Symbol(Symbol::Period) => rules
|
|
|
|
.extend(self.eat_rules(&Selector(Vec::new()), &mut self.global_scope.clone())),
|
2020-01-11 20:41:36 -05:00
|
|
|
TokenKind::Whitespace(_) => {
|
2020-01-04 22:55:04 -05:00
|
|
|
self.lexer.next();
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-18 20:24:28 -05:00
|
|
|
TokenKind::Variable(_) => {
|
|
|
|
let Token { pos, kind } = self
|
2020-01-07 18:37:28 -05:00
|
|
|
.lexer
|
|
|
|
.next()
|
2020-01-18 20:24:28 -05:00
|
|
|
.expect("this must exist because we have already peeked");
|
|
|
|
let name = match kind {
|
|
|
|
TokenKind::Variable(n) => n,
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
};
|
2020-01-12 20:15:27 -05:00
|
|
|
devour_whitespace(&mut self.lexer);
|
2020-01-07 19:58:38 -05:00
|
|
|
if self
|
2020-01-05 19:21:58 -05:00
|
|
|
.lexer
|
|
|
|
.next()
|
2020-01-07 18:37:28 -05:00
|
|
|
.unwrap_or_else(|| self.error(pos, "expected value after variable"))
|
2020-01-05 19:21:58 -05:00
|
|
|
.kind
|
|
|
|
!= TokenKind::Symbol(Symbol::Colon)
|
|
|
|
{
|
2020-01-07 18:37:28 -05:00
|
|
|
self.error(pos, "unexpected variable use at toplevel");
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
2020-01-14 19:34:13 -05:00
|
|
|
let val = eat_variable_value(&mut self.lexer, &self.global_scope)
|
2020-01-19 00:10:37 -05:00
|
|
|
.unwrap_or_else(|err| self.error(err.0, &err.1));
|
2020-01-12 17:44:49 -05:00
|
|
|
self.global_scope.vars.insert(name, val);
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
2020-01-18 20:24:28 -05:00
|
|
|
TokenKind::MultilineComment(_) => {
|
|
|
|
let comment = match self
|
|
|
|
.lexer
|
|
|
|
.next()
|
|
|
|
.expect("this must exist because we have already peeked")
|
|
|
|
.kind
|
|
|
|
{
|
|
|
|
TokenKind::MultilineComment(c) => c,
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
};
|
2020-01-08 20:39:05 -05:00
|
|
|
rules.push(Stmt::MultilineComment(comment));
|
2020-01-07 19:58:38 -05:00
|
|
|
}
|
2020-01-18 15:47:51 -05:00
|
|
|
TokenKind::AtRule(AtRule::Import) => {
|
2020-01-18 19:00:49 -05:00
|
|
|
let Token { pos, .. } = self
|
|
|
|
.lexer
|
|
|
|
.next()
|
|
|
|
.expect("this must exist because we have already peeked");
|
2020-01-18 15:47:51 -05:00
|
|
|
devour_whitespace(&mut self.lexer);
|
|
|
|
let mut file_name = String::new();
|
2020-01-18 19:00:49 -05:00
|
|
|
match self
|
|
|
|
.lexer
|
|
|
|
.next()
|
|
|
|
.unwrap_or_else(|| self.error(pos, "expected value after @import"))
|
|
|
|
.kind
|
|
|
|
{
|
2020-01-18 15:47:51 -05:00
|
|
|
TokenKind::Symbol(Symbol::DoubleQuote) => {
|
|
|
|
while let Some(tok) = self.lexer.next() {
|
|
|
|
if tok.kind == TokenKind::Symbol(Symbol::DoubleQuote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
file_name.push_str(&tok.kind.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::SingleQuote) => {
|
|
|
|
while let Some(tok) = self.lexer.next() {
|
|
|
|
if tok.kind == TokenKind::Symbol(Symbol::SingleQuote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
file_name.push_str(&tok.kind.to_string());
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 18:12:53 -05:00
|
|
|
_ => todo!("expected ' or \" after @import"),
|
2020-01-18 15:47:51 -05:00
|
|
|
}
|
2020-01-18 19:00:49 -05:00
|
|
|
let Token { kind, pos } = self
|
|
|
|
.lexer
|
|
|
|
.next()
|
|
|
|
.expect("this must exist because we have already peeked");
|
2020-01-18 15:47:51 -05:00
|
|
|
if kind != TokenKind::Symbol(Symbol::SemiColon) {
|
|
|
|
self.error(pos, "expected `;` after @import declaration");
|
|
|
|
}
|
|
|
|
|
2020-01-18 19:00:49 -05:00
|
|
|
let (new_rules, new_scope) = import(file_name)?;
|
2020-01-18 15:47:51 -05:00
|
|
|
rules.extend(new_rules);
|
|
|
|
self.global_scope.merge(new_scope);
|
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
TokenKind::AtRule(AtRule::Mixin) => {
|
2020-01-17 10:44:16 -05:00
|
|
|
let (name, mixin) =
|
2020-01-18 19:54:47 -05:00
|
|
|
Mixin::from_tokens(&mut self.lexer, &self.global_scope).unwrap();
|
2020-01-17 08:14:10 -05:00
|
|
|
self.global_scope.mixins.insert(name, mixin);
|
|
|
|
}
|
2020-01-12 20:15:27 -05:00
|
|
|
TokenKind::AtRule(_) => {
|
2020-01-17 08:14:10 -05:00
|
|
|
if let Some(Token {
|
2020-01-17 21:20:56 -05:00
|
|
|
kind: TokenKind::AtRule(ref rule),
|
2020-01-17 08:14:10 -05:00
|
|
|
pos,
|
|
|
|
}) = self.lexer.next()
|
|
|
|
{
|
2020-01-17 21:20:56 -05:00
|
|
|
match eat_at_rule(rule, pos, &mut self.lexer, &self.global_scope) {
|
2020-01-17 10:44:16 -05:00
|
|
|
Ok(_) => todo!(),
|
2020-01-17 08:14:10 -05:00
|
|
|
Err(Printer::Error(pos, message)) => self.error(pos, &message),
|
|
|
|
Err(Printer::Warn(pos, message)) => self.warn(pos, &message),
|
|
|
|
Err(Printer::Debug(pos, message)) => self.debug(pos, &message),
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 20:15:27 -05:00
|
|
|
}
|
2020-01-19 10:41:44 -05:00
|
|
|
_ => match self.lexer.next() {
|
|
|
|
Some(Token { pos, .. }) => self.error(pos, "unexpected toplevel token"),
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
},
|
2020-01-04 22:55:04 -05:00
|
|
|
};
|
|
|
|
}
|
2020-01-18 14:57:56 -05:00
|
|
|
Ok((rules, self.global_scope))
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 19:56:58 -05:00
|
|
|
fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec<Stmt> {
|
2020-01-04 22:55:04 -05:00
|
|
|
let mut stmts = Vec::new();
|
2020-01-17 21:42:51 -05:00
|
|
|
while let Some(tok) = eat_expr(&mut self.lexer, scope, super_selector)
|
2020-01-19 00:10:37 -05:00
|
|
|
.unwrap_or_else(|error| self.error(error.0, &error.1))
|
2020-01-17 21:42:51 -05:00
|
|
|
{
|
2020-01-04 22:55:04 -05:00
|
|
|
match tok {
|
|
|
|
Expr::Style(s) => stmts.push(Stmt::Style(s)),
|
2020-01-17 08:14:10 -05:00
|
|
|
Expr::MixinDecl(name, mixin) => {
|
|
|
|
scope.mixins.insert(name, mixin);
|
|
|
|
}
|
2020-01-04 22:55:04 -05:00
|
|
|
Expr::Selector(s) => {
|
2020-01-06 19:23:27 -05:00
|
|
|
self.scope += 1;
|
2020-01-18 21:05:26 -05:00
|
|
|
let rules = self.eat_rules(&super_selector.zip(&s), scope);
|
2020-01-04 22:55:04 -05:00
|
|
|
stmts.push(Stmt::RuleSet(RuleSet {
|
|
|
|
super_selector: super_selector.clone(),
|
|
|
|
selector: s,
|
|
|
|
rules,
|
|
|
|
}));
|
2020-01-06 19:23:27 -05:00
|
|
|
self.scope -= 1;
|
2020-01-07 18:37:28 -05:00
|
|
|
if self.scope == 0 {
|
|
|
|
return stmts;
|
|
|
|
}
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-01-05 19:21:58 -05:00
|
|
|
Expr::VariableDecl(name, val) => {
|
2020-01-06 19:23:27 -05:00
|
|
|
if self.scope == 0 {
|
2020-01-12 17:44:49 -05:00
|
|
|
scope.vars.insert(name.clone(), val.clone());
|
|
|
|
self.global_scope.vars.insert(name, val);
|
2020-01-06 19:23:27 -05:00
|
|
|
} else {
|
2020-01-12 17:44:49 -05:00
|
|
|
scope.vars.insert(name, val);
|
2020-01-06 19:23:27 -05:00
|
|
|
}
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
2020-01-19 10:41:44 -05:00
|
|
|
Expr::Include(rules) => stmts.extend(rules),
|
|
|
|
Expr::Debug(pos, ref message) => self.debug(pos, message),
|
|
|
|
Expr::Warn(pos, ref message) => self.warn(pos, message),
|
2020-01-08 20:39:05 -05:00
|
|
|
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stmts
|
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
|
2020-01-17 14:44:55 -05:00
|
|
|
fn eat_at_rule<I: Iterator<Item = Token>>(
|
2020-01-17 21:20:56 -05:00
|
|
|
rule: &AtRule,
|
2020-01-17 08:14:10 -05:00
|
|
|
pos: Pos,
|
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Result<Expr, Printer> {
|
2020-01-19 10:41:44 -05:00
|
|
|
devour_whitespace(toks);
|
2020-01-17 08:14:10 -05:00
|
|
|
match rule {
|
|
|
|
AtRule::Error => {
|
|
|
|
let message = toks
|
|
|
|
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>();
|
|
|
|
Err(Printer::Error(pos, message))
|
|
|
|
}
|
|
|
|
AtRule::Warn => {
|
|
|
|
let message = toks
|
|
|
|
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>();
|
|
|
|
Err(Printer::Warn(pos, message))
|
|
|
|
}
|
|
|
|
AtRule::Debug => {
|
|
|
|
let message = toks
|
|
|
|
.by_ref()
|
|
|
|
.take_while(|x| x.kind != TokenKind::Symbol(Symbol::SemiColon))
|
|
|
|
.map(|x| x.kind.to_string())
|
|
|
|
.collect::<String>();
|
|
|
|
Err(Printer::Debug(pos, message))
|
|
|
|
}
|
|
|
|
AtRule::Mixin => {
|
2020-01-18 19:54:47 -05:00
|
|
|
let (name, mixin) = Mixin::from_tokens(toks, scope)?;
|
2020-01-17 08:14:10 -05:00
|
|
|
Ok(Expr::MixinDecl(name, mixin))
|
|
|
|
}
|
|
|
|
// AtRule::Include => return Some(self.eat_include()),
|
|
|
|
_ => todo!("encountered unimplemented at rule"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 14:44:55 -05:00
|
|
|
pub(crate) fn eat_expr<I: Iterator<Item = Token>>(
|
2020-01-17 08:14:10 -05:00
|
|
|
toks: &mut Peekable<I>,
|
|
|
|
scope: &Scope,
|
|
|
|
super_selector: &Selector,
|
2020-01-19 00:10:37 -05:00
|
|
|
) -> Result<Option<Expr>, (Pos, String)> {
|
2020-01-17 08:14:10 -05:00
|
|
|
let mut values = Vec::with_capacity(5);
|
|
|
|
while let Some(tok) = toks.peek() {
|
|
|
|
match &tok.kind {
|
|
|
|
TokenKind::Symbol(Symbol::SemiColon) | TokenKind::Symbol(Symbol::CloseCurlyBrace) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
2020-01-18 20:24:28 -05:00
|
|
|
return Ok(Some(Expr::Style(match Style::from_tokens(values, scope) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return Ok(None),
|
|
|
|
})));
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::Symbol(Symbol::OpenCurlyBrace) => {
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
2020-01-17 21:42:51 -05:00
|
|
|
return Ok(Some(Expr::Selector(Selector::from_tokens(
|
2020-01-18 20:24:28 -05:00
|
|
|
&mut values.into_iter().peekable(),
|
2020-01-17 08:14:10 -05:00
|
|
|
scope,
|
2020-01-17 21:42:51 -05:00
|
|
|
))));
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::Variable(_) => {
|
2020-01-18 19:00:49 -05:00
|
|
|
let tok = toks
|
|
|
|
.next()
|
|
|
|
.expect("this must exist because we have already peeked");
|
2020-01-19 01:01:02 -05:00
|
|
|
let name = match tok.kind {
|
|
|
|
TokenKind::Variable(n) => n,
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
2020-01-17 08:14:10 -05:00
|
|
|
};
|
|
|
|
if let TokenKind::Symbol(Symbol::Colon) =
|
|
|
|
toks.peek().expect("expected something after variable").kind
|
|
|
|
{
|
|
|
|
toks.next();
|
|
|
|
devour_whitespace(toks);
|
2020-01-17 21:42:51 -05:00
|
|
|
return Ok(Some(Expr::VariableDecl(
|
2020-01-17 08:14:10 -05:00
|
|
|
name,
|
2020-01-17 22:40:13 -05:00
|
|
|
eat_variable_value(toks, scope)?,
|
2020-01-17 21:42:51 -05:00
|
|
|
)));
|
2020-01-17 08:14:10 -05:00
|
|
|
} else {
|
|
|
|
values.push(Token {
|
|
|
|
kind: TokenKind::Variable(name),
|
|
|
|
pos: tok.pos,
|
|
|
|
});
|
2020-01-05 19:21:58 -05:00
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::MultilineComment(_) => {
|
2020-01-18 19:00:49 -05:00
|
|
|
let tok = toks
|
|
|
|
.next()
|
|
|
|
.expect("this must exist because we have already peeked");
|
2020-01-17 08:14:10 -05:00
|
|
|
devour_whitespace(toks);
|
|
|
|
if values.is_empty() {
|
2020-01-19 00:29:45 -05:00
|
|
|
let s = match tok.kind {
|
|
|
|
TokenKind::MultilineComment(s) => s,
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
};
|
|
|
|
return Ok(Some(Expr::MultilineComment(s)));
|
2020-01-17 08:14:10 -05:00
|
|
|
} else {
|
2020-01-19 00:29:45 -05:00
|
|
|
values.push(tok);
|
2020-01-08 20:39:05 -05:00
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::AtRule(AtRule::Include) => {
|
2020-01-17 22:40:13 -05:00
|
|
|
return Ok(Some(Expr::Include(eat_include(
|
|
|
|
toks,
|
|
|
|
scope,
|
|
|
|
super_selector,
|
|
|
|
)?)));
|
2020-01-17 10:44:16 -05:00
|
|
|
}
|
|
|
|
TokenKind::AtRule(AtRule::Mixin) => {
|
|
|
|
toks.next();
|
2020-01-18 19:54:47 -05:00
|
|
|
let (name, mixin) = Mixin::from_tokens(toks, scope).unwrap();
|
2020-01-17 21:42:51 -05:00
|
|
|
return Ok(Some(Expr::MixinDecl(name, mixin)));
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::AtRule(_) => {
|
|
|
|
if let Some(Token {
|
2020-01-17 21:20:56 -05:00
|
|
|
kind: TokenKind::AtRule(ref rule),
|
2020-01-17 08:14:10 -05:00
|
|
|
pos,
|
|
|
|
}) = toks.next()
|
|
|
|
{
|
2020-01-19 00:10:37 -05:00
|
|
|
match eat_at_rule(rule, pos, toks, scope) {
|
|
|
|
Ok(a) => return Ok(Some(a)),
|
|
|
|
Err(Printer::Debug(a, b)) => return Ok(Some(Expr::Debug(a, b))),
|
|
|
|
Err(Printer::Warn(a, b)) => return Ok(Some(Expr::Warn(a, b))),
|
|
|
|
Err(Printer::Error(a, b)) => return Err((a, b)),
|
2020-01-12 19:56:58 -05:00
|
|
|
}
|
2020-01-12 20:15:27 -05:00
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
|
|
|
TokenKind::Interpolation => {
|
|
|
|
while let Some(tok) = toks.next() {
|
|
|
|
if tok.kind == TokenKind::Symbol(Symbol::CloseCurlyBrace) {
|
2020-01-12 10:54:46 -05:00
|
|
|
values.push(tok);
|
2020-01-17 08:14:10 -05:00
|
|
|
break;
|
2020-01-12 10:54:46 -05:00
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
values.push(tok);
|
2020-01-12 10:54:46 -05:00
|
|
|
}
|
2020-01-17 08:14:10 -05:00
|
|
|
}
|
2020-01-19 01:01:02 -05:00
|
|
|
_ => match toks.next() {
|
|
|
|
Some(tok) => values.push(tok),
|
|
|
|
_ => unsafe { std::hint::unreachable_unchecked() },
|
|
|
|
},
|
2020-01-17 08:14:10 -05:00
|
|
|
};
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
2020-01-17 21:42:51 -05:00
|
|
|
Ok(None)
|
2020-01-04 22:55:04 -05:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:41:36 -05:00
|
|
|
/// Functions that print to stdout or stderr
|
|
|
|
impl<'a> StyleSheetParser<'a> {
|
|
|
|
fn debug(&self, pos: Pos, message: &str) {
|
2020-01-18 21:05:26 -05:00
|
|
|
eprintln!("{}:{} Debug: {}", self.file, pos.line(), message);
|
2020-01-11 20:41:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn warn(&self, pos: Pos, message: &str) {
|
|
|
|
eprintln!(
|
|
|
|
"Warning: {}\n\t{} {}:{} todo!(scope)",
|
|
|
|
message,
|
|
|
|
self.file,
|
|
|
|
pos.line(),
|
|
|
|
pos.column()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error(&self, pos: Pos, message: &str) -> ! {
|
|
|
|
eprintln!("Error: {}", message);
|
|
|
|
eprintln!(
|
|
|
|
"{} {}:{} todo!(scope) on line {} at column {}",
|
|
|
|
self.file,
|
|
|
|
pos.line(),
|
|
|
|
pos.column(),
|
|
|
|
pos.line(),
|
|
|
|
pos.column()
|
|
|
|
);
|
|
|
|
let padding = vec![' '; format!("{}", pos.line()).len() + 1]
|
|
|
|
.iter()
|
|
|
|
.collect::<String>();
|
|
|
|
eprintln!("{}|", padding);
|
|
|
|
eprint!("{} | ", pos.line());
|
|
|
|
eprintln!("todo! get line to print as error");
|
|
|
|
eprintln!(
|
|
|
|
"{}| {}^",
|
|
|
|
padding,
|
|
|
|
vec![' '; pos.column() as usize].iter().collect::<String>()
|
|
|
|
);
|
|
|
|
eprintln!("{}|", padding);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 18:55:51 -05:00
|
|
|
fn main() -> SassResult<()> {
|
2020-01-18 18:36:00 -05:00
|
|
|
let mut stdout = BufWriter::new(stdout());
|
2020-01-18 18:12:53 -05:00
|
|
|
let mut args = std::env::args();
|
|
|
|
args.next();
|
|
|
|
for arg in args {
|
|
|
|
let s = StyleSheet::from_path(arg)?;
|
|
|
|
s.print_as_css(&mut stdout)?;
|
|
|
|
}
|
2020-01-04 22:55:04 -05:00
|
|
|
// dbg!(s);
|
2020-01-17 16:14:19 -05:00
|
|
|
// s.pretty_print(&mut stdout)?;
|
2020-01-05 12:52:50 -05:00
|
|
|
// s.pretty_print_selectors(&mut stdout)?;
|
2020-01-04 22:55:04 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-01-05 20:26:34 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-14 20:23:05 -05:00
|
|
|
macro_rules! test {
|
|
|
|
($func:ident, $input:literal) => {
|
|
|
|
#[test]
|
|
|
|
fn $func() {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
StyleSheet::new($input)
|
|
|
|
.expect(concat!("failed to parse on ", $input))
|
|
|
|
.print_as_css(&mut buf)
|
|
|
|
.expect(concat!("failed to pretty print on ", $input));
|
|
|
|
assert_eq!(
|
|
|
|
String::from($input),
|
|
|
|
String::from_utf8(buf).expect("produced invalid utf8")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($func:ident, $input:literal, $output:literal) => {
|
|
|
|
#[test]
|
|
|
|
fn $func() {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
StyleSheet::new($input)
|
|
|
|
.expect(concat!("failed to parse on ", $input))
|
|
|
|
.print_as_css(&mut buf)
|
|
|
|
.expect(concat!("failed to pretty print on ", $input));
|
|
|
|
assert_eq!(
|
|
|
|
String::from($output),
|
|
|
|
String::from_utf8(buf).expect("produced invalid utf8")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_variables {
|
2020-01-05 20:26:34 -05:00
|
|
|
use super::StyleSheet;
|
2020-01-14 20:23:05 -05:00
|
|
|
test!(
|
|
|
|
basic_variable,
|
|
|
|
"$height: 1px;\na {\n height: $height;\n}\n",
|
|
|
|
"a {\n height: 1px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
variable_redeclaration,
|
|
|
|
"$a: 1px;\n$a: 2px;\na {\n height: $a;\n}\n",
|
|
|
|
"a {\n height: 2px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
variable_shadowing,
|
|
|
|
"$a: 1px;\n$b: $a;\na {\n height: $b;\n}\n",
|
|
|
|
"a {\n height: 1px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
variable_shadowing_val_does_not_change,
|
|
|
|
"$a: 1px;\n$b: $a; $a: 2px;\na {\n height: $b;\n}\n",
|
|
|
|
"a {\n height: 1px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
variable_shadowing_val_does_not_change_complex,
|
|
|
|
"a {\n color: red;\n}\n$y: before;\n$x: 1 2 $y;\n$y: after;\nfoo {\n a: $x;\n}",
|
|
|
|
"a {\n color: red;\n}\nfoo {\n a: 1 2 before;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
variable_whitespace,
|
|
|
|
"$a : 1px ;\na {\n height: $a;\n}\n",
|
|
|
|
"a {\n height: 1px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_after_variable,
|
|
|
|
"$a: 1px;\na {\n height: $a;\n color: red;\n}\n",
|
|
|
|
"a {\n height: 1px;\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
literal_and_variable_as_val,
|
|
|
|
"$a: 1px;\na {\n height: 1 $a;\n}\n",
|
|
|
|
"a {\n height: 1 1px;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
literal_and_variable_as_var,
|
|
|
|
"$a: 1px;\n$b: 1 $a;\na {\n height: $b;\n}\n",
|
|
|
|
"a {\n height: 1 1px;\n}\n"
|
|
|
|
);
|
2020-01-17 21:03:01 -05:00
|
|
|
test!(
|
|
|
|
eats_whitespace_after_variable_value,
|
|
|
|
"a {\n b {\n $c: red;\n }\n color: red;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-17 21:15:38 -05:00
|
|
|
test!(
|
|
|
|
variable_changes_through_new_ruleset,
|
|
|
|
"a {\n $c: red;\nb {\n $c: blue;\n }\n color: $c;\n}\n",
|
|
|
|
"a {\n color: blue;\n}\n"
|
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
}
|
2020-01-05 20:26:34 -05:00
|
|
|
|
2020-01-14 20:23:05 -05:00
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_selectors {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::StyleSheet;
|
2020-01-05 20:51:14 -05:00
|
|
|
test!(
|
2020-01-11 14:51:31 -05:00
|
|
|
selector_nesting_el_mul_el,
|
2020-01-05 20:51:14 -05:00
|
|
|
"a, b {\n a, b {\n color: red\n}\n}\n",
|
2020-01-11 14:51:31 -05:00
|
|
|
"a a, a b, b a, b b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(selector_element, "a {\n color: red;\n}\n");
|
|
|
|
test!(selector_id, "#id {\n color: red;\n}\n");
|
|
|
|
test!(selector_class, ".class {\n color: red;\n}\n");
|
|
|
|
test!(selector_el_descendant, "a a {\n color: red;\n}\n");
|
|
|
|
test!(selector_universal, "* {\n color: red;\n}\n");
|
|
|
|
test!(selector_el_class_and, "a.class {\n color: red;\n}\n");
|
|
|
|
test!(selector_el_id_and, "a#class {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_el_class_descendant,
|
|
|
|
"a .class {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(selector_el_id_descendant, "a #class {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_el_universal_descendant,
|
|
|
|
"a * {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_universal_el_descendant,
|
|
|
|
"* a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
test!(selector_attribute_any, "[attr] {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_attribute_equals,
|
|
|
|
"[attr=val] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_attribute_single_quotes,
|
|
|
|
"[attr='val'] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_attribute_double_quotes,
|
|
|
|
"[attr=\"val\"] {\n color: red;\n}\n"
|
2020-01-05 20:51:14 -05:00
|
|
|
);
|
2020-01-11 14:51:31 -05:00
|
|
|
test!(selector_attribute_in, "[attr~=val] {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_attribute_begins_hyphen_or_exact,
|
|
|
|
"[attr|=val] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_attribute_starts_with,
|
|
|
|
"[attr^=val] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_attribute_ends_with,
|
|
|
|
"[attr$=val] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_attribute_contains,
|
|
|
|
"[attr*=val] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(selector_el_attribute_and, "a[attr] {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_el_attribute_descendant,
|
|
|
|
"a [attr] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(selector_el_mul_el, "a, b {\n color: red;\n}\n");
|
|
|
|
test!(
|
|
|
|
selector_el_immediate_child_el,
|
|
|
|
"a > b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(selector_el_following_el, "a + b {\n color: red;\n}\n");
|
|
|
|
test!(selector_el_preceding_el, "a ~ b {\n color: red;\n}\n");
|
|
|
|
test!(selector_pseudo, ":pseudo {\n color: red;\n}\n");
|
2020-01-12 17:44:49 -05:00
|
|
|
test!(selector_el_and_pseudo, "a:pseudo {\n color: red;\n}\n");
|
2020-01-11 14:51:31 -05:00
|
|
|
test!(
|
|
|
|
selector_el_pseudo_descendant,
|
|
|
|
"a :pseudo {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_pseudo_el_descendant,
|
|
|
|
":pseudo a {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-11 19:16:59 -05:00
|
|
|
test!(
|
|
|
|
selector_pseudo_paren_comma,
|
|
|
|
":pseudo(a, b, c) {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_pseudo_paren_space,
|
|
|
|
":pseudo(a b c) {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_el_pseudo_paren_and,
|
|
|
|
"a:pseudo(a, b, c) {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_el_pseudo_paren_descendant,
|
|
|
|
"a :pseudo(a, b, c) {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_pseudo_paren_el_descendant,
|
|
|
|
":pseudo(a, b, c) a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_pseudo_paren_el_nested,
|
|
|
|
"a {\n :pseudo(a, b, c) {\n color: red;\n }\n}\n",
|
|
|
|
"a :pseudo(a, b, c) {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
test!(selector_mul, "a, b {\n color: red;\n}\n");
|
2020-01-09 21:30:21 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
outer_ampersand,
|
|
|
|
"a, b {\n& c {\n color: red;\n}\n}\n",
|
|
|
|
"a c, b c {\n color: red;\n}\n"
|
2020-01-09 21:30:21 -05:00
|
|
|
);
|
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
inner_ampersand,
|
|
|
|
"a, b {\na & c {\n color: red;\n}\n}\n",
|
|
|
|
"a a c, a b c {\n color: red;\n}\n"
|
2020-01-09 21:30:21 -05:00
|
|
|
);
|
2020-01-14 17:39:19 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
ampersand_multiple_whitespace,
|
|
|
|
" a , b {\n&c {\n color: red;\n}\n}\n",
|
|
|
|
"ac, bc {\n color: red;\n}\n"
|
2020-01-14 17:39:19 -05:00
|
|
|
);
|
2020-01-08 20:39:05 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
ampersand_alone,
|
|
|
|
"a, b {\n& {\n color: red;\n}\n}\n",
|
|
|
|
"a, b {\n color: red;\n}\n"
|
2020-01-08 20:39:05 -05:00
|
|
|
);
|
2020-01-11 17:24:50 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
bem_dash_dash_selector,
|
|
|
|
"a {\n&--b {\n color: red;\n}\n}\n",
|
|
|
|
"a--b {\n color: red;\n}\n"
|
2020-01-11 17:24:50 -05:00
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
// test!(
|
|
|
|
// bem_underscore_selector,
|
|
|
|
// "a {\n&__b {\n color: red;\n}\n}\n",
|
|
|
|
// "a__b {\n color: red;\n}\n"
|
|
|
|
// );
|
|
|
|
test!(
|
|
|
|
selector_interpolation_start,
|
|
|
|
"#{a}bc {\n color: red;\n}\n",
|
|
|
|
"abc {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_interpolation_middle,
|
|
|
|
"a#{b}c {\n color: red;\n}\n",
|
|
|
|
"abc {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_interpolation_end,
|
|
|
|
"ab#{c} {\n color: red;\n}\n",
|
|
|
|
"abc {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_interpolation_variable,
|
|
|
|
"$a: foo;\nab#{$a} {\n color: red;\n}\n",
|
|
|
|
"abfoo {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
selector_whitespace,
|
|
|
|
" a > b , c ~ d e .f #g :h i.j [ k ] { color: red }",
|
|
|
|
"a > b, c ~ d e .f #g :h i.j [k] {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_units {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::StyleSheet;
|
|
|
|
test!(unit_none, "a {\n height: 1;\n}\n");
|
|
|
|
test!(unit_not_attached, "a {\n height: 1 px;\n}\n");
|
|
|
|
test!(unit_px, "a {\n height: 1px;\n}\n");
|
|
|
|
test!(unit_em, "a {\n height: 1em;\n}\n");
|
|
|
|
test!(unit_rem, "a {\n height: 1rem;\n}\n");
|
|
|
|
test!(unit_percent, "a {\n height: 1%;\n}\n");
|
|
|
|
}
|
2020-01-08 20:39:05 -05:00
|
|
|
|
2020-01-14 20:23:05 -05:00
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_comments {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::StyleSheet;
|
2020-01-08 20:39:05 -05:00
|
|
|
test!(
|
|
|
|
removes_inner_comments,
|
|
|
|
"a {\n color: red/* hi */;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
removes_inner_comments_whitespace,
|
|
|
|
"a {\n color: red /* hi */;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
preserves_outer_comments_before,
|
|
|
|
"a {\n /* hi */\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
preserves_outer_comments_after,
|
|
|
|
"a {\n color: red;\n /* hi */\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
preserves_outer_comments_two,
|
|
|
|
"a {\n /* foo */\n /* bar */\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
2020-01-09 20:56:09 -05:00
|
|
|
preserves_toplevel_comment_before,
|
2020-01-08 20:39:05 -05:00
|
|
|
"/* foo */\na {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-09 20:56:09 -05:00
|
|
|
test!(
|
|
|
|
preserves_toplevel_comment_after,
|
|
|
|
"a {\n color: red;\n}\n/* foo */\n"
|
|
|
|
);
|
2020-01-08 20:39:05 -05:00
|
|
|
test!(
|
|
|
|
removes_single_line_comment,
|
|
|
|
"// a { color: red }\na {\n height: 1 1px;\n}\n",
|
|
|
|
"a {\n height: 1 1px;\n}\n"
|
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
}
|
2020-01-11 16:12:23 -05:00
|
|
|
|
2020-01-14 20:23:05 -05:00
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_styles {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::StyleSheet;
|
|
|
|
test!(basic_style, "a {\n color: red;\n}\n");
|
|
|
|
test!(two_styles, "a {\n color: red;\n color: blue;\n}\n");
|
2020-01-11 16:12:23 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
two_inner_rulesets,
|
|
|
|
"a {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\n",
|
|
|
|
"a b {\n color: red;\n}\na c {\n color: white;\n}\n"
|
2020-01-11 16:12:23 -05:00
|
|
|
);
|
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
two_rulesets,
|
|
|
|
"a {\n color: red;\n}\nc {\n color: white;\n}\n"
|
2020-01-11 16:12:23 -05:00
|
|
|
);
|
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
two_inner_outer_rulesets,
|
|
|
|
"a {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\na {\n b {\n color: red;\n}\n c {\n color: white;\n}\n}\n",
|
|
|
|
"a b {\n color: red;\n}\na c {\n color: white;\n}\na b {\n color: red;\n}\na c {\n color: white;\n}\n"
|
2020-01-11 16:12:23 -05:00
|
|
|
);
|
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
removes_empty_outer_styles,
|
|
|
|
"a {\n b {\n color: red;\n }\n",
|
|
|
|
"a b {\n color: red;\n}\n"
|
2020-01-11 16:12:23 -05:00
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
test!(removes_empty_styles, "a {}\n", "");
|
2020-01-11 18:43:42 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
doesnt_eat_style_after_ruleset,
|
|
|
|
"a {\n b {\n color: red;\n}\n color: blue;\n}\n",
|
|
|
|
"a {\n color: blue;\n}\na b {\n color: red;\n}\n"
|
2020-01-11 18:43:42 -05:00
|
|
|
);
|
2020-01-12 10:54:46 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
multiline_style,
|
|
|
|
"a {\n color: red\n blue;\n}\n",
|
|
|
|
"a {\n color: red blue;\n}\n"
|
2020-01-12 10:54:46 -05:00
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
test!(hyphenated_style_property, "a {\n font-family: Arial;\n}\n");
|
|
|
|
test!(hyphenated_style_value, "a {\n color: Open-Sans;\n}\n");
|
2020-01-12 10:54:46 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
space_separated_style_value,
|
|
|
|
"a {\n border: solid red;\n}\n"
|
2020-01-12 10:54:46 -05:00
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
test!(single_quoted_style_value, "a {\n font: 'Open-Sans';\n}\n");
|
2020-01-12 10:54:46 -05:00
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
double_quoted_style_value,
|
|
|
|
"a {\n font: \"Open-Sans\";\n}\n"
|
2020-01-12 10:54:46 -05:00
|
|
|
);
|
|
|
|
test!(
|
2020-01-14 20:23:05 -05:00
|
|
|
comma_style_value,
|
|
|
|
"a {\n font: Open-Sans, sans-serif;\n}\n"
|
2020-01-12 10:54:46 -05:00
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_interpolation_start,
|
|
|
|
"a {\n #{c}olor: red;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_interpolation_middle,
|
|
|
|
"a {\n co#{l}or: red;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_interpolation_end,
|
|
|
|
"a {\n colo#{r}: red;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_interpolation_variable,
|
|
|
|
"$a: foo;\na {\n co#{$a}lor: red;\n}\n",
|
|
|
|
"a {\n cofoolor: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_val_interpolation_start,
|
|
|
|
"a {\n color: #{r}ed;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-11 18:43:42 -05:00
|
|
|
test!(
|
2020-01-12 10:54:46 -05:00
|
|
|
style_val_interpolation_middle,
|
|
|
|
"a {\n color: r#{e}d;\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_val_interpolation_end,
|
|
|
|
"a {\n color: re#{d};\n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_val_interpolation_variable,
|
|
|
|
"$a: foo;\na {\n color: r#{$a}ed;\n}\n",
|
|
|
|
"a {\n color: rfooed;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
style_whitespace,
|
|
|
|
"a {\n color : red ; \n}\n",
|
|
|
|
"a {\n color: red;\n}\n"
|
2020-01-11 18:43:42 -05:00
|
|
|
);
|
2020-01-05 20:51:14 -05:00
|
|
|
}
|
2020-01-14 20:23:05 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_misc {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::*;
|
|
|
|
test!(
|
|
|
|
combines_hyphens,
|
|
|
|
"a {\n foo: bar - baz;\n}\n",
|
|
|
|
"a {\n foo: bar-baz;\n}\n"
|
|
|
|
);
|
|
|
|
test!(does_not_combine_hyphens, "a {\n foo: bar -baz;\n}\n");
|
|
|
|
test!(
|
|
|
|
ident_starts_with_hyphen,
|
|
|
|
"a {\n foo: -webkit-bar-baz;\n}\n"
|
|
|
|
);
|
|
|
|
test!(ident_with_num, "el1 {\n a: b;\n}\n");
|
|
|
|
test!(keyword_important, "a {\n height: 1 !important;\n}\n");
|
|
|
|
test!(
|
|
|
|
keyword_important_uppercase,
|
|
|
|
"a {\n height: 1 !IMPORTANT;\n}\n",
|
|
|
|
"a {\n height: 1 !important;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
keyword_important_not_at_end,
|
|
|
|
"a {\n height: !important 1;\n}\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2020-01-18 15:47:51 -05:00
|
|
|
mod test_mixins {
|
2020-01-14 20:23:05 -05:00
|
|
|
use super::*;
|
2020-01-17 10:44:16 -05:00
|
|
|
test!(
|
|
|
|
basic_mixin,
|
|
|
|
"@mixin a {\n color: red;\n}\n\nb {\n @include a;\n}\n",
|
|
|
|
"b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_two_styles,
|
|
|
|
"@mixin a {\n color: red;\n color: blue;\n}\n\nb {\n @include a;\n}\n",
|
|
|
|
"b {\n color: red;\n color: blue;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_ruleset,
|
|
|
|
"@mixin a {\n b {\n color: red;\n }\n}\nb {\n @include a;\n}\n",
|
|
|
|
"b b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_two_rulesets,
|
|
|
|
"@mixin a {\n b {\n color: red;\n }\n c {\n color: blue;\n }\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d b {\n color: red;\n}\nd c {\n color: blue;\n}\n"
|
|
|
|
);
|
2020-01-17 16:14:19 -05:00
|
|
|
test!(
|
|
|
|
mixin_ruleset_and_style,
|
|
|
|
"@mixin a {\n b {\n color: red;\n }\n color: blue;\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d {\n color: blue;\n}\nd b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_style_and_ruleset,
|
|
|
|
"@mixin a {\n color: blue;\n b {\n color: red;\n}\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d {\n color: blue;\n}\nd b {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_nested_rulesets,
|
|
|
|
"@mixin a {\n b {\n c {\n color: red;\n}\n}\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d b c {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-17 21:15:38 -05:00
|
|
|
test!(
|
|
|
|
mixin_removes_empty_ruleset,
|
|
|
|
"@mixin a {\n color:red; b {\n}\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_variable_scope_one_ruleset,
|
|
|
|
"@mixin a {\n $a: blue;\nb {\n $a: red;\n} color: $a\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-18 09:03:38 -05:00
|
|
|
test!(
|
|
|
|
mixin_single_arg,
|
|
|
|
"@mixin a($b) {\n color: $b;\n}\nd {\n @include a(red);\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_two_args,
|
|
|
|
"@mixin a($b, $c) {\n color: $b;\n color: $c\n}\nd {\n @include a(red, blue);\n}\n",
|
|
|
|
"d {\n color: red;\n color: blue;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_arg_trailing_comma,
|
|
|
|
"@mixin a($b, $c,) {\n color: $b;\n color: $c\n}\nd {\n @include a(red, blue);\n}\n",
|
|
|
|
"d {\n color: red;\n color: blue;\n}\n"
|
|
|
|
);
|
2020-01-18 09:42:25 -05:00
|
|
|
test!(
|
|
|
|
mixin_property_interpolation,
|
|
|
|
"@mixin a($b) {\n #{$b}: red;\n}\nd {\n @include a(color);\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
|
|
|
test!(
|
|
|
|
mixin_style_interpolation,
|
|
|
|
"@mixin a($b) {\n color: #{$b};\n}\nd {\n @include a(red);\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-19 10:41:44 -05:00
|
|
|
test!(
|
|
|
|
mixin_default_value,
|
|
|
|
"@mixin a($b: red) {\n color: $b;\n}\nd {\n @include a;\n}\n",
|
|
|
|
"d {\n color: red;\n}\n"
|
|
|
|
);
|
2020-01-14 20:23:05 -05:00
|
|
|
}
|
2020-01-18 15:47:51 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_imports {
|
|
|
|
use super::*;
|
|
|
|
use tempfile::Builder;
|
2020-01-18 19:00:49 -05:00
|
|
|
use Write;
|
2020-01-18 15:47:51 -05:00
|
|
|
|
|
|
|
macro_rules! test_import {
|
|
|
|
($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => {
|
|
|
|
#[test]
|
|
|
|
fn $func() {
|
|
|
|
$(
|
|
|
|
write!(Builder::new().prefix($name).tempfile().unwrap(), $content).unwrap();
|
|
|
|
)*
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
StyleSheet::new($input)
|
|
|
|
.expect(concat!("failed to parse on ", $input))
|
|
|
|
.print_as_css(&mut buf)
|
|
|
|
.expect(concat!("failed to pretty print on ", $input));
|
|
|
|
assert_eq!(
|
|
|
|
String::from($output),
|
|
|
|
String::from_utf8(buf).expect("produced invalid utf8")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// redundant test to ensure that the import tests are working
|
|
|
|
test_import!(basic, "a {\n color: red;\n}\n" => "a {\n color: red;\n}\n" | );
|
|
|
|
|
|
|
|
test_import!(imports_variable, "@import \"foo\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "foo"("$a: red;"));
|
|
|
|
test_import!(single_quotes_import, "@import 'foo';\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "foo"("$a: red;"));
|
|
|
|
test_import!(finds_name_scss, "@import \"foo\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "foo.scss"("$a: red;"));
|
|
|
|
test_import!(finds_underscore_name_scss, "@import \"foo\";\na {\n color: $a;\n}" => "a {\n color: red;\n}\n" | "_foo.scss"("$a: red;"));
|
|
|
|
}
|