Skip to content

Fix HAML extraction with embedded Ruby #17846

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

Merged
merged 6 commits into from
May 5, 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 @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Ensure negative arbitrary `scale` values generate negative values ([#17831](https://github.com/tailwindlabs/tailwindcss/pull/17831))
- Fix HAML extraction with embedded Ruby ([#17846](https://github.com/tailwindlabs/tailwindcss/pull/17846))

## [4.1.5] - 2025-04-30

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ regex = "1.11.1"
[dev-dependencies]
tempfile = "3.13.0"
pretty_assertions = "1.4.1"
unicode-width = "0.2.0"
264 changes: 260 additions & 4 deletions crates/oxide/src/extractor/pre_processors/haml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::extractor::bracket_stack::BracketStack;
use crate::extractor::machine::{Machine, MachineState};
use crate::extractor::pre_processors::pre_processor::PreProcessor;
use crate::extractor::variant_machine::VariantMachine;
use crate::scanner::pre_process_input;
use bstr::ByteVec;

#[derive(Debug, Default)]
pub struct Haml;
Expand All @@ -14,8 +16,153 @@ impl PreProcessor for Haml {
let mut cursor = cursor::Cursor::new(content);
let mut bracket_stack = BracketStack::default();

// Haml Comments: -#
// https://haml.info/docs/yardoc/file.REFERENCE.html#ruby-evaluation
//
// > The hyphen followed immediately by the pound sign signifies a silent comment. Any text
// > following this isn’t rendered in the resulting document at all.
//
// ```haml
// %p foo
// -# This is a comment
// %p bar
// ```
//
// > You can also nest text beneath a silent comment. None of this text will be rendered.
//
// ```haml
// %p foo
// -#
// This won't be displayed
// Nor will this
// Nor will this.
// %p bar
// ```
//
// Ruby Evaluation
// https://haml.info/docs/yardoc/file.REFERENCE.html#ruby-evaluation
//
// When any of the following characters are the first non-whitespace character on the line,
// then the line is treated as Ruby code:
//
// - Inserting Ruby: =
// https://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby
//
// ```haml
// %p
// = ['hi', 'there', 'reader!'].join " "
// = "yo"
// ```
//
// - Running Ruby: -
// https://haml.info/docs/yardoc/file.REFERENCE.html#running-ruby--
//
// ```haml
// - foo = "hello"
// - foo << " there"
// - foo << " you!"
// %p= foo
// ```
//
// - Whitespace Preservation: ~
// https://haml.info/docs/yardoc/file.REFERENCE.html#tilde
//
// > ~ works just like =, except that it runs Haml::Helpers.preserve on its input.
//
// ```haml
// ~ "Foo\n<pre>Bar\nBaz</pre>"
// ```
//
// Important note:
//
// > A line of Ruby code can be stretched over multiple lines as long as each line but the
// > last ends with a comma.
//
// ```haml
// - links = {:home => "/",
// :docs => "/docs",
// :about => "/about"}
// ```
//
// Ruby Blocks:
// https://haml.info/docs/yardoc/file.REFERENCE.html#ruby-blocks
//
// > Ruby blocks, like XHTML tags, don’t need to be explicitly closed in Haml. Rather,
// > they’re automatically closed, based on indentation. A block begins whenever the
// > indentation is increased after a Ruby evaluation command. It ends when the indentation
// > decreases (as long as it’s not an else clause or something similar).
//
// ```haml
// - (42...47).each do |i|
// %p= i
// %p See, I can count!
// ```
//
let mut last_newline_position = 0;

while cursor.pos < len {
match cursor.curr {
// Escape the next character
b'\\' => {
cursor.advance_twice();
continue;
}

// Track the last newline position
b'\n' => {
last_newline_position = cursor.pos;
cursor.advance();
continue;
}

// Skip HAML comments. `-#`
b'-' if cursor.input[last_newline_position..cursor.pos]
.iter()
.all(u8::is_ascii_whitespace)
&& matches!(cursor.next, b'#') =>
{
// Just consume the comment
let updated_last_newline_position =
self.skip_indented_block(&mut cursor, last_newline_position);

// Override the last known newline position
last_newline_position = updated_last_newline_position;
}

// Skip HTML comments. `/`
b'/' if cursor.input[last_newline_position..cursor.pos]
.iter()
.all(u8::is_ascii_whitespace) =>
{
// Just consume the comment
let updated_last_newline_position =
self.skip_indented_block(&mut cursor, last_newline_position);

// Override the last known newline position
last_newline_position = updated_last_newline_position;
}

// Ruby evaluation
b'-' | b'=' | b'~'
if cursor.input[last_newline_position..cursor.pos]
.iter()
.all(u8::is_ascii_whitespace) =>
{
let mut start = cursor.pos;
let end = self.skip_indented_block(&mut cursor, last_newline_position);

// Increment start with 1 character to skip the `=` or `-` character
start += 1;

let ruby_code = &cursor.input[start..end];

// Override the last known newline position
last_newline_position = end;

let replaced = pre_process_input(ruby_code, "rb");
result.replace_range(start..end, replaced);
}

// Only replace `.` with a space if it's not surrounded by numbers. E.g.:
//
// ```diff
Expand Down Expand Up @@ -89,6 +236,107 @@ impl PreProcessor for Haml {
}
}

impl Haml {
fn skip_indented_block(
&self,
cursor: &mut cursor::Cursor,
last_known_newline_position: usize,
) -> usize {
let len = cursor.input.len();

// Special case: if the first character of the block is `=`, then newlines are only allowed
// _if_ the last character of the previous line is a comma `,`.
//
// https://haml.info/docs/yardoc/file.REFERENCE.html#inserting_ruby
//
// > A line of Ruby code can be stretched over multiple lines as long as each line but the
// > last ends with a comma. For example:
//
// ```haml
// = link_to_remote "Add to cart",
// :url => { :action => "add", :id => product.id },
// :update => { :success => "cart", :failure => "error" }
// ```
let evaluation_type = cursor.curr;

let block_indentation_level = cursor
.pos
.saturating_sub(last_known_newline_position)
.saturating_sub(1); /* The newline itself */

let mut last_newline_position = last_known_newline_position;

// Consume until the end of the line first
while cursor.pos < len && cursor.curr != b'\n' {
cursor.advance();
}

// Block is already done, aka just a line
if evaluation_type == b'=' && cursor.prev != b',' {
return cursor.pos;
}

'outer: while cursor.pos < len {
match cursor.curr {
// Escape the next character
b'\\' => {
cursor.advance_twice();
continue;
}

// Track the last newline position
b'\n' => {
last_newline_position = cursor.pos;

// We are done with this block
if evaluation_type == b'=' && cursor.prev != b',' {
break;
}

cursor.advance();
continue;
}

// Skip whitespace and compute the indentation level
x if x.is_ascii_whitespace() => {
// Find first non-whitespace character
while cursor.pos < len && cursor.curr.is_ascii_whitespace() {
if cursor.curr == b'\n' {
last_newline_position = cursor.pos;

if evaluation_type == b'=' && cursor.prev != b',' {
// We are done with this block
break 'outer;
}
}

cursor.advance();
}

let indentation = cursor
.pos
.saturating_sub(last_newline_position)
.saturating_sub(1); /* The newline itself */
if indentation < block_indentation_level {
// We are done with this block
break;
}
}

// Not whitespace, end of block
_ => break,
};

cursor.advance();
}

// Move the cursor to the last newline position
cursor.move_to(last_newline_position);

last_newline_position
}
}

#[cfg(test)]
mod tests {
use super::Haml;
Expand Down Expand Up @@ -173,10 +421,18 @@ mod tests {

// https://github.com/tailwindlabs/tailwindcss/pull/17051#issuecomment-2711181352
#[test]
fn test_haml_full_file() {
let processed = Haml.process(include_bytes!("./test-fixtures/haml/src-1.haml"));
let actual = std::str::from_utf8(&processed).unwrap();
let expected = include_str!("./test-fixtures/haml/dst-1.haml");
fn test_haml_full_file_17051() {
let actual = Haml::extract_annotated(include_bytes!("./test-fixtures/haml/src-17051.haml"));
let expected = include_str!("./test-fixtures/haml/dst-17051.haml");

assert_eq!(actual, expected);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a bit confusing to review. Looking at the snapshot alone I am really not sure if the right classes are extract. Should we run the extractor here and assert on the actual candidates being emitted?

}

// https://github.com/tailwindlabs/tailwindcss/issues/17813
#[test]
fn test_haml_full_file_17813() {
let actual = Haml::extract_annotated(include_bytes!("./test-fixtures/haml/src-17813.haml"));
let expected = include_str!("./test-fixtures/haml/dst-17813.haml");

assert_eq!(actual, expected);
}
Expand Down
Loading