Skip to content

Commit

Permalink
Fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
t32k committed Mar 15, 2014
2 parents 27de7df + 18ae2ae commit 5e6ec7d
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 119 deletions.
1 change: 1 addition & 0 deletions lib/defaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
"idSelectors": true,
"universalSelectors": true,
"unqualifiedAttributeSelectors": true,
"javascriptSpecificSelectors": /[#\.]js\-/g,
"importantKeywords": true,
"floatProperties": true,
"mediaQueries": true,
Expand Down
185 changes: 90 additions & 95 deletions lib/stylestats.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ function StyleStats(args, config) {
this.stylesheets = this.files.length + this.urls.length;
this.cssString = '';
this.cssArray = [];
this.declarations = [];
this.mediaQueries = 0;

var defaultOptions = require('./defaultOptions');
var customOptions = {};
Expand All @@ -92,51 +94,31 @@ StyleStats.prototype.getGzippedSize = function() {
return gzipSize.sync(this.cssString);
};

StyleStats.prototype.getPureRules = function() {
return _.filter(this.rules, function(rule) {
return rule.type === 'rule' || rule.type === 'media';
});
};

StyleStats.prototype.getSelectorCount = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.selectors)) {
count += rule.selectors.length;
}
count += rule.selectors.length;
});
return count;
};

StyleStats.prototype.getUniqueFont = function() {
var array = [];
this.rules.forEach(function(rule) {
if (Array.isArray(rule.declarations)) {
rule.declarations.forEach(function(declaration) {
if (declaration.property !== undefined) {
if (declaration.property.indexOf('font-size') > -1) {
array.push(declaration.value.replace(/\!important/, '').trim());
}
}
});
this.declarations.forEach(function(declaration) {
if (declaration.property.indexOf('font-size') > -1) {
array.push(declaration.value.replace(/\!important/, '').trim());
}
});
return _.uniq(array);
};

StyleStats.prototype.getUniqueColor = function() {
var array = [];
this.rules.forEach(function(rule) {
if (Array.isArray(rule.declarations)) {
rule.declarations.forEach(function(declaration) {
if (declaration.property !== undefined) {
if (declaration.property.match(/^color$/)) {
var color = declaration.value.replace(/\!important/, '');
color = color.toUpperCase().trim();
array.push(color);
}
}
});
this.declarations.forEach(function(declaration) {
if (declaration.property.match(/^color$/)) {
var color = declaration.value.replace(/\!important/, '');
color = color.toUpperCase().trim();
array.push(color);
}
});
array = _.without(array, 'TRANSPARENT')
Expand All @@ -159,98 +141,82 @@ StyleStats.prototype.getDeclarationCount = function() {
StyleStats.prototype.getIdentifierCount = function() {
var array = [];
this.rules.forEach(function(rule) {
if (Array.isArray(rule.selectors)) {
rule.selectors.forEach(function(selector) {
var trimedSelector = selector.replace(/\s?([\>|\+|\~])\s?/g, '$1');
trimedSelector = trimedSelector.replace(/\s+/g, ' ');
var identifiers = trimedSelector.split(/\s|\>|\+|\~/).length;
array.push({
selector: selector,
count: identifiers
});
rule.selectors.forEach(function(selector) {
var trimedSelector = selector.replace(/\s?([\>|\+|\~])\s?/g, '$1');
trimedSelector = trimedSelector.replace(/\s+/g, ' ');
var identifiers = trimedSelector.split(/\s|\>|\+|\~/).length;
array.push({
selector: selector,
count: identifiers
});
}
});
});
return array;
};

StyleStats.prototype.getIdSelectors = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.selectors)) {
rule.selectors.forEach(function(selector) {
if (selector.indexOf('#') > -1) {
count += 1;
}
});
}
rule.selectors.forEach(function(selector) {
if (selector.indexOf('#') > -1) {
count += 1;
}
});
});
return count;
};

StyleStats.prototype.getUniversalSelector = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.selectors)) {
rule.selectors.forEach(function(selector) {
if (selector.indexOf('*') > -1) {
count += 1;
}
});
}
rule.selectors.forEach(function(selector) {
if (selector.indexOf('*') > -1) {
count += 1;
}
});
});
return count;
};

StyleStats.prototype.getUnqualifiedAttributeSelector = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.selectors)) {
rule.selectors.forEach(function(selector) {
if (selector.trim().match(/\[.+\]$/g)) {
count += 1;
}
});
}
rule.selectors.forEach(function(selector) {
if (selector.trim().match(/\[.+\]$/g)) {
count += 1;
}
});
});
return count;
};
StyleStats.prototype.getImportantKeywords = function() {

StyleStats.prototype.getJSSpecificSelector = function() {
var count = 0;
var re = this.options.javascriptSpecificSelectors;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.declarations)) {
rule.declarations.forEach(function(declaration) {
if (declaration.value !== undefined) {
if (declaration.value.indexOf('!important') > -1) {
count += 1;
}
}
});
}
rule.selectors.forEach(function(selector) {
if (selector.trim().match(re)) {
count += 1;
}
});
});
return count;
};

StyleStats.prototype.getFloatProperty = function() {
StyleStats.prototype.getImportantKeywords = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (Array.isArray(rule.declarations)) {
rule.declarations.forEach(function(declaration) {
if (declaration.property !== undefined) {
if (declaration.property.indexOf('float') > -1) {
count += 1;
}
}
});
this.declarations.forEach(function(declaration) {
if (declaration.value.indexOf('!important') > -1) {
count += 1;
}
});
return count;
};

StyleStats.prototype.getMediaQueries = function() {
StyleStats.prototype.getFloatProperty = function() {
var count = 0;
this.rules.forEach(function(rule) {
if (rule.type.indexOf('media') > -1) {
this.declarations.forEach(function(declaration) {
if (declaration.property.indexOf('float') > -1) {
count += 1;
}
});
Expand All @@ -259,15 +225,11 @@ StyleStats.prototype.getMediaQueries = function() {

StyleStats.prototype.getProperty = function() {
var propMap = {};
this.rules.forEach(function(rule) {
if (Array.isArray(rule.declarations)) {
rule.declarations.forEach(function(declaration) {
if (propMap[declaration.property]) {
propMap[declaration.property] += 1;
} else {
propMap[declaration.property] = 1;
}
});
this.declarations.forEach(function(declaration) {
if (propMap[declaration.property]) {
propMap[declaration.property] += 1;
} else {
propMap[declaration.property] = 1;
}
});
return propMap;
Expand Down Expand Up @@ -300,7 +262,37 @@ StyleStats.prototype.parse = function(callback) {
}, function() {
that.cssString = that.cssArray.join('');
that.cssSize = Buffer.byteLength(that.cssString, 'utf8');
that.rules = cssParse(that.cssString).stylesheet.rules;

var rawRules = cssParse(that.cssString).stylesheet.rules;
var processedRules = [];
var processedDeclarations = [];


rawRules.forEach(function(rule) {
if (rule.type === 'rule') {
processedRules.push(rule);
rule.declarations.forEach(function(declaration) {
if (declaration.type === 'declaration') {
processedDeclarations.push(declaration);
}
});
} else if (rule.type === 'media') {
that.mediaQueries += 1;
rule.rules.forEach(function(rule) {
if (rule.type === 'rule') {
processedRules.push(rule);
rule.declarations.forEach(function(declaration) {
if (declaration.type === 'declaration') {
processedDeclarations.push(declaration);
}
});
}
});
}
});

that.rules = processedRules;
that.declarations = processedDeclarations;

var stats = {};
var options = that.options;
Expand Down Expand Up @@ -335,7 +327,7 @@ StyleStats.prototype.parse = function(callback) {
stats.gzippedSize = that.getGzippedSize();
}
if (options.rules) {
stats.rules = that.getPureRules().length;
stats.rules = that.rules.length;
}
if (options.selectors) {
stats.selectors = that.getSelectorCount();
Expand Down Expand Up @@ -380,14 +372,17 @@ StyleStats.prototype.parse = function(callback) {
if (options.unqualifiedAttributeSelectors) {
stats.unqualifiedAttributeSelectors = that.getUnqualifiedAttributeSelector();
}
if (options.javascriptSpecificSelectors) {
stats.javascriptSpecificSelectors = that.getJSSpecificSelector();
}
if (options.importantKeywords) {
stats.importantKeywords = that.getImportantKeywords();
}
if (options.floatProperties) {
stats.floatProperties = that.getFloatProperty();
}
if (options.mediaQueries) {
stats.mediaQueries = that.getMediaQueries();
stats.mediaQueries = that.mediaQueries;
}
if (options.propertiesCount) {
var properties = that.getProperty();
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
* { float: left; }
body { color: #333; }
h1, h2, h3, h4, h5, h6 { margin: 0; }
a[src] { color: red !important; }
.foo { color: #ccc; font-size: 12px; }
.foo .bar .baz { color: #ccc; font-size: 12px; }
#bar { margin: 10px; font-size: 16px; }
12 changes: 10 additions & 2 deletions test/fixture/test.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ h1, h2, h3, h4, h5, h6 {
}
section {
margin: 10px;
padding: 10px;
/* padding: 10px; */
font-size: 10px;
}
.foo .bar > .baz + .qux ~ .quux:before {
Expand All @@ -23,14 +23,22 @@ section {
/* Lowest Cohesion Selecotor */
hr {
display: block;
float: left;
margin: 10px 0;
padding: 0 !important;
height: 1px;
border: 0;
border-top: 1px solid red;
}
.js-foo { display: block; }
@media (max-width: 600px) {
.media-queries {
.media-queries-01 {
display: none;
}
* {
display: none;
}
.media-queries-02 {
display: none;
}
}
Loading

0 comments on commit 5e6ec7d

Please sign in to comment.