postcss-calc 5.2.1 → 6.0.1

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,21 @@
1
+ # 6.0.1 - 2017-10-10
2
+
3
+ - Fixed: throwing error for attribute selectors without a value
4
+
5
+ # 6.0.0 - 2017-05-08
6
+
7
+ - Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
8
+ to v2.x (thanks to @andyjansson).
9
+
10
+ # 5.3.1 - 2016-08-22
11
+
12
+ - Fixed: avoid security issue related to ``reduce-css-calc@< 1.2.4``.
13
+
14
+ # 5.3.0 - 2016-07-11
15
+
16
+ - Added: support for selector transformation via `selectors` option.
17
+ ([#29](https://github.com/postcss/postcss-calc/pull/29) - @uniquegestaltung)
18
+
1
19
  # 5.2.1 - 2016-04-10
2
20
 
3
21
  - Fixed: support for multiline value
package/README.md CHANGED
@@ -92,7 +92,7 @@ Checkout [tests](test) for more examples.
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()
@@ -134,6 +134,25 @@ var out = postcss()
134
134
  .css
135
135
  ```
136
136
 
137
+ #### `selectors` (default: `false`)
138
+
139
+ Allows calc() usage as part of selectors.
140
+
141
+ ```js
142
+ var out = postcss()
143
+ .use(calc({selectors: true}))
144
+ .process(css)
145
+ .css
146
+ ```
147
+
148
+ Example:
149
+
150
+ ```css
151
+ div[data-size="calc(3*3)"] {
152
+ width: 100px;
153
+ }
154
+ ```
155
+
137
156
  ---
138
157
 
139
158
  ## Contributing
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.2.1",
3
+ "version": "6.0.1",
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": "^6.0.0",
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,58 +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
-
20
- return function(style, result) {
21
- function transformValue(node, property) {
22
- var value = node[property]
23
-
24
- if (!value || !CONTAINS_CALC.test(value)) {
25
- return
26
- }
27
-
28
- helpers.try(function transformCSSCalc() {
29
- var reducedValue = reduceCSSCalc(value, precision)
30
-
31
- if (warnWhenCannotResolve && CONTAINS_CALC.test(reducedValue)) {
32
- result.warn("Could not reduce expression: " + value,
33
- {plugin: "postcss-calc", node: node})
34
- }
35
-
36
- if (!preserve) {
37
- node[property] = reducedValue
38
- return
39
- }
40
-
41
- if (reducedValue != value) {
42
- var clone = node.clone()
43
- clone[property] = reducedValue
44
- node.parent.insertBefore(node, clone)
45
- }
46
- }, node.source)
47
- }
48
-
49
- style.walk(function(rule) {
50
- if (mediaQueries && rule.type === "atrule") {
51
- return transformValue(rule, "params")
52
- }
53
- else if (rule.type === "decl") {
54
- return transformValue(rule, "value")
55
- }
56
- })
57
- }
58
- })