stylecow-plugin-calc
Advanced tools
Comparing version 1.0.1 to 1.1.0
100
index.js
@@ -1,41 +0,77 @@ | ||
module.exports = [ | ||
//Normalizes the calc | ||
{ | ||
"Function": { | ||
calc: function (fn) { | ||
var keyword = fn[0]; | ||
keyword.name = keyword.name.replace(/([\w\%])\s*([\+\-])\s*/g, '$1 $2 '); | ||
} | ||
module.exports = function (stylecow) { | ||
stylecow.addTask({ | ||
filter: { | ||
type: 'Function', | ||
name: 'calc' | ||
}, | ||
fn: function (fn) { | ||
resolve(fn); | ||
} | ||
}, | ||
}); | ||
//Adds -moz- vendor prefix | ||
{ | ||
disable: { | ||
firefox: 16.0 | ||
stylecow.addTask({ | ||
filter: { | ||
type: 'Expression' | ||
}, | ||
Declaration: function (declaration) { | ||
if (declaration.has('Function', 'calc')) { | ||
declaration.cloneBefore().search('Function', 'calc').forEach(function (fn) { | ||
fn.name = '-moz-' + fn.name; | ||
}); | ||
fn: function (expression) { | ||
resolve(expression); | ||
} | ||
}); | ||
function resolve (element) { | ||
var mainUnit; | ||
var units = element.getAll({ | ||
type: 'Unit' | ||
}); | ||
//Check the unit used in this function | ||
units.forEach(function (unit) { | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
if (mainUnit === undefined) { | ||
mainUnit = unit.name; | ||
} else if (mainUnit !== unit.name) { | ||
mainUnit = false; | ||
} | ||
}); | ||
//There are more than one unit | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
}, | ||
//Adds -webkit- vendor prefix | ||
{ | ||
disable: { | ||
chrome: 26.0, | ||
safari: 6.1, | ||
ios: 7.0 | ||
}, | ||
Declaration: function (declaration) { | ||
if (declaration.has('Function', 'calc')) { | ||
declaration.cloneBefore().search('Function', 'calc').forEach(function (fn) { | ||
fn.name = '-webkit-' + fn.name; | ||
}); | ||
//Replace all units by its numbers | ||
units.forEach(function (unit) { | ||
var number = unit.get('Number'); | ||
if (unit.name === '%') { | ||
number.name *= 0.01; | ||
} | ||
unit.replaceWith(unit.get('Number')); | ||
}); | ||
var number = (new stylecow.Number()).setName(eval(element.join(' '))); | ||
if (mainUnit === undefined) { | ||
round(number); | ||
element.replaceWith(number); | ||
} else { | ||
var unit = (new stylecow.Unit()).setName(mainUnit); | ||
if (unit.name === '%') { | ||
number.name *= 100; | ||
} | ||
round(number); | ||
unit.push(number); | ||
element.replaceWith(unit); | ||
} | ||
} | ||
]; | ||
function round (number) { | ||
number.name = parseFloat(number.name.toFixed(2)); | ||
} | ||
}; |
{ | ||
"name": "stylecow-plugin-calc", | ||
"description": "Stylecow plugin to add vendor prefixes to calc() and normalize to require spaces around binary '+' and '-' operators", | ||
"version": "1.0.1", | ||
"description": "Stylecow plugin to execute some calc functions", | ||
"version": "1.1.0", | ||
"author": "Oscar Otero <oom@oscarotero.com>", | ||
@@ -12,4 +12,10 @@ "homepage": "https://github.com/stylecow/stylecow-plugin-calc", | ||
}, | ||
"devDependencies": { | ||
"stylecow": "6.*" | ||
}, | ||
"scripts": { | ||
"test": "mocha tests/cases.js" | ||
}, | ||
"main": "index.js", | ||
"license": "MIT" | ||
} |
stylecow plugin calc | ||
==================== | ||
Stylecow plugin to add vendor prefixes to calc() and normalize to require spaces around binary '+' and '-' operators | ||
Stylecow plugin to resolve some `calc()` functions. | ||
@@ -9,4 +9,4 @@ You write: | ||
```css | ||
body { | ||
width: calc(100%-20); | ||
p { | ||
font-size: calc(2rem * 2); | ||
} | ||
@@ -18,7 +18,7 @@ ``` | ||
```css | ||
body { | ||
width: -moz-calc(100% - 20); | ||
width: -webkit-calc(100% - 20); | ||
width: calc(100% - 20); | ||
p { | ||
font-size: 4rem; | ||
} | ||
``` | ||
More demos in [the tests folder](https://github.com/stylecow/stylecow-plugin-calc/tree/master/tests/cases) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
25629
21
890
0
1
22
1