Skip to content

Commit

Permalink
Safeguard against trailing slash in purge matches (#2364)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan authored Sep 10, 2020
1 parent e88a4d3 commit a4b30a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions __tests__/fixtures/purge-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@
%samp.font-mono{:data-foo => "bar"}= output
.col-span-4[aria-hidden=true]
.tracking-tight#headline

<!-- JSON -->
{
"helloThere": "Hello there, <span class=\"whitespace-no-wrap\">Mr. Jones</span>"
}
2 changes: 1 addition & 1 deletion __tests__/purgeUnusedStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function assertPurged(result) {
expect(result.css).toContain('.font-mono')
expect(result.css).toContain('.col-span-4')
expect(result.css).toContain('.tracking-tight')
expect(result.css).toContain('.tracking-tight')
expect(result.css).toContain('.whitespace-no-wrap')
}

test('purges unused classes', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/lib/purgeUnusedStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,17 @@ export default function purgeUnusedUtilities(config, configChanged) {
defaultExtractor: content => {
// Capture as liberally as possible, including things like `h-(screen-1.5)`
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
const broadMatchesWithoutTrailingSlash = broadMatches.map(match => _.trimEnd(match, '\\'))

// Capture classes within other delimiters like .block(class="w-1/2") in Pug
const innerMatches = content.match(/[^<>"'`\s.(){}[\]#=%]*[^<>"'`\s.(){}[\]#=%:]/g) || []

const matches = broadMatches.concat(broadMatchesWithoutTrailingSlash).concat(innerMatches)

if (_.get(config, 'purge.preserveHtmlElements', true)) {
return [...htmlTags].concat(broadMatches).concat(innerMatches)
return [...htmlTags].concat(matches)
} else {
return broadMatches.concat(innerMatches)
return matches
}
},
...config.purge.options,
Expand Down

0 comments on commit a4b30a0

Please sign in to comment.