stylecow-plugin-calc
Advanced tools
Comparing version 1.2.0 to 2.0.0
115
index.js
@@ -1,77 +0,64 @@ | ||
module.exports = function (stylecow) { | ||
"use strict"; | ||
stylecow.addTask({ | ||
filter: { | ||
type: 'Function', | ||
name: 'calc' | ||
}, | ||
fn: function (fn) { | ||
resolve(fn); | ||
} | ||
}); | ||
module.exports = function (tasks) { | ||
stylecow.addTask({ | ||
filter: { | ||
type: 'Expression' | ||
}, | ||
fn: function (expression) { | ||
resolve(expression); | ||
} | ||
}); | ||
tasks.addTask({ | ||
filter: { | ||
type: 'Function', | ||
name: 'calc' | ||
}, | ||
fn: function (fn) { | ||
resolve(fn); | ||
} | ||
}); | ||
function resolve (element) { | ||
var mainUnit; | ||
var units = element.getAll({ | ||
type: 'Unit' | ||
}); | ||
tasks.addTask({ | ||
filter: 'Expression', | ||
fn: function (expression) { | ||
resolve(expression); | ||
} | ||
}); | ||
//Check the unit used in this function | ||
units.forEach(function (unit) { | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
function resolve (element) { | ||
var mainUnit, units = element.getAll('Unit'); | ||
if (mainUnit === undefined) { | ||
mainUnit = unit.name; | ||
} else if (mainUnit !== unit.name) { | ||
mainUnit = false; | ||
} | ||
}); | ||
//Check the unit used in this function | ||
units.forEach(function (unit) { | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
//There are more than one unit | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
if (mainUnit === undefined) { | ||
mainUnit = unit.name; | ||
} else if (mainUnit !== unit.name) { | ||
mainUnit = false; | ||
} | ||
}); | ||
//Replace all units by its numbers | ||
units.forEach(function (unit) { | ||
var number = unit.get('Number'); | ||
//There are more than one unit | ||
if (mainUnit === false) { | ||
return; | ||
} | ||
if (unit.name === '%') { | ||
number.name *= 0.01; | ||
} | ||
//Replace all units by its numbers | ||
units.forEach(function (unit) { | ||
var number = unit.get('Number'); | ||
unit.replaceWith(unit.get('Number')); | ||
}); | ||
if (unit.name === '%') { | ||
number.name *= 0.01; | ||
} | ||
var number = (new stylecow.Number()).setName(eval(element.join(' '))); | ||
unit.replaceWith(unit.get('Number')); | ||
}); | ||
if (mainUnit === undefined) { | ||
round(number); | ||
element.replaceWith(number); | ||
} else { | ||
var unit = (new stylecow.Unit()).setName(mainUnit); | ||
var number = eval(element.join(' ')); | ||
if (unit.name === '%') { | ||
number.name *= 100; | ||
} | ||
round(number); | ||
unit.push(number); | ||
element.replaceWith(unit); | ||
} | ||
} | ||
function round (number) { | ||
number.name = parseFloat(number.name.toFixed(2)); | ||
} | ||
if (mainUnit === undefined) { | ||
element.replaceWithCode(number.toFixed(2), 'Number'); | ||
} else if (mainUnit === '%') { | ||
element.replaceWithCode((number *= 100).toFixed(2) + '%', 'Unit'); | ||
} else { | ||
element.replaceWithCode(number.toFixed(2) + mainUnit, 'Unit'); | ||
} | ||
} | ||
}; |
{ | ||
"name": "stylecow-plugin-calc", | ||
"description": "Stylecow plugin to execute some calc functions", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"engines": { | ||
"node": ">=4.0" | ||
}, | ||
"author": "Oscar Otero <oom@oscarotero.com>", | ||
@@ -13,4 +16,4 @@ "homepage": "https://github.com/stylecow/stylecow-plugin-calc", | ||
"devDependencies": { | ||
"mocha": "^2.2.5", | ||
"stylecow-core": "^1.0.1" | ||
"mocha": "^2.3.2", | ||
"stylecow-core": "^2.0.0" | ||
}, | ||
@@ -17,0 +20,0 @@ "scripts": { |
@@ -1,29 +0,20 @@ | ||
var assert = require('assert'); | ||
var stylecow = require('stylecow-core'); | ||
stylecow | ||
.loadNpmModule(__dirname + '/../index') | ||
.minSupport({ | ||
"explorer": 0, | ||
"firefox": 0, | ||
"chrome": 0, | ||
"safari": 0, | ||
"opera": 0, | ||
"android": 0, | ||
"ios": 0 | ||
}) | ||
.testCases(__dirname + '/cases', function (test) { | ||
stylecow.run(test.css); | ||
var tests = new stylecow.Test(__dirname + '/cases'); | ||
var tasks = (new stylecow.Tasks()).use(require('../index')); | ||
describe('cases/' + test.name, function() { | ||
it('should match output.css', function() { | ||
//test.write('output.css', test.css.toString()); | ||
assert.equal(test.css.toString(), test.read('output.css')); | ||
}); | ||
tests.run(function (test) { | ||
tasks.run(test.css); | ||
it('should match ast.json', function() { | ||
//test.writeJson('ast.json', test.css.toAst()); | ||
assert.deepEqual(test.css.toAst(), test.readJson('ast.json')); | ||
}); | ||
}); | ||
}); | ||
describe('cases/' + test.name, function() { | ||
it('should match output.css', function() { | ||
//test.writeString() | ||
test.assertString(); | ||
}); | ||
it('should match ast.json', function() { | ||
//test.writeAst() | ||
test.assertAst(); | ||
}); | ||
}); | ||
}); |
{ | ||
"class": "Root", | ||
"type": "Root", | ||
"children": [ | ||
{ | ||
"class": "Rule", | ||
"type": "Rule", | ||
"children": [ | ||
{ | ||
"class": "Selectors", | ||
"type": "Selectors", | ||
"children": [ | ||
{ | ||
"class": "Selector", | ||
"type": "Selector", | ||
"children": [ | ||
{ | ||
"class": "TypeSelector", | ||
"type": "TypeSelector", | ||
"name": "div" | ||
@@ -22,6 +22,6 @@ } | ||
{ | ||
"class": "Block", | ||
"type": "Block", | ||
"children": [ | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -31,6 +31,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 111 | ||
@@ -43,3 +43,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -49,10 +49,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0.13 | ||
@@ -67,3 +67,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -73,6 +73,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Function", | ||
"type": "Function", | ||
"name": "calc", | ||
@@ -82,10 +82,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1.13 | ||
@@ -96,11 +96,11 @@ } | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "-" | ||
}, | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "px", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1 | ||
@@ -119,3 +119,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -125,6 +125,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Function", | ||
"type": "Function", | ||
"name": "calc", | ||
@@ -134,10 +134,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1.13 | ||
@@ -148,11 +148,11 @@ } | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "-" | ||
}, | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "px", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1 | ||
@@ -171,3 +171,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -177,6 +177,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Function", | ||
"type": "Function", | ||
"name": "calc", | ||
@@ -186,10 +186,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "px", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": -1 | ||
@@ -200,11 +200,11 @@ } | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "+" | ||
}, | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1.13 | ||
@@ -223,3 +223,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -229,10 +229,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 26.5 | ||
@@ -247,3 +247,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -253,6 +253,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Function", | ||
"type": "Function", | ||
"name": "calc", | ||
@@ -262,34 +262,34 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 8 | ||
}, | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "+" | ||
}, | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 4.2 | ||
}, | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "+" | ||
}, | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 1 | ||
}, | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "+" | ||
}, | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0.8 | ||
@@ -300,11 +300,11 @@ } | ||
{ | ||
"class": "Operator", | ||
"type": "Operator", | ||
"name": "+" | ||
}, | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "px", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0.8 | ||
@@ -311,0 +311,0 @@ } |
{ | ||
"class": "Root", | ||
"type": "Root", | ||
"children": [ | ||
{ | ||
"class": "Rule", | ||
"type": "Rule", | ||
"children": [ | ||
{ | ||
"class": "Selectors", | ||
"type": "Selectors", | ||
"children": [ | ||
{ | ||
"class": "Selector", | ||
"type": "Selector", | ||
"children": [ | ||
{ | ||
"class": "TypeSelector", | ||
"type": "TypeSelector", | ||
"name": "div" | ||
@@ -22,6 +22,6 @@ } | ||
{ | ||
"class": "Block", | ||
"type": "Block", | ||
"children": [ | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -31,6 +31,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 2 | ||
@@ -43,3 +43,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -49,6 +49,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0 | ||
@@ -61,3 +61,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -67,6 +67,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 4 | ||
@@ -79,3 +79,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -85,6 +85,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 4 | ||
@@ -97,3 +97,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -103,6 +103,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": -4 | ||
@@ -109,0 +109,0 @@ } |
{ | ||
"class": "Root", | ||
"type": "Root", | ||
"children": [ | ||
{ | ||
"class": "Rule", | ||
"type": "Rule", | ||
"children": [ | ||
{ | ||
"class": "Selectors", | ||
"type": "Selectors", | ||
"children": [ | ||
{ | ||
"class": "Selector", | ||
"type": "Selector", | ||
"children": [ | ||
{ | ||
"class": "TypeSelector", | ||
"type": "TypeSelector", | ||
"name": "div" | ||
@@ -22,6 +22,6 @@ } | ||
{ | ||
"class": "Block", | ||
"type": "Block", | ||
"children": [ | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -31,10 +31,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "%", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 100 | ||
@@ -49,3 +49,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -55,10 +55,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "%", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 60 | ||
@@ -65,0 +65,0 @@ } |
{ | ||
"class": "Root", | ||
"type": "Root", | ||
"children": [ | ||
{ | ||
"class": "Rule", | ||
"type": "Rule", | ||
"children": [ | ||
{ | ||
"class": "Selectors", | ||
"type": "Selectors", | ||
"children": [ | ||
{ | ||
"class": "Selector", | ||
"type": "Selector", | ||
"children": [ | ||
{ | ||
"class": "TypeSelector", | ||
"type": "TypeSelector", | ||
"name": "div" | ||
@@ -22,6 +22,6 @@ } | ||
{ | ||
"class": "Block", | ||
"type": "Block", | ||
"children": [ | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -31,10 +31,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "%", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 2 | ||
@@ -49,3 +49,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -55,10 +55,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 3.6 | ||
@@ -73,3 +73,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -79,6 +79,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0.33 | ||
@@ -91,3 +91,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -97,6 +97,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 0.33 | ||
@@ -109,3 +109,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -115,6 +115,6 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 3.6 | ||
@@ -121,0 +121,0 @@ } |
{ | ||
"class": "Root", | ||
"type": "Root", | ||
"children": [ | ||
{ | ||
"class": "Rule", | ||
"type": "Rule", | ||
"children": [ | ||
{ | ||
"class": "Selectors", | ||
"type": "Selectors", | ||
"children": [ | ||
{ | ||
"class": "Selector", | ||
"type": "Selector", | ||
"children": [ | ||
{ | ||
"class": "TypeSelector", | ||
"type": "TypeSelector", | ||
"name": "div" | ||
@@ -22,6 +22,6 @@ } | ||
{ | ||
"class": "Block", | ||
"type": "Block", | ||
"children": [ | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -31,10 +31,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "px", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 5 | ||
@@ -49,3 +49,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -55,10 +55,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "rem", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 5 | ||
@@ -73,3 +73,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -79,10 +79,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "em", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 5 | ||
@@ -97,3 +97,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -103,10 +103,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "pt", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 5 | ||
@@ -121,3 +121,3 @@ } | ||
{ | ||
"class": "Declaration", | ||
"type": "Declaration", | ||
"name": "width", | ||
@@ -127,10 +127,10 @@ "vendor": null, | ||
{ | ||
"class": "Value", | ||
"type": "Value", | ||
"children": [ | ||
{ | ||
"class": "Unit", | ||
"type": "Unit", | ||
"name": "vh", | ||
"children": [ | ||
{ | ||
"class": "Number", | ||
"type": "Number", | ||
"name": 5 | ||
@@ -137,0 +137,0 @@ } |
Sorry, the diff of this file is not supported yet
25772
868