index.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _interopDefault(ex) {
  6. return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;
  7. }
  8. var parse = require('postcss-value-parser');
  9. var parse__default = _interopDefault(parse);
  10. var camelizeStyleName = _interopDefault(require('camelize'));
  11. var cssColorKeywords = _interopDefault(require('css-color-keywords'));
  12. var matchString = function matchString(node) {
  13. if (node.type !== 'string') return null;
  14. return node.value.replace(/\\([0-9a-f]{1,6})(?:\s|$)/gi, function (match, charCode) {
  15. return String.fromCharCode(parseInt(charCode, 16));
  16. }).replace(/\\/g, '');
  17. };
  18. var hexColorRe = /^(#(?:[0-9a-f]{3,4}){1,2})$/i;
  19. var cssFunctionNameRe = /^(rgba?|hsla?|hwb|lab|lch|gray|color)$/;
  20. var matchColor = function matchColor(node) {
  21. if (node.type === 'word' && (hexColorRe.test(node.value) || node.value in cssColorKeywords || node.value === 'transparent')) {
  22. return node.value;
  23. } else if (node.type === 'function' && cssFunctionNameRe.test(node.value)) {
  24. return parse.stringify(node);
  25. }
  26. return null;
  27. };
  28. var noneRe = /^(none)$/i;
  29. var autoRe = /^(auto)$/i;
  30. var identRe = /(^-?[_a-z][_a-z0-9-]*$)/i; // Note if these are wrong, you'll need to change index.js too
  31. var numberRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?)$/i; // Note lengthRe is sneaky: you can omit units for 0
  32. var lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?)(?=px$))/i;
  33. var unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?(ch|em|ex|rem|vh|vw|vmin|vmax|cm|mm|in|pc|pt))$/i;
  34. var angleRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?(?:deg|rad))$/i;
  35. var percentRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?%)$/i;
  36. var noopToken = function noopToken(predicate) {
  37. return function (node) {
  38. return predicate(node) ? '<token>' : null;
  39. };
  40. };
  41. var valueForTypeToken = function valueForTypeToken(type) {
  42. return function (node) {
  43. return node.type === type ? node.value : null;
  44. };
  45. };
  46. var regExpToken = function regExpToken(regExp, transform) {
  47. if (transform === void 0) {
  48. transform = String;
  49. }
  50. return function (node) {
  51. if (node.type !== 'word') return null;
  52. var match = node.value.match(regExp);
  53. if (match === null) return null;
  54. var value = transform(match[1]);
  55. return value;
  56. };
  57. };
  58. var SPACE = noopToken(function (node) {
  59. return node.type === 'space';
  60. });
  61. var SLASH = noopToken(function (node) {
  62. return node.type === 'div' && node.value === '/';
  63. });
  64. var COMMA = noopToken(function (node) {
  65. return node.type === 'div' && node.value === ',';
  66. });
  67. var WORD = valueForTypeToken('word');
  68. var NONE = regExpToken(noneRe);
  69. var AUTO = regExpToken(autoRe);
  70. var NUMBER = regExpToken(numberRe, Number);
  71. var LENGTH = regExpToken(lengthRe, Number);
  72. var UNSUPPORTED_LENGTH_UNIT = regExpToken(unsupportedUnitRe);
  73. var ANGLE = regExpToken(angleRe, function (angle) {
  74. return angle.toLowerCase();
  75. });
  76. var PERCENT = regExpToken(percentRe);
  77. var IDENT = regExpToken(identRe);
  78. var STRING = matchString;
  79. var COLOR = matchColor;
  80. var LINE = regExpToken(/^(none|underline|line-through)$/i);
  81. var BORDER_STYLE = regExpToken(/^(solid|dashed|dotted)$/);
  82. var defaultBorderWidth = 1;
  83. var defaultBorderColor = 'black';
  84. var defaultBorderStyle = 'solid';
  85. var border = function border(tokenStream) {
  86. var borderWidth;
  87. var borderColor;
  88. var borderStyle;
  89. if (tokenStream.matches(NONE)) {
  90. tokenStream.expectEmpty();
  91. return {
  92. borderWidth: 0,
  93. borderColor: 'black',
  94. borderStyle: 'solid'
  95. };
  96. }
  97. var partsParsed = 0;
  98. while (partsParsed < 3 && tokenStream.hasTokens()) {
  99. if (partsParsed !== 0) tokenStream.expect(SPACE);
  100. if (borderWidth === undefined && tokenStream.matches(LENGTH, UNSUPPORTED_LENGTH_UNIT)) {
  101. borderWidth = tokenStream.lastValue;
  102. } else if (borderColor === undefined && tokenStream.matches(COLOR)) {
  103. borderColor = tokenStream.lastValue;
  104. } else if (borderStyle === undefined && tokenStream.matches(BORDER_STYLE)) {
  105. borderStyle = tokenStream.lastValue;
  106. } else {
  107. tokenStream["throw"]();
  108. }
  109. partsParsed += 1;
  110. }
  111. tokenStream.expectEmpty();
  112. if (borderWidth === undefined) borderWidth = defaultBorderWidth;
  113. if (borderColor === undefined) borderColor = defaultBorderColor;
  114. if (borderStyle === undefined) borderStyle = defaultBorderStyle;
  115. return {
  116. borderWidth: borderWidth,
  117. borderColor: borderColor,
  118. borderStyle: borderStyle
  119. };
  120. };
  121. var directionFactory = function directionFactory(_ref) {
  122. var _ref$types = _ref.types,
  123. types = _ref$types === void 0 ? [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT] : _ref$types,
  124. _ref$directions = _ref.directions,
  125. directions = _ref$directions === void 0 ? ['Top', 'Right', 'Bottom', 'Left'] : _ref$directions,
  126. _ref$prefix = _ref.prefix,
  127. prefix = _ref$prefix === void 0 ? '' : _ref$prefix,
  128. _ref$suffix = _ref.suffix,
  129. suffix = _ref$suffix === void 0 ? '' : _ref$suffix;
  130. return function (tokenStream) {
  131. var _ref2;
  132. var values = []; // borderWidth doesn't currently allow a percent value, but may do in the future
  133. values.push(tokenStream.expect.apply(tokenStream, types));
  134. while (values.length < 4 && tokenStream.hasTokens()) {
  135. tokenStream.expect(SPACE);
  136. values.push(tokenStream.expect.apply(tokenStream, types));
  137. }
  138. tokenStream.expectEmpty();
  139. var top = values[0],
  140. _values$ = values[1],
  141. right = _values$ === void 0 ? top : _values$,
  142. _values$2 = values[2],
  143. bottom = _values$2 === void 0 ? top : _values$2,
  144. _values$3 = values[3],
  145. left = _values$3 === void 0 ? right : _values$3;
  146. var keyFor = function keyFor(n) {
  147. return "" + prefix + directions[n] + suffix;
  148. };
  149. return _ref2 = {}, _ref2[keyFor(0)] = top, _ref2[keyFor(1)] = right, _ref2[keyFor(2)] = bottom, _ref2[keyFor(3)] = left, _ref2;
  150. };
  151. };
  152. var parseShadowOffset = function parseShadowOffset(tokenStream) {
  153. var width = tokenStream.expect(LENGTH);
  154. var height = tokenStream.matches(SPACE) ? tokenStream.expect(LENGTH) : width;
  155. tokenStream.expectEmpty();
  156. return {
  157. width: width,
  158. height: height
  159. };
  160. };
  161. var parseShadow = function parseShadow(tokenStream) {
  162. var offsetX;
  163. var offsetY;
  164. var radius;
  165. var color;
  166. if (tokenStream.matches(NONE)) {
  167. tokenStream.expectEmpty();
  168. return {
  169. offset: {
  170. width: 0,
  171. height: 0
  172. },
  173. radius: 0,
  174. color: 'black'
  175. };
  176. }
  177. var didParseFirst = false;
  178. while (tokenStream.hasTokens()) {
  179. if (didParseFirst) tokenStream.expect(SPACE);
  180. if (offsetX === undefined && tokenStream.matches(LENGTH, UNSUPPORTED_LENGTH_UNIT)) {
  181. offsetX = tokenStream.lastValue;
  182. tokenStream.expect(SPACE);
  183. offsetY = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
  184. tokenStream.saveRewindPoint();
  185. if (tokenStream.matches(SPACE) && tokenStream.matches(LENGTH, UNSUPPORTED_LENGTH_UNIT)) {
  186. radius = tokenStream.lastValue;
  187. } else {
  188. tokenStream.rewind();
  189. }
  190. } else if (color === undefined && tokenStream.matches(COLOR)) {
  191. color = tokenStream.lastValue;
  192. } else {
  193. tokenStream["throw"]();
  194. }
  195. didParseFirst = true;
  196. }
  197. if (offsetX === undefined) tokenStream["throw"]();
  198. return {
  199. offset: {
  200. width: offsetX,
  201. height: offsetY
  202. },
  203. radius: radius !== undefined ? radius : 0,
  204. color: color !== undefined ? color : 'black'
  205. };
  206. };
  207. var boxShadow = function boxShadow(tokenStream) {
  208. var _parseShadow = parseShadow(tokenStream),
  209. offset = _parseShadow.offset,
  210. radius = _parseShadow.radius,
  211. color = _parseShadow.color;
  212. return {
  213. shadowOffset: offset,
  214. shadowRadius: radius,
  215. shadowColor: color,
  216. shadowOpacity: 1
  217. };
  218. };
  219. var defaultFlexGrow = 1;
  220. var defaultFlexShrink = 1;
  221. var defaultFlexBasis = 0;
  222. var flex = function flex(tokenStream) {
  223. var flexGrow;
  224. var flexShrink;
  225. var flexBasis;
  226. if (tokenStream.matches(NONE)) {
  227. tokenStream.expectEmpty();
  228. return {
  229. flexGrow: 0,
  230. flexShrink: 0,
  231. flexBasis: 'auto'
  232. };
  233. }
  234. tokenStream.saveRewindPoint();
  235. if (tokenStream.matches(AUTO) && !tokenStream.hasTokens()) {
  236. return {
  237. flexGrow: 1,
  238. flexShrink: 1,
  239. flexBasis: 'auto'
  240. };
  241. }
  242. tokenStream.rewind();
  243. var partsParsed = 0;
  244. while (partsParsed < 2 && tokenStream.hasTokens()) {
  245. if (partsParsed !== 0) tokenStream.expect(SPACE);
  246. if (flexGrow === undefined && tokenStream.matches(NUMBER)) {
  247. flexGrow = tokenStream.lastValue;
  248. tokenStream.saveRewindPoint();
  249. if (tokenStream.matches(SPACE) && tokenStream.matches(NUMBER)) {
  250. flexShrink = tokenStream.lastValue;
  251. } else {
  252. tokenStream.rewind();
  253. }
  254. } else if (flexBasis === undefined && tokenStream.matches(LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT)) {
  255. flexBasis = tokenStream.lastValue;
  256. } else if (flexBasis === undefined && tokenStream.matches(AUTO)) {
  257. flexBasis = 'auto';
  258. } else {
  259. tokenStream["throw"]();
  260. }
  261. partsParsed += 1;
  262. }
  263. tokenStream.expectEmpty();
  264. if (flexGrow === undefined) flexGrow = defaultFlexGrow;
  265. if (flexShrink === undefined) flexShrink = defaultFlexShrink;
  266. if (flexBasis === undefined) flexBasis = defaultFlexBasis;
  267. return {
  268. flexGrow: flexGrow,
  269. flexShrink: flexShrink,
  270. flexBasis: flexBasis
  271. };
  272. };
  273. var FLEX_WRAP = regExpToken(/(nowrap|wrap|wrap-reverse)/);
  274. var FLEX_DIRECTION = regExpToken(/(row|row-reverse|column|column-reverse)/);
  275. var defaultFlexWrap = 'nowrap';
  276. var defaultFlexDirection = 'row';
  277. var flexFlow = function flexFlow(tokenStream) {
  278. var flexWrap;
  279. var flexDirection;
  280. var partsParsed = 0;
  281. while (partsParsed < 2 && tokenStream.hasTokens()) {
  282. if (partsParsed !== 0) tokenStream.expect(SPACE);
  283. if (flexWrap === undefined && tokenStream.matches(FLEX_WRAP)) {
  284. flexWrap = tokenStream.lastValue;
  285. } else if (flexDirection === undefined && tokenStream.matches(FLEX_DIRECTION)) {
  286. flexDirection = tokenStream.lastValue;
  287. } else {
  288. tokenStream["throw"]();
  289. }
  290. partsParsed += 1;
  291. }
  292. tokenStream.expectEmpty();
  293. if (flexWrap === undefined) flexWrap = defaultFlexWrap;
  294. if (flexDirection === undefined) flexDirection = defaultFlexDirection;
  295. return {
  296. flexWrap: flexWrap,
  297. flexDirection: flexDirection
  298. };
  299. };
  300. var fontFamily = function fontFamily(tokenStream) {
  301. var fontFamily;
  302. if (tokenStream.matches(STRING)) {
  303. fontFamily = tokenStream.lastValue;
  304. } else {
  305. fontFamily = tokenStream.expect(IDENT);
  306. while (tokenStream.hasTokens()) {
  307. tokenStream.expect(SPACE);
  308. var nextIdent = tokenStream.expect(IDENT);
  309. fontFamily += " " + nextIdent;
  310. }
  311. }
  312. tokenStream.expectEmpty();
  313. return {
  314. fontFamily: fontFamily
  315. };
  316. };
  317. var NORMAL = regExpToken(/^(normal)$/);
  318. var STYLE = regExpToken(/^(italic)$/);
  319. var WEIGHT = regExpToken(/^([1-9]00|bold)$/);
  320. var VARIANT = regExpToken(/^(small-caps)$/);
  321. var defaultFontStyle = 'normal';
  322. var defaultFontWeight = 'normal';
  323. var defaultFontVariant = [];
  324. var font = function font(tokenStream) {
  325. var fontStyle;
  326. var fontWeight;
  327. var fontVariant; // let fontSize;
  328. var lineHeight; // let fontFamily;
  329. var numStyleWeightVariantMatched = 0;
  330. while (numStyleWeightVariantMatched < 3 && tokenStream.hasTokens()) {
  331. if (tokenStream.matches(NORMAL)) ;else if (fontStyle === undefined && tokenStream.matches(STYLE)) {
  332. fontStyle = tokenStream.lastValue;
  333. } else if (fontWeight === undefined && tokenStream.matches(WEIGHT)) {
  334. fontWeight = tokenStream.lastValue;
  335. } else if (fontVariant === undefined && tokenStream.matches(VARIANT)) {
  336. fontVariant = [tokenStream.lastValue];
  337. } else {
  338. break;
  339. }
  340. tokenStream.expect(SPACE);
  341. numStyleWeightVariantMatched += 1;
  342. }
  343. var fontSize = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
  344. if (tokenStream.matches(SLASH)) {
  345. lineHeight = tokenStream.expect(LENGTH, UNSUPPORTED_LENGTH_UNIT);
  346. }
  347. tokenStream.expect(SPACE);
  348. var _fontFamily = fontFamily(tokenStream),
  349. fontFamily$1 = _fontFamily.fontFamily;
  350. if (fontStyle === undefined) fontStyle = defaultFontStyle;
  351. if (fontWeight === undefined) fontWeight = defaultFontWeight;
  352. if (fontVariant === undefined) fontVariant = defaultFontVariant;
  353. var out = {
  354. fontStyle: fontStyle,
  355. fontWeight: fontWeight,
  356. fontVariant: fontVariant,
  357. fontSize: fontSize,
  358. fontFamily: fontFamily$1
  359. };
  360. if (lineHeight !== undefined) out.lineHeight = lineHeight;
  361. return out;
  362. };
  363. var ALIGN_CONTENT = regExpToken(/(flex-(?:start|end)|center|stretch|space-(?:between|around))/);
  364. var JUSTIFY_CONTENT = regExpToken(/(flex-(?:start|end)|center|space-(?:between|around|evenly))/);
  365. var placeContent = function placeContent(tokenStream) {
  366. var alignContent = tokenStream.expect(ALIGN_CONTENT);
  367. var justifyContent;
  368. if (tokenStream.hasTokens()) {
  369. tokenStream.expect(SPACE);
  370. justifyContent = tokenStream.expect(JUSTIFY_CONTENT);
  371. } else {
  372. justifyContent = 'stretch';
  373. }
  374. tokenStream.expectEmpty();
  375. return {
  376. alignContent: alignContent,
  377. justifyContent: justifyContent
  378. };
  379. };
  380. var STYLE$1 = regExpToken(/^(solid|double|dotted|dashed)$/);
  381. var defaultTextDecorationLine = 'none';
  382. var defaultTextDecorationStyle = 'solid';
  383. var defaultTextDecorationColor = 'black';
  384. var textDecoration = function textDecoration(tokenStream) {
  385. var line;
  386. var style;
  387. var color;
  388. var didParseFirst = false;
  389. while (tokenStream.hasTokens()) {
  390. if (didParseFirst) tokenStream.expect(SPACE);
  391. if (line === undefined && tokenStream.matches(LINE)) {
  392. var lines = [tokenStream.lastValue.toLowerCase()];
  393. tokenStream.saveRewindPoint();
  394. if (lines[0] !== 'none' && tokenStream.matches(SPACE) && tokenStream.matches(LINE)) {
  395. lines.push(tokenStream.lastValue.toLowerCase()); // Underline comes before line-through
  396. lines.sort().reverse();
  397. } else {
  398. tokenStream.rewind();
  399. }
  400. line = lines.join(' ');
  401. } else if (style === undefined && tokenStream.matches(STYLE$1)) {
  402. style = tokenStream.lastValue;
  403. } else if (color === undefined && tokenStream.matches(COLOR)) {
  404. color = tokenStream.lastValue;
  405. } else {
  406. tokenStream["throw"]();
  407. }
  408. didParseFirst = true;
  409. }
  410. return {
  411. textDecorationLine: line !== undefined ? line : defaultTextDecorationLine,
  412. textDecorationColor: color !== undefined ? color : defaultTextDecorationColor,
  413. textDecorationStyle: style !== undefined ? style : defaultTextDecorationStyle
  414. };
  415. };
  416. var textDecorationLine = function textDecorationLine(tokenStream) {
  417. var lines = [];
  418. var didParseFirst = false;
  419. while (tokenStream.hasTokens()) {
  420. if (didParseFirst) tokenStream.expect(SPACE);
  421. lines.push(tokenStream.expect(LINE).toLowerCase());
  422. didParseFirst = true;
  423. }
  424. lines.sort().reverse();
  425. return {
  426. textDecorationLine: lines.join(' ')
  427. };
  428. };
  429. var textShadow = function textShadow(tokenStream) {
  430. var _parseShadow2 = parseShadow(tokenStream),
  431. offset = _parseShadow2.offset,
  432. radius = _parseShadow2.radius,
  433. color = _parseShadow2.color;
  434. return {
  435. textShadowOffset: offset,
  436. textShadowRadius: radius,
  437. textShadowColor: color
  438. };
  439. };
  440. var oneOfType = function oneOfType(tokenType) {
  441. return function (functionStream) {
  442. var value = functionStream.expect(tokenType);
  443. functionStream.expectEmpty();
  444. return value;
  445. };
  446. };
  447. var singleNumber = oneOfType(NUMBER);
  448. var singleLength = oneOfType(LENGTH);
  449. var singleAngle = oneOfType(ANGLE);
  450. var xyTransformFactory = function xyTransformFactory(tokenType) {
  451. return function (key, valueIfOmitted) {
  452. return function (functionStream) {
  453. var _ref3, _ref4;
  454. var x = functionStream.expect(tokenType);
  455. var y;
  456. if (functionStream.hasTokens()) {
  457. functionStream.expect(COMMA);
  458. y = functionStream.expect(tokenType);
  459. } else if (valueIfOmitted !== undefined) {
  460. y = valueIfOmitted;
  461. } else {
  462. // Assumption, if x === y, then we can omit XY
  463. // I.e. scale(5) => [{ scale: 5 }] rather than [{ scaleX: 5 }, { scaleY: 5 }]
  464. return x;
  465. }
  466. functionStream.expectEmpty();
  467. return [(_ref3 = {}, _ref3[key + "Y"] = y, _ref3), (_ref4 = {}, _ref4[key + "X"] = x, _ref4)];
  468. };
  469. };
  470. };
  471. var xyNumber = xyTransformFactory(NUMBER);
  472. var xyLength = xyTransformFactory(LENGTH);
  473. var xyAngle = xyTransformFactory(ANGLE);
  474. var partTransforms = {
  475. perspective: singleNumber,
  476. scale: xyNumber('scale'),
  477. scaleX: singleNumber,
  478. scaleY: singleNumber,
  479. translate: xyLength('translate', 0),
  480. translateX: singleLength,
  481. translateY: singleLength,
  482. rotate: singleAngle,
  483. rotateX: singleAngle,
  484. rotateY: singleAngle,
  485. rotateZ: singleAngle,
  486. skewX: singleAngle,
  487. skewY: singleAngle,
  488. skew: xyAngle('skew', '0deg')
  489. };
  490. var transform = function transform(tokenStream) {
  491. var transforms = [];
  492. var didParseFirst = false;
  493. while (tokenStream.hasTokens()) {
  494. if (didParseFirst) tokenStream.expect(SPACE);
  495. var functionStream = tokenStream.expectFunction();
  496. var functionName = functionStream.functionName;
  497. var transformedValues = partTransforms[functionName](functionStream);
  498. if (!Array.isArray(transformedValues)) {
  499. var _ref5;
  500. transformedValues = [(_ref5 = {}, _ref5[functionName] = transformedValues, _ref5)];
  501. }
  502. transforms = transformedValues.concat(transforms);
  503. didParseFirst = true;
  504. }
  505. return {
  506. transform: transforms
  507. };
  508. };
  509. var background = function background(tokenStream) {
  510. return {
  511. backgroundColor: tokenStream.expect(COLOR)
  512. };
  513. };
  514. var borderColor = directionFactory({
  515. types: [COLOR],
  516. prefix: 'border',
  517. suffix: 'Color'
  518. });
  519. var borderRadius = directionFactory({
  520. directions: ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'],
  521. prefix: 'border',
  522. suffix: 'Radius'
  523. });
  524. var borderWidth = directionFactory({
  525. prefix: 'border',
  526. suffix: 'Width'
  527. });
  528. var margin = directionFactory({
  529. types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
  530. prefix: 'margin'
  531. });
  532. var padding = directionFactory({
  533. prefix: 'padding'
  534. });
  535. var fontVariant = function fontVariant(tokenStream) {
  536. return {
  537. fontVariant: [tokenStream.expect(IDENT)]
  538. };
  539. };
  540. var fontWeight = function fontWeight(tokenStream) {
  541. return {
  542. fontWeight: tokenStream.expect(WORD) // Also match numbers as strings
  543. };
  544. };
  545. var shadowOffset = function shadowOffset(tokenStream) {
  546. return {
  547. shadowOffset: parseShadowOffset(tokenStream)
  548. };
  549. };
  550. var textShadowOffset = function textShadowOffset(tokenStream) {
  551. return {
  552. textShadowOffset: parseShadowOffset(tokenStream)
  553. };
  554. };
  555. var transforms = {
  556. background: background,
  557. border: border,
  558. borderColor: borderColor,
  559. borderRadius: borderRadius,
  560. borderWidth: borderWidth,
  561. boxShadow: boxShadow,
  562. flex: flex,
  563. flexFlow: flexFlow,
  564. font: font,
  565. fontFamily: fontFamily,
  566. fontVariant: fontVariant,
  567. fontWeight: fontWeight,
  568. margin: margin,
  569. padding: padding,
  570. placeContent: placeContent,
  571. shadowOffset: shadowOffset,
  572. textShadow: textShadow,
  573. textShadowOffset: textShadowOffset,
  574. textDecoration: textDecoration,
  575. textDecorationLine: textDecorationLine,
  576. transform: transform
  577. };
  578. var propertiesWithoutUnits;
  579. if (process.env.NODE_ENV !== 'production') {
  580. propertiesWithoutUnits = ['aspectRatio', 'elevation', 'flexGrow', 'flexShrink', 'opacity', 'shadowOpacity', 'zIndex'];
  581. }
  582. var devPropertiesWithUnitsRegExp = propertiesWithoutUnits != null ? new RegExp(propertiesWithoutUnits.join('|')) : null;
  583. var SYMBOL_MATCH = 'SYMBOL_MATCH';
  584. var TokenStream =
  585. /*#__PURE__*/
  586. function () {
  587. function TokenStream(nodes, parent) {
  588. this.index = 0;
  589. this.nodes = nodes;
  590. this.functionName = parent != null ? parent.value : null;
  591. this.lastValue = null;
  592. this.rewindIndex = -1;
  593. }
  594. var _proto = TokenStream.prototype;
  595. _proto.hasTokens = function hasTokens() {
  596. return this.index <= this.nodes.length - 1;
  597. };
  598. _proto[SYMBOL_MATCH] = function () {
  599. if (!this.hasTokens()) return null;
  600. var node = this.nodes[this.index];
  601. for (var i = 0; i < arguments.length; i += 1) {
  602. var tokenDescriptor = i < 0 || arguments.length <= i ? undefined : arguments[i];
  603. var value = tokenDescriptor(node);
  604. if (value !== null) {
  605. this.index += 1;
  606. this.lastValue = value;
  607. return value;
  608. }
  609. }
  610. return null;
  611. };
  612. _proto.matches = function matches() {
  613. return this[SYMBOL_MATCH].apply(this, arguments) !== null;
  614. };
  615. _proto.expect = function expect() {
  616. var value = this[SYMBOL_MATCH].apply(this, arguments);
  617. return value !== null ? value : this["throw"]();
  618. };
  619. _proto.matchesFunction = function matchesFunction() {
  620. var node = this.nodes[this.index];
  621. if (node.type !== 'function') return null;
  622. var value = new TokenStream(node.nodes, node);
  623. this.index += 1;
  624. this.lastValue = null;
  625. return value;
  626. };
  627. _proto.expectFunction = function expectFunction() {
  628. var value = this.matchesFunction();
  629. return value !== null ? value : this["throw"]();
  630. };
  631. _proto.expectEmpty = function expectEmpty() {
  632. if (this.hasTokens()) this["throw"]();
  633. };
  634. _proto["throw"] = function _throw() {
  635. throw new Error("Unexpected token type: " + this.nodes[this.index].type);
  636. };
  637. _proto.saveRewindPoint = function saveRewindPoint() {
  638. this.rewindIndex = this.index;
  639. };
  640. _proto.rewind = function rewind() {
  641. if (this.rewindIndex === -1) throw new Error('Internal error');
  642. this.index = this.rewindIndex;
  643. this.lastValue = null;
  644. };
  645. return TokenStream;
  646. }();
  647. /* eslint-disable no-param-reassign */
  648. // Note if this is wrong, you'll need to change tokenTypes.js too
  649. var numberOrLengthRe = /^([+-]?(?:\d*\.)?\d+(?:e[+-]?\d+)?)(?:px)?$/i;
  650. var numberOnlyRe = /^[+-]?(?:\d*\.\d*|[1-9]\d*)(?:e[+-]?\d+)?$/i;
  651. var boolRe = /^true|false$/i;
  652. var nullRe = /^null$/i;
  653. var undefinedRe = /^undefined$/i; // Undocumented export
  654. var transformRawValue = function transformRawValue(propName, value) {
  655. if (process.env.NODE_ENV !== 'production') {
  656. var needsUnit = !devPropertiesWithUnitsRegExp.test(propName);
  657. var isNumberWithoutUnit = numberOnlyRe.test(value);
  658. if (needsUnit && isNumberWithoutUnit) {
  659. // eslint-disable-next-line no-console
  660. console.warn("Expected style \"" + propName + ": " + value + "\" to contain units");
  661. }
  662. if (!needsUnit && value !== '0' && !isNumberWithoutUnit) {
  663. // eslint-disable-next-line no-console
  664. console.warn("Expected style \"" + propName + ": " + value + "\" to be unitless");
  665. }
  666. }
  667. var numberMatch = value.match(numberOrLengthRe);
  668. if (numberMatch !== null) return Number(numberMatch[1]);
  669. var boolMatch = value.match(boolRe);
  670. if (boolMatch !== null) return boolMatch[0].toLowerCase() === 'true';
  671. var nullMatch = value.match(nullRe);
  672. if (nullMatch !== null) return null;
  673. var undefinedMatch = value.match(undefinedRe);
  674. if (undefinedMatch !== null) return undefined;
  675. return value;
  676. };
  677. var baseTransformShorthandValue = function baseTransformShorthandValue(propName, value) {
  678. var ast = parse__default(value);
  679. var tokenStream = new TokenStream(ast.nodes);
  680. return transforms[propName](tokenStream);
  681. };
  682. var transformShorthandValue = process.env.NODE_ENV === 'production' ? baseTransformShorthandValue : function (propName, value) {
  683. try {
  684. return baseTransformShorthandValue(propName, value);
  685. } catch (e) {
  686. throw new Error("Failed to parse declaration \"" + propName + ": " + value + "\"");
  687. }
  688. };
  689. var getStylesForProperty = function getStylesForProperty(propName, inputValue, allowShorthand) {
  690. var _ref6;
  691. var isRawValue = allowShorthand === false || !(propName in transforms);
  692. var value = inputValue.trim();
  693. var propValues = isRawValue ? (_ref6 = {}, _ref6[propName] = transformRawValue(propName, value), _ref6) : transformShorthandValue(propName, value);
  694. return propValues;
  695. };
  696. var getPropertyName = function getPropertyName(propName) {
  697. var isCustomProp = /^--\w+/.test(propName);
  698. if (isCustomProp) {
  699. return propName;
  700. }
  701. return camelizeStyleName(propName);
  702. };
  703. var index = function index(rules, shorthandBlacklist) {
  704. if (shorthandBlacklist === void 0) {
  705. shorthandBlacklist = [];
  706. }
  707. return rules.reduce(function (accum, rule) {
  708. var propertyName = getPropertyName(rule[0]);
  709. var value = rule[1];
  710. var allowShorthand = shorthandBlacklist.indexOf(propertyName) === -1;
  711. return Object.assign(accum, getStylesForProperty(propertyName, value, allowShorthand));
  712. }, {});
  713. };
  714. exports["default"] = index;
  715. exports.getPropertyName = getPropertyName;
  716. exports.getStylesForProperty = getStylesForProperty;
  717. exports.transformRawValue = transformRawValue;