Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: css variable calc minification #526

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib.rs
Expand Up @@ -7462,6 +7462,14 @@ mod tests {
".foo{transform:rotateX(-40deg)rotateY(50deg)}",
);
minify_test(".foo { width: calc(10px * mod(18, 5)) }", ".foo{width:30px}");
minify_test(".foo { --foo: calc(1px)}",".foo{--foo:1px}");
minify_test(".foo { --foo: calc(calc(1px + 2px) + 2px)}",".foo{--foo:calc((1px + 2px) + 2px)}");
minify_test(".foo { --foo: calc(max(20px, 1em) + 2px)}",".foo{--foo:calc(max(20px,1em) + 2px)}");
minify_test(".foo { --foo: calc(2px + (2px))}",".foo{--foo:calc(2px + 2px)}");
//TODO implement
//minify_test(":root { --foo: calc(1px + 2px)}",":root{--foo:3px}");
//TODO implement
//minify_test(":root { --foo: calc(1px + 2px + var(--foo))}",":root{--foo:calc(3px + var(--foo))}");
}

#[test]
Expand Down
46 changes: 46 additions & 0 deletions src/properties/custom.rs
Expand Up @@ -393,6 +393,52 @@ impl<'i> TokenList<'i> {
tokens.push(TokenOrValue::Url(Url::parse(input)?));
last_is_delim = false;
last_is_whitespace = false;
} else if f == "calc" {
let arguments = input.parse_nested_block(|input| TokenList::parse(input, options, depth + 1))?;
if arguments.0.len() == 0 {
// this is invalid css
tokens.push(TokenOrValue::Function(Function {
name: Ident(f),
arguments
}));
} else if arguments.0.len() == 1 {
tokens.push(arguments.0.first().unwrap().clone());
} else {
let mut new_arguments = vec![];
let mut n = 0;
while n < arguments.0.len() {
let argument = &arguments.0[n];
match argument {
TokenOrValue::Token(Token::ParenthesisBlock) => {
if n + 2 < arguments.0.len() && arguments.0[n + 2] == TokenOrValue::Token(Token::CloseParenthesis) {
new_arguments.push(arguments.0[n + 1].clone());
n += 2;
} else {
new_arguments.push(argument.clone());
}
}
TokenOrValue::Function(inner_f) => {
if inner_f.name == "calc" {
new_arguments.push(TokenOrValue::Token(Token::ParenthesisBlock));
new_arguments.append(&mut inner_f.arguments.0.to_vec());
new_arguments.push(TokenOrValue::Token(Token::CloseParenthesis));
} else {
new_arguments.push(argument.clone());
}
},
_ => {
new_arguments.push(argument.clone());
}
}
n += 1;
}
tokens.push(TokenOrValue::Function(Function {
name: Ident(f),
arguments: TokenList(new_arguments)
}));
}
last_is_delim = true; // Whitespace is not required after any of these chars.
last_is_whitespace = false;
} else if f == "var" {
let var = input.parse_nested_block(|input| {
let var = Variable::parse(input, options, depth + 1)?;
Expand Down