|
| 1 | +use crate::cursor; |
| 2 | +use crate::extractor::bracket_stack::BracketStack; |
| 3 | +use crate::extractor::machine::Machine; |
| 4 | +use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 5 | +use crate::StringMachine; |
| 6 | + |
| 7 | +#[derive(Debug, Default)] |
| 8 | +pub struct Slim; |
| 9 | + |
| 10 | +impl PreProcessor for Slim { |
| 11 | + fn process(&self, content: &[u8]) -> Vec<u8> { |
| 12 | + let len = content.len(); |
| 13 | + let mut result = content.to_vec(); |
| 14 | + let mut cursor = cursor::Cursor::new(content); |
| 15 | + let mut string_machine = StringMachine; |
| 16 | + let mut bracket_stack = BracketStack::default(); |
| 17 | + |
| 18 | + while cursor.pos < len { |
| 19 | + match cursor.curr { |
| 20 | + // Consume strings as-is |
| 21 | + b'\'' | b'"' => { |
| 22 | + string_machine.next(&mut cursor); |
| 23 | + } |
| 24 | + |
| 25 | + // Replace dots with spaces |
| 26 | + b'.' if bracket_stack.is_empty() => { |
| 27 | + result[cursor.pos] = b' '; |
| 28 | + } |
| 29 | + |
| 30 | + // Any `[` preceded by an alphanumeric value will not be part of a candidate. |
| 31 | + // |
| 32 | + // E.g.: |
| 33 | + // |
| 34 | + // ``` |
| 35 | + // .text-xl.text-red-600[ |
| 36 | + // ^ not part of the `text-red-600` candidate |
| 37 | + // data-foo="bar" |
| 38 | + // ] |
| 39 | + // | This line should be red |
| 40 | + // ``` |
| 41 | + // |
| 42 | + // We know that `-[` is valid for an arbitrary value and that `:[` is valid as a |
| 43 | + // variant. However `[color:red]` is also valid, in this case `[` will be preceded |
| 44 | + // by nothing or a boundary character. |
| 45 | + // Instead of listing all boundary characters, let's list the characters we know |
| 46 | + // will be invalid instead. |
| 47 | + b'[' if bracket_stack.is_empty() |
| 48 | + && matches!(cursor.prev, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9') => |
| 49 | + { |
| 50 | + result[cursor.pos] = b' '; |
| 51 | + bracket_stack.push(cursor.curr); |
| 52 | + } |
| 53 | + |
| 54 | + b'(' | b'[' | b'{' => { |
| 55 | + bracket_stack.push(cursor.curr); |
| 56 | + } |
| 57 | + |
| 58 | + b')' | b']' | b'}' if !bracket_stack.is_empty() => { |
| 59 | + bracket_stack.pop(cursor.curr); |
| 60 | + } |
| 61 | + |
| 62 | + // Consume everything else |
| 63 | + _ => {} |
| 64 | + }; |
| 65 | + |
| 66 | + cursor.advance(); |
| 67 | + } |
| 68 | + |
| 69 | + result |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::Slim; |
| 76 | + use crate::extractor::pre_processors::pre_processor::PreProcessor; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_slim_pre_processor() { |
| 80 | + for (input, expected) in [ |
| 81 | + // Convert dots to spaces |
| 82 | + ("div.flex.bg-red-500", "div flex bg-red-500"), |
| 83 | + (".flex.bg-red-500", " flex bg-red-500"), |
| 84 | + // Keep dots in strings |
| 85 | + (r#"div(class="px-2.5")"#, r#"div(class="px-2.5")"#), |
| 86 | + // Replace top-level `(a-z0-9)[` with `$1 `. E.g.: `.flex[x]` -> `.flex x]` |
| 87 | + (".text-xl.text-red-600[", " text-xl text-red-600 "), |
| 88 | + // But keep important brackets: |
| 89 | + (".text-[#0088cc]", " text-[#0088cc]"), |
| 90 | + // Arbitrary value and arbitrary modifier |
| 91 | + ( |
| 92 | + ".text-[#0088cc].bg-[#0088cc]/[20%]", |
| 93 | + " text-[#0088cc] bg-[#0088cc]/[20%]", |
| 94 | + ), |
| 95 | + // Start of arbitrary property |
| 96 | + ("[color:red]", "[color:red]"), |
| 97 | + // Arbitrary container query |
| 98 | + ("@[320px]:flex", "@[320px]:flex"), |
| 99 | + // Nested brackets |
| 100 | + ( |
| 101 | + "bg-[url(https://example.com/?q=[1,2])]", |
| 102 | + "bg-[url(https://example.com/?q=[1,2])]", |
| 103 | + ), |
| 104 | + // Nested brackets, with "invalid" syntax but valid due to nesting |
| 105 | + ("content-['50[]']", "content-['50[]']"), |
| 106 | + ] { |
| 107 | + Slim::test(input, expected); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments