Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure arbitrary variables with data types are extracted correctly #16986

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure classes containing numbers followed by dash or underscore are extracted correctly ([#16980](https://github.com/tailwindlabs/tailwindcss/pull/16980))
- Ensure arbitrary container queries are extracted correctly ([#16984](https://github.com/tailwindlabs/tailwindcss/pull/16984))
- Ensure classes ending in `[` are extracted in Slim templating language ([#16985](https://github.com/tailwindlabs/tailwindcss/pull/16985))
- Ensure arbitrary variables with data types are extracted correctly ([#16986](https://github.com/tailwindlabs/tailwindcss/pull/16986))

## [4.0.10] - 2025-03-05

Expand Down
2 changes: 2 additions & 0 deletions crates/oxide/src/extractor/arbitrary_value_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ mod tests {
"[&>[data-slot=icon]:last-child]",
vec!["[&>[data-slot=icon]:last-child]"],
),
// With data types
("[length:32rem]", vec!["[length:32rem]"]),
// Spaces are not allowed
("[ #0088cc ]", vec![]),
// Unbalanced brackets are not allowed
Expand Down
49 changes: 49 additions & 0 deletions crates/oxide/src/extractor/arbitrary_variable_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ enum State {
#[default]
Idle,

/// Currently parsing the data type of the arbitrary variable
///
/// ```text
/// (length:--my-opacity)
/// ^^^^^^^
/// ```
ParsingDataType,

/// Currently parsing the inside of the arbitrary variable
///
/// ```text
Expand Down Expand Up @@ -81,6 +89,13 @@ impl Machine for ArbitraryVariableMachine {
self.next(cursor)
}

Class::AlphaLower => {
self.start_pos = cursor.pos;
self.state = State::ParsingDataType;
cursor.advance();
self.next(cursor)
}

_ => MachineState::Idle,
},

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

State::ParsingDataType => {
while cursor.pos < len {
match cursor.curr.into() {
// Valid data type characters
//
// E.g.: `(length:--my-length)`
// ^
Class::AlphaLower | Class::Dash => {
cursor.advance();
}

// End of the data type
//
// E.g.: `(length:--my-length)`
// ^
Class::Colon => match cursor.next.into() {
Class::Dash => {
self.state = State::Parsing;
cursor.advance();
return self.next(cursor);
}

_ => return self.restart(),
},

// Anything else is not a valid character
_ => return self.restart(),
};
}
self.restart()
}

State::Parsing => match self.css_variable_machine.next(cursor) {
MachineState::Idle => self.restart(),
MachineState::Done(_) => match cursor.next.into() {
Expand Down Expand Up @@ -286,6 +333,8 @@ mod tests {
"(--my-img,url('https://example.com?q=(][)'))",
vec!["(--my-img,url('https://example.com?q=(][)'))"],
),
// With a type hint
("(length:--my-length)", vec!["(length:--my-length)"]),
// --------------------------------------------------------

// Exceptions:
Expand Down
11 changes: 10 additions & 1 deletion crates/oxide/src/extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ mod tests {
],
);
}

// https://github.com/tailwindlabs/tailwindcss/issues/16978
#[test]
fn test_classes_containing_number_followed_by_dash_or_underscore() {
Expand All @@ -856,6 +856,15 @@ mod tests {
);
}

// https://github.com/tailwindlabs/tailwindcss/issues/16983
#[test]
fn test_arbitrary_variable_with_data_type() {
assert_extract_sorted_candidates(
r#"<div class="bg-(length:--my-length) bg-[color:var(--my-color)]"></div>"#,
vec!["bg-(length:--my-length)", "bg-[color:var(--my-color)]"],
);
}

#[test]
fn test_extract_css_variables() {
for (input, expected) in [
Expand Down