postcss-calc 5.3.0 → 6.0.2

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,3 +1,20 @@
1
+ # 6.0.2 - 2018-09-25
2
+
3
+ - Fixed: use PostCSS 7 (thanks to @douglasduteil)
4
+
5
+ # 6.0.1 - 2017-10-10
6
+
7
+ - Fixed: throwing error for attribute selectors without a value
8
+
9
+ # 6.0.0 - 2017-05-08
10
+
11
+ - Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
12
+ to v2.x (thanks to @andyjansson).
13
+
14
+ # 5.3.1 - 2016-08-22
15
+
16
+ - Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
17
+
1
18
  # 5.3.0 - 2016-07-11
2
19
 
3
20
  - Added: support for selector transformation via `selectors` option.
package/README.md CHANGED
@@ -86,13 +86,13 @@ h1 {
86
86
  }
87
87
  ```
88
88
 
89
- Checkout [tests](test) for more examples.
89
+ Checkout [tests](src/__tests__/index.js) for more examples.
90
90
 
91
91
  ### Options
92
92
 
93
93
  #### `precision` (default: `5`)
94
94
 
95
- Allow you to definine the precision for decimal numbers.
95
+ Allow you to define the precision for decimal numbers.
96
96
 
97
97
  ```js
98
98
  var out = postcss()
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _postcss = require('postcss');
8
+
9
+ var _transform = require('./lib/transform');
10
+
11
+ var _transform2 = _interopRequireDefault(_transform);
12
+
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+
15
+ exports.default = (0, _postcss.plugin)('postcss-calc', function (opts) {
16
+ var options = Object.assign({
17
+ precision: 5,
18
+ preserve: false,
19
+ warnWhenCannotResolve: false,
20
+ mediaQueries: false,
21
+ selectors: false
22
+ }, opts);
23
+
24
+ return function (css, result) {
25
+ css.walk(function (node) {
26
+ var type = node.type;
27
+
28
+ if (type === 'decl') (0, _transform2.default)(node, "value", options, result);
29
+ if (type === 'atrule' && options.mediaQueries) (0, _transform2.default)(node, "params", options, result);
30
+ if (type === 'rule' && options.selectors) (0, _transform2.default)(node, "selector", options, result);
31
+ });
32
+ };
33
+ });
34
+ module.exports = exports['default'];
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _postcssSelectorParser = require('postcss-selector-parser');
8
+
9
+ var _postcssSelectorParser2 = _interopRequireDefault(_postcssSelectorParser);
10
+
11
+ var _reduceCssCalc = require('reduce-css-calc');
12
+
13
+ var _reduceCssCalc2 = _interopRequireDefault(_reduceCssCalc);
14
+
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+
17
+ var MATCH_CALC = /((?:\-[a-z]+\-)?calc)/;
18
+
19
+ function transformValue(value, options, result, item) {
20
+ if (!value) {
21
+ return value;
22
+ }
23
+
24
+ var reduced = (0, _reduceCssCalc2.default)(value, options.precision);
25
+ // if the warnWhenCannotResolve option is on, inform the user that the calc
26
+ // expression could not be resolved to a single value
27
+ if (options.warnWhenCannotResolve && MATCH_CALC.test(reduced)) {
28
+ result.warn("Could not reduce expression: " + value, { plugin: 'postcss-calc', node: item });
29
+ }
30
+
31
+ return reduced;
32
+ }
33
+
34
+ function transformSelector(value, options, result, item) {
35
+ return (0, _postcssSelectorParser2.default)(function (selectors) {
36
+ selectors.walk(function (node) {
37
+ // attribute value
38
+ // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
39
+ if (node.type === 'attribute') {
40
+ var val = transformValue(node.raws.unquoted, options, result, item);
41
+ node.value = node.quoted ? '"' + val + '"' : val;
42
+ }
43
+
44
+ // tag value
45
+ // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
46
+ if (node.type === 'tag') node.value = transformValue(node.value, options, result, item);
47
+
48
+ return;
49
+ });
50
+ }).process(value).result.toString();
51
+ }
52
+
53
+ exports.default = function (node, property, options, result) {
54
+ var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
55
+
56
+ // if the preserve option is enabled and the value has changed, write the
57
+ // transformed value into a cloned node which is inserted before the current
58
+ // node, preserving the original value. Otherwise, overwrite the original
59
+ // value.
60
+ if (options.preserve && node[property] !== value) {
61
+ var clone = node.clone();
62
+ clone[property] = value;
63
+ node.parent.insertBefore(node, clone);
64
+ } else node[property] = value;
65
+ };
66
+
67
+ module.exports = exports['default'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-calc",
3
- "version": "5.3.0",
3
+ "version": "6.0.2",
4
4
  "description": "PostCSS plugin to reduce calc()",
5
5
  "keywords": [
6
6
  "css",
@@ -9,25 +9,46 @@
9
9
  "calculation",
10
10
  "calc"
11
11
  ],
12
+ "main": "dist/index.js",
13
+ "files": [
14
+ "dist",
15
+ "LICENSE"
16
+ ],
17
+ "scripts": {
18
+ "prepublish": "npm run build && del-cli dist/__tests__",
19
+ "build": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist",
20
+ "pretest": "eslint src && npm run build",
21
+ "test": "ava src/__tests__/"
22
+ },
12
23
  "author": "Maxime Thirouin",
13
24
  "license": "MIT",
14
25
  "repository": "https://github.com/postcss/postcss-calc.git",
15
- "files": [
16
- "index.js"
17
- ],
18
- "dependencies": {
19
- "postcss-message-helpers": "^2.0.0",
20
- "reduce-css-calc": "^1.2.0",
21
- "postcss": "^5.0.2"
26
+ "eslintConfig": {
27
+ "parser": "babel-eslint",
28
+ "extends": "eslint-config-i-am-meticulous"
22
29
  },
23
30
  "devDependencies": {
24
- "eslint": "^1.0.0",
25
- "npmpub": "^3.1.0",
26
- "postcss-custom-properties": "^5.0.0",
27
- "tape": "^3.0.0"
31
+ "ava": "^0.19.1",
32
+ "babel-cli": "^6.18.0",
33
+ "babel-core": "^6.21.0",
34
+ "babel-eslint": "^7.1.1",
35
+ "babel-plugin-add-module-exports": "^0.2.1",
36
+ "babel-preset-env": "^1.4.0",
37
+ "babel-register": "^6.18.0",
38
+ "cross-env": "^4.0.0",
39
+ "del-cli": "^0.2.1",
40
+ "eslint": "^3.12.2",
41
+ "eslint-config-i-am-meticulous": "^6.0.1",
42
+ "eslint-plugin-babel": "^4.0.0",
43
+ "eslint-plugin-import": "^2.2.0"
28
44
  },
29
- "scripts": {
30
- "test": "eslint . && tape test",
31
- "release": "npmpub"
45
+ "dependencies": {
46
+ "css-unit-converter": "^1.1.1",
47
+ "postcss": "^7.0.2",
48
+ "postcss-selector-parser": "^2.2.2",
49
+ "reduce-css-calc": "^2.0.0"
50
+ },
51
+ "ava": {
52
+ "require": "babel-register"
32
53
  }
33
54
  }
package/index.js DELETED
@@ -1,62 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
- var reduceCSSCalc = require("reduce-css-calc")
5
- var helpers = require("postcss-message-helpers")
6
- var postcss = require("postcss")
7
-
8
- var CONTAINS_CALC = /\bcalc\([\s\S]*?\)/
9
-
10
- /**
11
- * PostCSS plugin to reduce calc() function calls.
12
- */
13
- module.exports = postcss.plugin("postcss-calc", function(options) {
14
- options = options || {}
15
- var precision = options.precision
16
- var preserve = options.preserve
17
- var warnWhenCannotResolve = options.warnWhenCannotResolve
18
- var mediaQueries = options.mediaQueries
19
- var selectors = options.selectors
20
-
21
- return function(style, result) {
22
- function transformValue(node, property) {
23
- var value = node[property]
24
-
25
- if (!value || !CONTAINS_CALC.test(value)) {
26
- return
27
- }
28
-
29
- helpers.try(function transformCSSCalc() {
30
- var reducedValue = reduceCSSCalc(value, precision)
31
-
32
- if (warnWhenCannotResolve && CONTAINS_CALC.test(reducedValue)) {
33
- result.warn("Could not reduce expression: " + value,
34
- {plugin: "postcss-calc", node: node})
35
- }
36
-
37
- if (!preserve) {
38
- node[property] = reducedValue
39
- return
40
- }
41
-
42
- if (reducedValue != value) {
43
- var clone = node.clone()
44
- clone[property] = reducedValue
45
- node.parent.insertBefore(node, clone)
46
- }
47
- }, node.source)
48
- }
49
-
50
- style.walk(function(rule) {
51
- if (mediaQueries && rule.type === "atrule") {
52
- return transformValue(rule, "params")
53
- }
54
- else if (rule.type === "decl") {
55
- return transformValue(rule, "value")
56
- }
57
- else if (selectors && rule.type === "rule") {
58
- return transformValue(rule, "selector")
59
- }
60
- })
61
- }
62
- })