Skip to content

Commit

Permalink
Strip URLs out of values before testing them.
Browse files Browse the repository at this point in the history
Prevents false positives on URL strings containing values we're testing
for.

Closes #17
  • Loading branch information
Anand Thakker committed Jun 21, 2015
1 parent 6014672 commit 6dc4b8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/detect-feature-use.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var features = require('../data/features')
* searchfor: string or pattern to search for.
*/
function isFoundIn (str) {
str = stripUrls(str)
return function find (searchfor) {
if (searchfor instanceof RegExp) return searchfor.test(str)
else if (_.isFunction(searchfor)) return searchfor(str)
Expand All @@ -14,7 +15,15 @@ function isFoundIn (str) {
}

/*
* postcss the use of any of a given list of CSS features.
* Strip the contents of url literals so they aren't matched
* by our naive substring matching.
*/
function stripUrls (str) {
return str.replace(/url\([^\)]*\)/g, 'url()')
}

/**
* Detect the use of any of a given list of CSS features.
* ```
* var detector = new Detector(featureList)
* detector.process(css, cb)
Expand All @@ -30,7 +39,6 @@ function isFoundIn (str) {
* }
* ```
*/

module.exports = class Detector {
constructor (featureList) {
this.features = _.pick(features, featureList)
Expand Down
9 changes: 6 additions & 3 deletions test/detect-feature-use.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ var runTest = function (tc, cssString, expected, only) {
detector.process(postcss.parse(cssString), cb)

var res = Object.keys(cb.results).sort()
t.deepEqual(res, features)

for (var feature in expected) {
t.equal(cb.results[feature].length, expected[feature], 'count of ' + feature)
if (expected[feature] === 0) {
t.notOk(cb.results[feature])
} else {
t.deepEqual(res, features)
t.equal(cb.results[feature].length, expected[feature], 'count of ' + feature)
}
}

t.end()
Expand Down

0 comments on commit 6dc4b8e

Please sign in to comment.