Home / Function/ test_utility_extraction() — tailwindcss Function Reference

test_utility_extraction() — tailwindcss Function Reference

Architecture documentation for the test_utility_extraction() function in utility_machine.rs from the tailwindcss codebase.

Entity Profile

Relationship Graph

Source Code

crates/oxide/src/extractor/utility_machine.rs lines 209–345

    fn test_utility_extraction() {
        for (input, expected) in [
            // Simple utility
            ("flex", vec!["flex"]),
            // Simple utility with special character(s)
            ("@container", vec!["@container"]),
            // Single character utility
            ("a", vec!["a"]),
            // Important utilities
            ("!flex", vec!["!flex"]),
            ("flex!", vec!["flex!"]),
            ("flex! block", vec!["flex!", "block"]),
            // With dashes
            ("items-center", vec!["items-center"]),
            ("items--center", vec!["items--center"]),
            // Inside a string
            ("'flex'", vec!["flex"]),
            // Multiple utilities
            ("flex items-center", vec!["flex", "items-center"]),
            // Arbitrary property
            ("[color:red]", vec!["[color:red]"]),
            ("![color:red]", vec!["![color:red]"]),
            ("[color:red]!", vec!["[color:red]!"]),
            ("[color:red]/20", vec!["[color:red]/20"]),
            ("![color:red]/20", vec!["![color:red]/20"]),
            ("[color:red]/20!", vec!["[color:red]/20!"]),
            // Modifiers
            ("bg-red-500/20", vec!["bg-red-500/20"]),
            ("bg-red-500/[20%]", vec!["bg-red-500/[20%]"]),
            (
                "bg-red-500/(--my-opacity)",
                vec!["bg-red-500/(--my-opacity)"],
            ),
            // Modifiers with important (legacy)
            ("!bg-red-500/20", vec!["!bg-red-500/20"]),
            ("!bg-red-500/[20%]", vec!["!bg-red-500/[20%]"]),
            (
                "!bg-red-500/(--my-opacity)",
                vec!["!bg-red-500/(--my-opacity)"],
            ),
            // Modifiers with important
            ("bg-red-500/20!", vec!["bg-red-500/20!"]),
            ("bg-red-500/[20%]!", vec!["bg-red-500/[20%]!"]),
            (
                "bg-red-500/(--my-opacity)!",
                vec!["bg-red-500/(--my-opacity)!"],
            ),
            // Arbitrary value with bracket notation
            ("bg-[#0088cc]", vec!["bg-[#0088cc]"]),
            // Arbitrary value with arbitrary property shorthand modifier
            (
                "bg-[#0088cc]/(--my-opacity)",
                vec!["bg-[#0088cc]/(--my-opacity)"],
            ),
            // Arbitrary value with CSS property shorthand
            ("bg-(--my-color)", vec!["bg-(--my-color)"]),
            // Multiple utilities including arbitrary property shorthand
            (
                "bg-(--my-color) flex px-(--my-padding)",
                vec!["bg-(--my-color)", "flex", "px-(--my-padding)"],
            ),
            // --------------------------------------------------------

            // Exceptions:
            ("bg-red-500/20/20", vec![]),
            ("bg-[#0088cc]/20/20", vec![]),
        ] {
            for (wrapper, additional) in [
                // No wrapper
                ("{}", vec![]),
                // With leading spaces
                (" {}", vec![]),
                // With trailing spaces
                ("{} ", vec![]),
                // Surrounded by spaces
                (" {} ", vec![]),
                // Inside a string
                ("'{}'", vec![]),
                // Inside a function call
                ("fn('{}')", vec![]),
                // Inside nested function calls
                ("fn1(fn2('{}'))", vec!["fn1", "fn2"]),
                // --------------------------
                //
                // HTML
                // Inside a class (on its own)
                (r#"<div class="{}"></div>"#, vec!["div", "class"]),
                // Inside a class (first)
                (r#"<div class="{} foo"></div>"#, vec!["div", "class", "foo"]),
                // Inside a class (second)
                (r#"<div class="foo {}"></div>"#, vec!["div", "class", "foo"]),
                // Inside a class (surrounded)
                (
                    r#"<div class="foo {} bar"></div>"#,
                    vec!["div", "class", "foo", "bar"],
                ),
                // --------------------------
                //
                // JavaScript
                // Inside a variable
                (r#"let classes = '{}';"#, vec!["let", "classes"]),
                // Inside an object (key)
                (
                    r#"let classes = { '{}': true };"#,
                    vec!["let", "classes", "true"],
                ),
                // Inside an object (no spaces, key)
                (r#"let classes = {'{}':true};"#, vec!["let", "classes"]),
                // Inside an object (value)
                (
                    r#"let classes = { primary: '{}' };"#,
                    vec!["let", "classes", "primary"],
                ),
                // Inside an object (no spaces, value)
                (
                    r#"let classes = {primary:'{}'};"#,
                    vec!["let", "classes", "primary"],
                ),
                // Inside an array
                (r#"let classes = ['{}'];"#, vec!["let", "classes"]),
            ] {
                let input = wrapper.replace("{}", input);

                let mut expected = expected.clone();
                expected.extend(additional);
                expected.sort();

                let mut actual = UtilityMachine::test_extract_all(&input);
                actual.sort();

                if actual != expected {
                    dbg!(&input);
                }
                assert_eq!(actual, expected);
            }
        }
    }

Domain

Subdomains

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free