Skip to content

Commit 644ef86

Browse files
authored
Merge pull request #539 from dev-five-git/fix-selector-debug
Fix selector debug
2 parents 42589ed + 907a11f commit 644ef86

11 files changed

Lines changed: 254 additions & 286 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"bindings/devup-ui-wasm/package.json":"Patch"},"note":"Fix selector debug","date":"2026-01-05T11:12:03.161304100Z"}

libs/css/src/lib.rs

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub mod utils;
1313

1414
use once_cell::sync::Lazy;
1515
use std::collections::BTreeMap;
16-
use std::hash::{DefaultHasher, Hash, Hasher};
1716
use std::sync::Mutex;
1817

1918
use crate::class_map::GLOBAL_CLASS_MAP;
@@ -157,6 +156,53 @@ pub fn keyframes_to_keyframes_name(keyframes: &str, filename: Option<&str>) -> S
157156
}
158157
}
159158

159+
fn encode_selector(selector: &str) -> String {
160+
let mut result = String::with_capacity(selector.len() * 2);
161+
for c in selector.chars() {
162+
match c {
163+
'&' => result.push_str("_a_"),
164+
':' => result.push_str("_c_"),
165+
'(' => result.push_str("_lp_"),
166+
')' => result.push_str("_rp_"),
167+
'[' => result.push_str("_lb_"),
168+
']' => result.push_str("_rb_"),
169+
'=' => result.push_str("_eq_"),
170+
'>' => result.push_str("_gt_"),
171+
'<' => result.push_str("_lt_"),
172+
'~' => result.push_str("_tl_"),
173+
'+' => result.push_str("_pl_"),
174+
' ' => result.push_str("_s_"),
175+
'*' => result.push_str("_st_"),
176+
'.' => result.push_str("_d_"),
177+
'#' => result.push_str("_h_"),
178+
',' => result.push_str("_cm_"),
179+
'"' => result.push_str("_dq_"),
180+
'\'' => result.push_str("_sq_"),
181+
'/' => result.push_str("_sl_"),
182+
'\\' => result.push_str("_bs_"),
183+
'%' => result.push_str("_pc_"),
184+
'^' => result.push_str("_cr_"),
185+
'$' => result.push_str("_dl_"),
186+
'|' => result.push_str("_pp_"),
187+
'@' => result.push_str("_at_"),
188+
'!' => result.push_str("_ex_"),
189+
'?' => result.push_str("_qm_"),
190+
';' => result.push_str("_sc_"),
191+
'{' => result.push_str("_lc_"),
192+
'}' => result.push_str("_rc_"),
193+
'-' => result.push('-'),
194+
'_' => result.push('_'),
195+
_ if c.is_ascii_alphanumeric() => result.push(c),
196+
_ => {
197+
result.push_str("_u");
198+
result.push_str(&format!("{:04x}", c as u32));
199+
result.push('_');
200+
}
201+
}
202+
}
203+
result
204+
}
205+
160206
pub fn sheet_to_classname(
161207
property: &str,
162208
level: u8,
@@ -183,9 +229,7 @@ pub fn sheet_to_classname(
183229
if selector.is_empty() {
184230
"".to_string()
185231
} else {
186-
let mut hasher = DefaultHasher::new();
187-
selector.hash(&mut hasher);
188-
hasher.finish().to_string()
232+
encode_selector(selector)
189233
},
190234
style_order.unwrap_or(255),
191235
filename
@@ -242,9 +286,7 @@ pub fn sheet_to_variable_name(property: &str, level: u8, selector: Option<&str>)
242286
if selector.is_empty() {
243287
"".to_string()
244288
} else {
245-
let mut hasher = DefaultHasher::new();
246-
selector.hash(&mut hasher);
247-
hasher.finish().to_string()
289+
encode_selector(selector)
248290
}
249291
)
250292
} else {
@@ -278,8 +320,37 @@ mod tests {
278320
};
279321

280322
use super::*;
323+
use rstest::rstest;
281324
use serial_test::serial;
282325

326+
#[rstest]
327+
#[case("hover", "hover")]
328+
#[case("&:hover", "_a__c_hover")]
329+
#[case("&::before", "_a__c__c_before")]
330+
#[case("nth(1)", "nth_lp_1_rp_")]
331+
#[case("nth-child(2)", "nth-child_lp_2_rp_")]
332+
#[case("&:nth-child(2n+1)", "_a__c_nth-child_lp_2n_pl_1_rp_")]
333+
#[case("[data-theme=dark]", "_lb_data-theme_eq_dark_rb_")]
334+
#[case("& > div", "_a__s__gt__s_div")]
335+
#[case("& + span", "_a__s__pl__s_span")]
336+
#[case("& ~ p", "_a__s__tl__s_p")]
337+
#[case(".class-name", "_d_class-name")]
338+
#[case("#id-name", "_h_id-name")]
339+
#[case("&:hover:focus", "_a__c_hover_c_focus")]
340+
#[case("&::placeholder", "_a__c__c_placeholder")]
341+
#[case(
342+
":root[data-theme=\"dark\"]",
343+
"_c_root_lb_data-theme_eq__dq_dark_dq__rb_"
344+
)]
345+
#[case("&:not(.active)", "_a__c_not_lp__d_active_rp_")]
346+
#[case("simple", "simple")]
347+
#[case("with-dash", "with-dash")]
348+
#[case("with_underscore", "with_underscore")]
349+
#[case("CamelCase123", "CamelCase123")]
350+
fn test_encode_selector(#[case] input: &str, #[case] expected: &str) {
351+
assert_eq!(encode_selector(input), expected);
352+
}
353+
283354
#[test]
284355
#[serial]
285356
fn test_sheet_to_variable_name() {
@@ -307,15 +378,15 @@ mod tests {
307378
);
308379
assert_eq!(
309380
sheet_to_variable_name("background", 0, Some("hover")),
310-
"--background-0-12448419602614487988"
381+
"--background-0-hover"
311382
);
312383
assert_eq!(
313384
sheet_to_variable_name("background", 1, None),
314385
"--background-1-"
315386
);
316387
assert_eq!(
317388
sheet_to_variable_name("background", 1, Some("hover")),
318-
"--background-1-12448419602614487988"
389+
"--background-1-hover"
319390
);
320391
}
321392

@@ -601,15 +672,15 @@ mod tests {
601672
);
602673
assert_eq!(
603674
sheet_to_classname("background", 0, Some("red"), Some("hover"), None, None),
604-
"background-0-red-12448419602614487988-255"
675+
"background-0-red-hover-255"
605676
);
606677
assert_eq!(
607678
sheet_to_classname("background", 1, None, None, None, None),
608679
"background-1---255"
609680
);
610681
assert_eq!(
611682
sheet_to_classname("background", 1, Some("red"), Some("hover"), None, None),
612-
"background-1-red-12448419602614487988-255"
683+
"background-1-red-hover-255"
613684
);
614685
}
615686

0 commit comments

Comments
 (0)