From 97ccf810b12d594d8b0c2dc8aebde302c799d9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=B6vesdi=20Gy=C3=B6rgy?= Date: Fri, 22 Nov 2019 15:10:55 +0100 Subject: [PATCH 1/2] fix for tags with the letter "n" in them are not purged #195 --- __tests__/purgecss.test.js | 3 +++ __tests__/test_examples/nth_child/nth_child.css | 3 +++ __tests__/test_examples/nth_child/nth_child.html | 2 +- src/index.js | 15 +++++++++++---- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/__tests__/purgecss.test.js b/__tests__/purgecss.test.js index 657b1ff2..1251ab48 100644 --- a/__tests__/purgecss.test.js +++ b/__tests__/purgecss.test.js @@ -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) }) }) diff --git a/__tests__/test_examples/nth_child/nth_child.css b/__tests__/test_examples/nth_child/nth_child.css index a4a53d0d..59f7daec 100644 --- a/__tests__/test_examples/nth_child/nth_child.css +++ b/__tests__/test_examples/nth_child/nth_child.css @@ -4,3 +4,6 @@ .some-item:nth-child(2n+1){ color: blue } +canvas { + display: none +} diff --git a/__tests__/test_examples/nth_child/nth_child.html b/__tests__/test_examples/nth_child/nth_child.html index 3ce6da09..d49b41fa 100644 --- a/__tests__/test_examples/nth_child/nth_child.html +++ b/__tests__/test_examples/nth_child/nth_child.html @@ -3,4 +3,4 @@
-
+ \ No newline at end of file diff --git a/src/index.js b/src/index.js index c46308df..a8c5df8b 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -317,7 +319,8 @@ class Purgecss { return } - let keepSelector = true + let keepSelector = true + node.selector = selectorParser(selectorsParsed => { selectorsParsed.walk(selector => { const selectorsInRule = [] @@ -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) } } From 7c10f47dc02c7d7409d7e811fb28023d76d012cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=B6vesdi=20Gy=C3=B6rgy?= Date: Fri, 22 Nov 2019 15:32:35 +0100 Subject: [PATCH 2/2] build + bump version --- lib/purgecss.es.js | 6 ++++-- lib/purgecss.js | 6 ++++-- package.json | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/purgecss.es.js b/lib/purgecss.es.js index d0c5c22e..31699e2b 100644 --- a/lib/purgecss.es.js +++ b/lib/purgecss.es.js @@ -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 () { @@ -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); } } diff --git a/lib/purgecss.js b/lib/purgecss.js index 9f5a8276..c8d2eb9a 100644 --- a/lib/purgecss.js +++ b/lib/purgecss.js @@ -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 () { @@ -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); } } diff --git a/package.json b/package.json index 0e9dbada..ab078f0d 100644 --- a/package.json +++ b/package.json @@ -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",