Skip to content

Commit bff387b

Browse files
Ensure arbitrary variables with data types are extracted correctly (#16986)
Closes #16983 This PR fixes an issue where the arbitrary variable machine did not extract data type correctly. ## Test plan - Added regression test
1 parent af132fb commit bff387b

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Ensure classes containing numbers followed by dash or underscore are extracted correctly ([#16980](https://github.com/tailwindlabs/tailwindcss/pull/16980))
2424
- Ensure arbitrary container queries are extracted correctly ([#16984](https://github.com/tailwindlabs/tailwindcss/pull/16984))
2525
- Ensure classes ending in `[` are extracted in Slim templating language ([#16985](https://github.com/tailwindlabs/tailwindcss/pull/16985))
26+
- Ensure arbitrary variables with data types are extracted correctly ([#16986](https://github.com/tailwindlabs/tailwindcss/pull/16986))
2627

2728
## [4.0.10] - 2025-03-05
2829

Diff for: crates/oxide/src/extractor/arbitrary_value_machine.rs

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ mod tests {
176176
"[&>[data-slot=icon]:last-child]",
177177
vec!["[&>[data-slot=icon]:last-child]"],
178178
),
179+
// With data types
180+
("[length:32rem]", vec!["[length:32rem]"]),
179181
// Spaces are not allowed
180182
("[ #0088cc ]", vec![]),
181183
// Unbalanced brackets are not allowed

Diff for: crates/oxide/src/extractor/arbitrary_variable_machine.rs

+49
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ enum State {
3636
#[default]
3737
Idle,
3838

39+
/// Currently parsing the data type of the arbitrary variable
40+
///
41+
/// ```text
42+
/// (length:--my-opacity)
43+
/// ^^^^^^^
44+
/// ```
45+
ParsingDataType,
46+
3947
/// Currently parsing the inside of the arbitrary variable
4048
///
4149
/// ```text
@@ -81,6 +89,13 @@ impl Machine for ArbitraryVariableMachine {
8189
self.next(cursor)
8290
}
8391

92+
Class::AlphaLower => {
93+
self.start_pos = cursor.pos;
94+
self.state = State::ParsingDataType;
95+
cursor.advance();
96+
self.next(cursor)
97+
}
98+
8499
_ => MachineState::Idle,
85100
},
86101

@@ -89,6 +104,38 @@ impl Machine for ArbitraryVariableMachine {
89104
_ => MachineState::Idle,
90105
},
91106

107+
State::ParsingDataType => {
108+
while cursor.pos < len {
109+
match cursor.curr.into() {
110+
// Valid data type characters
111+
//
112+
// E.g.: `(length:--my-length)`
113+
// ^
114+
Class::AlphaLower | Class::Dash => {
115+
cursor.advance();
116+
}
117+
118+
// End of the data type
119+
//
120+
// E.g.: `(length:--my-length)`
121+
// ^
122+
Class::Colon => match cursor.next.into() {
123+
Class::Dash => {
124+
self.state = State::Parsing;
125+
cursor.advance();
126+
return self.next(cursor);
127+
}
128+
129+
_ => return self.restart(),
130+
},
131+
132+
// Anything else is not a valid character
133+
_ => return self.restart(),
134+
};
135+
}
136+
self.restart()
137+
}
138+
92139
State::Parsing => match self.css_variable_machine.next(cursor) {
93140
MachineState::Idle => self.restart(),
94141
MachineState::Done(_) => match cursor.next.into() {
@@ -286,6 +333,8 @@ mod tests {
286333
"(--my-img,url('https://example.com?q=(][)'))",
287334
vec!["(--my-img,url('https://example.com?q=(][)'))"],
288335
),
336+
// With a type hint
337+
("(length:--my-length)", vec!["(length:--my-length)"]),
289338
// --------------------------------------------------------
290339

291340
// Exceptions:

Diff for: crates/oxide/src/extractor/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ mod tests {
846846
],
847847
);
848848
}
849-
849+
850850
// https://github.com/tailwindlabs/tailwindcss/issues/16978
851851
#[test]
852852
fn test_classes_containing_number_followed_by_dash_or_underscore() {
@@ -856,6 +856,15 @@ mod tests {
856856
);
857857
}
858858

859+
// https://github.com/tailwindlabs/tailwindcss/issues/16983
860+
#[test]
861+
fn test_arbitrary_variable_with_data_type() {
862+
assert_extract_sorted_candidates(
863+
r#"<div class="bg-(length:--my-length) bg-[color:var(--my-color)]"></div>"#,
864+
vec!["bg-(length:--my-length)", "bg-[color:var(--my-color)]"],
865+
);
866+
}
867+
859868
#[test]
860869
fn test_extract_css_variables() {
861870
for (input, expected) in [

0 commit comments

Comments
 (0)