Skip to content

Commit a4b30a0

Browse files
authored
Safeguard against trailing slash in purge matches (#2364)
1 parent e88a4d3 commit a4b30a0

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

__tests__/fixtures/purge-example.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@
3737
%samp.font-mono{:data-foo => "bar"}= output
3838
.col-span-4[aria-hidden=true]
3939
.tracking-tight#headline
40+
41+
<!-- JSON -->
42+
{
43+
"helloThere": "Hello there, <span class=\"whitespace-no-wrap\">Mr. Jones</span>"
44+
}

__tests__/purgeUnusedStyles.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function assertPurged(result) {
9090
expect(result.css).toContain('.font-mono')
9191
expect(result.css).toContain('.col-span-4')
9292
expect(result.css).toContain('.tracking-tight')
93-
expect(result.css).toContain('.tracking-tight')
93+
expect(result.css).toContain('.whitespace-no-wrap')
9494
}
9595

9696
test('purges unused classes', () => {

src/lib/purgeUnusedStyles.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ export default function purgeUnusedUtilities(config, configChanged) {
112112
defaultExtractor: content => {
113113
// Capture as liberally as possible, including things like `h-(screen-1.5)`
114114
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
115+
const broadMatchesWithoutTrailingSlash = broadMatches.map(match => _.trimEnd(match, '\\'))
115116

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

120+
const matches = broadMatches.concat(broadMatchesWithoutTrailingSlash).concat(innerMatches)
121+
119122
if (_.get(config, 'purge.preserveHtmlElements', true)) {
120-
return [...htmlTags].concat(broadMatches).concat(innerMatches)
123+
return [...htmlTags].concat(matches)
121124
} else {
122-
return broadMatches.concat(innerMatches)
125+
return matches
123126
}
124127
},
125128
...config.purge.options,

0 commit comments

Comments
 (0)