postcss-calc 2.1.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,15 +1,34 @@
1
+ # 4.1.0 - 2015-04-09
2
+
3
+ - Added: compatibility with postcss v4.1.x ([#12](https://github.com/postcss/postcss-calc/pull/12))
4
+
5
+ # 4.0.1 - 2015-04-09
6
+
7
+ - Fixed: `preserve` option does not create duplicated values ([#7](https://github.com/postcss/postcss-calc/issues/7))
8
+
9
+ # 4.0.0 - 2015-01-26
10
+
11
+ - Added: compatibility with postcss v4.x
12
+ - Changed: partial compatiblity with postcss v3.x (stack traces have lost filename)
13
+
14
+ # 3.0.0 - 2014-11-24
15
+
16
+ - Added: GNU like exceptions ([#4](https://github.com/postcss/postcss-calc/issues/4))
17
+ - Added: `precision` option ([#5](https://github.com/postcss/postcss-calc/issues/5))
18
+ - Added: `preserve` option ([#6](https://github.com/postcss/postcss-calc/issues/6))
19
+
1
20
  # 2.1.0 - 2014-10-15
2
21
 
3
- - Add source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
22
+ - Added: source of the error (gnu like message) (fix [#3](https://github.com/postcss/postcss-calc/issues/3))
4
23
 
5
24
  # 2.0.1 - 2014-08-10
6
25
 
7
- - Correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
26
+ - Fixed: correctly ignore unrecognized values (fix [#2](https://github.com/postcss/postcss-calc/issues/2))
8
27
 
9
28
  # 2.0.0 - 2014-08-06
10
29
 
11
- - Plugin now return a function to have a consistent api. ([ref 1](https://github.com/ianstormtaylor/rework-color-function/issues/6), [ref 2](https://twitter.com/jongleberry/status/496552790416576513))
30
+ - Changed: Plugin now return a function to have a consistent api. ([ref 1](https://github.com/ianstormtaylor/rework-color-function/issues/6), [ref 2](https://twitter.com/jongleberry/status/496552790416576513))
12
31
 
13
32
  # 1.0.0 - 2014-08-04
14
33
 
15
- First release based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 (code mainly exported to [`reduce-css-calc`](https://github.com/MoOx/reduce-css-calc))
34
+ First release based on [rework-calc](https://github.com/reworkcss/rework-calc) v1.1.0 (code mainly exported to [`reduce-css-calc`](https://github.com/MoOx/reduce-css-calc))
package/README.md CHANGED
@@ -9,7 +9,9 @@ This can be particularly useful with the [postcss-custom-properties](https://git
9
9
 
10
10
  ## Installation
11
11
 
12
- $ npm install postcss-calc
12
+ ```console
13
+ $ npm install postcss-calc
14
+ ```
13
15
 
14
16
  ## Usage
15
17
 
@@ -81,16 +83,42 @@ h1 {
81
83
 
82
84
  Checkout [tests](test) for more examples.
83
85
 
86
+ ### Options
87
+
88
+ #### `precision` (default: `5`)
89
+
90
+ Allow you to definine the precision for decimal numbers.
91
+
92
+ ```js
93
+ var out = postcss()
94
+ .use(calc({precision: 10}))
95
+ .process(css)
96
+ .css
97
+ ```
98
+
99
+ #### `preserve` (default: `false`)
100
+
101
+ Allow you to preserve calc() usage in output so browsers will handle decimal precision themselves.
102
+
103
+ ```js
104
+ var out = postcss()
105
+ .use(calc({preserve: true}))
106
+ .process(css)
107
+ .css
108
+ ```
109
+
84
110
  ---
85
111
 
86
112
  ## Contributing
87
113
 
88
114
  Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
89
115
 
90
- $ git clone https://github.com/postcss/postcss-calc.git
91
- $ git checkout -b patch-1
92
- $ npm install
93
- $ npm test
116
+ ```console
117
+ $ git clone https://github.com/postcss/postcss-calc.git
118
+ $ git checkout -b patch-1
119
+ $ npm install
120
+ $ npm test
121
+ ```
94
122
 
95
123
  ## [Changelog](CHANGELOG.md)
96
124
 
package/index.js CHANGED
@@ -2,48 +2,37 @@
2
2
  * Module dependencies.
3
3
  */
4
4
  var reduceCSSCalc = require("reduce-css-calc")
5
+ var helpers = require("postcss-message-helpers")
6
+ var postcss = require("postcss");
5
7
 
6
8
  /**
7
9
  * PostCSS plugin to reduce calc() function calls.
8
10
  */
9
- module.exports = function plugin() {
11
+ module.exports = postcss.plugin("postcss-calc", function(options) {
12
+ options = options || {}
13
+ var precision = options.precision
14
+ var preserve = options.preserve
15
+
10
16
  return function(style) {
11
- style.eachDecl(function transformDecl(dec) {
12
- if (!dec.value) {
17
+ style.eachDecl(function transformDecl(decl) {
18
+ if (!decl.value || decl.value.indexOf("calc(") === -1) {
13
19
  return
14
20
  }
15
21
 
16
- try {
17
- dec.value = transform(dec.value)
18
- }
19
- catch (err) {
20
- err.message = gnuMessage(err.message, dec.source)
21
- throw err
22
- }
23
- })
24
- }
25
- }
22
+ helpers.try(function transformCSSCalc() {
23
+ var value = reduceCSSCalc(decl.value, precision)
26
24
 
27
- /**
28
- * Reduce CSS calc() on a declaration value
29
- *
30
- * @param {String} string
31
- * @return {String}
32
- */
33
- function transform(string) {
34
- if (string.indexOf("calc(") === -1) {
35
- return string
36
- }
37
-
38
- return reduceCSSCalc(string)
39
- }
25
+ if (!preserve) {
26
+ decl.value = value
27
+ return
28
+ }
40
29
 
41
- /**
42
- * return GNU style message
43
- *
44
- * @param {String} message
45
- * @param {Object} source
46
- */
47
- function gnuMessage(message, source) {
48
- return (source ? (source.file ? source.file : "<css input>") + ":" + source.start.line + ":" + source.start.column : "") + " " + message
49
- }
30
+ if (value != decl.value) {
31
+ var clone = decl.clone()
32
+ clone.value = value
33
+ decl.parent.insertBefore(decl, clone)
34
+ }
35
+ }, decl.source)
36
+ })
37
+ }
38
+ });
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "postcss-calc",
3
- "version": "2.1.0",
3
+ "version": "4.1.0",
4
4
  "description": "PostCSS plugin to reduce calc()",
5
5
  "keywords": [
6
6
  "css",
7
7
  "postcss",
8
- "postcss-plugins",
8
+ "postcss-plugin",
9
9
  "calculation",
10
10
  "calc"
11
11
  ],
@@ -18,17 +18,17 @@
18
18
  "files": [
19
19
  "CHANGELOG.md",
20
20
  "LICENSE",
21
- "README.md",
22
21
  "index.js"
23
22
  ],
24
23
  "dependencies": {
25
- "reduce-css-calc": "^1.1.2"
24
+ "postcss-message-helpers": "^2.0.0",
25
+ "reduce-css-calc": "^1.2.0",
26
+ "postcss": "^4.1.11"
26
27
  },
27
28
  "devDependencies": {
28
29
  "jscs": "^1.6.2",
29
30
  "jshint": "^2.5.6",
30
- "postcss": "^2.2.5",
31
- "postcss-custom-properties": "^0.4.0",
31
+ "postcss-custom-properties": "^3.0.0",
32
32
  "tape": "^3.0.0"
33
33
  },
34
34
  "scripts": {