taro-css-to-react-native 3.5.0-canary.1 → 3.5.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.
@@ -9,7 +9,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
9
9
 
10
10
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
11
11
 
12
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
12
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
13
 
14
14
  var SYMBOL_MATCH = 'SYMBOL_MATCH';
15
15
 
@@ -3,22 +3,22 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = exports.getPropertyName = exports.getStylesForProperty = exports.transformRawValue = void 0;
7
-
8
- var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
6
+ exports.transformRawValue = exports.getStylesForProperty = exports.getPropertyName = exports["default"] = void 0;
9
7
 
10
8
  var _camelize = _interopRequireDefault(require("camelize"));
11
9
 
12
- var _index = _interopRequireDefault(require("./transforms/index"));
10
+ var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
13
11
 
14
12
  var _TokenStream = _interopRequireDefault(require("./TokenStream"));
15
13
 
14
+ var _index = _interopRequireDefault(require("./transforms/index"));
15
+
16
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
17
17
 
18
18
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
19
 
20
20
  // Note if this is wrong, you'll need to change tokenTypes.js too
21
- var numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i;
21
+ var numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?:px)|(?:vw$)|(?:vh$)|(?:vmin$)|(?:vmax$))?$/i;
22
22
  var boolRe = /^true|false$/i;
23
23
  var nullRe = /^null$/i;
24
24
  var undefinedRe = /^undefined$/i; // Undocumented export
@@ -29,8 +29,12 @@ var transformRawValue = function transformRawValue(input) {
29
29
 
30
30
  if (numberMatch !== null) {
31
31
  var num = Number(numberMatch[1]);
32
+ var unit = numberMatch[2];
33
+ var isViewportUnit = ['vw', 'vh', 'vmin', 'vmax'].includes(unit);
32
34
 
33
- if (/(\d+)px/.test(value)) {
35
+ if (isViewportUnit) {
36
+ return "scaleVu2dp(".concat(num, ", '").concat(unit, "')");
37
+ } else if (/(\d+)px/.test(value)) {
34
38
  return "scalePx2dp(".concat(num, ")");
35
39
  } else {
36
40
  return num;
@@ -5,12 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.tokens = exports.regExpToken = void 0;
7
7
 
8
- var _postcssValueParser = require("postcss-value-parser");
9
-
10
8
  var _cssColorKeywords = _interopRequireDefault(require("css-color-keywords"));
11
9
 
10
+ var _postcssValueParser = require("postcss-value-parser");
11
+
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
13
 
14
+ /* eslint-disable react/display-name */
14
15
  var matchString = function matchString(node) {
15
16
  if (node.type !== 'string') return null;
16
17
  return node.value.replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, function (match, charCode) {
@@ -37,10 +38,11 @@ var identRe = /(^-?[_a-z][_a-z0-9-]*$)/i; // Note if these are wrong, you'll nee
37
38
 
38
39
  var numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/; // Note lengthRe is sneaky: you can omit units for 0
39
40
 
40
- var lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?=px$)|(?=Px$)|(?=PX$)|(?=pX$)))/;
41
- var unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|rem|vh|vw|vmin|vmax|cm|mm|in|pc|pt))$/;
41
+ var lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?=px$)|(?=Px$)|(?=PX$)|(?=pX$)|(?=vw$)|(?=vh$)|(?=vmin$)|(?=vmax$)))/;
42
+ var unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|rem|cm|mm|in|pc|pt))$/;
42
43
  var angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/;
43
44
  var percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
45
+ var viewportUnitRe = /(\d+)(vw|vh|vmin|vmax)/;
44
46
 
45
47
  var noopToken = function noopToken(predicate) {
46
48
  return function (node) {
@@ -54,15 +56,39 @@ var valueForTypeToken = function valueForTypeToken(type) {
54
56
  };
55
57
  };
56
58
 
59
+ var functionValueForTypeToken = function functionValueForTypeToken(type) {
60
+ return function (node) {
61
+ if (node.type === type) {
62
+ // handle rgb(a) function value
63
+ if (/^rgba?$/i.test(node.value)) {
64
+ var result = node.nodes.filter(function (token) {
65
+ return token.type === 'word';
66
+ }).map(function (token) {
67
+ return token.value;
68
+ });
69
+ return "".concat(node.value, "(").concat(result.join(', '), ")");
70
+ }
71
+ }
72
+
73
+ return null;
74
+ };
75
+ };
76
+
57
77
  var regExpToken = function regExpToken(regExp) {
58
78
  var transform = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String;
59
79
  return function (node) {
80
+ var _node$value$match;
81
+
60
82
  if (node.type !== 'word') return null;
61
83
  var match = node.value.match(regExp);
62
84
  if (match === null) return null;
63
85
  var value = transform(match[1]);
86
+ var unit = (_node$value$match = node.value.match(viewportUnitRe)) === null || _node$value$match === void 0 ? void 0 : _node$value$match[2];
87
+ var isViewportUnit = ['vh', 'vw', 'vmin', 'vmax'].includes(unit);
64
88
 
65
- if (/(\d+)px/.test(node.value)) {
89
+ if (isViewportUnit) {
90
+ return "scaleVu2dp(".concat(value, ", '").concat(unit, "')");
91
+ } else if (/(\d+)px/.test(node.value)) {
66
92
  return "scalePx2dp(".concat(value, ")");
67
93
  } else {
68
94
  return value;
@@ -82,6 +108,7 @@ var tokens = {
82
108
  return node.type === 'div' && node.value === ',';
83
109
  }),
84
110
  WORD: valueForTypeToken('word'),
111
+ FUNC: functionValueForTypeToken('function'),
85
112
  NONE: regExpToken(noneRe),
86
113
  AUTO: regExpToken(autoRe),
87
114
  NUMBER: regExpToken(numberRe, Number),
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.borderWidth = exports.borderTop = exports.borderRight = exports.borderRadius = exports.borderLeft = exports.borderColor = exports.borderBottom = exports.border = void 0;
7
+
8
+ var _tokenTypes = require("../tokenTypes");
9
+
10
+ var _util = require("./util");
11
+
12
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
+
14
+ var WORD = _tokenTypes.tokens.WORD,
15
+ FUNC = _tokenTypes.tokens.FUNC,
16
+ COLOR = _tokenTypes.tokens.COLOR,
17
+ LENGTH = _tokenTypes.tokens.LENGTH,
18
+ UNSUPPORTED_LENGTH_UNIT = _tokenTypes.tokens.UNSUPPORTED_LENGTH_UNIT;
19
+
20
+ function borderDirectionFactory() {
21
+ var _anyOrderFactory;
22
+
23
+ var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
24
+ var prefix = "border".concat(direction);
25
+ return (0, _util.anyOrderFactory)((_anyOrderFactory = {}, _defineProperty(_anyOrderFactory, "".concat(prefix, "Width"), {
26
+ tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
27
+ "default": 1
28
+ }), _defineProperty(_anyOrderFactory, "".concat(prefix, "Color"), {
29
+ tokens: [COLOR],
30
+ "default": 'black'
31
+ }), _defineProperty(_anyOrderFactory, "".concat(prefix, "Style"), {
32
+ tokens: [(0, _tokenTypes.regExpToken)(/^(solid|dashed|dotted)$/)],
33
+ "default": 'solid'
34
+ }), _anyOrderFactory));
35
+ }
36
+
37
+ var border = borderDirectionFactory();
38
+ exports.border = border;
39
+ var borderTop = borderDirectionFactory('Top');
40
+ exports.borderTop = borderTop;
41
+ var borderRight = borderDirectionFactory('Right');
42
+ exports.borderRight = borderRight;
43
+ var borderBottom = borderDirectionFactory('Bottom');
44
+ exports.borderBottom = borderBottom;
45
+ var borderLeft = borderDirectionFactory('Left');
46
+ exports.borderLeft = borderLeft;
47
+ var borderColor = (0, _util.directionFactory)({
48
+ types: [WORD, FUNC],
49
+ prefix: 'border',
50
+ suffix: 'Color'
51
+ });
52
+ exports.borderColor = borderColor;
53
+ var borderRadius = (0, _util.directionFactory)({
54
+ directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
55
+ prefix: 'border',
56
+ suffix: 'Radius'
57
+ });
58
+ exports.borderRadius = borderRadius;
59
+ var borderWidth = (0, _util.directionFactory)({
60
+ prefix: 'border',
61
+ suffix: 'Width'
62
+ });
63
+ exports.borderWidth = borderWidth;
@@ -5,10 +5,10 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = void 0;
7
7
 
8
- var _fontFamily = _interopRequireDefault(require("./fontFamily"));
9
-
10
8
  var _tokenTypes = require("../tokenTypes");
11
9
 
10
+ var _fontFamily = _interopRequireDefault(require("./fontFamily"));
11
+
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
13
 
14
14
  var SPACE = _tokenTypes.tokens.SPACE,
@@ -7,6 +7,8 @@ exports["default"] = void 0;
7
7
 
8
8
  var _tokenTypes = require("../tokenTypes");
9
9
 
10
+ var _border = require("./border");
11
+
10
12
  var _boxShadow = _interopRequireDefault(require("./boxShadow"));
11
13
 
12
14
  var _flex = _interopRequireDefault(require("./flex"));
@@ -15,12 +17,12 @@ var _font = _interopRequireDefault(require("./font"));
15
17
 
16
18
  var _fontFamily = _interopRequireDefault(require("./fontFamily"));
17
19
 
18
- var _textShadow = _interopRequireDefault(require("./textShadow"));
19
-
20
20
  var _textDecoration = _interopRequireDefault(require("./textDecoration"));
21
21
 
22
22
  var _textDecorationLine = _interopRequireDefault(require("./textDecorationLine"));
23
23
 
24
+ var _textShadow = _interopRequireDefault(require("./textShadow"));
25
+
24
26
  var _transform = _interopRequireDefault(require("./transform"));
25
27
 
26
28
  var _util = require("./util");
@@ -43,34 +45,6 @@ var background = function background(tokenStream) {
43
45
  };
44
46
  };
45
47
 
46
- var border = (0, _util.anyOrderFactory)({
47
- borderWidth: {
48
- tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
49
- "default": 1
50
- },
51
- borderColor: {
52
- tokens: [COLOR],
53
- "default": 'black'
54
- },
55
- borderStyle: {
56
- tokens: [(0, _tokenTypes.regExpToken)(/^(solid|dashed|dotted)$/)],
57
- "default": 'solid'
58
- }
59
- });
60
- var borderColor = (0, _util.directionFactory)({
61
- types: [WORD],
62
- prefix: 'border',
63
- suffix: 'Color'
64
- });
65
- var borderRadius = (0, _util.directionFactory)({
66
- directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
67
- prefix: 'border',
68
- suffix: 'Radius'
69
- });
70
- var borderWidth = (0, _util.directionFactory)({
71
- prefix: 'border',
72
- suffix: 'Width'
73
- });
74
48
  var margin = (0, _util.directionFactory)({
75
49
  types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
76
50
  prefix: 'margin'
@@ -102,10 +76,14 @@ var shadowOffset = (0, _util.shadowOffsetFactory)();
102
76
  var textShadowOffset = (0, _util.shadowOffsetFactory)();
103
77
  var _default = {
104
78
  background: background,
105
- border: border,
106
- borderColor: borderColor,
107
- borderRadius: borderRadius,
108
- borderWidth: borderWidth,
79
+ border: _border.border,
80
+ borderTop: _border.borderTop,
81
+ borderRight: _border.borderRight,
82
+ borderBottom: _border.borderBottom,
83
+ borderLeft: _border.borderLeft,
84
+ borderColor: _border.borderColor,
85
+ borderRadius: _border.borderRadius,
86
+ borderWidth: _border.borderWidth,
109
87
  boxShadow: _boxShadow["default"],
110
88
  flex: _flex["default"],
111
89
  flexFlow: flexFlow,
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.parseShadow = exports.shadowOffsetFactory = exports.anyOrderFactory = exports.directionFactory = void 0;
6
+ exports.shadowOffsetFactory = exports.parseShadow = exports.directionFactory = exports.anyOrderFactory = void 0;
7
7
 
8
8
  var _tokenTypes = require("../tokenTypes");
9
9
 
package/dist/index.js CHANGED
@@ -3,20 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports["default"] = void 0;
6
7
  Object.defineProperty(exports, "transformCSS", {
7
8
  enumerable: true,
8
9
  get: function get() {
9
10
  return _cssToReactNative["default"];
10
11
  }
11
12
  });
12
- exports["default"] = void 0;
13
+
14
+ var _parse = _interopRequireDefault(require("css/lib/parse"));
13
15
 
14
16
  var _cssMediaquery = _interopRequireDefault(require("css-mediaquery"));
15
17
 
16
18
  var _cssToReactNative = _interopRequireDefault(require("./css-to-react-native"));
17
19
 
18
- var _parse = _interopRequireDefault(require("css/lib/parse"));
19
-
20
20
  var _features = require("./transforms/media-queries/features");
21
21
 
22
22
  var _types = require("./transforms/media-queries/types");
@@ -53,7 +53,8 @@ var transformDecls = function transformDecls(styles, declarations, result) {
53
53
  var isUnsupportedUnit = unsupportedUnitRe.test(value);
54
54
 
55
55
  if (property === 'line-height' && !isLengthUnit && !isViewportUnit && !isPercent && !isUnsupportedUnit) {
56
- throw new Error("Failed to parse declaration \"".concat(property, ": ").concat(value, "\""));
56
+ // ignore invalid value avoid throw error cause app crash
57
+ continue;
57
58
  }
58
59
 
59
60
  if (!result.__viewportUnits && isViewportUnit) {
@@ -62,8 +63,13 @@ var transformDecls = function transformDecls(styles, declarations, result) {
62
63
  // do not be wrapped by scalePx2dp function
63
64
 
64
65
 
65
- if (isLengthUnit && typeof options.scalable === 'boolean' && !options.scalable && /(\d+)px/.test(value)) {
66
- value = value.replace(/(\d+)px/, '$1PX');
66
+ if (typeof options.scalable === 'boolean' && !options.scalable && /(\d+)px/.test(value)) {
67
+ value = value.replace(/(\d+)px/g, '$1PX');
68
+ } // expect value is legal so that remove !import
69
+
70
+
71
+ if (/!import/i.test(value)) {
72
+ value = value.replace(/!import/, '');
67
73
  }
68
74
 
69
75
  if (shorthandBorderProps.indexOf(property) > -1) {
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.mediaQueryTypes = exports.reactNativeMediaQueryTypes = exports.cssnextMediaQueryTypes = exports.defaultTypes = void 0;
6
+ exports.reactNativeMediaQueryTypes = exports.mediaQueryTypes = exports.defaultTypes = exports.cssnextMediaQueryTypes = void 0;
7
7
  var defaultTypes = ['all', 'braille', 'embossed', 'handheld', 'print', 'projection', 'screen', 'speech', 'tty', 'tv'];
8
8
  exports.defaultTypes = defaultTypes;
9
9
  var cssnextMediaQueryTypes = ['pointer', 'hover', 'block-overflow'];
@@ -9,7 +9,7 @@ function camelCase(str) {
9
9
  // Lower cases the string
10
10
  return str.toLowerCase() // Replaces any - or _ characters with a space
11
11
  .replace(/[-_]+/g, ' ') // Removes any non alphanumeric characters
12
- .replace(/[^\w\s]/g, '') // Uppercases the first character in each group immediately following a space
12
+ .replace(/[^\w\s]/g, '') // Uppercase the first character in each group immediately following a space
13
13
  // (delimited by spaces)
14
14
  .replace(/ (.)/g, function ($1) {
15
15
  return $1.toUpperCase();
package/package.json CHANGED
@@ -1,20 +1,16 @@
1
1
  {
2
2
  "name": "taro-css-to-react-native",
3
3
  "description": "Convert CSS text to a React Native stylesheet object",
4
- "version": "3.5.0-canary.1",
4
+ "version": "3.5.0",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "dependencies": {
8
8
  "camelize": "^1.0.0",
9
- "css": "^2.2.4",
9
+ "css": "^3.0.0",
10
10
  "css-color-keywords": "^1.0.0",
11
11
  "css-mediaquery": "^0.1.2",
12
12
  "postcss-value-parser": "^3.3.0"
13
13
  },
14
- "scripts": {
15
- "build": "babel src --ignore *.spec.js --out-dir ./dist",
16
- "test": "jest"
17
- },
18
14
  "repository": {
19
15
  "type": "git",
20
16
  "url": "git+https://github.com/NervJS/taro.git"
@@ -47,15 +43,6 @@
47
43
  "<rootDir>/node_modules"
48
44
  ]
49
45
  },
50
- "lint-staged": {
51
- "*.{js,json,md}": [
52
- "prettier --write",
53
- "git add"
54
- ]
55
- },
56
- "prettier": {
57
- "trailingComma": "all"
58
- },
59
46
  "files": [
60
47
  "dist",
61
48
  "src",
@@ -63,5 +50,8 @@
63
50
  "CHANGELOG.md",
64
51
  "README.md"
65
52
  ],
66
- "gitHead": "c61624d2f763e6d31e67d6cf9c564efc8b0d0887"
67
- }
53
+ "scripts": {
54
+ "build": "babel src --ignore *.spec.js --out-dir ./dist",
55
+ "test": "jest"
56
+ }
57
+ }
@@ -1,23 +1,29 @@
1
1
  /* eslint-disable no-param-reassign */
2
- import parse from 'postcss-value-parser'
3
2
  import camelizeStyleName from 'camelize'
4
- import transforms from './transforms/index'
3
+ import parse from 'postcss-value-parser'
4
+
5
5
  import TokenStream from './TokenStream'
6
+ import transforms from './transforms/index'
6
7
 
7
8
  // Note if this is wrong, you'll need to change tokenTypes.js too
8
- const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i
9
+ const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?:px)|(?:vw$)|(?:vh$)|(?:vmin$)|(?:vmax$))?$/i
9
10
  const boolRe = /^true|false$/i
10
11
  const nullRe = /^null$/i
11
12
  const undefinedRe = /^undefined$/i
12
13
 
13
14
  // Undocumented export
14
- export const transformRawValue = input => {
15
+ export const transformRawValue = (input) => {
15
16
  const value = input.trim()
16
17
 
17
18
  const numberMatch = value.match(numberOrLengthRe)
18
19
  if (numberMatch !== null) {
19
20
  const num = Number(numberMatch[1])
20
- if (/(\d+)px/.test(value)) {
21
+ const unit = numberMatch[2]
22
+ const isViewportUnit = ['vw', 'vh', 'vmin', 'vmax'].includes(unit)
23
+
24
+ if (isViewportUnit) {
25
+ return `scaleVu2dp(${num}, '${unit}')`
26
+ } else if (/(\d+)px/.test(value)) {
21
27
  return `scalePx2dp(${num})`
22
28
  } else {
23
29
  return num
@@ -47,7 +53,9 @@ const checkBaseTransformShorthandValue = (propName, inputValue) => {
47
53
  try {
48
54
  return baseTransformShorthandValue(propName, inputValue)
49
55
  } catch (e) {
50
- throw new Error(`${e.message} Failed to parse declaration "${propName}: ${inputValue}"`)
56
+ throw new Error(
57
+ `${e.message} Failed to parse declaration "${propName}: ${inputValue}"`,
58
+ )
51
59
  }
52
60
  }
53
61
 
@@ -58,11 +66,15 @@ const transformShorthandValue =
58
66
 
59
67
  export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
60
68
  const isRawValue = allowShorthand === false || !(propName in transforms)
61
- const propValue = isRawValue ? transformRawValue(inputValue) : transformShorthandValue(propName, inputValue.trim())
62
- return propValue && propValue.$merge ? propValue.$merge : { [propName]: propValue }
69
+ const propValue = isRawValue
70
+ ? transformRawValue(inputValue)
71
+ : transformShorthandValue(propName, inputValue.trim())
72
+ return propValue && propValue.$merge
73
+ ? propValue.$merge
74
+ : { [propName]: propValue }
63
75
  }
64
76
 
65
- export const getPropertyName = propName => {
77
+ export const getPropertyName = (propName) => {
66
78
  const isCustomProp = /^--\w+/.test(propName)
67
79
  if (isCustomProp) {
68
80
  return propName
@@ -75,5 +87,8 @@ export default (rules, shorthandBlacklist = []) =>
75
87
  const propertyName = getPropertyName(rule[0])
76
88
  const value = rule[1]
77
89
  const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1
78
- return Object.assign(accum, getStylesForProperty(propertyName, value, allowShorthand))
90
+ return Object.assign(
91
+ accum,
92
+ getStylesForProperty(propertyName, value, allowShorthand),
93
+ )
79
94
  }, {})
@@ -1,20 +1,25 @@
1
- import { stringify } from 'postcss-value-parser'
1
+ /* eslint-disable react/display-name */
2
2
  import cssColorKeywords from 'css-color-keywords'
3
+ import { stringify } from 'postcss-value-parser'
3
4
 
4
- const matchString = node => {
5
+ const matchString = (node) => {
5
6
  if (node.type !== 'string') return null
6
7
  return node.value
7
- .replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, (match, charCode) => String.fromCharCode(parseInt(charCode, 16)))
8
+ .replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, (match, charCode) =>
9
+ String.fromCharCode(parseInt(charCode, 16)),
10
+ )
8
11
  .replace(/\\/g, '')
9
12
  }
10
13
 
11
14
  const hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i
12
15
  const cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/
13
16
 
14
- const matchColor = node => {
17
+ const matchColor = (node) => {
15
18
  if (
16
19
  node.type === 'word' &&
17
- (hexColorRe.test(node.value) || node.value in cssColorKeywords || node.value === 'transparent')
20
+ (hexColorRe.test(node.value) ||
21
+ node.value in cssColorKeywords ||
22
+ node.value === 'transparent')
18
23
  ) {
19
24
  return node.value
20
25
  } else if (node.type === 'function' && cssFunctionNameRe.test(node.value)) {
@@ -29,34 +34,58 @@ const identRe = /(^-?[_a-z][_a-z0-9-]*$)/i
29
34
  // Note if these are wrong, you'll need to change index.js too
30
35
  const numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/
31
36
  // Note lengthRe is sneaky: you can omit units for 0
32
- const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?=px$)|(?=Px$)|(?=PX$)|(?=pX$)))/
33
- const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|rem|vh|vw|vmin|vmax|cm|mm|in|pc|pt))$/
37
+ const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?=px$)|(?=Px$)|(?=PX$)|(?=pX$)|(?=vw$)|(?=vh$)|(?=vmin$)|(?=vmax$)))/
38
+ const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|rem|cm|mm|in|pc|pt))$/
34
39
  const angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/
35
40
  const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/
41
+ const viewportUnitRe = /(\d+)(vw|vh|vmin|vmax)/
36
42
 
37
- const noopToken = predicate => node => (predicate(node) ? '<token>' : null)
43
+ const noopToken = (predicate) => (node) => predicate(node) ? '<token>' : null
38
44
 
39
- const valueForTypeToken = type => node => (node.type === type ? node.value : null)
45
+ const valueForTypeToken = (type) => (node) =>
46
+ node.type === type ? node.value : null
40
47
 
41
- export const regExpToken = (regExp, transform = String) => node => {
42
- if (node.type !== 'word') return null
48
+ const functionValueForTypeToken = (type) => (node) => {
49
+ if (node.type === type) {
50
+ // handle rgb(a) function value
51
+ if (/^rgba?$/i.test(node.value)) {
52
+ const result = node.nodes
53
+ .filter((token) => token.type === 'word')
54
+ .map((token) => token.value)
43
55
 
44
- const match = node.value.match(regExp)
45
- if (match === null) return null
46
-
47
- const value = transform(match[1])
48
- if (/(\d+)px/.test(node.value)) {
49
- return `scalePx2dp(${value})`
50
- } else {
51
- return value
56
+ return `${node.value}(${result.join(', ')})`
57
+ }
52
58
  }
59
+ return null
53
60
  }
54
61
 
62
+ export const regExpToken =
63
+ (regExp, transform = String) =>
64
+ (node) => {
65
+ if (node.type !== 'word') return null
66
+
67
+ const match = node.value.match(regExp)
68
+ if (match === null) return null
69
+
70
+ const value = transform(match[1])
71
+ const unit = node.value.match(viewportUnitRe)?.[2]
72
+ const isViewportUnit = ['vh', 'vw', 'vmin', 'vmax'].includes(unit)
73
+
74
+ if (isViewportUnit) {
75
+ return `scaleVu2dp(${value}, '${unit}')`
76
+ } else if (/(\d+)px/.test(node.value)) {
77
+ return `scalePx2dp(${value})`
78
+ } else {
79
+ return value
80
+ }
81
+ }
82
+
55
83
  export const tokens = {
56
- SPACE: noopToken(node => node.type === 'space'),
57
- SLASH: noopToken(node => node.type === 'div' && node.value === '/'),
58
- COMMA: noopToken(node => node.type === 'div' && node.value === ','),
84
+ SPACE: noopToken((node) => node.type === 'space'),
85
+ SLASH: noopToken((node) => node.type === 'div' && node.value === '/'),
86
+ COMMA: noopToken((node) => node.type === 'div' && node.value === ','),
59
87
  WORD: valueForTypeToken('word'),
88
+ FUNC: functionValueForTypeToken('function'),
60
89
  NONE: regExpToken(noneRe),
61
90
  AUTO: regExpToken(autoRe),
62
91
  NUMBER: regExpToken(numberRe, Number),
@@ -67,5 +96,5 @@ export const tokens = {
67
96
  IDENT: regExpToken(identRe),
68
97
  STRING: matchString,
69
98
  COLOR: matchColor,
70
- LINE: regExpToken(/^(none|underline|line-through)$/i)
99
+ LINE: regExpToken(/^(none|underline|line-through)$/i),
71
100
  }
@@ -0,0 +1,46 @@
1
+ import { regExpToken, tokens } from '../tokenTypes'
2
+ import { anyOrderFactory, directionFactory } from './util'
3
+
4
+ const { WORD, FUNC, COLOR, LENGTH, UNSUPPORTED_LENGTH_UNIT } = tokens
5
+
6
+ function borderDirectionFactory (direction = '') {
7
+ const prefix = `border${direction}`
8
+ return anyOrderFactory({
9
+ [`${prefix}Width`]: {
10
+ tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
11
+ default: 1,
12
+ },
13
+ [`${prefix}Color`]: {
14
+ tokens: [COLOR],
15
+ default: 'black',
16
+ },
17
+ [`${prefix}Style`]: {
18
+ tokens: [regExpToken(/^(solid|dashed|dotted)$/)],
19
+ default: 'solid',
20
+ },
21
+ })
22
+ }
23
+
24
+ export const border = borderDirectionFactory()
25
+
26
+ export const borderTop = borderDirectionFactory('Top')
27
+ export const borderRight = borderDirectionFactory('Right')
28
+ export const borderBottom = borderDirectionFactory('Bottom')
29
+ export const borderLeft = borderDirectionFactory('Left')
30
+
31
+ export const borderColor = directionFactory({
32
+ types: [WORD, FUNC],
33
+ prefix: 'border',
34
+ suffix: 'Color',
35
+ })
36
+
37
+ export const borderRadius = directionFactory({
38
+ directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
39
+ prefix: 'border',
40
+ suffix: 'Radius',
41
+ })
42
+
43
+ export const borderWidth = directionFactory({
44
+ prefix: 'border',
45
+ suffix: 'Width',
46
+ })
@@ -1,5 +1,5 @@
1
- import parseFontFamily from './fontFamily'
2
1
  import { regExpToken, tokens } from '../tokenTypes'
2
+ import parseFontFamily from './fontFamily'
3
3
 
4
4
  const { SPACE, LENGTH, UNSUPPORTED_LENGTH_UNIT, NUMBER, SLASH } = tokens
5
5
  const NORMAL = regExpToken(/^(normal)$/)
@@ -11,7 +11,7 @@ const defaultFontStyle = 'normal'
11
11
  const defaultFontWeight = 'normal'
12
12
  const defaultFontVariant = []
13
13
 
14
- export default tokenStream => {
14
+ export default (tokenStream) => {
15
15
  let fontStyle
16
16
  let fontWeight
17
17
  let fontVariant
@@ -25,9 +25,15 @@ export default tokenStream => {
25
25
  /* pass */
26
26
  } else if (typeof fontStyle === 'undefined' && tokenStream.matches(STYLE)) {
27
27
  fontStyle = tokenStream.lastValue
28
- } else if (typeof fontWeight === 'undefined' && tokenStream.matches(WEIGHT)) {
28
+ } else if (
29
+ typeof fontWeight === 'undefined' &&
30
+ tokenStream.matches(WEIGHT)
31
+ ) {
29
32
  fontWeight = tokenStream.lastValue
30
- } else if (typeof fontVariant === 'undefined' && tokenStream.matches(VARIANT)) {
33
+ } else if (
34
+ typeof fontVariant === 'undefined' &&
35
+ tokenStream.matches(VARIANT)
36
+ ) {
31
37
  fontVariant = [tokenStream.lastValue]
32
38
  } else {
33
39
  break
@@ -41,7 +47,10 @@ export default tokenStream => {
41
47
 
42
48
  if (tokenStream.matches(SLASH)) {
43
49
  if (tokenStream.matches(NUMBER)) {
44
- const size = typeof fontSize === 'string' ? fontSize.replace(/scalePx2dp\((\d+)\)/, '$1') : fontSize
50
+ const size =
51
+ typeof fontSize === 'string'
52
+ ? fontSize.replace(/scalePx2dp\((\d+)\)/, '$1')
53
+ : fontSize
45
54
  lineHeight = size * tokenStream.lastValue
46
55
  } else {
47
56
  lineHeight = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT)
@@ -1,75 +1,57 @@
1
1
  import { regExpToken, tokens } from '../tokenTypes'
2
+ import {
3
+ border,
4
+ borderBottom,
5
+ borderColor,
6
+ borderLeft,
7
+ borderRadius,
8
+ borderRight,
9
+ borderTop,
10
+ borderWidth,
11
+ } from './border'
2
12
  import boxShadow from './boxShadow'
3
13
  import flex from './flex'
4
14
  import font from './font'
5
15
  import fontFamily from './fontFamily'
6
- import textShadow from './textShadow'
7
16
  import textDecoration from './textDecoration'
8
17
  import textDecorationLine from './textDecorationLine'
18
+ import textShadow from './textShadow'
9
19
  import transform from './transform'
10
- import { directionFactory, anyOrderFactory, shadowOffsetFactory } from './util'
20
+ import { anyOrderFactory, directionFactory, shadowOffsetFactory } from './util'
11
21
 
12
- const {
13
- IDENT,
14
- WORD,
15
- COLOR,
16
- LENGTH,
17
- UNSUPPORTED_LENGTH_UNIT,
18
- PERCENT,
19
- AUTO
20
- } = tokens
22
+ const { IDENT, WORD, COLOR, LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO } =
23
+ tokens
21
24
 
22
- const background = tokenStream => ({
23
- $merge: { backgroundColor: tokenStream.expect(COLOR) }
25
+ const background = (tokenStream) => ({
26
+ $merge: { backgroundColor: tokenStream.expect(COLOR) },
24
27
  })
25
- const border = anyOrderFactory({
26
- borderWidth: {
27
- tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
28
- default: 1
29
- },
30
- borderColor: {
31
- tokens: [COLOR],
32
- default: 'black'
33
- },
34
- borderStyle: {
35
- tokens: [regExpToken(/^(solid|dashed|dotted)$/)],
36
- default: 'solid'
37
- }
38
- })
39
- const borderColor = directionFactory({
40
- types: [WORD],
41
- prefix: 'border',
42
- suffix: 'Color'
43
- })
44
- const borderRadius = directionFactory({
45
- directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
46
- prefix: 'border',
47
- suffix: 'Radius'
48
- })
49
- const borderWidth = directionFactory({ prefix: 'border', suffix: 'Width' })
50
28
  const margin = directionFactory({
51
29
  types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
52
- prefix: 'margin'
30
+ prefix: 'margin',
53
31
  })
54
32
  const padding = directionFactory({ prefix: 'padding' })
55
33
  const flexFlow = anyOrderFactory({
56
34
  flexWrap: {
57
35
  tokens: [regExpToken(/(nowrap|wrap|wrap-reverse)/)],
58
- default: 'nowrap'
36
+ default: 'nowrap',
59
37
  },
60
38
  flexDirection: {
61
39
  tokens: [regExpToken(/(row|row-reverse|column|column-reverse)/)],
62
- default: 'row'
63
- }
40
+ default: 'row',
41
+ },
64
42
  })
65
- const fontVariant = tokenStream => [tokenStream.expect(IDENT)]
66
- const fontWeight = tokenStream => tokenStream.expect(WORD) // Also match numbers as strings
43
+ const fontVariant = (tokenStream) => [tokenStream.expect(IDENT)]
44
+ const fontWeight = (tokenStream) => tokenStream.expect(WORD) // Also match numbers as strings
67
45
  const shadowOffset = shadowOffsetFactory()
68
46
  const textShadowOffset = shadowOffsetFactory()
69
47
 
70
48
  export default {
71
49
  background,
72
50
  border,
51
+ borderTop,
52
+ borderRight,
53
+ borderBottom,
54
+ borderLeft,
73
55
  borderColor,
74
56
  borderRadius,
75
57
  borderWidth,
@@ -87,5 +69,5 @@ export default {
87
69
  textShadowOffset,
88
70
  textDecoration,
89
71
  textDecorationLine,
90
- transform
72
+ transform,
91
73
  }
package/src/index.js CHANGED
@@ -1,9 +1,10 @@
1
+ import parseCSS from 'css/lib/parse'
1
2
  import mediaQuery from 'css-mediaquery'
3
+
2
4
  import transformCSS from './css-to-react-native'
3
- import parseCSS from 'css/lib/parse'
4
5
  import {
5
6
  dimensionFeatures,
6
- mediaQueryFeatures
7
+ mediaQueryFeatures,
7
8
  } from './transforms/media-queries/features'
8
9
  import { mediaQueryTypes } from './transforms/media-queries/types'
9
10
  import { remToPx } from './transforms/rem'
@@ -15,12 +16,13 @@ import { values } from './utils/values'
15
16
  const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px|rem$))/
16
17
  const viewportUnitRe = /^([+-]?[0-9.]+)(vh|vw|vmin|vmax)$/
17
18
  const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/
18
- const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/
19
+ const unsupportedUnitRe =
20
+ /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/
19
21
  const shorthandBorderProps = [
20
22
  'border-radius',
21
23
  'border-width',
22
24
  'border-color',
23
- 'border-style'
25
+ 'border-style',
24
26
  ]
25
27
 
26
28
  const transformDecls = (styles, declarations, result, options = {}) => {
@@ -43,7 +45,8 @@ const transformDecls = (styles, declarations, result, options = {}) => {
43
45
  !isPercent &&
44
46
  !isUnsupportedUnit
45
47
  ) {
46
- throw new Error(`Failed to parse declaration "${property}: ${value}"`)
48
+ // ignore invalid value avoid throw error cause app crash
49
+ continue
47
50
  }
48
51
 
49
52
  if (!result.__viewportUnits && isViewportUnit) {
@@ -52,12 +55,15 @@ const transformDecls = (styles, declarations, result, options = {}) => {
52
55
  // scalable option, when it is false, transform single value 'px' unit to 'PX'
53
56
  // do not be wrapped by scalePx2dp function
54
57
  if (
55
- isLengthUnit &&
56
58
  typeof options.scalable === 'boolean' &&
57
59
  !options.scalable &&
58
60
  /(\d+)px/.test(value)
59
61
  ) {
60
- value = value.replace(/(\d+)px/, '$1PX')
62
+ value = value.replace(/(\d+)px/g, '$1PX')
63
+ }
64
+ // expect value is legal so that remove !import
65
+ if (/!import/i.test(value)) {
66
+ value = value.replace(/!import/, '')
61
67
  }
62
68
 
63
69
  if (shorthandBorderProps.indexOf(property) > -1) {
@@ -99,7 +105,7 @@ const transform = (css, options) => {
99
105
 
100
106
  if (isAlreadyDefinedAsClass) {
101
107
  throw new Error(
102
- `Failed to parse :export block because a CSS class in the same file is already using the name "${property}"`
108
+ `Failed to parse :export block because a CSS class in the same file is already using the name "${property}"`,
103
109
  )
104
110
  }
105
111
 
@@ -132,12 +138,12 @@ const transform = (css, options) => {
132
138
  ) {
133
139
  const parsed = mediaQuery.parse(rule.media)
134
140
 
135
- parsed.forEach(mq => {
141
+ parsed.forEach((mq) => {
136
142
  if (mediaQueryTypes.indexOf(mq.type) === -1) {
137
143
  throw new Error(`Failed to parse media query type "${mq.type}"`)
138
144
  }
139
145
 
140
- mq.expressions.forEach(e => {
146
+ mq.expressions.forEach((e) => {
141
147
  const mf = e.modifier ? `${e.modifier}-${e.feature}` : e.feature
142
148
  const val = e.value ? `: ${e.value}` : ''
143
149
 
@@ -150,7 +156,7 @@ const transform = (css, options) => {
150
156
  lengthRe.test(e.value) === false
151
157
  ) {
152
158
  throw new Error(
153
- `Failed to parse media query expression "(${mf}${val})"`
159
+ `Failed to parse media query expression "(${mf}${val})"`,
154
160
  )
155
161
  }
156
162
  })
@@ -182,8 +188,6 @@ const transform = (css, options) => {
182
188
  return result
183
189
  }
184
190
 
185
- export {
186
- transformCSS
187
- }
191
+ export { transformCSS }
188
192
 
189
193
  export default transform
@@ -7,7 +7,7 @@ export function camelCase (str) {
7
7
  .replace(/[-_]+/g, ' ')
8
8
  // Removes any non alphanumeric characters
9
9
  .replace(/[^\w\s]/g, '')
10
- // Uppercases the first character in each group immediately following a space
10
+ // Uppercase the first character in each group immediately following a space
11
11
  // (delimited by spaces)
12
12
  .replace(/ (.)/g, function ($1) {
13
13
  return $1.toUpperCase()