Skip to content

Commit 8d7cbe9

Browse files
committed
Optimize minus
1 parent ceab704 commit 8d7cbe9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

libs/css/src/constant.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,11 @@ pub(super) static DOT_ZERO_RE: Lazy<Regex> =
9595
pub(super) static COLOR_HASH: Lazy<Regex> = Lazy::new(|| Regex::new(r"#([0-9a-zA-Z]+)").unwrap());
9696
pub(super) static INNER_TRIM_RE: Lazy<Regex> =
9797
Lazy::new(|| Regex::new(r"\(\s*([^)]*?)\s*\)").unwrap());
98+
99+
pub(super) static RM_MINUS_ZERO_RE: Lazy<Regex> =
100+
Lazy::new(|| Regex::new(r"-0(px|em|rem|vh|vw|%|dvh|dvw|\)|,)").unwrap());
101+
102+
pub(super) static NUM_TRIM_RE: Lazy<Regex> =
103+
Lazy::new(|| Regex::new(r"(\d(px|em|rem|vh|vw|%|dvh|dvw)?)\s+(\d)").unwrap());
98104
pub(super) static ZERO_RE: Lazy<Regex> =
99-
Lazy::new(|| Regex::new(r"(^|\s|\(|,)-?0(px|em|rem|vh|vw|%|dvh|dvw)").unwrap());
105+
Lazy::new(|| Regex::new(r"(\b|,|\(|^|\s)-?0(px|em|rem|vh|vw|%|dvh|dvw)").unwrap());

libs/css/src/optimize_value.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
use crate::{
22
COLOR_HASH, F_SPACE_RE, ZERO_RE,
3-
constant::{DOT_ZERO_RE, F_DOT_RE, INNER_TRIM_RE, ZERO_PERCENT_FUNCTION},
3+
constant::{
4+
DOT_ZERO_RE, F_DOT_RE, INNER_TRIM_RE, NUM_TRIM_RE, RM_MINUS_ZERO_RE, ZERO_PERCENT_FUNCTION,
5+
},
46
};
57

68
pub fn optimize_value(value: &str) -> String {
79
let mut ret = value.trim().to_string();
810
ret = INNER_TRIM_RE.replace_all(&ret, "(${1})").to_string();
11+
ret = RM_MINUS_ZERO_RE.replace_all(&ret, "0${1}").to_string();
12+
ret = NUM_TRIM_RE.replace_all(&ret, "${1} ${3}").to_string();
13+
914
if ret.contains(",") {
1015
ret = F_SPACE_RE.replace_all(&ret, ",").trim().to_string();
1116
}
@@ -128,6 +133,8 @@ mod tests {
128133
#[case("0dvh", "0")]
129134
#[case("0dvw", "0")]
130135
#[case("0px 0px", "0 0")]
136+
#[case("-0px -0px", "0 0")]
137+
#[case("0.0px -0px", "0 0")]
131138
#[case("0em 0em", "0 0")]
132139
#[case("0rem 0rem", "0 0")]
133140
#[case("0vh 0vh", "0 0")]
@@ -158,6 +165,7 @@ mod tests {
158165
#[case("min(10px, 0)", "min(10px,0%)")]
159166
#[case("max(10px, 0)", "max(10px,0%)")]
160167
#[case("max(some(0), 0)", "max(some(0),0%)")]
168+
#[case("max(some(0), -0)", "max(some(0),0%)")]
161169
#[case("translate(0, min(0, 10px))", "translate(0,min(0%,10px))")]
162170
#[case("\"red\"", "\"red\"")]
163171
#[case("'red'", "'red'")]

0 commit comments

Comments
 (0)