From: GoalSmashers Date: Sat, 3 Nov 2012 19:40:48 +0000 (+0000) Subject: Minor cleanup. Added JSHint validation. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=8996b9234a533d9545315116e948722569cc3298;p=clean-css.git Minor cleanup. Added JSHint validation. * JSHint validation via 'make check' (it's a new dev dependency). * Fixed minor errors pointed out by JSHint. * Added license info link and library header. --- diff --git a/.jshintignore b/.jshintignore new file mode 100644 index 00000000..4e06b706 --- /dev/null +++ b/.jshintignore @@ -0,0 +1,3 @@ +node_modules +.git + diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 00000000..d233bc3d --- /dev/null +++ b/.jshintrc @@ -0,0 +1,16 @@ +{ + "node" : true, + "es5" : true, + "browser" : false, + + "camelcase": true, + "curly": false, + "eqeqeq": false, + "latedef": true, + "noarg": true, + "plusplus": false, + "regexp": false, + "strict": false, + "trailing": false, + "unused": true +} diff --git a/Makefile b/Makefile index 15c2a68f..27fec289 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ TEST_DIR = test -VOWS = ./node_modules/vows/bin/vows +VOWS = ./node_modules/.bin/vows +JSHINT = ./node_modules/.bin/jshint all: test @@ -11,4 +12,8 @@ bench: @@echo "Running benchmark on big.css file" @@node test/bench.js -.PHONY: all test bench \ No newline at end of file +check: + @@echo "Running JSHint on all project files..." + @@${JSHINT} . + +.PHONY: all test bench check \ No newline at end of file diff --git a/README.md b/README.md index 28524d98..ae8b197c 100644 --- a/README.md +++ b/README.md @@ -65,4 +65,4 @@ on *nix systems. If you are under Windows then run: ## License ## -Clean-css is released under the MIT license. +Clean-css is released under the [MIT License](http://opensource.org/licenses/MIT). diff --git a/lib/clean.js b/lib/clean.js index e10edd08..1c22c0e7 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -1,4 +1,9 @@ -var util = require('util'); +/** + * Clean-css - https://github.com/GoalSmashers/clean-css + * Released under the terms of MIT license + * + * Copyright (C) 2011-2012 GoalSmashers.com + */ var CleanCSS = { colors: { @@ -13,7 +18,7 @@ var CleanCSS = { specialComments: [], contentBlocks: [] }; - var replace = function(pattern, replacement) { + var replace = function() { if (typeof arguments[0] == 'function') arguments[0](); else @@ -89,7 +94,7 @@ var CleanCSS = { replace(/content :/g, 'content:'); // restore spaces inside IE filters (IE 7 issue) - replace(/progid:[^(]+\(([^\)]+)/g, function(match, contents) { + replace(/progid:[^(]+\(([^\)]+)/g, function(match) { return match.replace(/,/g, ', '); }); @@ -117,8 +122,10 @@ var CleanCSS = { // replace standard colors with hex values (only if color name is longer then hex value) replace(/(color|background):(\w+)/g, function(match, property, colorName) { - if (CleanCSS.colors[colorName]) return property + ':' + CleanCSS.colors[colorName]; - else return match; + if (CleanCSS.colors[colorName]) + return property + ':' + CleanCSS.colors[colorName]; + else + return match; }); // replace #f00 with red as it's shorter @@ -126,8 +133,10 @@ var CleanCSS = { // replace font weight with numerical value replace(/font\-weight:(\w+)/g, function(match, weight) { - if (weight == 'normal') return 'font-weight:400'; - else if (weight == 'bold') return 'font-weight:700'; + if (weight == 'normal') + return 'font-weight:400'; + else if (weight == 'bold') + return 'font-weight:700'; else return match; }); @@ -155,7 +164,7 @@ var CleanCSS = { // empty elements if (options.removeEmpty) - replace(/[^}]+?{}/g, ''); + replace(/[^\}]+?\{\}/g, ''); // move first charset to the beginning if (data.indexOf('charset') > 0) @@ -173,7 +182,7 @@ var CleanCSS = { replace(/calc\([^\}]+\}/g, function(match) { return match.replace(/\+/g, ' + '); }); - replace(/__CSSCOMMENT__/g, function(i) { + replace(/__CSSCOMMENT__/g, function() { switch (options.keepSpecialComments) { case '*': return context.specialComments.shift(); @@ -205,7 +214,8 @@ var CleanCSS = { for (; nextEnd < data.length; ) { nextStart = data.indexOf('/*', nextEnd); nextEnd = data.indexOf('*/', nextStart); - if (nextStart == -1 || nextEnd == -1) break; + if (nextStart == -1 || nextEnd == -1) + break; tempData.push(data.substring(cursor, nextStart)); if (data[nextStart + 2] == '!') { @@ -228,7 +238,6 @@ var CleanCSS = { var tempData = [], nextStart = 0, nextEnd = 0, - tempStart = 0, cursor = 0, matchedParenthesis = null; @@ -240,24 +249,30 @@ var CleanCSS = { if (matchedParenthesis) { min = data.indexOf(matchedParenthesis, pos); - if (min == -1) min = max; + if (min == -1) + min = max; } else { var next1 = data.indexOf("'", pos); var next2 = data.indexOf('"', pos); - if (next1 == -1) next1 = max; - if (next2 == -1) next2 = max; + if (next1 == -1) + next1 = max; + if (next2 == -1) + next2 = max; min = next1 > next2 ? next2 : next1; } - if (min == max) return -1; + if (min == max) + return -1; if (matchedParenthesis) { matchedParenthesis = null; return min; } else { // check if there's anything else between pos and min that doesn't match ':' or whitespace - if (/[^:\s]/.test(data.substring(pos, min))) return -1; + if (/[^:\s]/.test(data.substring(pos, min))) + return -1; + matchedParenthesis = data.charAt(min); return min + 1; } @@ -265,11 +280,13 @@ var CleanCSS = { for (; nextEnd < data.length; ) { nextStart = data.indexOf('content', nextEnd); - if (nextStart == -1) break; + if (nextStart == -1) + break; nextStart = nextParenthesis(nextStart + 7); nextEnd = nextParenthesis(nextStart); - if (nextStart == -1 || nextEnd == -1) break; + if (nextStart == -1 || nextEnd == -1) + break; tempData.push(data.substring(cursor, nextStart - 1)); tempData.push('__CSSCONTENT__'); diff --git a/package.json b/package.json index 54c446c4..00208494 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "optimist": "0.3.x" }, "devDependencies": { - "vows": "0.6.x" + "vows": "0.6.x", + "jshint": "0.9.x" }, "engines": { "node": ">=0.6.0" diff --git a/test/batch-test.js b/test/batch-test.js index acc70ea0..c7ecfa99 100644 --- a/test/batch-test.js +++ b/test/batch-test.js @@ -22,7 +22,7 @@ var batchContexts = function() { minimized: fs.readFileSync(minPath, 'utf-8') }; } - } + }; context[testName]['minimizing ' + testName + '.css'] = function(data) { var processed = cleanCSS.process(data.plain, { removeEmpty: true, diff --git a/test/binary-test.js b/test/binary-test.js index eb323620..6f9129c4 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -34,7 +34,7 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({ } }), 'help': binaryContext('-h', { - 'should output help': function(error, stdout, stderr) { + 'should output help': function(error, stdout) { assert.equal(/usage:/.test(stdout), true); } }), @@ -79,7 +79,7 @@ exports.commandsSuite = vows.describe('binary commands').addBatch({ 'should give no output': function(error, stdout) { assert.equal(stdout, ''); }, - 'should minimize': function(stdout) { + 'should minimize': function() { var minimized = fs.readFileSync('./test/data/reset-min.css', 'utf-8').replace(lineBreak, ''); var target = fs.readFileSync('./reset-min.css', 'utf-8').replace(lineBreak, ''); assert.equal(minimized, target); diff --git a/test/custom-test.js b/test/custom-test.js index ca863f7f..6c537ab8 100644 --- a/test/custom-test.js +++ b/test/custom-test.js @@ -13,5 +13,4 @@ vows.describe('clean-custom') } } }) - .export(module) - + .export(module); diff --git a/test/unit-test.js b/test/unit-test.js index 98e09abc..5370883a 100644 --- a/test/unit-test.js +++ b/test/unit-test.js @@ -7,7 +7,7 @@ var cssContext = function(groups, options) { var clean = function(cleanedCSS) { return function(css) { assert.equal(cleanCSS.process(css, options), cleanedCSS); - } + }; }; for (var g in groups) { @@ -340,7 +340,7 @@ vows.describe('clean-units').addBatch({ ], 'not add a space before url\'s hash': [ "url(\"../fonts/d90b3358-e1e2-4abb-ba96-356983a54c22.svg#d90b3358-e1e2-4abb-ba96-356983a54c22\")", - "url(../fonts/d90b3358-e1e2-4abb-ba96-356983a54c22.svg#d90b3358-e1e2-4abb-ba96-356983a54c22)", + "url(../fonts/d90b3358-e1e2-4abb-ba96-356983a54c22.svg#d90b3358-e1e2-4abb-ba96-356983a54c22)" ] }), 'ie filters': cssContext({ @@ -395,6 +395,6 @@ vows.describe('clean-units').addBatch({ 'empty #1': 'a{}', 'empty #2': 'div>a{}', 'empty #3': 'div:nth-child(2n){}', - 'empty #3': 'a{color:#fff}div{}p{line-height:2em}' + 'empty #4': 'a{color:#fff}div{}p{line-height:2em}' }) }).export(module); \ No newline at end of file