Skip to content

Commit 09d80da

Browse files
RobinMalfaitphilipp-spiess
authored andcommitted
Extract special @("@")md:… syntax in Razor files (#17427)
This PR fixes an extraction issue in Razor files where `@@md:bg-red-500` can't always be extracted properly. We already convert `@@md:bg-red-500` to ` @md:bg-red-500` but in certain situations Razor will emit the double `@@` to the DOM. A workaround in Razor land would be to write `@("@")md:bg-red-500` instead. See: dotnet/aspnetcore#38595 But then we don't extract the `@md:bg-red-500` properly anymore. This is where this PR comes in, essentially we will pre process the Razor contents and apply the following replacement internally: ```diff - @("@")md:bg-red-500 + @md:bg-red-500 ``` Notice that the `)` looks like it's replaced with `@`. This will have a small side effect later when we get to the testing part. But this way we properly see the `@md:bg-red-500` class during class extraction. > [!WARNING] > There is technically a bug here because of the replacement with `@`, because if you now run the `npx @tailwindcss/upgrade@latest` tool, then we would replace `)md:bg-red-500` if changes are required, not the `@("@")md:bg-red-500` part. We can try to fix that in this PR but it seems unlikely that we will actually run into this issue realistically speaking. I think fixing the actual extraction here is much more important than the upgrade tooling that could fail _if_ we ever have to migrate `@md:…` to something else. Fixes: #17424 ## Test plan 1. Added a test to verify the fix 2. Existing tests pass 3. Verified this in the extractor tool, but it looks a tiny bit funky. Typically we remove characters by replacing it with a space ` `. But this time, we replace it with some spaces _and_ an `@` character that didn't exist at that specific position. If you look at the diff above, you will notice that `)` was replaced with `@`. That's why in the extractor tool it is highlighted that it could extract it, but it's just funny looking because it highlights `)md:bg-red-500` <img width="1816" alt="image" src="https://github.com/user-attachments/assets/57e6a3ac-bfd5-4cad-a1ce-0039b4d7d9b5" />
1 parent 3dc9f3f commit 09d80da

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Fix symlink issues when resolving `@source` directives ([#17391](https://github.com/tailwindlabs/tailwindcss/pull/17391))
2828
- `@tailwindcss/cli` considers ignore rules in `--watch` mode ([#17255](https://github.com/tailwindlabs/tailwindcss/pull/17255))
2929
- Fix negated `content` rules in legacy JavaScript configuration ([#17255](https://github.com/tailwindlabs/tailwindcss/pull/17255))
30+
- Extract special `@("@")md:…` syntax in Razor files ([#17427](https://github.com/tailwindlabs/tailwindcss/pull/17427))
3031

3132
### Changed
3233

crates/oxide/src/extractor/pre_processors/razor.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub struct Razor;
66

77
impl PreProcessor for Razor {
88
fn process(&self, content: &[u8]) -> Vec<u8> {
9-
content.replace("@@", " @")
9+
content.replace("@@", " @").replace(r#"@("@")"#, " @")
1010
}
1111
}
1212

@@ -24,4 +24,19 @@ mod tests {
2424
Razor::test(input, expected);
2525
Razor::test_extract_contains(input, vec!["@sm:text-red-500"]);
2626
}
27+
28+
// https://github.com/tailwindlabs/tailwindcss/issues/17424
29+
#[test]
30+
fn test_razor_syntax_with() {
31+
let (input, expected) = (
32+
r#"<p class="@("@")md:bg-red-500 @@md:border-green-500 border-8">With 2 elements</p>"#,
33+
r#"<p class=" @md:bg-red-500 @md:border-green-500 border-8">With 2 elements</p>"#,
34+
);
35+
36+
Razor::test(input, expected);
37+
Razor::test_extract_contains(
38+
input,
39+
vec!["@md:bg-red-500", "@md:border-green-500", "border-8"],
40+
);
41+
}
2742
}

0 commit comments

Comments
 (0)