Skip to content

Commit

Permalink
Merge pull request #58 from cmizzi/master
Browse files Browse the repository at this point in the history
Allows preserving children from whitelistPatterns
  • Loading branch information
Ffloriel authored Mar 20, 2018
2 parents be5ac0f + 772eccf commit f25921e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
40 changes: 40 additions & 0 deletions __tests__/purgecss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,3 +701,43 @@ describe('purge methods with files and legacy extractor', () => {
})
})
})

describe('whitelistPatternsChildren', () => {
let purgecssResult
beforeAll(() => {
purgecssResult = new Purgecss({
content: [`${root}whitelist_patterns_children/whitelist_patterns_children.html`],
css: [`${root}whitelist_patterns_children/whitelist_patterns_children.css`],
legacy: false,
whitelistPatternsChildren: [/^card$/]
}).purge()[0].css
})

it('finds card class', () => {
expect(purgecssResult.includes('.card')).toBe(true)
})

it('finds card--title', () => {
expect(purgecssResult.includes('.title')).toBe(false)
})

it('finds card--content', () => {
expect(purgecssResult.includes('.card .content')).toBe(true)
})

it('finds btn', () => {
expect(purgecssResult.includes('.btn')).toBe(true)
})

it('finds btn yellow', () => {
expect(purgecssResult.includes('.card .btn .yellow')).toBe(true)
})

it('finds btn red', () => {
expect(purgecssResult.includes('.btn .red')).toBe(false)
})

it('excludes btn--green', () => {
expect(purgecssResult.includes('.btn__green')).toBe(false)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.card {}
.title {}
.card .content {}

.btn {}
.btn .red {}
.btngreen {}

.card .btn .yellow {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<div class="card">
<div class="card--title"></div>
<div class="card--content">
<div class="btn"></div>
</div>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions flow-typed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare type Options = {
extractors?: Array<ExtractorsObj>,
whitelist?: Array<string>,
whitelistPatterns?: Array<RegExp>,
whitelistPatternsChildren?: Array<RegExp>,
output?: string,
stdout?: boolean,
stdin?: boolean,
Expand Down
1 change: 1 addition & 0 deletions lib/purgecss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare namespace Purgecss {
extractors?: Array<ExtractorsObj>
whitelist?: Array<string>
whitelistPatterns?: Array<RegExp>
whitelistPatternsChildren?: Array<RegExp>
output?: string
stdout?: boolean
stdin?: boolean
Expand Down
2 changes: 1 addition & 1 deletion lib/purgecss.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/purgecss.js

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ class Purgecss {
const sels = selector.split(/[^a-z]/g)
let keepSelector = false
for (let sel of sels) {
if (this.isSelectorWhitelistedChildren(sel)) {
keepSelector = true
break
}

if (!sel) continue
if (!selectorsInContent.has(sel)) break
keepSelector = true
Expand All @@ -407,7 +412,17 @@ class Purgecss {
// non legacy extractors
// pseudo class
const unescapedSelector = selector.replace(/\\/g, '')
if (unescapedSelector.startsWith(':')) continue

if (unescapedSelector.startsWith(':')) {
continue
}

// If the selector is whitelisted with children keep, simply
// returns true to keep all children selectors
if (this.isSelectorWhitelistedChildren(unescapedSelector)) {
return true
}

if (
!(
selectorsInContent.has(unescapedSelector) ||
Expand Down Expand Up @@ -435,6 +450,19 @@ class Purgecss {
this.options.whitelistPatterns.some((v: RegExp) => v.test(selector)))
)
}

/**
* Check if the selector is whitelisted by the whitelistPatternsChildren
* options element
*
* @param {string} selector
*/
isSelectorWhitelistedChildren(selector: string): boolean {
return !!(
this.options.whitelistPatternsChildren &&
this.options.whitelistPatternsChildren.some((v: RegExp) => v.test(selector))
)
}
}

export default Purgecss

0 comments on commit f25921e

Please sign in to comment.