Skip to content

Commit

Permalink
fix: case insensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Mar 6, 2017
1 parent cc485e5 commit 2817bcb
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/css-nuker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class CssNuker {
this._blacklistedTokens = new Set()

if (options.whitelist) {
options.whitelist.forEach(token => this._usedTokens.add(token))
options.whitelist.forEach(token => this._usedTokens.add(token.toLowerCase()))
}

if (options.blacklist) {
options.blacklist.forEach(token => this._blacklistedTokens.add(token))
options.blacklist.forEach(token => this._blacklistedTokens.add(token.toLowerCase()))
}
}

Expand Down Expand Up @@ -51,6 +51,7 @@ class CssNuker {

_isEveryTokenUsed(tokens) {
return _.every(tokens, token => {
token = token.toLowerCase()
if (this._usedTokens.has(token)) {
return true
} else if (this._blacklistedTokens.has(token)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/sources/html-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HtmlSource extends ParsedSource {
onopentag(name, attributes) {
[name, attributes.id, attributes.class]
.filter(candidate => typeof candidate === 'string')
.forEach(candidate => tokens.add(candidate))
.forEach(candidate => tokens.add(candidate.toLowerCase()))
},
onerror(err) {
error = err
Expand Down
2 changes: 1 addition & 1 deletion lib/sources/js-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JsSource extends ParsedSource {
static parse(text) {
const jsStrings = esprima.tokenize(text)
.filter(token => token.type === 'String')
.map(token => token.value.substr(1, token.value.length - 2))
.map(token => token.value.substr(1, token.value.length - 2).toLowerCase())
return new Set(jsStrings)
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/nuke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ describe('nuke.js', () => {
})

it('should respect the whitelist', () => {
const whitelist = ['foo-bar-3']
const whitelist = ['foO-Bar-3']
const result = nukecss(htmlContent, cssContent, {whitelist})
expect(result).to.contain('.foo-bar-3')
})

it('should respect the blacklist', () => {
const blacklist = ['something']
const blacklist = ['blAckList-test']
const extraHtml = '<div id="blacklist-test">test</div>'
const extraCss = '\n.something, #blacklist-test { color: white; }'
const extraCss = '\n#blacklist-test { color: white; }'
const result = nukecss([htmlContent, extraHtml], cssContent + extraCss, {blacklist})
expect(result).to.contain('#blacklist-test')
expect(result).to.not.contain('#blacklist-test')
})

it('should support multiple sources', () => {
Expand Down
1 change: 1 addition & 0 deletions test/sources/html-source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('sources/html-source.js', () => {

it('should find tokens as classes', () => {
expect(source).to.contain('container')
expect(source).to.contain('ConTainer')
expect(source).to.contain('lead')
})

Expand Down
1 change: 1 addition & 0 deletions test/sources/js-source.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('sources/js-source.js', () => {
expect(source).to.contain('the-class')
expect(source).to.contain('the-other-class')
expect(source).to.contain('fa')
expect(source).to.contain('tHe-cLaSs')
})

it('should find tokens within strings', () => {
Expand Down

0 comments on commit 2817bcb

Please sign in to comment.