Skip to content

Commit

Permalink
Merge pull request #241 from samzlab/hotfix/selector-regexp-fix
Browse files Browse the repository at this point in the history
Hotfix/selector regexp fix
  • Loading branch information
Ffloriel committed Dec 8, 2019
2 parents a7da401 + 7c10f47 commit bc78298
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 10 deletions.
3 changes: 3 additions & 0 deletions __tests__/purgecss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ describe('nth-child', () => {
})
it('finds some-item:nth-child(2n+1)', () => {
expect(purgecssResult.includes('some-item:nth-child(2n+1)')).toBe(true)
})
it('removes canvas (contains "n")', () => {
expect(purgecssResult.includes('canvas')).toBe(false)
})
})

Expand Down
3 changes: 3 additions & 0 deletions __tests__/test_examples/nth_child/nth_child.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
.some-item:nth-child(2n+1){
color: blue
}
canvas {
display: none
}
2 changes: 1 addition & 1 deletion __tests__/test_examples/nth_child/nth_child.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
<div class="some-item">
</div>
<div class="some-item">
</div>
</div>
6 changes: 4 additions & 2 deletions lib/purgecss.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ var CSS_WHITELIST = ['*', '::-webkit-scrollbar', '::selection', ':root', '::befo

var SELECTOR_STANDARD_TYPES = ['class', 'id', 'universal', 'pseudo'];

var IS_NTH = /^:nth-/;

var Purgecss =
/*#__PURE__*/
function () {
Expand Down Expand Up @@ -825,8 +827,8 @@ function () {

if (SELECTOR_STANDARD_TYPES.includes(type) && typeof value !== 'undefined') {
selectorsInRule.push(value);
} else if (type === 'tag' && !/[+]|n|-|(even)|(odd)|^from$|^to$|^\d/.test(value)) {
// test if we do not have a pseudo class parameter (e.g. 2n in :nth-child(2n))
} else if (type === 'tag' && !( // skip everything inside :nth-* pseudo selectors
selector.parent && selector.parent.type === 'pseudo' && IS_NTH.test(selector.parent.value))) {
selectorsInRule.push(value);
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/purgecss.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ var CSS_WHITELIST = ['*', '::-webkit-scrollbar', '::selection', ':root', '::befo

var SELECTOR_STANDARD_TYPES = ['class', 'id', 'universal', 'pseudo'];

var IS_NTH = /^:nth-/;

var Purgecss =
/*#__PURE__*/
function () {
Expand Down Expand Up @@ -829,8 +831,8 @@ function () {

if (SELECTOR_STANDARD_TYPES.includes(type) && typeof value !== 'undefined') {
selectorsInRule.push(value);
} else if (type === 'tag' && !/[+]|n|-|(even)|(odd)|^from$|^to$|^\d/.test(value)) {
// test if we do not have a pseudo class parameter (e.g. 2n in :nth-child(2n))
} else if (type === 'tag' && !( // skip everything inside :nth-* pseudo selectors
selector.parent && selector.parent.type === 'pseudo' && IS_NTH.test(selector.parent.value))) {
selectorsInRule.push(value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "purgecss",
"version": "1.4.1",
"version": "1.4.2",
"description": "Remove unused css selectors.",
"main": "./lib/purgecss.js",
"module": "./lib/purgecss.es.js",
Expand Down
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
import CSS_WHITELIST from './constants/cssWhitelist'
import SELECTOR_STANDARD_TYPES from './constants/selectorTypes'

const IS_NTH = /^:nth-/;

class Purgecss {
options: Options
root: Object
Expand Down Expand Up @@ -317,7 +319,8 @@ class Purgecss {
return
}

let keepSelector = true
let keepSelector = true

node.selector = selectorParser(selectorsParsed => {
selectorsParsed.walk(selector => {
const selectorsInRule = []
Expand All @@ -331,16 +334,20 @@ class Purgecss {
return
}
for (const { type, value } of selector.nodes) {

if (
SELECTOR_STANDARD_TYPES.includes(type) &&
typeof value !== 'undefined'
) {
selectorsInRule.push(value)
} else if (
type === 'tag' &&
!/[+]|n|-|(even)|(odd)|^from$|^to$|^\d/.test(value)
type === 'tag' &&
!( // skip everything inside :nth-* pseudo selectors
selector.parent &&
selector.parent.type === 'pseudo' &&
IS_NTH.test(selector.parent.value)
)
) {
// test if we do not have a pseudo class parameter (e.g. 2n in :nth-child(2n))
selectorsInRule.push(value)
}
}
Expand Down

0 comments on commit bc78298

Please sign in to comment.