Skip to content

Commit 4033ff4

Browse files
committed
feat(css): add normalize_spacing_only_value function for improved whitespace handling
Introduces a new helper function, `normalize_spacing_only_value`, to enhance the normalization of CSS property values by managing whitespace more effectively. This function replaces the previous inline logic, ensuring consistent handling of spaces, particularly around parentheses. Additionally, updates the test for relative RGB color to account for trailing whitespace, ensuring accurate normalization in various scenarios.
1 parent c7020e9 commit 4033ff4

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

crates/stylex-css/src/css/common.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,27 @@ fn has_relative_color_call(value: &str, function_name: &str) -> bool {
485485
})
486486
}
487487

488+
fn normalize_spacing_only_value(value: &str) -> String {
489+
let value = MANY_SPACES.replace_all(value, " ");
490+
let mut normalized = String::with_capacity(value.len());
491+
let mut previous_char: Option<char> = None;
492+
let mut chars = value.chars().peekable();
493+
494+
while let Some(ch) = chars.next() {
495+
if ch == ' ' {
496+
match (previous_char, chars.peek()) {
497+
(Some('('), _) | (_, Some(')')) => continue,
498+
_ => {},
499+
}
500+
}
501+
502+
normalized.push(ch);
503+
previous_char = Some(ch);
504+
}
505+
506+
normalized
507+
}
508+
488509
fn is_escaped(value: &[u8], index: usize) -> bool {
489510
let mut backslash_count = 0;
490511
let mut cursor = index;
@@ -661,9 +682,7 @@ pub fn normalize_css_property_value(
661682
detect_unclosed_strings(css_property_value);
662683

663684
if should_normalize_spacing_only {
664-
return MANY_SPACES
665-
.replace_all(css_property_value, " ")
666-
.replace("( ", "(");
685+
return normalize_spacing_only_value(css_property_value);
667686
}
668687

669688
let (parsed_css, errors) = swc_parse_css(css_rule.as_str());

crates/stylex-css/src/css/tests/common_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ mod normalize_css_property_value_tests {
624624
#[test]
625625
fn relative_rgb_color_collapses_whitespace_after_open_paren() {
626626
let opts = default_options();
627-
let result = normalize_css_property_value("color", "rgb( from red r g b)", &opts);
627+
let result = normalize_css_property_value("color", "rgb( from red r g b )", &opts);
628628
assert_eq!(result, "rgb(from red r g b)");
629629
}
630630

0 commit comments

Comments
 (0)