Skip to content

Commit

Permalink
Fix bug with multiple declarations, bump to 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hemphill committed May 23, 2015
1 parent 631272d commit 0648adb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
47 changes: 18 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,39 @@ var postcss = require('postcss');
module.exports = postcss.plugin('postcss-verthorz', function (opts) {
opts = opts || {};

var PROPVALUES = {
var VALUES = {
pv: ['padding-top', 'padding-bottom'],
ph: ['padding-left', 'padding-right'],
mv: ['margin-top', 'margin-bottom'],
mh: ['margin-left', 'margin-right']
}
};

var PROPS = {
'padding-vert': PROPVALUES.pv,
'padding-horz': PROPVALUES.ph,
'pv': PROPVALUES.pv,
'ph': PROPVALUES.ph,
'margin-vert': PROPVALUES.mv,
'margin-horz': PROPVALUES.mh,
'mv': PROPVALUES.mv,
'mh': PROPVALUES.mh
'padding-vert': VALUES.pv,
'padding-horz': VALUES.ph,
'pv': VALUES.pv,
'ph': VALUES.ph,
'margin-vert': VALUES.mv,
'margin-horz': VALUES.mh,
'mv': VALUES.mv,
'mh': VALUES.mh
};

// Work with CSS here
return function (css) {
css.eachRule(function (rule) {
rule.nodes.forEach(function(node) {
var prop = node.prop;
rule.each(function(decl) {
var prop = decl.prop;

// Return if the value isn't in PROPS
if (! PROPS.hasOwnProperty(prop)) return;
if (!PROPS.hasOwnProperty(prop)) return;

css.eachDecl(prop, function (decl) {
var declarations = PROPS[prop];
var properties = PROPS[prop];

for (var i = 0; i < declarations.length; i++) {
decl.cloneBefore({ prop: declarations[i], value: decl.value });
}
decl.removeSelf();
properties.forEach(function(property, index) {
decl.cloneBefore({ prop: properties[index], value: decl.value });
});

decl.removeSelf();
});
});


// This works!
// css.eachDecl('padding-vert', function (decl) {
// decl.cloneBefore({ prop: 'padding-top', value: decl.value });
// decl.cloneBefore({ prop: 'padding-bottom', value: decl.value });
// decl.removeSelf();
// });
};
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-verthorz",
"version": "0.0.0",
"version": "0.5.0",
"description": "PostCSS plugin to add vertical and horizontal spacing rules",
"keywords": ["postcss", "css", "postcss-plugin", "margin", "padding", "spacing"],
"author": "David Hemphill <[email protected]>",
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ describe('postcss-verthorz', function () {
test('a { mh: 2rem; }', 'a { margin-left: 2rem; margin-right: 2rem; }', { }, done);
});

it('handles auto', function (done) {
test('a { ph: 3rem; mh: auto; }', 'a { padding-left: 3rem; padding-right: 3rem; margin-left: auto; margin-right: auto; }', { }, done);
});

it('handles multiples', function (done) {
test('a { padding-vert: 3rem; mh: 2rem; }', 'a { padding-top: 3rem; padding-bottom: 3rem; margin-left: 2rem; margin-right: 2rem; }', { }, done);
});

it('handles `failures`', function (done) {
test('a { padding-nope: 20px; }', 'a { padding-nope: 20px; }', { }, done);
});

});

0 comments on commit 0648adb

Please sign in to comment.