From 9d1113fd90e73449a8527efa16eb56200a8e730f Mon Sep 17 00:00:00 2001 From: kangax Date: Wed, 3 Apr 2013 14:13:17 +0200 Subject: [PATCH] Lint minifier, linter via JSHint --- .jshintrc | 34 +++ package.json | 2 +- src/htmlminifier.js | 18 +- tests/lint_test.js | 52 ---- tests/minify_test.js | 580 ------------------------------------------- 5 files changed, 44 insertions(+), 642 deletions(-) create mode 100644 .jshintrc delete mode 100644 tests/lint_test.js delete mode 100644 tests/minify_test.js diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..eb853c2 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,34 @@ +{ + "node": true, + "es5": false, + "browser": true, + + "boss": false, + "curly": false, + "debug": false, + "devel": false, + "eqeqeq": true, + "eqnull": true, + "evil": true, + "expr": true, + "forin": false, + "immed": true, + "laxbreak": true, + "loopfunc": true, + "multistr": true, + "newcap": true, + "noarg": true, + "noempty": false, + "nonew": false, + "nomen": false, + "onevar": false, + "plusplus": false, + "regexp": false, + "undef": true, + "sub": true, + "strict": false, + "white": false, + "unused": true, + "lastsemic": true, + "newcap": false +} diff --git a/package.json b/package.json index 088f2bf..1354e32 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "src": "./src" }, "scripts": { - "test": "node test.js" + "test": "jshint src/htmlminifier.js src/htmllint.js && node test.js" }, "devDependencies": { "qunit": "0.5.x", diff --git a/src/htmlminifier.js b/src/htmlminifier.js index a751768..f0bafa3 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -27,9 +27,9 @@ HTMLParser = require('./htmlparser').HTMLParser; } - function trimWhitespace(str) { + var trimWhitespace = function(str) { return str.replace(/^\s+/, '').replace(/\s+$/, ''); - } + }; if (String.prototype.trim) { trimWhitespace = function(str) { return str.trim(); @@ -135,7 +135,7 @@ (tag === 'textarea' && (/^(?:rows|cols|tabindex)$/).test(attrName)) || (tag === 'colgroup' && attrName === 'span') || (tag === 'col' && attrName === 'span') || - ((tag === 'th' || tag == 'td') && (attrName === 'rowspan' || attrName === 'colspan')) + ((tag === 'th' || tag === 'td') && (attrName === 'rowspan' || attrName === 'colspan')) ); } @@ -261,7 +261,7 @@ if (!options[defaultTesters[i]]) { options[defaultTesters[i]] = function() { return false; - } + }; } } } @@ -280,7 +280,7 @@ stackNoTrimWhitespace = [], stackNoCollapseWhitespace = [], lint = options.lint, - t = new Date() + t = new Date(); function _canCollapseWhitespace(tag, attrs) { return canCollapseWhitespace(tag) || options.canTrimWhitespace(tag, attrs); @@ -291,7 +291,7 @@ } HTMLParser(value, { - start: function( tag, attrs, unary ) { + start: function( tag, attrs ) { tag = tag.toLowerCase(); currentTag = tag; currentChars = ''; @@ -322,11 +322,11 @@ // check if current tag is in a whitespace stack if (options.collapseWhitespace) { if (stackNoTrimWhitespace.length && - tag == stackNoTrimWhitespace[stackNoTrimWhitespace.length - 1]) { + tag === stackNoTrimWhitespace[stackNoTrimWhitespace.length - 1]) { stackNoTrimWhitespace.pop(); } if (stackNoCollapseWhitespace.length && - tag == stackNoCollapseWhitespace[stackNoCollapseWhitespace.length - 1]) { + tag === stackNoCollapseWhitespace[stackNoCollapseWhitespace.length - 1]) { stackNoCollapseWhitespace.pop(); } } @@ -390,7 +390,7 @@ } }); - results.push.apply(results, buffer) + results.push.apply(results, buffer); var str = results.join(''); log('minified in: ' + (new Date() - t) + 'ms'); return str; diff --git a/tests/lint_test.js b/tests/lint_test.js deleted file mode 100644 index 5299679..0000000 --- a/tests/lint_test.js +++ /dev/null @@ -1,52 +0,0 @@ -(function(global){ - - var minify, QUnit, - test, equal, ok, - input, output, HTMLLint, lint; - - if (typeof require === 'function') { - QUnit = require('./qunit'); - minify = require('../src/htmlminifier').minify; - HTMLLint = require('../src/htmllint').HTMLLint; - } else { - QUnit = global.QUnit; - minify = global.minify; - HTMLLint = global.HTMLLint; - } - - test = QUnit.test; - equal = QUnit.equal; - ok = QUnit.ok; - - QUnit.module('', { - setup: function() { - lint = new HTMLLint(); - } - }); - - test('lint exists', function() { - ok(typeof lint !== 'undefined'); - }); - - test('lint is instance of HTMLLint', function() { - ok(lint instanceof HTMLLint); - }); - - test('lint API', function() { - equal(0, lint.log.length, '`log` property exists'); - equal("function", typeof lint.populate, '`populate` method exists'); - equal("function", typeof lint.test, '`test` method exists'); - equal("function", typeof lint.testElement, '`testElement` method exists'); - equal("function", typeof lint.testAttribute, '`testAttribute` method exists'); - }); - - test('deprecated element (font)', function(){ - minify('foo', { lint: lint }); - var log = lint.log.join(''); - - ok(log.indexOf('font') > -1); - ok(log.indexOf('deprecated') > -1); - ok(log.indexOf('element') > -1); - }); - -}(this)); \ No newline at end of file diff --git a/tests/minify_test.js b/tests/minify_test.js deleted file mode 100644 index e610426..0000000 --- a/tests/minify_test.js +++ /dev/null @@ -1,580 +0,0 @@ -(function(global){ - - var minify, QUnit, - test, equal, ok, - input, output; - - if (typeof require === 'function') { - QUnit = require('./qunit'); - minify = require('../src/htmlminifier').minify; - } else { - QUnit = global.QUnit; - minify = global.minify; - } - - test = QUnit.test; - equal = QUnit.equal; - ok = QUnit.ok; - - test('parsing non-trivial markup', function() { - equal(minify('

x

'), '

x

'); - equal(minify('

x

'), '

x

'); - equal(minify('

x

'), '

x

'); - equal(minify('

xxx

'), '

xxx

'); - equal(minify('

xxx

'), '

xxx

'); - - input = '
'+ - 'i\'m 10 levels deep'+ - '
'; - - equal(minify(input), input); - - equal(minify('