diff options
Diffstat (limited to 'node_modules/reduce-css-calc')
| -rwxr-xr-x | node_modules/reduce-css-calc/CHANGELOG.md | 87 | ||||
| -rwxr-xr-x | node_modules/reduce-css-calc/LICENSE | 20 | ||||
| -rwxr-xr-x | node_modules/reduce-css-calc/README.md | 72 | ||||
| -rwxr-xr-x | node_modules/reduce-css-calc/index.js | 164 | ||||
| -rw-r--r-- | node_modules/reduce-css-calc/package.json | 33 |
5 files changed, 376 insertions, 0 deletions
diff --git a/node_modules/reduce-css-calc/CHANGELOG.md b/node_modules/reduce-css-calc/CHANGELOG.md new file mode 100755 index 00000000..fff0c07c --- /dev/null +++ b/node_modules/reduce-css-calc/CHANGELOG.md @@ -0,0 +1,87 @@ +# 1.3.0 - 2016-08-26 + +- Added: calc identifier from unresolved nested expressions are removed for + better browser support + ([#19](https://github.com/MoOx/reduce-css-calc/pull/19) - @ben-eb) + +# 1.2.8 - 2016-08-26 + +- Fixed: regression from 1.2.5 on calc() with value without leading 0 + ([#17](https://github.com/MoOx/reduce-css-calc/pull/17) - @ben-eb) + +# 1.2.7 - 2016-08-22 + +- Fixed: regression from 1.2.5 on calc() with value without leading 0 + (@MoOx) + +# 1.2.6 - 2016-08-22 + +- Fixed: regression from 1.2.5 on calc() on multiple lines + (@MoOx) + +# 1.2.5 - 2016-08-22 + +- Fixed: security issue due to the usage of ``eval()``. + This is to avoid an arbitrary code execution. + Now operations are resolved using + [``math-expression-evaluator``](https://github.com/redhivesoftware/math-expression-evaluator) + +# 1.2.4 - 2016-06-09 + +- Fixed: zero values are not unitless anymore. + Browsers do not calculate calc() with 0 unitless values. + http://jsbin.com/punivivipo/edit?html,css,output + ([#11](https://github.com/MoOx/reduce-css-calc/pull/11))
+ +# 1.2.3 - 2016-04-28 + +- Fixed: wrong rouding in some edge cases + ([#10](https://github.com/MoOx/reduce-css-calc/pull/10)) + +# 1.2.2 - 2016-04-19 + +- Fixed: Don't reduce expression containing CSS variables. + ([#9](https://github.com/MoOx/reduce-css-calc/pull/9)) + +# 1.2.1 - 2016-02-22 + +- Fixed: uppercase letters in units are now supported + ([#8](https://github.com/MoOx/reduce-css-calc/pull/8)) + +# 1.2.0 - 2014-11-24 + +- Decimal precision is now customisable as the `precision` option + +# 1.1.4 - 2014-11-12 + +- 5 decimals rounding for everything + +# 1.1.3 - 2014-08-13 + +- 5 decimals rounding for percentage + +# 1.1.2 - 2014-08-10 + +- Prevent infinite loop by adding a `Call stack overflow` +- Correctly ignore unrecognized values (safer evaluation for nested expressions, + see [postcss/postcss-calc#2](https://github.com/postcss/postcss-calc/issues/2)) +- Handle rounding issues (eg: 10% * 20% now give 2%, not 2.0000000000000004%) + +# 1.1.1 - 2014-08-06 + +- Fix issue when using mutiples differents prefixes in the same function + +# 1.1.0 - 2014-08-06 + +- support more complex formulas +- use `reduce-function-call` +- better error message + + +# 1.0.0 - 2014-08-04 + +First release + +- based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 +- add error if the calc() embed an empty calc() or empty () +- jscs + jshint added before tests diff --git a/node_modules/reduce-css-calc/LICENSE b/node_modules/reduce-css-calc/LICENSE new file mode 100755 index 00000000..7cc9dd08 --- /dev/null +++ b/node_modules/reduce-css-calc/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2014 Maxime Thirouin & Joakim Bengtson + +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. diff --git a/node_modules/reduce-css-calc/README.md b/node_modules/reduce-css-calc/README.md new file mode 100755 index 00000000..0ba73d30 --- /dev/null +++ b/node_modules/reduce-css-calc/README.md @@ -0,0 +1,72 @@ +# reduce-css-calc [](https://travis-ci.org/MoOx/reduce-css-calc) + +> Reduce CSS calc() function to the maximum. + +Particularly useful for packages like [rework-calc](https://github.com/reworkcss/rework-calc) or [postcss-calc](https://github.com/postcss/postcss-calc). + +## Installation + +```console +$ npm install reduce-css-calc +``` + +## Usage + +### `var reducedString = reduceCSSCalc(string, precision)` + +```javascript +var reduceCSSCalc = require('reduce-css-calc') + +reduceCSSCalc("calc(1 + 1)") +// 2 + +reduceCSSCalc("calc((6 / 2) - (4 * 2) + 1)") +// -4 + +reduceCSSCalc("calc(1/3)") +// 0.33333 + +reduceCSSCalc("calc(1/3)", 10) +// 0.3333333333 + +reduceCSSCalc("calc(3rem * 2 - 1rem)") +// 5rem + +reduceCSSCalc("calc(2 * 50%)") +// 100% + +reduceCSSCalc("calc(120% * 50%)") +// 60% + +reduceCSSCalc("a calc(1 + 1) b calc(1 - 1) c") +// a 2 b 0 c + +reduceCSSCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1rem)") +// 0.125rem + +reduceCSSCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1px)") +// calc(1.125rem - 1px) + +reduceCSSCalc("-moz-calc(100px / 2)") +// 50px + +reduceCSSCalc("-moz-calc(50% - 2em)") +// -moz-calc(50% - 2em) +``` + +See [unit tests](test/index.js) for others examples. + +## Contributing + +Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature. + +```console +$ git clone https://github.com/MoOx/reduce-css-calc.git +$ git checkout -b patch-1 +$ npm install +$ npm test +``` + +## [Changelog](CHANGELOG.md) + +## [License](LICENSE-MIT) diff --git a/node_modules/reduce-css-calc/index.js b/node_modules/reduce-css-calc/index.js new file mode 100755 index 00000000..3ec72471 --- /dev/null +++ b/node_modules/reduce-css-calc/index.js @@ -0,0 +1,164 @@ +/** + * Module dependencies + */ +var balanced = require("balanced-match") +var reduceFunctionCall = require("reduce-function-call") +var mexp = require("math-expression-evaluator") + +/** + * Constantes + */ +var MAX_STACK = 100 // should be enough for a single calc()... +var NESTED_CALC_RE = /(\+|\-|\*|\\|[^a-z]|)(\s*)(\()/g + +/** + * Global variables + */ +var stack + +/** + * Expose reduceCSSCalc plugin + * + * @type {Function} + */ +module.exports = reduceCSSCalc + +/** + * Reduce CSS calc() in a string, whenever it's possible + * + * @param {String} value css input + */ +function reduceCSSCalc(value, decimalPrecision) { + stack = 0 + decimalPrecision = Math.pow(10, decimalPrecision === undefined ? 5 : decimalPrecision) + + // Allow calc() on multiple lines + value = value.replace(/\n+/g, " ") + + /** + * Evaluates an expression + * + * @param {String} expression + * @returns {String} + */ + function evaluateExpression (expression, functionIdentifier, call) { + if (stack++ > MAX_STACK) { + stack = 0 + throw new Error("Call stack overflow for " + call) + } + + if (expression === "") { + throw new Error(functionIdentifier + "(): '" + call + "' must contain a non-whitespace string") + } + + expression = evaluateNestedExpression(expression, call) + + var units = getUnitsInExpression(expression) + + // If the expression contains multiple units or CSS variables, + // then let the expression be (i.e. browser calc()) + if (units.length > 1 || expression.indexOf("var(") > -1) { + return functionIdentifier + "(" + expression + ")" + } + + var unit = units[0] || "" + + if (unit === "%") { + // Convert percentages to numbers, to handle expressions like: 50% * 50% (will become: 25%): + // console.log(expression) + expression = expression.replace(/\b[0-9\.]+%/g, function(percent) { + return parseFloat(percent.slice(0, -1)) * 0.01 + }) + } + + // Remove units in expression: + var toEvaluate = expression.replace(new RegExp(unit, "gi"), "") + var result + + try { + result = mexp.eval(toEvaluate) + } + catch (e) { + return functionIdentifier + "(" + expression + ")" + } + + // Transform back to a percentage result: + if (unit === "%") { + result *= 100 + } + + // adjust rounding shit + // (0.1 * 0.2 === 0.020000000000000004) + if (functionIdentifier.length || unit === "%") { + result = Math.round(result * decimalPrecision) / decimalPrecision + } + + // Add unit + result += unit + + return result + } + + /** + * Evaluates nested expressions + * + * @param {String} expression + * @returns {String} + */ + function evaluateNestedExpression(expression, call) { + // Remove the calc part from nested expressions to ensure + // better browser compatibility + expression = expression.replace(/((?:\-[a-z]+\-)?calc)/g, "") + var evaluatedPart = "" + var nonEvaluatedPart = expression + var matches + while ((matches = NESTED_CALC_RE.exec(nonEvaluatedPart))) { + if (matches[0].index > 0) { + evaluatedPart += nonEvaluatedPart.substring(0, matches[0].index) + } + + var balancedExpr = balanced("(", ")", nonEvaluatedPart.substring([0].index)) + if (balancedExpr.body === "") { + throw new Error("'" + expression + "' must contain a non-whitespace string") + } + + var evaluated = evaluateExpression(balancedExpr.body, "", call) + + evaluatedPart += balancedExpr.pre + evaluated + nonEvaluatedPart = balancedExpr.post + } + + return evaluatedPart + nonEvaluatedPart + } + + return reduceFunctionCall(value, /((?:\-[a-z]+\-)?calc)\(/, evaluateExpression) +} + +/** + * Checks what units are used in an expression + * + * @param {String} expression + * @returns {Array} + */ + +function getUnitsInExpression(expression) { + var uniqueUnits = [] + var uniqueLowerCaseUnits = [] + var unitRegEx = /[\.0-9]([%a-z]+)/gi + var matches = unitRegEx.exec(expression) + + while (matches) { + if (!matches || !matches[1]) { + continue + } + + if (uniqueLowerCaseUnits.indexOf(matches[1].toLowerCase()) === -1) { + uniqueUnits.push(matches[1]) + uniqueLowerCaseUnits.push(matches[1].toLowerCase()) + } + + matches = unitRegEx.exec(expression) + } + + return uniqueUnits +} diff --git a/node_modules/reduce-css-calc/package.json b/node_modules/reduce-css-calc/package.json new file mode 100644 index 00000000..6baa0601 --- /dev/null +++ b/node_modules/reduce-css-calc/package.json @@ -0,0 +1,33 @@ +{ + "name": "reduce-css-calc", + "version": "1.3.0", + "description": "Reduce CSS calc() function to the maximum", + "keywords": [ + "css", + "calculation", + "calc" + ], + "author": "Maxime Thirouin", + "license": "MIT", + "repository": "https://github.com/MoOx/reduce-css-calc.git", + "files": [ + "index.js" + ], + "dependencies": { + "balanced-match": "^0.4.2", + "math-expression-evaluator": "^1.2.14", + "reduce-function-call": "^1.0.1" + }, + "devDependencies": { + "jscs": "^1.5.9", + "jshint": "^2.5.2", + "npmpub": "^3.0.3", + "tape": "^2.13.4" + }, + "scripts": { + "jscs": "jscs *.js **/*.js", + "jshint": "jshint . --exclude node_modules", + "test": "npm run jscs && npm run jshint && tape test", + "release": "npmpub" + } +} |
