Permalink
Browse files

Fixes issue #46 by removing empty selectors

  • Loading branch information...
1 parent acaf1bc commit 50482ffdf27515d5ebe940789f738f269053e3ff @kristoferjoseph kristoferjoseph committed Mar 13, 2013
Showing with 42 additions and 45 deletions.
  1. +41 −35 lib/plugins/extend.js
  2. +0 −5 test/fixtures/extend.nested.placeholder.out.css
  3. +1 −5 test/fixtures/extend.out.css
View
@@ -1,4 +1,3 @@
-
/**
* Module dependencies.
*/
@@ -10,18 +9,25 @@ var debug = require('debug')('rework:extend');
*/
module.exports = function() {
- debug('use extend');
- return function(style, rework){
- var map = {};
- style.rules.forEach(function(rule){
- if (!rule.declarations) return;
- rule.selectors.forEach(function(sel, i){
- map[sel] = rule;
- if ('%' == sel[0]) rule.selectors.splice(i, 1);
- });
- visit(rule, map);
- });
- }
+ debug('use extend');
+ return function(style, rework) {
+ var map = {},
+ rules = style.rules.length,
+ j = 0;
+ for (j; j < rules; j++) {
+ var rule = style.rules[j];
+ if (!rule || !rule.selectors) return;
+ rule.selectors.forEach(function(sel, i) {
+ map[sel] = rule;
+ if ('%' == sel[0]) rule.selectors.splice(i, 1);
+ });
+ visit(rule, map);
+ if (!rule.declarations.length) {
+ style.rules.splice(j, 1);
+ j--;
+ }
+ };
+ }
};
/**
@@ -33,26 +39,26 @@ module.exports = function() {
*/
function visit(rule, map) {
- for (var i = 0; i < rule.declarations.length; ++i) {
- var decl = rule.declarations[i];
- var key = decl.property;
- var val = decl.value;
- if (!/^extends?$/.test(key)) continue;
-
- var extend = map[val];
- if (!extend) throw new Error('failed to extend "' + val + '"');
-
- var keys = Object.keys(map);
- keys.forEach(function(key){
- if (0 != key.indexOf(val)) return;
- var extend = map[key];
- var suffix = key.replace(val, '');
- debug('extend %j with %j', rule.selectors, extend.selectors);
- extend.selectors = extend.selectors.concat(rule.selectors.map(function(sel){
- return sel + suffix;
- }));
- });
-
- rule.declarations.splice(i--, 1);
- }
+ for (var i = 0; i < rule.declarations.length; ++i) {
+ var decl = rule.declarations[i];
+ var key = decl.property;
+ var val = decl.value;
+ if (!/^extends?$/.test(key)) continue;
+
+ var extend = map[val];
+ if (!extend) throw new Error('failed to extend "' + val + '"');
+
+ var keys = Object.keys(map);
+ keys.forEach(function(key) {
+ if (0 != key.indexOf(val)) return;
+ var extend = map[key];
+ var suffix = key.replace(val, '');
+ debug('extend %j with %j', rule.selectors, extend.selectors);
+ extend.selectors = extend.selectors.concat(rule.selectors.map(function(sel) {
+ return sel + suffix;
+ }));
+ });
+
+ rule.declarations.splice(i--, 1);
+ }
}
@@ -19,8 +19,3 @@ input[type='submit']:hover .icon {
.actions a {
padding: 5px 10px
}
-
-button,
-input[type='submit'] {
-
-}
@@ -14,12 +14,8 @@ a.join {
padding: 10px 15px
}
-a.join {
-
-}
-
a.button
input[type='submit'],
input[type='button'] {
margin: 2px
-}
+}

0 comments on commit 50482ff

Please sign in to comment.