Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #55

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data/warning-figures-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
'total-stylesheets': [6, 9],
'total-stylesheet-size': [234767, 377908.0],
'total-rules': [1965.4, 2789.8],
'total-selectors': [2780.4, 4095.6],
'total-identifiers': [8249.8, 11383.8],
'total-declarations': [4884, 7226.8],
'selectors-per-rule': [1.5, 1.6],
'identifiers-per-selector': [3.0, 3.6],
'specificity-per-selector': [53.7, 70.6],
'top-selector-specificity': [311.0, 362],
'total-id-selectors': [96.4, 188.4],
'total-unique-colours': [112.0, 158.2],
'total-important-keywords': [115.4, 179.0],
'total-media-queries': [16, 24]
};
16 changes: 16 additions & 0 deletions data/warning-figures-strict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
'total-stylesheets': [3, 4],
'total-stylesheet-size': [56764.4, 84259],
'total-rules': [529.4, 762],
'total-selectors': [724.2, 990],
'total-identifiers': [1654.2, 2415],
'total-declarations': [1395, 1981],
'selectors-per-rule': [1.2, 1.3],
'identifiers-per-selector': [2.2, 2.4],
'specificity-per-selector': [27.4, 32.5],
'top-selector-specificity': [90, 100],
'total-id-selectors': [6, 13],
'total-unique-colours': [41, 58],
'total-important-keywords': [15.0, 29],
'total-media-queries': [5, 6.0]
};
15 changes: 14 additions & 1 deletion docs/metrics/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Parker has a suite of metrics that are useful for measuring stylesheets. What fo
4. [Total Declarations](#total-declarations)
5. [Selectors Per Rule](#selectors-per-rule)
6. [Identifiers Per Selectors](#identifiers-per-selector)
7. [Declarations Per Rule](#declarations-per-rule)
3. [Specificity](#specificity)
1. [Specificity Per Selector](#specificity-per-selector)
2. [Top Selector Specificity](#top-selector-specificity)
Expand Down Expand Up @@ -78,7 +79,7 @@ Measures the total number of rules. Each rule defines a specific behaviour of th
- __aggregate__: sum
- __format__: number


<a name="total-selectors"></a>
#### Total Selectors
Measures the total number of selectors. Each selector defines a group of elements affected by the design. Stylesheets with fewer selectors are simpler.

Expand Down Expand Up @@ -108,6 +109,7 @@ Measures the total number of property declarations. Each property declaration de
- __aggregate__: sum
- __format__: number

<a name="selectors-per-rule"></a>
#### Selectors Per Rule
Measures the average number of selectors in every rule. Stylesheet rules can be applied to several groups of elements using multiple selectors, separated by a comma. Fewer selectors in a rule makes its properties specific to a smaller group of elements, and also makes a rule easier to read in text editors and developer tools.

Expand All @@ -117,6 +119,7 @@ Measures the average number of selectors in every rule. Stylesheet rules can be
- __aggregate__: sum
- __format__: number

<a name="identifiers-per-selector"></a>
#### Identifiers Per Selector
Measures the average number of identifiers in every selector. Selectors can be made more specific to combinations of elements by adding more identifiers to a selector. Fewer identifiers in a given selector reduces its dependency on certain DOM structures, allowing more changes to your HTML before being forced to change your CSS. Selectors with fewer identifiers are also more readable.

Expand All @@ -126,6 +129,16 @@ Measures the average number of identifiers in every selector. Selectors can be m
- __aggregate__: mean
- __format__: number

<a name="declarations-per-rule"></a>
#### Declarations Per Rule
Measures the average number of declarations in every rule. Rules with fewer property declarations are simpler, and also makes rules easier to read in text editors and developer tools.

- __id__: declarations-per-rule
- __name__: Declarations Per Rule
- __type__: rule
- __aggregate__: mean
- __format__: number

<a name="specificity"></a>
### Specificity
A rule can be overrided by another rule with a more specific selector. Complexity is added to stylesheets when multiple levels of cascading rules are used in stylesheets, because it becomes more difficult to predict which properties apply to a given element without keeping in mind other rules.
Expand Down
14 changes: 12 additions & 2 deletions lib/CliController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

Expand All @@ -25,6 +25,16 @@ CliController.prototype.dispatch = function (argv) {
if (argv.n || argv.numeric) {
this.emit('showNumericOnly');
}
if (argv['warning-figures']) {
var warningFigures = argv['warning-figures'];
this.emit('setWarningFigures', warningFigures);
}
if (argv.strict) {
this.emit('setWarningFigures', 'strict');
}
if (argv['surpress-colours']) {
this.emit('setSurpressColours');
}
if (argv._ && argv._.length) {
this.emit('runPaths', argv._);
}
Expand All @@ -37,4 +47,4 @@ CliController.prototype.dispatch = function (argv) {
}
};

module.exports = CliController;
module.exports = CliController;
8 changes: 4 additions & 4 deletions lib/CliFormatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

Expand All @@ -9,13 +9,13 @@ function CliFormatter() {
}

CliFormatter.prototype.format = function (data, metrics) {
var output = '';
var output = '';
_.each(metrics, function(metric) {
output += metric.name + ": " + data[metric.id];
output += "\n";
});

return output;
return output;
};

module.exports = CliFormatter;
module.exports = CliFormatter;
32 changes: 32 additions & 0 deletions lib/ConfigController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*! Parker v0.1.0 - MIT license */

'use strict';

var util = require('util'),
events = require('events');

function ConfigController() {
events.EventEmitter.call(this);
}

util.inherits(ConfigController, events.EventEmitter);

ConfigController.prototype.dispatch = function (config) {
if (config.files) {
this.emit('runPaths', config.files);
}
if (config.format) {
this.emit('setFormat', config.format);
}
if (config['show-numeric-only']) {
this.emit('showNumericOnly');
}
if (config['warning-figures']) {
this.emit('setWarningFigures', config['warning-figures']);
}
if (config['surpress-colours']) {
this.emit('setSurpressColours');
}
};

module.exports = ConfigController;
4 changes: 2 additions & 2 deletions lib/CssDeclaration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

Expand All @@ -23,4 +23,4 @@ CssDeclaration.prototype.getValue = function () {
return this.raw.split(':')[1].trim();
};

module.exports = CssDeclaration;
module.exports = CssDeclaration;
24 changes: 12 additions & 12 deletions lib/CssMediaQuery.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

function CssMediaQuery(raw) {
this.raw = raw;
this.raw = raw;
}

CssMediaQuery.prototype.getQueries = function () {
var pattern = /@media\w*(.+?)\s?{/,
queries = pattern.exec(this.raw)[1];
var pattern = /@media\w*(.+?)\s?{/,
queries = pattern.exec(this.raw)[1];

return queries.split(/ or |,/g).map(trimQuery);
return queries.split(/ or |,/g).map(trimQuery);
};

CssMediaQuery.prototype.getRules = function () {
Expand All @@ -19,17 +19,17 @@ CssMediaQuery.prototype.getRules = function () {
rule = '';

for (var index = 0; index < this.raw.length; index++) {
if (depth > 0) {
rule += this.raw.charAt(index);
}
if (depth > 0) {
rule += this.raw.charAt(index);
}
if (this.raw.charAt(index) === '{') {
depth ++;
}
else if (this.raw.charAt(index) == '}') {
else if (this.raw.charAt(index) === '}') {
depth --;
}

if (depth === 1 && this.raw.charAt(index) == '}') {
if (depth === 1 && this.raw.charAt(index) === '}') {
rules.push(rule.trim());
rule = '';
}
Expand All @@ -38,7 +38,7 @@ CssMediaQuery.prototype.getRules = function () {
};

var trimQuery = function (query) {
return query.trim();
return query.trim();
};

module.exports = CssMediaQuery;
module.exports = CssMediaQuery;
2 changes: 1 addition & 1 deletion lib/CssRule.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

Expand Down
47 changes: 38 additions & 9 deletions lib/CssSelector.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

var _ = require('underscore');
var _ = require('underscore'),
DELIMITERS = ['.', '#', '>', '[', ' ', ':', '*'];

function CssSelector(raw) {
this.raw = raw;
this.identifiers = [];
}

CssSelector.prototype.getIdentifiers = function () {
var identifiers = [],
segments = this.raw.split(/\s+[\s\+>]\s?|~^=/g);
var identifier = '',
bracketDepth = 0,
parenDepth = 0;

_.each(segments, function (segment) {
identifiers = identifiers.concat(segment.match(/[#\.:]?[\w\-\*]+|\[[\w=\-~'"\|]+\]|:{2}[\w-]+/g));
});
_.each(this.raw, function (character, index) {
var insideBrackets = bracketDepth || parenDepth,
isSecondColon = character == ':' && this.raw[index - 1] == ':';

return identifiers;
if (!insideBrackets && isDelimiter(character) && !isSecondColon) {
this.addIdentifier(identifier);
identifier = '';
}

switch(character) {
case '(': parenDepth++; break;
case ')': parenDepth--; break;
case '[': bracketDepth++; break;
case ']': bracketDepth--; break;
}

if (!_.contains([' ', '>'], character)) {
identifier += character;
}
}, this);

this.addIdentifier(identifier);
return _.without(this.identifiers, ' ', '');
}

CssSelector.prototype.addIdentifier = function (identifier) {
this.identifiers.push(identifier);
};

module.exports = CssSelector;
function isDelimiter(character) {
return _.contains(DELIMITERS, character)
}

module.exports = CssSelector;
34 changes: 17 additions & 17 deletions lib/CssStylesheet.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Parker v0.0.0 - MIT license */
/*! Parker v0.1.0 - MIT license */

'use strict';

Expand Down Expand Up @@ -42,7 +42,7 @@ var getChildren = function (raw) {
if (stylesheet.charAt(index) === '{') {
depth ++;
}
else if (stylesheet.charAt(index) == '}') {
else if (stylesheet.charAt(index) === '}') {
depth --;
}

Expand All @@ -54,40 +54,40 @@ var getChildren = function (raw) {
return children;
};

var stripComments = function (string) {
function stripComments (string) {
return string.replace(/\/\*.+?\*\//g, '');
};
}

var stripFormatting = function (string) {
function stripFormatting (string) {
return stripNewlines(trimWhitespace(string));
};
}

var trimWhitespace = function (string) {
function trimWhitespace (string) {
return string.replace(/[ ]+/g, ' ');
};
}

var stripNewlines = function (string) {
function stripNewlines(string) {
return string.replace(/\n|\r|\r\n/g, '');
};
}

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

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

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

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

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

module.exports = CssStylesheet;
module.exports = CssStylesheet;
Loading