taro-css-to-react-native 3.5.0-theta.1 → 3.5.1-aplha.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.
@@ -18,7 +18,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
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;
@@ -38,7 +42,7 @@ var transformRawValue = function transformRawValue(input) {
38
42
  }
39
43
 
40
44
  var boolMatch = input.match(boolRe);
41
- if (boolMatch !== null) return boolMatch[0].toLowerCase() === "true";
45
+ if (boolMatch !== null) return boolMatch[0].toLowerCase() === 'true';
42
46
  var nullMatch = input.match(nullRe);
43
47
  if (nullMatch !== null) return null;
44
48
  var undefinedMatch = input.match(undefinedRe);
@@ -63,7 +67,7 @@ var checkBaseTransformShorthandValue = function checkBaseTransformShorthandValue
63
67
  }
64
68
  };
65
69
 
66
- var transformShorthandValue = process.env.NODE_ENV === "production" ? baseTransformShorthandValue : checkBaseTransformShorthandValue;
70
+ var transformShorthandValue = process.env.NODE_ENV === 'production' ? baseTransformShorthandValue : checkBaseTransformShorthandValue;
67
71
 
68
72
  var getStylesForProperty = function getStylesForProperty(propName, inputValue, allowShorthand) {
69
73
  var isRawValue = allowShorthand === false || !(propName in _index["default"]);
@@ -11,20 +11,21 @@ var _postcssValueParser = require("postcss-value-parser");
11
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
- if (node.type !== "string") return null;
16
+ if (node.type !== 'string') return null;
16
17
  return node.value.replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, function (match, charCode) {
17
18
  return String.fromCharCode(parseInt(charCode, 16));
18
- }).replace(/\\/g, "");
19
+ }).replace(/\\/g, '');
19
20
  };
20
21
 
21
22
  var hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i;
22
23
  var cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/;
23
24
 
24
25
  var matchColor = function matchColor(node) {
25
- if (node.type === "word" && (hexColorRe.test(node.value) || node.value in _cssColorKeywords["default"] || node.value === "transparent")) {
26
+ if (node.type === 'word' && (hexColorRe.test(node.value) || node.value in _cssColorKeywords["default"] || node.value === 'transparent')) {
26
27
  return node.value;
27
- } else if (node.type === "function" && cssFunctionNameRe.test(node.value)) {
28
+ } else if (node.type === 'function' && cssFunctionNameRe.test(node.value)) {
28
29
  return (0, _postcssValueParser.stringify)(node);
29
30
  }
30
31
 
@@ -37,14 +38,15 @@ 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) {
47
- return predicate(node) ? "<token>" : null;
49
+ return predicate(node) ? '<token>' : null;
48
50
  };
49
51
  };
50
52
 
@@ -60,11 +62,11 @@ var functionValueForTypeToken = function functionValueForTypeToken(type) {
60
62
  // handle rgb(a) function value
61
63
  if (/^rgba?$/i.test(node.value)) {
62
64
  var result = node.nodes.filter(function (token) {
63
- return token.type === "word";
65
+ return token.type === 'word';
64
66
  }).map(function (token) {
65
67
  return token.value;
66
68
  });
67
- return "".concat(node.value, "(").concat(result.join(", "), ")");
69
+ return "".concat(node.value, "(").concat(result.join(', '), ")");
68
70
  }
69
71
  }
70
72
 
@@ -75,12 +77,18 @@ var functionValueForTypeToken = function functionValueForTypeToken(type) {
75
77
  var regExpToken = function regExpToken(regExp) {
76
78
  var transform = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : String;
77
79
  return function (node) {
78
- if (node.type !== "word") return null;
80
+ var _node$value$match;
81
+
82
+ if (node.type !== 'word') return null;
79
83
  var match = node.value.match(regExp);
80
84
  if (match === null) return null;
81
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);
82
88
 
83
- if (/(\d+)px/.test(node.value)) {
89
+ if (isViewportUnit) {
90
+ return "scaleVu2dp(".concat(value, ", '").concat(unit, "')");
91
+ } else if (/(\d+)px/.test(node.value)) {
84
92
  return "scalePx2dp(".concat(value, ")");
85
93
  } else {
86
94
  return value;
@@ -91,16 +99,16 @@ var regExpToken = function regExpToken(regExp) {
91
99
  exports.regExpToken = regExpToken;
92
100
  var tokens = {
93
101
  SPACE: noopToken(function (node) {
94
- return node.type === "space";
102
+ return node.type === 'space';
95
103
  }),
96
104
  SLASH: noopToken(function (node) {
97
- return node.type === "div" && node.value === "/";
105
+ return node.type === 'div' && node.value === '/';
98
106
  }),
99
107
  COMMA: noopToken(function (node) {
100
- return node.type === "div" && node.value === ",";
108
+ return node.type === 'div' && node.value === ',';
101
109
  }),
102
- WORD: valueForTypeToken("word"),
103
- FUNC: functionValueForTypeToken("function"),
110
+ WORD: valueForTypeToken('word'),
111
+ FUNC: functionValueForTypeToken('function'),
104
112
  NONE: regExpToken(noneRe),
105
113
  AUTO: regExpToken(autoRe),
106
114
  NUMBER: regExpToken(numberRe, Number),
@@ -20,44 +20,44 @@ var WORD = _tokenTypes.tokens.WORD,
20
20
  function borderDirectionFactory() {
21
21
  var _anyOrderFactory;
22
22
 
23
- var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
23
+ var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
24
24
  var prefix = "border".concat(direction);
25
25
  return (0, _util.anyOrderFactory)((_anyOrderFactory = {}, _defineProperty(_anyOrderFactory, "".concat(prefix, "Width"), {
26
26
  tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
27
27
  "default": 1
28
28
  }), _defineProperty(_anyOrderFactory, "".concat(prefix, "Color"), {
29
29
  tokens: [COLOR],
30
- "default": "black"
30
+ "default": 'black'
31
31
  }), _defineProperty(_anyOrderFactory, "".concat(prefix, "Style"), {
32
32
  tokens: [(0, _tokenTypes.regExpToken)(/^(solid|dashed|dotted)$/)],
33
- "default": "solid"
33
+ "default": 'solid'
34
34
  }), _anyOrderFactory));
35
35
  }
36
36
 
37
37
  var border = borderDirectionFactory();
38
38
  exports.border = border;
39
- var borderTop = borderDirectionFactory("Top");
39
+ var borderTop = borderDirectionFactory('Top');
40
40
  exports.borderTop = borderTop;
41
- var borderRight = borderDirectionFactory("Right");
41
+ var borderRight = borderDirectionFactory('Right');
42
42
  exports.borderRight = borderRight;
43
- var borderBottom = borderDirectionFactory("Bottom");
43
+ var borderBottom = borderDirectionFactory('Bottom');
44
44
  exports.borderBottom = borderBottom;
45
- var borderLeft = borderDirectionFactory("Left");
45
+ var borderLeft = borderDirectionFactory('Left');
46
46
  exports.borderLeft = borderLeft;
47
47
  var borderColor = (0, _util.directionFactory)({
48
48
  types: [WORD, FUNC],
49
- prefix: "border",
50
- suffix: "Color"
49
+ prefix: 'border',
50
+ suffix: 'Color'
51
51
  });
52
52
  exports.borderColor = borderColor;
53
53
  var borderRadius = (0, _util.directionFactory)({
54
- directions: ["TopLeft", "TopRight", "BottomRight", "BottomLeft"],
55
- prefix: "border",
56
- suffix: "Radius"
54
+ directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
55
+ prefix: 'border',
56
+ suffix: 'Radius'
57
57
  });
58
58
  exports.borderRadius = borderRadius;
59
59
  var borderWidth = (0, _util.directionFactory)({
60
- prefix: "border",
61
- suffix: "Width"
60
+ prefix: 'border',
61
+ suffix: 'Width'
62
62
  });
63
63
  exports.borderWidth = borderWidth;
@@ -20,8 +20,8 @@ var NORMAL = (0, _tokenTypes.regExpToken)(/^(normal)$/);
20
20
  var STYLE = (0, _tokenTypes.regExpToken)(/^(italic)$/);
21
21
  var WEIGHT = (0, _tokenTypes.regExpToken)(/^([1-9]00|bold)$/);
22
22
  var VARIANT = (0, _tokenTypes.regExpToken)(/^(small-caps)$/);
23
- var defaultFontStyle = "normal";
24
- var defaultFontWeight = "normal";
23
+ var defaultFontStyle = 'normal';
24
+ var defaultFontWeight = 'normal';
25
25
  var defaultFontVariant = [];
26
26
 
27
27
  var _default = function _default(tokenStream) {
@@ -36,11 +36,11 @@ var _default = function _default(tokenStream) {
36
36
  while (numStyleWeightVariantMatched < 3 && tokenStream.hasTokens()) {
37
37
  if (tokenStream.matches(NORMAL)) {
38
38
  /* pass */
39
- } else if (typeof fontStyle === "undefined" && tokenStream.matches(STYLE)) {
39
+ } else if (typeof fontStyle === 'undefined' && tokenStream.matches(STYLE)) {
40
40
  fontStyle = tokenStream.lastValue;
41
- } else if (typeof fontWeight === "undefined" && tokenStream.matches(WEIGHT)) {
41
+ } else if (typeof fontWeight === 'undefined' && tokenStream.matches(WEIGHT)) {
42
42
  fontWeight = tokenStream.lastValue;
43
- } else if (typeof fontVariant === "undefined" && tokenStream.matches(VARIANT)) {
43
+ } else if (typeof fontVariant === 'undefined' && tokenStream.matches(VARIANT)) {
44
44
  fontVariant = [tokenStream.lastValue];
45
45
  } else {
46
46
  break;
@@ -54,7 +54,7 @@ var _default = function _default(tokenStream) {
54
54
 
55
55
  if (tokenStream.matches(SLASH)) {
56
56
  if (tokenStream.matches(NUMBER)) {
57
- var size = typeof fontSize === "string" ? fontSize.replace(/scalePx2dp\((\d+)\)/, "$1") : fontSize;
57
+ var size = typeof fontSize === 'string' ? fontSize.replace(/scalePx2dp\((\d+)\)/, '$1') : fontSize;
58
58
  lineHeight = size * tokenStream.lastValue;
59
59
  } else {
60
60
  lineHeight = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
@@ -63,9 +63,9 @@ var _default = function _default(tokenStream) {
63
63
 
64
64
  tokenStream.expect(SPACE);
65
65
  var fontFamily = (0, _fontFamily["default"])(tokenStream);
66
- if (typeof fontStyle === "undefined") fontStyle = defaultFontStyle;
67
- if (typeof fontWeight === "undefined") fontWeight = defaultFontWeight;
68
- if (typeof fontVariant === "undefined") fontVariant = defaultFontVariant;
66
+ if (typeof fontStyle === 'undefined') fontStyle = defaultFontStyle;
67
+ if (typeof fontWeight === 'undefined') fontWeight = defaultFontWeight;
68
+ if (typeof fontVariant === 'undefined') fontVariant = defaultFontVariant;
69
69
  var out = {
70
70
  fontStyle: fontStyle,
71
71
  fontWeight: fontWeight,
@@ -73,7 +73,7 @@ var _default = function _default(tokenStream) {
73
73
  fontSize: fontSize,
74
74
  fontFamily: fontFamily
75
75
  };
76
- if (typeof lineHeight !== "undefined") out.lineHeight = lineHeight;
76
+ if (typeof lineHeight !== 'undefined') out.lineHeight = lineHeight;
77
77
  return {
78
78
  $merge: out
79
79
  };
@@ -47,19 +47,19 @@ var background = function background(tokenStream) {
47
47
 
48
48
  var margin = (0, _util.directionFactory)({
49
49
  types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
50
- prefix: "margin"
50
+ prefix: 'margin'
51
51
  });
52
52
  var padding = (0, _util.directionFactory)({
53
- prefix: "padding"
53
+ prefix: 'padding'
54
54
  });
55
55
  var flexFlow = (0, _util.anyOrderFactory)({
56
56
  flexWrap: {
57
57
  tokens: [(0, _tokenTypes.regExpToken)(/(nowrap|wrap|wrap-reverse)/)],
58
- "default": "nowrap"
58
+ "default": 'nowrap'
59
59
  },
60
60
  flexDirection: {
61
61
  tokens: [(0, _tokenTypes.regExpToken)(/(row|row-reverse|column|column-reverse)/)],
62
- "default": "row"
62
+ "default": 'row'
63
63
  }
64
64
  });
65
65
 
package/dist/index.js CHANGED
@@ -37,14 +37,14 @@ var lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px|rem$))/;
37
37
  var viewportUnitRe = /^([+-]?[0-9.]+)(vh|vw|vmin|vmax)$/;
38
38
  var percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
39
39
  var unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/;
40
- var shorthandBorderProps = ["border-radius", "border-width", "border-color", "border-style"];
40
+ var shorthandBorderProps = ['border-radius', 'border-width', 'border-color', 'border-style'];
41
41
 
42
42
  var transformDecls = function transformDecls(styles, declarations, result) {
43
43
  var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
44
44
 
45
45
  for (var d in declarations) {
46
46
  var declaration = declarations[d];
47
- if (declaration.type !== "declaration") continue;
47
+ if (declaration.type !== 'declaration') continue;
48
48
  var property = declaration.property;
49
49
  var value = (0, _rem.remToPx)(declaration.value);
50
50
  var isLengthUnit = lengthRe.test(value);
@@ -52,7 +52,7 @@ var transformDecls = function transformDecls(styles, declarations, result) {
52
52
  var isPercent = percentRe.test(value);
53
53
  var isUnsupportedUnit = unsupportedUnitRe.test(value);
54
54
 
55
- if (property === "line-height" && !isLengthUnit && !isViewportUnit && !isPercent && !isUnsupportedUnit) {
55
+ if (property === 'line-height' && !isLengthUnit && !isViewportUnit && !isPercent && !isUnsupportedUnit) {
56
56
  // ignore invalid value avoid throw error cause app crash
57
57
  continue;
58
58
  }
@@ -63,13 +63,13 @@ var transformDecls = function transformDecls(styles, declarations, result) {
63
63
  // do not be wrapped by scalePx2dp function
64
64
 
65
65
 
66
- if (typeof options.scalable === "boolean" && !options.scalable && /(\d+)px/.test(value)) {
67
- value = value.replace(/(\d+)px/g, "$1PX");
66
+ if (typeof options.scalable === 'boolean' && !options.scalable && /(\d+)px/.test(value)) {
67
+ value = value.replace(/(\d+)px/g, '$1PX');
68
68
  } // expect value is legal so that remove !import
69
69
 
70
70
 
71
71
  if (/!import/i.test(value)) {
72
- value = value.replace(/!import/, "");
72
+ value = value.replace(/!import/, '');
73
73
  }
74
74
 
75
75
  if (shorthandBorderProps.indexOf(property) > -1) {
@@ -102,7 +102,7 @@ var transform = function transform(css, options) {
102
102
  var rule = rules[r];
103
103
 
104
104
  for (var s in rule.selectors) {
105
- if (rule.selectors[s] === ":export") {
105
+ if (rule.selectors[s] === ':export') {
106
106
  if (!result.__exportProps) {
107
107
  result.__exportProps = {};
108
108
  }
@@ -110,7 +110,7 @@ var transform = function transform(css, options) {
110
110
  rule.declarations.forEach(function (_ref) {
111
111
  var property = _ref.property,
112
112
  value = _ref.value;
113
- var isAlreadyDefinedAsClass = typeof result[property] !== "undefined" && typeof result.__exportProps[property] === "undefined";
113
+ var isAlreadyDefinedAsClass = typeof result[property] !== 'undefined' && typeof result.__exportProps[property] === 'undefined';
114
114
 
115
115
  if (isAlreadyDefinedAsClass) {
116
116
  throw new Error("Failed to parse :export block because a CSS class in the same file is already using the name \"".concat(property, "\""));
@@ -121,16 +121,16 @@ var transform = function transform(css, options) {
121
121
  continue;
122
122
  }
123
123
 
124
- if (rule.selectors[s].indexOf(".") !== 0 || rule.selectors[s].indexOf(":") !== -1 || rule.selectors[s].indexOf("[") !== -1 || rule.selectors[s].indexOf("~") !== -1 || rule.selectors[s].indexOf(">") !== -1 || rule.selectors[s].indexOf("+") !== -1 || rule.selectors[s].indexOf(" ") !== -1) {
124
+ if (rule.selectors[s].indexOf('.') !== 0 || rule.selectors[s].indexOf(':') !== -1 || rule.selectors[s].indexOf('[') !== -1 || rule.selectors[s].indexOf('~') !== -1 || rule.selectors[s].indexOf('>') !== -1 || rule.selectors[s].indexOf('+') !== -1 || rule.selectors[s].indexOf(' ') !== -1) {
125
125
  continue;
126
126
  }
127
127
 
128
- var selector = rule.selectors[s].replace(/^\./, "");
128
+ var selector = rule.selectors[s].replace(/^\./, '');
129
129
  var styles = result[selector] = result[selector] || {};
130
130
  transformDecls(styles, rule.declarations, result, options);
131
131
  }
132
132
 
133
- if (rule.type === "media" && options != null && options.parseMediaQueries === true) {
133
+ if (rule.type === 'media' && options != null && options.parseMediaQueries === true) {
134
134
  var parsed = _cssMediaquery["default"].parse(rule.media);
135
135
 
136
136
  parsed.forEach(function (mq) {
@@ -140,7 +140,7 @@ var transform = function transform(css, options) {
140
140
 
141
141
  mq.expressions.forEach(function (e) {
142
142
  var mf = e.modifier ? "".concat(e.modifier, "-").concat(e.feature) : e.feature;
143
- var val = e.value ? ": ".concat(e.value) : "";
143
+ var val = e.value ? ": ".concat(e.value) : '';
144
144
 
145
145
  if (_features.mediaQueryFeatures.indexOf(e.feature) === -1) {
146
146
  throw new Error("Failed to parse media query feature \"".concat(mf, "\""));
@@ -151,7 +151,7 @@ var transform = function transform(css, options) {
151
151
  }
152
152
  });
153
153
  });
154
- var media = "@media " + rule.media;
154
+ var media = '@media ' + rule.media;
155
155
  result.__mediaQueries = result.__mediaQueries || {};
156
156
  result.__mediaQueries[media] = parsed;
157
157
 
@@ -161,7 +161,7 @@ var transform = function transform(css, options) {
161
161
  for (var _s in ruleRule.selectors) {
162
162
  result[media] = result[media] || {};
163
163
 
164
- var _selector = ruleRule.selectors[_s].replace(/^\./, "");
164
+ var _selector = ruleRule.selectors[_s].replace(/^\./, '');
165
165
 
166
166
  var mediaStyles = result[media][_selector] = result[media][_selector] || {};
167
167
  transformDecls(mediaStyles, ruleRule.declarations, result, options);
@@ -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,12 +1,12 @@
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-theta.1",
4
+ "version": "3.5.1-aplha.1",
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"
@@ -43,15 +43,6 @@
43
43
  "<rootDir>/node_modules"
44
44
  ]
45
45
  },
46
- "lint-staged": {
47
- "*.{js,json,md}": [
48
- "prettier --write",
49
- "git add"
50
- ]
51
- },
52
- "prettier": {
53
- "trailingComma": "all"
54
- },
55
46
  "files": [
56
47
  "dist",
57
48
  "src",
@@ -1,89 +1,94 @@
1
1
  /* eslint-disable no-param-reassign */
2
- import camelizeStyleName from "camelize";
3
- import parse from "postcss-value-parser";
2
+ import camelizeStyleName from 'camelize'
3
+ import parse from 'postcss-value-parser'
4
4
 
5
- import TokenStream from "./TokenStream";
6
- import transforms from "./transforms/index";
5
+ import TokenStream from './TokenStream'
6
+ import transforms from './transforms/index'
7
7
 
8
8
  // Note if this is wrong, you'll need to change tokenTypes.js too
9
- const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?:px)?$/i;
10
- const boolRe = /^true|false$/i;
11
- const nullRe = /^null$/i;
12
- const undefinedRe = /^undefined$/i;
9
+ const numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?:px)|(?:vw$)|(?:vh$)|(?:vmin$)|(?:vmax$))?$/i
10
+ const boolRe = /^true|false$/i
11
+ const nullRe = /^null$/i
12
+ const undefinedRe = /^undefined$/i
13
13
 
14
14
  // Undocumented export
15
- export const transformRawValue = input => {
16
- const value = input.trim();
15
+ export const transformRawValue = (input) => {
16
+ const value = input.trim()
17
17
 
18
- const numberMatch = value.match(numberOrLengthRe);
18
+ const numberMatch = value.match(numberOrLengthRe)
19
19
  if (numberMatch !== null) {
20
- const num = Number(numberMatch[1]);
21
- if (/(\d+)px/.test(value)) {
22
- return `scalePx2dp(${num})`;
20
+ const num = Number(numberMatch[1])
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)) {
27
+ return `scalePx2dp(${num})`
23
28
  } else {
24
- return num;
29
+ return num
25
30
  }
26
31
  }
27
32
 
28
- const boolMatch = input.match(boolRe);
29
- if (boolMatch !== null) return boolMatch[0].toLowerCase() === "true";
33
+ const boolMatch = input.match(boolRe)
34
+ if (boolMatch !== null) return boolMatch[0].toLowerCase() === 'true'
30
35
 
31
- const nullMatch = input.match(nullRe);
32
- if (nullMatch !== null) return null;
36
+ const nullMatch = input.match(nullRe)
37
+ if (nullMatch !== null) return null
33
38
 
34
- const undefinedMatch = input.match(undefinedRe);
35
- if (undefinedMatch !== null) return undefined;
39
+ const undefinedMatch = input.match(undefinedRe)
40
+ if (undefinedMatch !== null) return undefined
36
41
 
37
- return value;
38
- };
42
+ return value
43
+ }
39
44
 
40
45
  const baseTransformShorthandValue = (propName, inputValue) => {
41
46
  // const ast = parse(inputValue.trim().replace(/PX|Px|pX$/g, ""));
42
- const ast = parse(inputValue);
43
- const tokenStream = new TokenStream(ast.nodes);
44
- return transforms[propName](tokenStream);
45
- };
47
+ const ast = parse(inputValue)
48
+ const tokenStream = new TokenStream(ast.nodes)
49
+ return transforms[propName](tokenStream)
50
+ }
46
51
 
47
52
  const checkBaseTransformShorthandValue = (propName, inputValue) => {
48
53
  try {
49
- return baseTransformShorthandValue(propName, inputValue);
54
+ return baseTransformShorthandValue(propName, inputValue)
50
55
  } catch (e) {
51
56
  throw new Error(
52
57
  `${e.message} Failed to parse declaration "${propName}: ${inputValue}"`,
53
- );
58
+ )
54
59
  }
55
- };
60
+ }
56
61
 
57
62
  const transformShorthandValue =
58
- process.env.NODE_ENV === "production"
63
+ process.env.NODE_ENV === 'production'
59
64
  ? baseTransformShorthandValue
60
- : checkBaseTransformShorthandValue;
65
+ : checkBaseTransformShorthandValue
61
66
 
62
67
  export const getStylesForProperty = (propName, inputValue, allowShorthand) => {
63
- const isRawValue = allowShorthand === false || !(propName in transforms);
68
+ const isRawValue = allowShorthand === false || !(propName in transforms)
64
69
  const propValue = isRawValue
65
70
  ? transformRawValue(inputValue)
66
- : transformShorthandValue(propName, inputValue.trim());
71
+ : transformShorthandValue(propName, inputValue.trim())
67
72
  return propValue && propValue.$merge
68
73
  ? propValue.$merge
69
- : { [propName]: propValue };
70
- };
74
+ : { [propName]: propValue }
75
+ }
71
76
 
72
- export const getPropertyName = propName => {
73
- const isCustomProp = /^--\w+/.test(propName);
77
+ export const getPropertyName = (propName) => {
78
+ const isCustomProp = /^--\w+/.test(propName)
74
79
  if (isCustomProp) {
75
- return propName;
80
+ return propName
76
81
  }
77
- return camelizeStyleName(propName);
78
- };
82
+ return camelizeStyleName(propName)
83
+ }
79
84
 
80
85
  export default (rules, shorthandBlacklist = []) =>
81
86
  rules.reduce((accum, rule) => {
82
- const propertyName = getPropertyName(rule[0]);
83
- const value = rule[1];
84
- const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1;
87
+ const propertyName = getPropertyName(rule[0])
88
+ const value = rule[1]
89
+ const allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1
85
90
  return Object.assign(
86
91
  accum,
87
92
  getStylesForProperty(propertyName, value, allowShorthand),
88
- );
89
- }, {});
93
+ )
94
+ }, {})
@@ -1,82 +1,91 @@
1
- import cssColorKeywords from "css-color-keywords";
2
- import { stringify } from "postcss-value-parser";
1
+ /* eslint-disable react/display-name */
2
+ import cssColorKeywords from 'css-color-keywords'
3
+ import { stringify } from 'postcss-value-parser'
3
4
 
4
- const matchString = node => {
5
- if (node.type !== "string") return null;
5
+ const matchString = (node) => {
6
+ if (node.type !== 'string') return null
6
7
  return node.value
7
8
  .replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, (match, charCode) =>
8
9
  String.fromCharCode(parseInt(charCode, 16)),
9
10
  )
10
- .replace(/\\/g, "");
11
- };
11
+ .replace(/\\/g, '')
12
+ }
12
13
 
13
- const hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i;
14
- const cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/;
14
+ const hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i
15
+ const cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/
15
16
 
16
- const matchColor = node => {
17
+ const matchColor = (node) => {
17
18
  if (
18
- node.type === "word" &&
19
+ node.type === 'word' &&
19
20
  (hexColorRe.test(node.value) ||
20
21
  node.value in cssColorKeywords ||
21
- node.value === "transparent")
22
+ node.value === 'transparent')
22
23
  ) {
23
- return node.value;
24
- } else if (node.type === "function" && cssFunctionNameRe.test(node.value)) {
25
- return stringify(node);
24
+ return node.value
25
+ } else if (node.type === 'function' && cssFunctionNameRe.test(node.value)) {
26
+ return stringify(node)
26
27
  }
27
- return null;
28
- };
28
+ return null
29
+ }
29
30
 
30
- const noneRe = /^(none)$/i;
31
- const autoRe = /^(auto)$/i;
32
- const identRe = /(^-?[_a-z][_a-z0-9-]*$)/i;
31
+ const noneRe = /^(none)$/i
32
+ const autoRe = /^(auto)$/i
33
+ const identRe = /(^-?[_a-z][_a-z0-9-]*$)/i
33
34
  // Note if these are wrong, you'll need to change index.js too
34
- const numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/;
35
+ const numberRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)$/
35
36
  // Note lengthRe is sneaky: you can omit units for 0
36
- const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)((?=px$)|(?=Px$)|(?=PX$)|(?=pX$)))/;
37
- const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|rem|vh|vw|vmin|vmax|cm|mm|in|pc|pt))$/;
38
- const angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/;
39
- const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
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))$/
39
+ const angleRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(?:deg|rad))$/
40
+ const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/
41
+ const viewportUnitRe = /(\d+)(vw|vh|vmin|vmax)/
40
42
 
41
- const noopToken = predicate => node => (predicate(node) ? "<token>" : null);
43
+ const noopToken = (predicate) => (node) => predicate(node) ? '<token>' : null
42
44
 
43
- const valueForTypeToken = type => node =>
44
- node.type === type ? node.value : null;
45
+ const valueForTypeToken = (type) => (node) =>
46
+ node.type === type ? node.value : null
45
47
 
46
- const functionValueForTypeToken = type => node => {
48
+ const functionValueForTypeToken = (type) => (node) => {
47
49
  if (node.type === type) {
48
50
  // handle rgb(a) function value
49
51
  if (/^rgba?$/i.test(node.value)) {
50
52
  const result = node.nodes
51
- .filter(token => token.type === "word")
52
- .map(token => token.value);
53
+ .filter((token) => token.type === 'word')
54
+ .map((token) => token.value)
53
55
 
54
- return `${node.value}(${result.join(", ")})`;
56
+ return `${node.value}(${result.join(', ')})`
55
57
  }
56
58
  }
57
- return null;
58
- };
59
+ return null
60
+ }
59
61
 
60
- export const regExpToken = (regExp, transform = String) => node => {
61
- if (node.type !== "word") return null;
62
+ export const regExpToken =
63
+ (regExp, transform = String) =>
64
+ (node) => {
65
+ if (node.type !== 'word') return null
62
66
 
63
- const match = node.value.match(regExp);
64
- if (match === null) return null;
67
+ const match = node.value.match(regExp)
68
+ if (match === null) return null
65
69
 
66
- const value = transform(match[1]);
67
- if (/(\d+)px/.test(node.value)) {
68
- return `scalePx2dp(${value})`;
69
- } else {
70
- return value;
71
- }
72
- };
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
+ }
73
82
 
74
83
  export const tokens = {
75
- SPACE: noopToken(node => node.type === "space"),
76
- SLASH: noopToken(node => node.type === "div" && node.value === "/"),
77
- COMMA: noopToken(node => node.type === "div" && node.value === ","),
78
- WORD: valueForTypeToken("word"),
79
- FUNC: functionValueForTypeToken("function"),
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 === ','),
87
+ WORD: valueForTypeToken('word'),
88
+ FUNC: functionValueForTypeToken('function'),
80
89
  NONE: regExpToken(noneRe),
81
90
  AUTO: regExpToken(autoRe),
82
91
  NUMBER: regExpToken(numberRe, Number),
@@ -88,4 +97,4 @@ export const tokens = {
88
97
  STRING: matchString,
89
98
  COLOR: matchColor,
90
99
  LINE: regExpToken(/^(none|underline|line-through)$/i),
91
- };
100
+ }
@@ -1,10 +1,10 @@
1
- import { regExpToken, tokens } from "../tokenTypes";
2
- import { anyOrderFactory, directionFactory } from "./util";
1
+ import { regExpToken, tokens } from '../tokenTypes'
2
+ import { anyOrderFactory, directionFactory } from './util'
3
3
 
4
- const { WORD, FUNC, COLOR, LENGTH, UNSUPPORTED_LENGTH_UNIT } = tokens;
4
+ const { WORD, FUNC, COLOR, LENGTH, UNSUPPORTED_LENGTH_UNIT } = tokens
5
5
 
6
- function borderDirectionFactory(direction = "") {
7
- const prefix = `border${direction}`;
6
+ function borderDirectionFactory (direction = '') {
7
+ const prefix = `border${direction}`
8
8
  return anyOrderFactory({
9
9
  [`${prefix}Width`]: {
10
10
  tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
@@ -12,35 +12,35 @@ function borderDirectionFactory(direction = "") {
12
12
  },
13
13
  [`${prefix}Color`]: {
14
14
  tokens: [COLOR],
15
- default: "black",
15
+ default: 'black',
16
16
  },
17
17
  [`${prefix}Style`]: {
18
18
  tokens: [regExpToken(/^(solid|dashed|dotted)$/)],
19
- default: "solid",
19
+ default: 'solid',
20
20
  },
21
- });
21
+ })
22
22
  }
23
23
 
24
- export const border = borderDirectionFactory();
24
+ export const border = borderDirectionFactory()
25
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");
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
30
 
31
31
  export const borderColor = directionFactory({
32
32
  types: [WORD, FUNC],
33
- prefix: "border",
34
- suffix: "Color",
35
- });
33
+ prefix: 'border',
34
+ suffix: 'Color',
35
+ })
36
36
 
37
37
  export const borderRadius = directionFactory({
38
- directions: ["TopLeft", "TopRight", "BottomRight", "BottomLeft"],
39
- prefix: "border",
40
- suffix: "Radius",
41
- });
38
+ directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
39
+ prefix: 'border',
40
+ suffix: 'Radius',
41
+ })
42
42
 
43
43
  export const borderWidth = directionFactory({
44
- prefix: "border",
45
- suffix: "Width",
46
- });
44
+ prefix: 'border',
45
+ suffix: 'Width',
46
+ })
@@ -1,72 +1,72 @@
1
- import { regExpToken, tokens } from "../tokenTypes";
2
- import parseFontFamily from "./fontFamily";
1
+ import { regExpToken, tokens } from '../tokenTypes'
2
+ import parseFontFamily from './fontFamily'
3
3
 
4
- const { SPACE, LENGTH, UNSUPPORTED_LENGTH_UNIT, NUMBER, SLASH } = tokens;
5
- const NORMAL = regExpToken(/^(normal)$/);
6
- const STYLE = regExpToken(/^(italic)$/);
7
- const WEIGHT = regExpToken(/^([1-9]00|bold)$/);
8
- const VARIANT = regExpToken(/^(small-caps)$/);
4
+ const { SPACE, LENGTH, UNSUPPORTED_LENGTH_UNIT, NUMBER, SLASH } = tokens
5
+ const NORMAL = regExpToken(/^(normal)$/)
6
+ const STYLE = regExpToken(/^(italic)$/)
7
+ const WEIGHT = regExpToken(/^([1-9]00|bold)$/)
8
+ const VARIANT = regExpToken(/^(small-caps)$/)
9
9
 
10
- const defaultFontStyle = "normal";
11
- const defaultFontWeight = "normal";
12
- const defaultFontVariant = [];
10
+ const defaultFontStyle = 'normal'
11
+ const defaultFontWeight = 'normal'
12
+ const defaultFontVariant = []
13
13
 
14
- export default tokenStream => {
15
- let fontStyle;
16
- let fontWeight;
17
- let fontVariant;
14
+ export default (tokenStream) => {
15
+ let fontStyle
16
+ let fontWeight
17
+ let fontVariant
18
18
  // let fontSize;
19
- let lineHeight;
19
+ let lineHeight
20
20
  // let fontFamily;
21
21
 
22
- let numStyleWeightVariantMatched = 0;
22
+ let numStyleWeightVariantMatched = 0
23
23
  while (numStyleWeightVariantMatched < 3 && tokenStream.hasTokens()) {
24
24
  if (tokenStream.matches(NORMAL)) {
25
25
  /* pass */
26
- } else if (typeof fontStyle === "undefined" && tokenStream.matches(STYLE)) {
27
- fontStyle = tokenStream.lastValue;
26
+ } else if (typeof fontStyle === 'undefined' && tokenStream.matches(STYLE)) {
27
+ fontStyle = tokenStream.lastValue
28
28
  } else if (
29
- typeof fontWeight === "undefined" &&
29
+ typeof fontWeight === 'undefined' &&
30
30
  tokenStream.matches(WEIGHT)
31
31
  ) {
32
- fontWeight = tokenStream.lastValue;
32
+ fontWeight = tokenStream.lastValue
33
33
  } else if (
34
- typeof fontVariant === "undefined" &&
34
+ typeof fontVariant === 'undefined' &&
35
35
  tokenStream.matches(VARIANT)
36
36
  ) {
37
- fontVariant = [tokenStream.lastValue];
37
+ fontVariant = [tokenStream.lastValue]
38
38
  } else {
39
- break;
39
+ break
40
40
  }
41
41
 
42
- tokenStream.expect(SPACE);
43
- numStyleWeightVariantMatched += 1;
42
+ tokenStream.expect(SPACE)
43
+ numStyleWeightVariantMatched += 1
44
44
  }
45
45
 
46
- const fontSize = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
46
+ const fontSize = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT)
47
47
 
48
48
  if (tokenStream.matches(SLASH)) {
49
49
  if (tokenStream.matches(NUMBER)) {
50
50
  const size =
51
- typeof fontSize === "string"
52
- ? fontSize.replace(/scalePx2dp\((\d+)\)/, "$1")
53
- : fontSize;
54
- lineHeight = size * tokenStream.lastValue;
51
+ typeof fontSize === 'string'
52
+ ? fontSize.replace(/scalePx2dp\((\d+)\)/, '$1')
53
+ : fontSize
54
+ lineHeight = size * tokenStream.lastValue
55
55
  } else {
56
- lineHeight = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
56
+ lineHeight = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT)
57
57
  }
58
58
  }
59
59
 
60
- tokenStream.expect(SPACE);
60
+ tokenStream.expect(SPACE)
61
61
 
62
- const fontFamily = parseFontFamily(tokenStream);
62
+ const fontFamily = parseFontFamily(tokenStream)
63
63
 
64
- if (typeof fontStyle === "undefined") fontStyle = defaultFontStyle;
65
- if (typeof fontWeight === "undefined") fontWeight = defaultFontWeight;
66
- if (typeof fontVariant === "undefined") fontVariant = defaultFontVariant;
64
+ if (typeof fontStyle === 'undefined') fontStyle = defaultFontStyle
65
+ if (typeof fontWeight === 'undefined') fontWeight = defaultFontWeight
66
+ if (typeof fontVariant === 'undefined') fontVariant = defaultFontVariant
67
67
 
68
- const out = { fontStyle, fontWeight, fontVariant, fontSize, fontFamily };
69
- if (typeof lineHeight !== "undefined") out.lineHeight = lineHeight;
68
+ const out = { fontStyle, fontWeight, fontVariant, fontSize, fontFamily }
69
+ if (typeof lineHeight !== 'undefined') out.lineHeight = lineHeight
70
70
 
71
- return { $merge: out };
72
- };
71
+ return { $merge: out }
72
+ }
@@ -1,4 +1,4 @@
1
- import { regExpToken, tokens } from "../tokenTypes";
1
+ import { regExpToken, tokens } from '../tokenTypes'
2
2
  import {
3
3
  border,
4
4
  borderBottom,
@@ -8,49 +8,42 @@ import {
8
8
  borderRight,
9
9
  borderTop,
10
10
  borderWidth,
11
- } from "./border";
12
- import boxShadow from "./boxShadow";
13
- import flex from "./flex";
14
- import font from "./font";
15
- import fontFamily from "./fontFamily";
16
- import textDecoration from "./textDecoration";
17
- import textDecorationLine from "./textDecorationLine";
18
- import textShadow from "./textShadow";
19
- import transform from "./transform";
20
- import { anyOrderFactory, directionFactory, shadowOffsetFactory } from "./util";
11
+ } from './border'
12
+ import boxShadow from './boxShadow'
13
+ import flex from './flex'
14
+ import font from './font'
15
+ import fontFamily from './fontFamily'
16
+ import textDecoration from './textDecoration'
17
+ import textDecorationLine from './textDecorationLine'
18
+ import textShadow from './textShadow'
19
+ import transform from './transform'
20
+ import { anyOrderFactory, directionFactory, shadowOffsetFactory } from './util'
21
21
 
22
- const {
23
- IDENT,
24
- WORD,
25
- COLOR,
26
- LENGTH,
27
- UNSUPPORTED_LENGTH_UNIT,
28
- PERCENT,
29
- AUTO,
30
- } = tokens;
22
+ const { IDENT, WORD, COLOR, LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO } =
23
+ tokens
31
24
 
32
- const background = tokenStream => ({
25
+ const background = (tokenStream) => ({
33
26
  $merge: { backgroundColor: tokenStream.expect(COLOR) },
34
- });
27
+ })
35
28
  const margin = directionFactory({
36
29
  types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
37
- prefix: "margin",
38
- });
39
- const padding = directionFactory({ prefix: "padding" });
30
+ prefix: 'margin',
31
+ })
32
+ const padding = directionFactory({ prefix: 'padding' })
40
33
  const flexFlow = anyOrderFactory({
41
34
  flexWrap: {
42
35
  tokens: [regExpToken(/(nowrap|wrap|wrap-reverse)/)],
43
- default: "nowrap",
36
+ default: 'nowrap',
44
37
  },
45
38
  flexDirection: {
46
39
  tokens: [regExpToken(/(row|row-reverse|column|column-reverse)/)],
47
- default: "row",
40
+ default: 'row',
48
41
  },
49
- });
50
- const fontVariant = tokenStream => [tokenStream.expect(IDENT)];
51
- const fontWeight = tokenStream => tokenStream.expect(WORD); // Also match numbers as strings
52
- const shadowOffset = shadowOffsetFactory();
53
- const textShadowOffset = shadowOffsetFactory();
42
+ })
43
+ const fontVariant = (tokenStream) => [tokenStream.expect(IDENT)]
44
+ const fontWeight = (tokenStream) => tokenStream.expect(WORD) // Also match numbers as strings
45
+ const shadowOffset = shadowOffsetFactory()
46
+ const textShadowOffset = shadowOffsetFactory()
54
47
 
55
48
  export default {
56
49
  background,
@@ -77,4 +70,4 @@ export default {
77
70
  textDecoration,
78
71
  textDecorationLine,
79
72
  transform,
80
- };
73
+ }
package/src/index.js CHANGED
@@ -1,153 +1,154 @@
1
- import parseCSS from "css/lib/parse";
2
- import mediaQuery from "css-mediaquery";
1
+ import parseCSS from 'css/lib/parse'
2
+ import mediaQuery from 'css-mediaquery'
3
3
 
4
- import transformCSS from "./css-to-react-native";
4
+ import transformCSS from './css-to-react-native'
5
5
  import {
6
6
  dimensionFeatures,
7
7
  mediaQueryFeatures,
8
- } from "./transforms/media-queries/features";
9
- import { mediaQueryTypes } from "./transforms/media-queries/types";
10
- import { remToPx } from "./transforms/rem";
11
- import { allEqual } from "./utils/allEqual";
12
- import { camelCase } from "./utils/camelCase";
13
- import { sortRules } from "./utils/sortRules";
14
- import { values } from "./utils/values";
15
-
16
- const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px|rem$))/;
17
- const viewportUnitRe = /^([+-]?[0-9.]+)(vh|vw|vmin|vmax)$/;
18
- const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
19
- const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/;
8
+ } from './transforms/media-queries/features'
9
+ import { mediaQueryTypes } from './transforms/media-queries/types'
10
+ import { remToPx } from './transforms/rem'
11
+ import { allEqual } from './utils/allEqual'
12
+ import { camelCase } from './utils/camelCase'
13
+ import { sortRules } from './utils/sortRules'
14
+ import { values } from './utils/values'
15
+
16
+ const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px|rem$))/
17
+ const viewportUnitRe = /^([+-]?[0-9.]+)(vh|vw|vmin|vmax)$/
18
+ const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/
19
+ const unsupportedUnitRe =
20
+ /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/
20
21
  const shorthandBorderProps = [
21
- "border-radius",
22
- "border-width",
23
- "border-color",
24
- "border-style",
25
- ];
22
+ 'border-radius',
23
+ 'border-width',
24
+ 'border-color',
25
+ 'border-style',
26
+ ]
26
27
 
27
28
  const transformDecls = (styles, declarations, result, options = {}) => {
28
29
  for (const d in declarations) {
29
- const declaration = declarations[d];
30
- if (declaration.type !== "declaration") continue;
30
+ const declaration = declarations[d]
31
+ if (declaration.type !== 'declaration') continue
31
32
 
32
- const property = declaration.property;
33
- let value = remToPx(declaration.value);
33
+ const property = declaration.property
34
+ let value = remToPx(declaration.value)
34
35
 
35
- const isLengthUnit = lengthRe.test(value);
36
- const isViewportUnit = viewportUnitRe.test(value);
37
- const isPercent = percentRe.test(value);
38
- const isUnsupportedUnit = unsupportedUnitRe.test(value);
36
+ const isLengthUnit = lengthRe.test(value)
37
+ const isViewportUnit = viewportUnitRe.test(value)
38
+ const isPercent = percentRe.test(value)
39
+ const isUnsupportedUnit = unsupportedUnitRe.test(value)
39
40
 
40
41
  if (
41
- property === "line-height" &&
42
+ property === 'line-height' &&
42
43
  !isLengthUnit &&
43
44
  !isViewportUnit &&
44
45
  !isPercent &&
45
46
  !isUnsupportedUnit
46
47
  ) {
47
48
  // ignore invalid value avoid throw error cause app crash
48
- continue;
49
+ continue
49
50
  }
50
51
 
51
52
  if (!result.__viewportUnits && isViewportUnit) {
52
- result.__viewportUnits = true;
53
+ result.__viewportUnits = true
53
54
  }
54
55
  // scalable option, when it is false, transform single value 'px' unit to 'PX'
55
56
  // do not be wrapped by scalePx2dp function
56
57
  if (
57
- typeof options.scalable === "boolean" &&
58
+ typeof options.scalable === 'boolean' &&
58
59
  !options.scalable &&
59
60
  /(\d+)px/.test(value)
60
61
  ) {
61
- value = value.replace(/(\d+)px/g, "$1PX");
62
+ value = value.replace(/(\d+)px/g, '$1PX')
62
63
  }
63
64
  // expect value is legal so that remove !import
64
65
  if (/!import/i.test(value)) {
65
- value = value.replace(/!import/, "");
66
+ value = value.replace(/!import/, '')
66
67
  }
67
68
 
68
69
  if (shorthandBorderProps.indexOf(property) > -1) {
69
70
  // transform single value shorthand border properties back to
70
71
  // shorthand form to support styling `Image`.
71
- const transformed = transformCSS([[property, value]]);
72
- const vals = values(transformed);
72
+ const transformed = transformCSS([[property, value]])
73
+ const vals = values(transformed)
73
74
  if (allEqual(vals)) {
74
- const replacement = {};
75
- replacement[camelCase(property)] = vals[0];
76
- Object.assign(styles, replacement);
75
+ const replacement = {}
76
+ replacement[camelCase(property)] = vals[0]
77
+ Object.assign(styles, replacement)
77
78
  } else {
78
- Object.assign(styles, transformed);
79
+ Object.assign(styles, transformed)
79
80
  }
80
81
  } else {
81
- Object.assign(styles, transformCSS([[property, value]]));
82
+ Object.assign(styles, transformCSS([[property, value]]))
82
83
  }
83
84
  }
84
- };
85
+ }
85
86
 
86
87
  const transform = (css, options) => {
87
- const { stylesheet } = parseCSS(css);
88
- const rules = sortRules(stylesheet.rules);
88
+ const { stylesheet } = parseCSS(css)
89
+ const rules = sortRules(stylesheet.rules)
89
90
 
90
- const result = {};
91
+ const result = {}
91
92
 
92
93
  for (const r in rules) {
93
- const rule = rules[r];
94
+ const rule = rules[r]
94
95
  for (const s in rule.selectors) {
95
- if (rule.selectors[s] === ":export") {
96
+ if (rule.selectors[s] === ':export') {
96
97
  if (!result.__exportProps) {
97
- result.__exportProps = {};
98
+ result.__exportProps = {}
98
99
  }
99
100
 
100
101
  rule.declarations.forEach(({ property, value }) => {
101
102
  const isAlreadyDefinedAsClass =
102
- typeof result[property] !== "undefined" &&
103
- typeof result.__exportProps[property] === "undefined";
103
+ typeof result[property] !== 'undefined' &&
104
+ typeof result.__exportProps[property] === 'undefined'
104
105
 
105
106
  if (isAlreadyDefinedAsClass) {
106
107
  throw new Error(
107
108
  `Failed to parse :export block because a CSS class in the same file is already using the name "${property}"`,
108
- );
109
+ )
109
110
  }
110
111
 
111
- result.__exportProps[property] = value;
112
- });
113
- continue;
112
+ result.__exportProps[property] = value
113
+ })
114
+ continue
114
115
  }
115
116
 
116
117
  if (
117
- rule.selectors[s].indexOf(".") !== 0 ||
118
- rule.selectors[s].indexOf(":") !== -1 ||
119
- rule.selectors[s].indexOf("[") !== -1 ||
120
- rule.selectors[s].indexOf("~") !== -1 ||
121
- rule.selectors[s].indexOf(">") !== -1 ||
122
- rule.selectors[s].indexOf("+") !== -1 ||
123
- rule.selectors[s].indexOf(" ") !== -1
118
+ rule.selectors[s].indexOf('.') !== 0 ||
119
+ rule.selectors[s].indexOf(':') !== -1 ||
120
+ rule.selectors[s].indexOf('[') !== -1 ||
121
+ rule.selectors[s].indexOf('~') !== -1 ||
122
+ rule.selectors[s].indexOf('>') !== -1 ||
123
+ rule.selectors[s].indexOf('+') !== -1 ||
124
+ rule.selectors[s].indexOf(' ') !== -1
124
125
  ) {
125
- continue;
126
+ continue
126
127
  }
127
128
 
128
- const selector = rule.selectors[s].replace(/^\./, "");
129
- const styles = (result[selector] = result[selector] || {});
130
- transformDecls(styles, rule.declarations, result, options);
129
+ const selector = rule.selectors[s].replace(/^\./, '')
130
+ const styles = (result[selector] = result[selector] || {})
131
+ transformDecls(styles, rule.declarations, result, options)
131
132
  }
132
133
 
133
134
  if (
134
- rule.type === "media" &&
135
+ rule.type === 'media' &&
135
136
  options != null &&
136
137
  options.parseMediaQueries === true
137
138
  ) {
138
- const parsed = mediaQuery.parse(rule.media);
139
+ const parsed = mediaQuery.parse(rule.media)
139
140
 
140
- parsed.forEach(mq => {
141
+ parsed.forEach((mq) => {
141
142
  if (mediaQueryTypes.indexOf(mq.type) === -1) {
142
- throw new Error(`Failed to parse media query type "${mq.type}"`);
143
+ throw new Error(`Failed to parse media query type "${mq.type}"`)
143
144
  }
144
145
 
145
- mq.expressions.forEach(e => {
146
- const mf = e.modifier ? `${e.modifier}-${e.feature}` : e.feature;
147
- const val = e.value ? `: ${e.value}` : "";
146
+ mq.expressions.forEach((e) => {
147
+ const mf = e.modifier ? `${e.modifier}-${e.feature}` : e.feature
148
+ const val = e.value ? `: ${e.value}` : ''
148
149
 
149
150
  if (mediaQueryFeatures.indexOf(e.feature) === -1) {
150
- throw new Error(`Failed to parse media query feature "${mf}"`);
151
+ throw new Error(`Failed to parse media query feature "${mf}"`)
151
152
  }
152
153
 
153
154
  if (
@@ -156,37 +157,37 @@ const transform = (css, options) => {
156
157
  ) {
157
158
  throw new Error(
158
159
  `Failed to parse media query expression "(${mf}${val})"`,
159
- );
160
+ )
160
161
  }
161
- });
162
- });
162
+ })
163
+ })
163
164
 
164
- const media = "@media " + rule.media;
165
+ const media = '@media ' + rule.media
165
166
 
166
- result.__mediaQueries = result.__mediaQueries || {};
167
- result.__mediaQueries[media] = parsed;
167
+ result.__mediaQueries = result.__mediaQueries || {}
168
+ result.__mediaQueries[media] = parsed
168
169
 
169
170
  for (const r in rule.rules) {
170
- const ruleRule = rule.rules[r];
171
+ const ruleRule = rule.rules[r]
171
172
  for (const s in ruleRule.selectors) {
172
- result[media] = result[media] || {};
173
- const selector = ruleRule.selectors[s].replace(/^\./, "");
173
+ result[media] = result[media] || {}
174
+ const selector = ruleRule.selectors[s].replace(/^\./, '')
174
175
  const mediaStyles = (result[media][selector] =
175
- result[media][selector] || {});
176
- transformDecls(mediaStyles, ruleRule.declarations, result, options);
176
+ result[media][selector] || {})
177
+ transformDecls(mediaStyles, ruleRule.declarations, result, options)
177
178
  }
178
179
  }
179
180
  }
180
181
  }
181
182
 
182
183
  if (result.__exportProps) {
183
- Object.assign(result, result.__exportProps);
184
- delete result.__exportProps;
184
+ Object.assign(result, result.__exportProps)
185
+ delete result.__exportProps
185
186
  }
186
187
 
187
- return result;
188
- };
188
+ return result
189
+ }
189
190
 
190
- export { transformCSS };
191
+ export { transformCSS }
191
192
 
192
- export default transform;
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()