Skip to content

Commit 2f7e543

Browse files
authored
Merge pull request #152 from dev-five-git/optimize-value
Fix optimize value
2 parents b22f914 + c7ae821 commit 2f7e543

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

.changeset/odd-moles-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Optimize value

libs/css/src/lib.rs

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,34 @@ pub fn short_to_long(property: &str) -> String {
352352
}
353353

354354
static F_SPACE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\s*,\s*").unwrap());
355+
static COLOR_HASH: Lazy<Regex> = Lazy::new(|| Regex::new(r"#([0-9a-zA-Z]+)").unwrap());
356+
357+
fn optimize_color(value: &str) -> String {
358+
let mut ret = value.to_string().to_uppercase();
359+
360+
if ret.len() == 6 {
361+
let ch = ret.chars().collect::<Vec<char>>();
362+
if ch[0] == ch[1] && ch[2] == ch[3] && ch[4] == ch[5] {
363+
ret = format!("{}{}{}", ch[0], ch[2], ch[4]);
364+
}
365+
}
366+
367+
format!("#{}", ret)
368+
}
369+
370+
fn optimize_value(value: &str) -> String {
371+
let mut ret = value.trim().to_string();
372+
if ret.contains(",") {
373+
ret = F_SPACE_RE.replace_all(&ret, ",").trim().to_string();
374+
}
375+
if ret.contains("#") {
376+
ret = COLOR_HASH
377+
.replace_all(&ret, |c: &regex::Captures| optimize_color(&c[1]))
378+
.to_string();
379+
}
380+
ret
381+
}
382+
355383
pub fn sheet_to_classname(
356384
property: &str,
357385
level: u8,
@@ -365,7 +393,7 @@ pub fn sheet_to_classname(
365393
"{}-{}-{}-{}-{}",
366394
property.trim(),
367395
level,
368-
F_SPACE_RE.replace_all(value.unwrap_or(""), ",").trim(),
396+
optimize_value(value.unwrap_or("")),
369397
if selector.is_empty() {
370398
"".to_string()
371399
} else {
@@ -380,7 +408,7 @@ pub fn sheet_to_classname(
380408
"{}-{}-{}-{}-{}",
381409
property.trim(),
382410
level,
383-
F_SPACE_RE.replace_all(value.unwrap_or(""), ",").trim(),
411+
optimize_value(value.unwrap_or("")),
384412
selector.unwrap_or("").trim(),
385413
style_order.unwrap_or(255)
386414
);
@@ -476,15 +504,15 @@ mod tests {
476504
"--background-0-"
477505
);
478506
assert_eq!(
479-
sheet_to_variable_name("background", 0, Some("hover".into())),
507+
sheet_to_variable_name("background", 0, Some("hover")),
480508
"--background-0-12448419602614487988"
481509
);
482510
assert_eq!(
483511
sheet_to_variable_name("background", 1, None),
484512
"--background-1-"
485513
);
486514
assert_eq!(
487-
sheet_to_variable_name("background", 1, Some("hover".into())),
515+
sheet_to_variable_name("background", 1, Some("hover")),
488516
"--background-1-12448419602614487988"
489517
);
490518
}
@@ -509,34 +537,58 @@ mod tests {
509537
);
510538

511539
reset_class_map();
512-
assert_eq!(sheet_to_classname("background", 0, None, None, None), "d0");
513-
assert_eq!(sheet_to_classname("background", 0, None, None, None), "d0");
514540
assert_eq!(
515-
sheet_to_classname("background", 0, Some("red"), None, None),
516-
"d1"
541+
sheet_to_classname("background", 0, None, None, None),
542+
sheet_to_classname("background", 0, None, None, None)
517543
);
518544
assert_eq!(
519545
sheet_to_classname("background", 0, Some("red"), None, None),
520-
"d1"
546+
sheet_to_classname("background", 0, Some("red"), None, None),
521547
);
522548
assert_eq!(
549+
sheet_to_classname("background", 0, Some("red"), None, None),
523550
sheet_to_classname(" background ", 0, Some(" red "), None, None),
524-
"d1"
525551
);
526-
527552
assert_eq!(
528553
sheet_to_classname("background", 0, Some("rgba(255, 0, 0, 0.5)"), None, None),
529-
"d2"
530-
);
531-
assert_eq!(
532554
sheet_to_classname("background", 0, Some("rgba(255,0,0,0.5)"), None, None),
533-
"d2"
534555
);
535556

536557
{
537558
let map = GLOBAL_CLASS_MAP.lock().unwrap();
538559
assert_eq!(map.get("background-0-rgba(255,0,0,0.5)--255"), Some(&2));
539560
}
561+
assert_eq!(
562+
sheet_to_classname("background", 0, Some("#fff"), None, None),
563+
sheet_to_classname(" background ", 0, Some("#FFF"), None, None),
564+
);
565+
566+
assert_eq!(
567+
sheet_to_classname("background", 0, Some("#ffffff"), None, None),
568+
sheet_to_classname("background", 0, Some("#FFF"), None, None),
569+
);
570+
571+
assert_eq!(
572+
sheet_to_classname("background", 0, Some("#ffffff"), None, None),
573+
sheet_to_classname("background", 0, Some("#FFFFFF"), None, None),
574+
);
575+
576+
assert_eq!(
577+
sheet_to_classname(
578+
"background",
579+
0,
580+
Some("color-mix(in srgb,var(--primary) 80%, #000 20%)"),
581+
None,
582+
None
583+
),
584+
sheet_to_classname(
585+
"background",
586+
0,
587+
Some("color-mix(in srgb, var(--primary) 80%, #000000 20%)"),
588+
None,
589+
None
590+
),
591+
);
540592

541593
reset_class_map();
542594
assert_eq!(sheet_to_classname("background", 0, None, None, None), "d0");

0 commit comments

Comments
 (0)