Skip to content

Commit 7d31725

Browse files
authored
Handle Ruby %w syntax in Slim pre processing (#17557)
This PR fixes an issue where the Ruby `%w[…]` syntax causes utilities to not be properly extracted. This PR will now ensure that it does get extracted correctly. Given this input: ```slim div[ class=%w[bg-blue-500 w-10 h-10] ] div[ class=%w[w-10 bg-green-500 h-10] ] ``` Before this PR, we extracted everything but the `bg-blue-500`. The `w-10` was extracted but not because of the second div, but because of the first one. Fixes: #17542 ## Test plan 1. Added a test to ensure it's working correctly. Looking at the extractor tool, you can see that it now gets extracted correctly. Top is before, bottom is with this change. <img width="1199" alt="image" src="https://github.com/user-attachments/assets/028d9abd-8917-438c-a423-88ba887b7f97" />
1 parent e085977 commit 7d31725

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Show warning when using unsupported bare value data type in `--value(…)` ([#17464](https://github.com/tailwindlabs/tailwindcss/pull/17464))
1313
- PostCSS: Resolve an issue where changes to the input CSS file showed outdated content when using Turbopack ([#17554](https://github.com/tailwindlabs/tailwindcss/pull/17554))
14+
- Handle Ruby `%w` syntax in Slim pre processing ([#17557](https://github.com/tailwindlabs/tailwindcss/pull/17557))
1415

1516
## [4.1.2] - 2025-04-03
1617

Diff for: crates/oxide/src/extractor/pre_processors/slim.rs

+47
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ impl PreProcessor for Slim {
5656
}
5757
}
5858

59+
// Handle Ruby syntax with `%w[]` arrays embedded in Slim directly.
60+
//
61+
// E.g.:
62+
//
63+
// ```
64+
// div [
65+
// class=%w[bg-blue-500 w-10 h-10]
66+
// ]
67+
// ```
68+
b'%' if matches!(cursor.next, b'w' | b'W')
69+
&& matches!(cursor.input.get(cursor.pos + 2), Some(b'[' | b'(' | b'{')) =>
70+
{
71+
result[cursor.pos] = b' '; // Replace `%`
72+
cursor.advance();
73+
result[cursor.pos] = b' '; // Replace `w`
74+
cursor.advance();
75+
result[cursor.pos] = b' '; // Replace `[` or `(` or `{`
76+
bracket_stack.push(cursor.curr);
77+
cursor.advance(); // Move past the bracket
78+
continue;
79+
}
80+
5981
// Any `[` preceded by an alphanumeric value will not be part of a candidate.
6082
//
6183
// E.g.:
@@ -281,4 +303,29 @@ mod tests {
281303
"#;
282304
Slim::test_extract_contains(input, vec!["flex", "items-center"]);
283305
}
306+
307+
// https://github.com/tailwindlabs/tailwindcss/issues/17542
308+
#[test]
309+
fn test_embedded_ruby_percent_w_extraction() {
310+
let input = r#"
311+
div[
312+
class=%w[bg-blue-500 w-10 h-10]
313+
]
314+
div[
315+
class=%w[w-10 bg-green-500 h-10]
316+
]
317+
"#;
318+
319+
let expected = r#"
320+
div
321+
class= bg-blue-500 w-10 h-10]
322+
]
323+
div
324+
class= w-10 bg-green-500 h-10]
325+
]
326+
"#;
327+
328+
Slim::test(input, expected);
329+
Slim::test_extract_contains(input, vec!["bg-blue-500", "bg-green-500", "w-10", "h-10"]);
330+
}
284331
}

0 commit comments

Comments
 (0)