Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Katie Fenn committed Aug 7, 2014
2 parents 0095a9a + 1dd14dd commit 52bd1a3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/CssRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var getSelectors = function (selectorBlock) {
return untrimmed.trim();
});

return trimmedSelectors;
return _.compact(trimmedSelectors);
};

var getDeclarationBlock = function (rule) {
Expand All @@ -52,4 +52,4 @@ var getDeclarations = function (declarationBlock) {
return trimmedDeclarations;
};

module.exports = CssRule;
module.exports = CssRule;
32 changes: 30 additions & 2 deletions lib/CssStylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ CssStylesheet.prototype.getRules = function () {
this.children = this.children || getChildren(this.raw);

return this.children.filter(function (child) {
return !child.match(/^@/g);
return isRule(child);
});
};

CssStylesheet.prototype.getMalformedStatements = function () {
this.children = this.children || getChildren(this.raw);

return this.children.filter(function (child) {
return isMalformedStatement(child);
});
};

CssStylesheet.prototype.getMediaQueries = function () {
this.children = this.children || getChildren(this.raw);
return this.children.filter(function (child) {
return child.match(/^@media/g);
return isMediaQuery(child);
});
};

Expand Down Expand Up @@ -62,4 +70,24 @@ var stripNewlines = function (string) {
return string.replace(/\n|\r|\r\n/g, '');
};

var isRule = function (string) {
return !isMediaQuery(string) && hasRuleBlock(string) && hasSelectorBlock(string);
}

var isMalformedStatement = function (string) {
return !isRule(string) && !isMediaQuery(string);
}

var hasRuleBlock = function (string) {
return string.indexOf('{') !== -1 && string.indexOf('}') !== -1;
}

var hasSelectorBlock = function (string) {
return string.match(/^[^\{]+/g)
}

var isMediaQuery = function (string) {
return string.match(/^@media/g);
}

module.exports = CssStylesheet;
1 change: 1 addition & 0 deletions metrics/All.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = [
require('./SpecificityPerSelector.js'),
require('./TopSelectorSpecificity.js'),
require('./TopSelectorSpecificitySelector.js'),
require('./TotalIdSelectors.js'),

// Colour
require('./TotalUniqueColours.js'),
Expand Down
17 changes: 17 additions & 0 deletions metrics/TotalIdSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

/*! Parker v0.0.0 - MIT license */

'use strict';

var _ = require('underscore');

module.exports = {
id: 'total-id-selectors',
name: 'Total Id Selectors',
type: 'selector',
aggregate: 'sum',
format: 'number',
measure: function (selector) {
return selector.indexOf('#') > 0 ? 1 : 0;
}
};
12 changes: 8 additions & 4 deletions parker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ var _ = require('underscore'),
path = require('path'),
info = require('./lib/Info');

var cliController = new CliController(),
parker = new Parker(metrics),
formatter = formatters['human'];
var cliController = new CliController();

cliController.on('runPaths', function (filePaths) {
var stylesheets = [];
Expand Down Expand Up @@ -112,4 +110,10 @@ var runReport = function (stylesheets, metrics) {
console.log(formatter(metrics, results));
};

cliController.dispatch(argv);
if (module.parent) {
module.exports = Parker;
} else {
var parker = new Parker(metrics),
formatter = formatters['human'];
cliController.dispatch(argv);
}
13 changes: 13 additions & 0 deletions test/CssStylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ describe('The Stylesheet Parser', function() {
expect(cssStylesheet.getRules()[0]).to.equal("body {border: 0;}");
expect(cssStylesheet.getRules()[1]).to.equal("a{color: #fff;}");
});

it('should return an array of malformed statements for a string containing malformed statements', function () {
var cssStylesheet = new CssStylesheet('body {border: 0;} h1; h2 {font-weight: bold;}');
expect(cssStylesheet.getRules()).to.be.an('array');
expect(cssStylesheet.getMalformedStatements()[0]).to.equal('h1;');
});

it('should identify a rule with an unexpected colon as a malformed statement', function () {
var cssStylesheet = new CssStylesheet('body {border: 0;} h1;{font-weight: bold;}');
expect(cssStylesheet.getRules()).to.be.an('array');
expect(cssStylesheet.getMalformedStatements()[0]).to.equal('h1;');
expect(cssStylesheet.getMalformedStatements()[1]).to.equal('{font-weight: bold;}');
});
});
10 changes: 10 additions & 0 deletions test/TopSelectorSpecificity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var expect = require('chai').expect,
metric = require('../metrics/TopSelectorSpecificity.js');

describe('The top-selector-specificity metric', function () {
it('should count only the specificity of the child selector of a :not identifier', function () {
expect(metric.measure(':not(body)')).to.equal(1);
expect(metric.measure(':not(.sidebar)')).to.equal(10);
expect(metric.measure(':not(h1#main)')).to.equal(101);
});
});

0 comments on commit 52bd1a3

Please sign in to comment.