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

Fix class detection in markdown code fences and slim templates #8569

Merged
merged 3 commits into from Jun 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix candidate extractor regression ([#8558](https://github.com/tailwindlabs/tailwindcss/pull/8558))
- Split `::backdrop`` into separate defaults group ([#8567](https://github.com/tailwindlabs/tailwindcss/pull/8567))
- Fix postcss plugin type ([#8564](https://github.com/tailwindlabs/tailwindcss/pull/8564))
- Fix class detection in markdown code fences and slim templates ([#8569](https://github.com/tailwindlabs/tailwindcss/pull/8569))

## [3.1.0] - 2022-06-08

Expand Down
14 changes: 7 additions & 7 deletions src/lib/defaultExtractor.js
Expand Up @@ -25,7 +25,7 @@ function* buildRegExps(context) {

let utility = regex.any([
// Arbitrary properties
/\[[^\s:'"]+:[^\s\]]+\]/,
/\[[^\s:'"`]+:[^\s\]]+\]/,

// Utilities
regex.pattern([
Expand All @@ -43,7 +43,7 @@ function* buildRegExps(context) {
/(?![{([]])/,

// optionally followed by an opacity modifier
/(?:\/[^\s'"\\><$]*)?/,
/(?:\/[^\s'"`\\><$]*)?/,
]),

regex.pattern([
Expand All @@ -54,11 +54,11 @@ function* buildRegExps(context) {
/(?![{([]])/,

// optionally followed by an opacity modifier
/(?:\/[^\s'"\\$]*)?/,
/(?:\/[^\s'"`\\$]*)?/,
]),

// Normal values w/o quotes — may include an opacity modifier
/[-\/][^\s'"\\$={><]*/,
/[-\/][^\s'"`\\$={><]*/,
])
),
]),
Expand All @@ -69,8 +69,8 @@ function* buildRegExps(context) {
'((?=((',
regex.any(
[
regex.pattern([/([^\s"'\[\\]+-)?\[[^\s"'\\]+\]/, separator]),
regex.pattern([/[^\s"'\[\\]+/, separator]),
regex.pattern([/([^\s"'`\[\\]+-)?\[[^\s"'`\\]+\]/, separator]),
regex.pattern([/[^\s"'`\[\\]+/, separator]),
],
true
),
Expand All @@ -91,7 +91,7 @@ function* buildRegExps(context) {
])

// 5. Inner matches
// yield /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g
yield /[^<>"'`\s.(){}[\]#=%$]*[^<>"'`\s.(){}[\]#=%:$]/g
}

// We want to capture any "special" characters
Expand Down
27 changes: 23 additions & 4 deletions tests/default-extractor.test.js
Expand Up @@ -415,28 +415,24 @@ test('with single quotes array within template literal', async () => {
const extractions = defaultExtractor(`<div class=\`\${['pr-1.5']}\`></div>`)

expect(extractions).toContain('pr-1.5')
expect(extractions).not.toContain('pr-1')
})

test('with double quotes array within template literal', async () => {
const extractions = defaultExtractor(`<div class=\`\${["pr-1.5"]}\`></div>`)

expect(extractions).toContain('pr-1.5')
expect(extractions).not.toContain('pr-1')
})

test('with single quotes array within function', async () => {
const extractions = defaultExtractor(`document.body.classList.add(['pl-1.5'].join(" "));`)

expect(extractions).toContain('pl-1.5')
expect(extractions).not.toContain('pl-1')
})

test('with double quotes array within function', async () => {
const extractions = defaultExtractor(`document.body.classList.add(["pl-1.5"].join(" "));`)

expect(extractions).toContain('pl-1.5')
expect(extractions).not.toContain('pl-1')
})

test('with angle brackets', async () => {
Expand All @@ -449,3 +445,26 @@ test('with angle brackets', async () => {
expect(extractions).not.toContain('>shadow-xl')
expect(extractions).not.toContain('shadow-xl<')
})

test('markdown code fences', async () => {
const extractions = defaultExtractor('<!-- this should work: `.font-bold`, `.font-normal` -->')

expect(extractions).toContain('font-bold')
expect(extractions).toContain('font-normal')
expect(extractions).not.toContain('.font-bold')
expect(extractions).not.toContain('.font-normal')
})

test('classes in slim templates', async () => {
const extractions = defaultExtractor(`
p.bg-red-500.text-sm
'This is a paragraph
small.italic.text-gray-500
'(Look mom, no closing tag!)
`)

expect(extractions).toContain('bg-red-500')
expect(extractions).toContain('text-sm')
expect(extractions).toContain('italic')
expect(extractions).toContain('text-gray-500')
})