Skip to content

Commit

Permalink
add support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 7, 2012
1 parent cdbc175 commit 012bd31
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 1 deletion.
51 changes: 51 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,57 @@ $ rework -v webkit,moz < my.css > my.reworked.css
- [colors](#colors) — add colour helpers like `rgba(#fc0, .5)`
- [references](#references) — add property references support `height: @width` etc
- [mixin](#mixinobjects) — add custom property logic with mixing
- [extend](#extend) — add `extend: selector` support

### .extend()

Add support for extending existing rulesets:

```css

button {
padding: 5px 10px;
border: 1px solid #eee;
border-bottom-color: #ddd;
}

.green {
background: green;
padding: 10px 15px
}

a.join {
extend: button;
extend: .green;
}

a.button
input[type='submit'],
input[type='button'] {
extend: button
}
```

yields:

```css

button,
a.button,
input[type='submit'],
input[type='button'],
a.join {
padding: 5px 10px;
border: 1px solid #eee;
border-bottom-color: #ddd;
}

.green,
a.join {
background: green;
padding: 10px 15px
}
```

### .media(obj)

Expand Down
22 changes: 22 additions & 0 deletions examples/extend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

button {
padding: 5px 10px;
border: 1px solid #eee;
border-bottom-color: #ddd;
}

.green {
background: green;
padding: 10px 15px
}

a.join {
extend: button;
extend: .green;
}

a.button
input[type='submit'],
input[type='button'] {
extend: button
}
9 changes: 9 additions & 0 deletions examples/extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

var rework = require('..')
, read = require('fs').readFileSync;

var css = rework(read('examples/extend.css', 'utf8'))
.use(rework.extend())
.toString();

console.log(css);
48 changes: 48 additions & 0 deletions lib/plugins/extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

/**
* Module dependencies.
*/

var debug = require('debug')('rework:extend');

/**
* Define custom mixins.
*/

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(selector){
map[selector] = rule;
});
visit(rule, map);
});
}
};

/**
* Visit declarations and extensions.
*
* @param {Object} rule
* @param {Object} map
* @api private
*/

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 ('extend' != key) continue;

var extend = map[val];
if (!extend) throw new Error('failed to extend "' + val + '"');

debug('extend %j with %j', rule.selectors, extend.selectors);
extend.selectors = extend.selectors.concat(rule.selectors);
rule.declarations.splice(i--, 1);
}
}
1 change: 1 addition & 0 deletions lib/rework.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ exports.media = require('./plugins/media');
exports.mixin = require('./plugins/mixin');
exports.prefix = require('./plugins/prefix');
exports.colors = require('./plugins/colors');
exports.extend = require('./plugins/extend');
exports.references = require('./plugins/references');
exports.prefixValue = require('./plugins/prefix-value');
exports.prefixSelectors = require('./plugins/prefix-selectors');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"dependencies": {
"css": "1.0.7",
"commander": "1.0.4",
"color-parser": "0.0.1"
"color-parser": "0.0.1",
"debug": "*"
},
"devDependencies": {
"mocha": "*",
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/extend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

button {
padding: 5px 10px;
border: 1px solid #eee;
border-bottom-color: #ddd;
}

.green {
background: green;
padding: 10px 15px
}

a.join {
extend: button;
extend: .green;
}

a.button
input[type='submit'],
input[type='button'] {
extend: button;
margin: 2px;
}
25 changes: 25 additions & 0 deletions test/fixtures/extend.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
button,
a.join,
a.button
input[type='submit'],
input[type='button'] {
padding: 5px 10px;
border: 1px solid #eee;
border-bottom-color: #ddd
}

.green,
a.join {
background: green;
padding: 10px 15px
}

a.join {

}

a.button
input[type='submit'],
input[type='button'] {
margin: 2px
}
9 changes: 9 additions & 0 deletions test/rework.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ describe('rework', function(){
})
})

describe('.extend()', function(){
it('should allow extending rulesets', function(){
rework(fixture('extend'))
.use(rework.extend())
.toString()
.should.equal(fixture('extend.out'));
})
})

describe('.colors()', function(){
it('should support rgba(color, a)', function(){
rework(fixture('colors'))
Expand Down

0 comments on commit 012bd31

Please sign in to comment.