Skip to content

Commit

Permalink
New processor added: opacity fallback
Browse files Browse the repository at this point in the history
(Fix #16)
  • Loading branch information
iamvdo committed Aug 25, 2014
1 parent a8b30c6 commit 315c068
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Pleeease.prototype.process = function (css) {
filter = require('pleeease-filters'),
rem = require('pixrem'),
pseudoElements = require('../lib/processors/pseudoElements'),
opacity = require('../lib/processors/opacity'),
mqpacker = require('css-mqpacker'),
prefixer = require('autoprefixer-core'),
minifier = require('csswring');
Expand All @@ -47,6 +48,7 @@ Pleeease.prototype.process = function (css) {
(opts = this.opts.filters) ? filter(opts).postcss : false,
(opts = this.opts.rem) ? rem(opts).postcss : false,
(opts = this.opts.pseudoElements) ? pseudoElements.processor : false,
(opts = this.opts.opacity) ? opacity.processor : false,
(opts = this.opts.mqpacker) ? mqpacker.processor : false,
(opts = this.opts.autoprefixer) ? prefixer(opts).postcss : false,
(opts = this.opts.minifier) ? minifier(opts).postcss : false
Expand Down
1 change: 1 addition & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function Options () {
'filters': true,
'rem': true,
'pseudoElements': true,
'opacity': true,

'import': true,
'mqpacker': true,
Expand Down
41 changes: 41 additions & 0 deletions lib/processors/opacity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

/**
*
* Fallback opacity property
*
*/
module.exports.processor = function (css) {

var PROP = 'opacity';
var PROP_REPLACE = 'filter';

// for each rules, each decl
css.eachRule(function (rule) {
rule.eachDecl(function (decl) {

if (decl.prop !== PROP) { return; }

// get amount and create value
var amount = Math.floor(decl.value * 100);
var VAL_REPLACE = 'alpha(opacity=' + amount + ')';

// find if a filter opacity is already present
var isFilterAlreadyPresent = false;
rule.eachDecl(function (d) {
if (d.prop === PROP_REPLACE && d.value === VAL_REPLACE) {
isFilterAlreadyPresent = true;
}
});

// adds new porperty only if it's not present yet
if (!isFilterAlreadyPresent) {
rule.insertAfter(decl, {prop: PROP_REPLACE, value: VAL_REPLACE});
}

});
});

return css;

};
11 changes: 11 additions & 0 deletions test/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ describe('Features', function () {

});

describe('Opacity', function () {

it('should convert opacity into filter', function () {

// options
test('opacity', opts);

});

});

describe('MQs', function () {

it('should combine media-queries', function () {
Expand Down
10 changes: 10 additions & 0 deletions test/features/opacity.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.opacity {
opacity: .5;
}
.opacity {
opacity: .216708;
}
.opacity {
opacity: .5;
filter: alpha(opacity=50);
}
12 changes: 12 additions & 0 deletions test/features/opacity.out.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.opacity {
opacity: .5;
filter: alpha(opacity=50);
}
.opacity {
opacity: .216708;
filter: alpha(opacity=21);
}
.opacity {
opacity: .5;
filter: alpha(opacity=50);
}

0 comments on commit 315c068

Please sign in to comment.