Skip to content

Commit 002ab7f

Browse files
asynclizcopybara-github
authored andcommitted
fix(sass): don't trim extra parens in vars
PiperOrigin-RevId: 404674130
1 parent d3f5ae9 commit 002ab7f

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

components/sass/_string-ext.scss

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,28 @@
3333
///
3434
/// @example - scss
3535
/// @debug trim-prefix(' foo bar ', ' '); // "foo bar "
36-
/// @debug trim-prefix('var(--foo)', 'var('); // "--foo)"
3736
///
3837
/// @param {String} $string - The string to trim.
3938
/// @param {String} $prefix - The repeating prefix string to trim.
4039
/// @return {String} The string with the prefix trimmed from the start.
4140
@function trim-prefix($string, $prefix) {
4241
@while has-prefix($string, $prefix) {
42+
$string: trim-prefix-once($string, $prefix);
43+
}
44+
45+
@return $string;
46+
}
47+
48+
/// Trims a prefix from the start of a string.
49+
///
50+
/// @example - scss
51+
/// @debug trim-prefix('var(--foo)', 'var('); // "--foo)"
52+
///
53+
/// @param {String} $string - The string to trim.
54+
/// @param {String} $prefix - The prefix string to trim.
55+
/// @return {String} The string with the prefix trimmed from the start.
56+
@function trim-prefix-once($string, $prefix) {
57+
@if has-prefix($string, $prefix) {
4358
$string: string.slice($string, string.length($prefix) + 1);
4459
}
4560

@@ -57,6 +72,22 @@
5772
/// @return {String} The string with the suffix trimmed from the end.
5873
@function trim-suffix($string, $suffix) {
5974
@while has-suffix($string, $suffix) {
75+
$string: trim-suffix-once($string, $suffix);
76+
}
77+
78+
@return $string;
79+
}
80+
81+
/// Trims a suffix from the end of a string.
82+
///
83+
/// @example - scss
84+
/// @debug trim-suffix('var(--foo)', ')'); // "var(--foo"
85+
///
86+
/// @param {String} $string - The string to trim.
87+
/// @param {String} $suffix - The suffix string to trim.
88+
/// @return {String} The string with the suffix trimmed from the end.
89+
@function trim-suffix-once($string, $suffix) {
90+
@if has-suffix($string, $suffix) {
6091
$string: string.slice($string, 1, -1 * string.length($suffix) - 1);
6192
}
6293

@@ -69,7 +100,6 @@
69100
///
70101
/// @example - scss
71102
/// @debug trim(' foo bar ', ' '); // "foo bar"
72-
/// @debug trim('var(--foo)', 'var(', ')'); // "--foo"
73103
///
74104
/// @param {String} $string - The string to trim.
75105
/// @param {String} $prefix - The repeating prefix string to trim.
@@ -78,3 +108,18 @@
78108
@function trim($string, $prefix, $suffix: $prefix) {
79109
@return trim-prefix(trim-suffix($string, $suffix), $prefix);
80110
}
111+
112+
/// Trims a prefix and suffix from the start and end of a string.
113+
///
114+
/// If a suffix is not provided, the prefix is used as the suffix to trim.
115+
///
116+
/// @example - scss
117+
/// @debug trim('var(--foo)', 'var(', ')'); // "--foo"
118+
///
119+
/// @param {String} $string - The string to trim.
120+
/// @param {String} $prefix - The prefix string to trim.
121+
/// @param {String} $suffix [$prefix] - The suffix string to trim.
122+
/// @return {String} The string with the prefix and suffix trimmed.
123+
@function trim-once($string, $prefix, $suffix: $prefix) {
124+
@return trim-prefix-once(trim-suffix-once($string, $suffix), $prefix);
125+
}

components/sass/_var.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
}
5252

5353
// Remove function name and parens
54-
$var: string-ext.trim($var, 'var(', ')');
54+
$var: string-ext.trim-once($var, 'var(', ')');
5555

5656
$name: string-ext.trim($var, ' ');
5757
$fallback: null;

0 commit comments

Comments
 (0)