legacyLogicalProperties.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  2. function splitValues(value) {
  3. if (typeof value === 'number') {
  4. return [[value], false];
  5. }
  6. var rawStyle = String(value).trim();
  7. var importantCells = rawStyle.match(/(.*)(!important)/);
  8. var splitStyle = (importantCells ? importantCells[1] : rawStyle).trim().split(/\s+/);
  9. // Combine styles split in brackets, like `calc(1px + 2px)`
  10. var temp = [];
  11. var brackets = 0;
  12. return [splitStyle.reduce(function (list, item) {
  13. if (item.includes('(') || item.includes(')')) {
  14. var left = item.split('(').length - 1;
  15. var right = item.split(')').length - 1;
  16. brackets += left - right;
  17. }
  18. if (brackets >= 0) temp.push(item);
  19. if (brackets === 0) {
  20. list.push(temp.join(' '));
  21. temp = [];
  22. }
  23. return list;
  24. }, []), !!importantCells];
  25. }
  26. function noSplit(list) {
  27. list.notSplit = true;
  28. return list;
  29. }
  30. var keyMap = {
  31. // Inset
  32. inset: ['top', 'right', 'bottom', 'left'],
  33. insetBlock: ['top', 'bottom'],
  34. insetBlockStart: ['top'],
  35. insetBlockEnd: ['bottom'],
  36. insetInline: ['left', 'right'],
  37. insetInlineStart: ['left'],
  38. insetInlineEnd: ['right'],
  39. // Margin
  40. marginBlock: ['marginTop', 'marginBottom'],
  41. marginBlockStart: ['marginTop'],
  42. marginBlockEnd: ['marginBottom'],
  43. marginInline: ['marginLeft', 'marginRight'],
  44. marginInlineStart: ['marginLeft'],
  45. marginInlineEnd: ['marginRight'],
  46. // Padding
  47. paddingBlock: ['paddingTop', 'paddingBottom'],
  48. paddingBlockStart: ['paddingTop'],
  49. paddingBlockEnd: ['paddingBottom'],
  50. paddingInline: ['paddingLeft', 'paddingRight'],
  51. paddingInlineStart: ['paddingLeft'],
  52. paddingInlineEnd: ['paddingRight'],
  53. // Border
  54. borderBlock: noSplit(['borderTop', 'borderBottom']),
  55. borderBlockStart: noSplit(['borderTop']),
  56. borderBlockEnd: noSplit(['borderBottom']),
  57. borderInline: noSplit(['borderLeft', 'borderRight']),
  58. borderInlineStart: noSplit(['borderLeft']),
  59. borderInlineEnd: noSplit(['borderRight']),
  60. // Border width
  61. borderBlockWidth: ['borderTopWidth', 'borderBottomWidth'],
  62. borderBlockStartWidth: ['borderTopWidth'],
  63. borderBlockEndWidth: ['borderBottomWidth'],
  64. borderInlineWidth: ['borderLeftWidth', 'borderRightWidth'],
  65. borderInlineStartWidth: ['borderLeftWidth'],
  66. borderInlineEndWidth: ['borderRightWidth'],
  67. // Border style
  68. borderBlockStyle: ['borderTopStyle', 'borderBottomStyle'],
  69. borderBlockStartStyle: ['borderTopStyle'],
  70. borderBlockEndStyle: ['borderBottomStyle'],
  71. borderInlineStyle: ['borderLeftStyle', 'borderRightStyle'],
  72. borderInlineStartStyle: ['borderLeftStyle'],
  73. borderInlineEndStyle: ['borderRightStyle'],
  74. // Border color
  75. borderBlockColor: ['borderTopColor', 'borderBottomColor'],
  76. borderBlockStartColor: ['borderTopColor'],
  77. borderBlockEndColor: ['borderBottomColor'],
  78. borderInlineColor: ['borderLeftColor', 'borderRightColor'],
  79. borderInlineStartColor: ['borderLeftColor'],
  80. borderInlineEndColor: ['borderRightColor'],
  81. // Border radius
  82. borderStartStartRadius: ['borderTopLeftRadius'],
  83. borderStartEndRadius: ['borderTopRightRadius'],
  84. borderEndStartRadius: ['borderBottomLeftRadius'],
  85. borderEndEndRadius: ['borderBottomRightRadius']
  86. };
  87. function wrapImportantAndSkipCheck(value, important) {
  88. var parsedValue = value;
  89. if (important) {
  90. parsedValue = "".concat(parsedValue, " !important");
  91. }
  92. return {
  93. _skip_check_: true,
  94. value: parsedValue
  95. };
  96. }
  97. /**
  98. * Convert css logical properties to legacy properties.
  99. * Such as: `margin-block-start` to `margin-top`.
  100. * Transform list:
  101. * - inset
  102. * - margin
  103. * - padding
  104. * - border
  105. */
  106. var transform = {
  107. visit: function visit(cssObj) {
  108. var clone = {};
  109. Object.keys(cssObj).forEach(function (key) {
  110. var value = cssObj[key];
  111. var matchValue = keyMap[key];
  112. if (matchValue && (typeof value === 'number' || typeof value === 'string')) {
  113. var _splitValues = splitValues(value),
  114. _splitValues2 = _slicedToArray(_splitValues, 2),
  115. _values = _splitValues2[0],
  116. _important = _splitValues2[1];
  117. if (matchValue.length && matchValue.notSplit) {
  118. // not split means always give same value like border
  119. matchValue.forEach(function (matchKey) {
  120. clone[matchKey] = wrapImportantAndSkipCheck(value, _important);
  121. });
  122. } else if (matchValue.length === 1) {
  123. // Handle like `marginBlockStart` => `marginTop`
  124. clone[matchValue[0]] = wrapImportantAndSkipCheck(_values[0], _important);
  125. } else if (matchValue.length === 2) {
  126. // Handle like `marginBlock` => `marginTop` & `marginBottom`
  127. matchValue.forEach(function (matchKey, index) {
  128. var _values$index;
  129. clone[matchKey] = wrapImportantAndSkipCheck((_values$index = _values[index]) !== null && _values$index !== void 0 ? _values$index : _values[0], _important);
  130. });
  131. } else if (matchValue.length === 4) {
  132. // Handle like `inset` => `top` & `right` & `bottom` & `left`
  133. matchValue.forEach(function (matchKey, index) {
  134. var _ref, _values$index2;
  135. clone[matchKey] = wrapImportantAndSkipCheck((_ref = (_values$index2 = _values[index]) !== null && _values$index2 !== void 0 ? _values$index2 : _values[index - 2]) !== null && _ref !== void 0 ? _ref : _values[0], _important);
  136. });
  137. } else {
  138. clone[key] = value;
  139. }
  140. } else {
  141. clone[key] = value;
  142. }
  143. });
  144. return clone;
  145. }
  146. };
  147. export default transform;