Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
David Hemphill committed May 23, 2015
0 parents commit 631272d
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-unused-expressions": [0],
"no-underscore-dangle": [0],
"no-reserved-keys": [2],
"no-multi-spaces": [0],
"no-extra-parens": [2],
"no-unused-vars": [2],
"no-loop-func": [0],
"key-spacing": [0],
"max-len": [2],
"strict": [0],
"indent": [2],
"quotes": [2, "single", "avoid-escape"],
"curly": [0]
},
"env": {
"mocha": true,
"node": true
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.gitignore

node_modules/

test/
.travis.yml

gulpfile.js
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
node_js:
- iojs
- "0.12"
- "0.10"
Empty file added CHANGELOG.md
Empty file.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright 2015 David Hemphill <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# PostCSS Verthorz [![Build Status][ci-img]][ci]

[PostCSS] plugin to add vertical and horizontal spacing rules.

[PostCSS]: https://github.com/postcss/postcss
[ci-img]: https://travis-ci.org/davidhemphill/postcss-verthorz.svg
[ci]: https://travis-ci.org/davidhemphill/postcss-verthorz

```css
.foo {
padding-vert: 2rem;
margin-horz: auto;
}
```

```css
.foo {
padding-top: 2rem;
padding-bottom: 2rem;
margin-left: auto;
margin-right: auto;
}
```

## Usage

```js
postcss([ require('postcss-verthorz') ])
```

See [PostCSS] docs for examples for your environment.
16 changes: 16 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var gulp = require('gulp');

gulp.task('lint', function () {
var eslint = require('gulp-eslint');
return gulp.src(['index.js', 'test/*.js', 'gulpfile.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('test', function () {
var mocha = require('gulp-mocha');
return gulp.src('test/*.js', { read: false }).pipe(mocha());
});

gulp.task('default', ['lint', 'test']);
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var postcss = require('postcss');

module.exports = postcss.plugin('postcss-verthorz', function (opts) {
opts = opts || {};

var PROPVALUES = {
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
};

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

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

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

for (var i = 0; i < declarations.length; i++) {
decl.cloneBefore({ prop: declarations[i], 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();
// });
};
});
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "postcss-verthorz",
"version": "0.0.0",
"description": "PostCSS plugin to add vertical and horizontal spacing rules",
"keywords": ["postcss", "css", "postcss-plugin", "margin", "padding", "spacing"],
"author": "David Hemphill <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/davidhemphill/postcss-verthorz.git"
},
"dependencies": {
"postcss": "^4.1.9"
},
"devDependencies": {
"gulp-eslint": "^0.12.0",
"gulp-mocha": "^2.0.1",
"chai": "^2.3.0",
"gulp": "^3.8.11"
},
"scripts": {
"test": "gulp"
}
}
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var postcss = require('postcss');
var expect = require('chai').expect;

var plugin = require('../');

var test = function (input, output, opts, done) {
postcss([ plugin(opts) ]).process(input).then(function (result) {
expect(result.css).to.eql(output);
expect(result.warnings()).to.be.empty;
done();
}).catch(function (error) {
done(error);
});
};

describe('postcss-verthorz', function () {

it('handles `padding-vert`', function (done) {
test('a { padding-vert: 20px; }', 'a { padding-top: 20px; padding-bottom: 20px; }', { }, done);
});

it('handles `padding-horz`', function (done) {
test('a { padding-horz: 1.5em; }', 'a { padding-left: 1.5em; padding-right: 1.5em; }', { }, done);
});

it('handles `pv`', function (done) {
test('a { pv: 2rem; }', 'a { padding-top: 2rem; padding-bottom: 2rem; }', { }, done);
});

it('handles `ph`', function (done) {
test('a { ph: 2rem; }', 'a { padding-left: 2rem; padding-right: 2rem; }', { }, done);
});

it('handles `margin-vert`', function (done) {
test('a { margin-vert: 2rem; }', 'a { margin-top: 2rem; margin-bottom: 2rem; }', { }, done);
});

it('handles `margin-horz`', function (done) {
test('a { margin-horz: 2rem; }', 'a { margin-left: 2rem; margin-right: 2rem; }', { }, done);
});

it('handles `mv`', function (done) {
test('a { mv: 2rem; }', 'a { margin-top: 2rem; margin-bottom: 2rem; }', { }, done);
});

it('handles `mh`', function (done) {
test('a { mh: 2rem; }', 'a { margin-left: 2rem; margin-right: 2rem; }', { }, done);
});

});

0 comments on commit 631272d

Please sign in to comment.