Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Apr 6, 2013
1 parent a704c07 commit 734fa59
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ var css = rework(inputCSS)
.toString()
```

#### Inherit(options{})

Option parameters:

* `propertyRegExp` - Regular expression to match the "inherit" property.
By default, it is `/^inherits?$/`, so it matches "inherit" as well as "inherits".
Set as `/^extends?$/` if you want to use the "extend" keyword.
* `disableMediaInheritance` - Disable inheritance from within media queries.

### Examples

#### Regular inherit
Expand Down
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
exports = module.exports = function () {
return Inherit
exports = module.exports = function (options) {
return function inherit(style) {
return new Inherit(style, options || {})
}
}

exports.Inherit = Inherit

function Inherit(style) {
function Inherit(style, options) {
if (!(this instanceof Inherit))
return new Inherit(style);
return new Inherit(style, options);

this.propertyRegExp = options.propertyRegExp || /^inherits?$/

var rules = this.rules = style.rules
this.matches = {}

for (var i = 0; i < rules.length; i++) {
var rule = rules[i]
if (rule.rules) {
if (rule.rules && !options.disableMediaInheritance) {
// Media queries
this.inheritMedia(rule)
if (!rule.rules.length) rules.splice(i--, 1);
} else if (rule.selectors) {
// Regular rules
this.inheritRules(rule)
if (!rule.declarations.length) rules.splice(i--, 1);
}
Expand Down Expand Up @@ -80,7 +86,7 @@ Inherit.prototype.inheritRules = function (rule) {

for (var i = 0; i < declarations.length; i++) {
var decl = declarations[i]
if (!/^inherits?$/.test(decl.property)) continue;
if (!this.propertyRegExp.test(decl.property)) continue;

decl.value.split(',').map(trim).forEach(function (val) {
this.inheritRule(val, selectors)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "rework-inherit",
"description": "Inherit rules from other selectors",
"version": "0.0.2",
"version": "0.1.0",
"peerDependencies": {
"rework": "*"
"rework": ">= 0.13.2"
}
}
9 changes: 9 additions & 0 deletions test/fixtures/disable.media.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.gray {
color: gray
}

@media (min-width: 320px) {
.button {
inherit: .gray
}
}
11 changes: 11 additions & 0 deletions test/fixtures/extend.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.a {
color: red;
}

.b {
extend: .a;
}

.c {
extends: .b;
}
14 changes: 13 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ test('substring', 'Inherit substring failed')
test('multiple', 'Inherit multiple selectors failed')
test('tag', 'Inherit a tag failed')
test('chain', 'Chained inheritance failed')
test('unordered', 'Out of order inheritance failed')
test('unordered', 'Out of order inheritance failed')

var ext = rework(read('extend')).use(inherit({
propertyRegExp: /^extends?$/
})).toString()

assert.equal(ext, read('chain.out'), 'Extends regexp failed:\n' + ext)

var media = rework(read('media')).use(inherit({
disableMediaInheritance: true
})).toString()

assert.equal(media, read('disable.media.out'), 'Disable media inheritance failed:\n' + media)

0 comments on commit 734fa59

Please sign in to comment.