-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters