Repository: tailwindlabs/tailwindcss (TypeScript)

Issue:
no css output for inline classes which specify an attribute value in an arbitrary variant
<!-- Please provide all of the information requested below. We're a small team and without all of this information it's not possible for us to help and your bug report will be closed. -->

**What version of Tailwind CSS are you using?**

3.1.3

**What build tool (or framework if it abstracts the build tool) are you using?**

https://play.tailwindcss.com

**What version of Node.js are you using?**

<https://play.tailwindcss.com>'s

**What browser are you using?**

Chrome 102.0.5005.115

**What operating system are you using?**

macOS 12.4

**Reproduction URL**

https://play.tailwindcss.com/zYyWNdDUB4

**Describe your issue**

Arbitrary variants don't support specifying attribute values. Using the form `[&[my-attribute]]:` generates the expected CSS. Adding a value —`[&[my-attribute='val']]:` or `[&[my-attribute="val"]]:`— results in no CSS being generated for that class.


Hints:

Patch:
diff --git a/src/lib/defaultExtractor.js b/src/lib/defaultExtractor.js
--- a/src/lib/defaultExtractor.js
+++ b/src/lib/defaultExtractor.js
@@ -64,31 +64,41 @@ function* buildRegExps(context) {
     ]),
   ])
 
-  yield regex.pattern([
-    // Variants
-    '((?=((',
-    regex.any(
-      [
-        regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`]+\]/, separator]),
-        regex.pattern([/[^\s"'`\[\\]+/, separator]),
-      ],
-      true
-    ),
-    ')+))\\2)?',
-
-    // Important (optional)
-    /!?/,
-
-    variantGroupingEnabled
-      ? regex.any([
-          // Or any of those things but grouped separated by commas
-          regex.pattern([/\(/, utility, regex.zeroOrMore([/,/, utility]), /\)/]),
-
-          // Arbitrary properties, constrained utilities, arbitrary values, etc…
-          utility,
-        ])
-      : utility,
-  ])
+  let variantPatterns = [
+    // Without quotes
+    regex.any([
+      regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`]+\]/, separator]),
+      regex.pattern([/[^\s"'`\[\\]+/, separator]),
+    ]),
+
+    // With quotes allowed
+    regex.any([
+      regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s`]+\]/, separator]),
+      regex.pattern([/[^\s`\[\\]+/, separator]),
+    ]),
+  ]
+
+  for (const variantPattern of variantPatterns) {
+    yield regex.pattern([
+      // Variants
+      '((?=((',
+      variantPattern,
+      ')+))\\2)?',
+
+      // Important (optional)
+      /!?/,
+
+      variantGroupingEnabled
+        ? regex.any([
+            // Or any of those things but grouped separated by commas
+            regex.pattern([/\(/, utility, regex.zeroOrMore([/,/, utility]), /\)/]),
+
+            // Arbitrary properties, constrained utilities, arbitrary values, etc…
+            utility,
+          ])
+        : utility,
+    ])
+  }
 
   // 5. Inner matches
   yield /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g


Test patch:
diff --git a/tests/arbitrary-variants.test.js b/tests/arbitrary-variants.test.js
--- a/tests/arbitrary-variants.test.js
+++ b/tests/arbitrary-variants.test.js
@@ -493,3 +493,37 @@ test('keeps escaped underscores in arbitrary variants mixed with normal variants
     `)
   })
 })
+
+test('allows attribute variants with quotes', () => {
+  let config = {
+    content: [
+      {
+        raw: `
+          <div class="[&[data-test='2']]:underline"></div>
+          <div class='[&[data-test="2"]]:underline'></div>
+        `,
+      },
+    ],
+    corePlugins: { preflight: false },
+  }
+
+  let input = `
+    @tailwind base;
+    @tailwind components;
+    @tailwind utilities;
+  `
+
+  return run(input, config).then((result) => {
+    expect(result.css).toMatchFormattedCss(css`
+      ${defaults}
+
+      .\[\&\[data-test\=\'2\'\]\]\:underline[data-test="2"] {
+        text-decoration-line: underline;
+      }
+
+      .\[\&\[data-test\=\"2\"\]\]\:underline[data-test='2'] {
+        text-decoration-line: underline;
+      }
+    `)
+  })
+})