Treat starting single quote as verbatim text in Slim (#17085)

This PR fixes an issue in Slim templates where a single quote `'` at the
start of the line (excluding white space) is considered a line indicator
for verbatim text. It is not considered a string in this scenario.

So something like this:
```slim
div
  'Foo'
```

Will compile to:
```html
<div>Foo'</div>
```

Fixes: #17081
This commit is contained in:
Robin Malfait 2025-03-10 11:45:26 +01:00 committed by GitHub
parent bc5a8c3683
commit cc3e852791
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 0 deletions

View File

@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix `haml` pre-processing ([#17051](https://github.com/tailwindlabs/tailwindcss/pull/17051))
- Ensure classes between `>` and `<` are properly extracted ([#17094](https://github.com/tailwindlabs/tailwindcss/pull/17094))
- Treat starting single quote as verbatim text in Slim ([#17085](https://github.com/tailwindlabs/tailwindcss/pull/17085))
## [4.0.12] - 2025-03-07

View File

@ -11,9 +11,26 @@ impl PreProcessor for Slim {
let mut result = content.to_vec();
let mut cursor = cursor::Cursor::new(content);
let mut bracket_stack = BracketStack::default();
let mut line_start_pos = 0x00;
while cursor.pos < len {
match cursor.curr {
b'\n' => {
line_start_pos = cursor.pos + 1;
}
// Line indicators:
//
// > Verbatim text with trailing white space '
// > See: https://github.com/slim-template/slim?tab=readme-ov-file#verbatim-text-with-trailing-white-space-
b'\''
if cursor.input[line_start_pos..cursor.pos]
.iter()
.all(|x| x.is_ascii_whitespace()) =>
{
// Do not treat the `'` as a string
}
// Consume strings as-is
b'\'' | b'"' => {
let len = cursor.input.len();
@ -154,4 +171,30 @@ mod tests {
Slim::test(input, expected);
Slim::test_extract_contains(input, vec!["text-black", "bg-green-300", "bg-red-300"]);
}
// https://github.com/tailwindlabs/tailwindcss/issues/17081
// https://github.com/slim-template/slim?tab=readme-ov-file#verbatim-text-with-trailing-white-space-
#[test]
fn test_single_quotes_to_enforce_trailing_whitespace() {
let input = r#"
div
'A single quote enforces trailing white space
= 1234
.text-red-500.text-3xl
| This text should be red
"#;
let expected = r#"
div
'A single quote enforces trailing white space
= 1234
text-red-500 text-3xl
| This text should be red
"#;
Slim::test(input, expected);
Slim::test_extract_contains(input, vec!["text-red-500", "text-3xl"]);
}
}