You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
6.7 KiB

2 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. function _export(target, all) {
  6. for(var name in all)Object.defineProperty(target, name, {
  7. enumerable: true,
  8. get: all[name]
  9. });
  10. }
  11. _export(exports, {
  12. updateAllClasses: ()=>updateAllClasses,
  13. asValue: ()=>asValue,
  14. parseColorFormat: ()=>parseColorFormat,
  15. asColor: ()=>asColor,
  16. asLookupValue: ()=>asLookupValue,
  17. coerceValue: ()=>coerceValue
  18. });
  19. const _postcssSelectorParser = /*#__PURE__*/ _interopRequireDefault(require("postcss-selector-parser"));
  20. const _escapeCommas = /*#__PURE__*/ _interopRequireDefault(require("./escapeCommas"));
  21. const _withAlphaVariable = require("./withAlphaVariable");
  22. const _dataTypes = require("./dataTypes");
  23. const _negateValue = /*#__PURE__*/ _interopRequireDefault(require("./negateValue"));
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule ? obj : {
  26. default: obj
  27. };
  28. }
  29. function updateAllClasses(selectors, updateClass) {
  30. let parser = (0, _postcssSelectorParser.default)((selectors)=>{
  31. selectors.walkClasses((sel)=>{
  32. let updatedClass = updateClass(sel.value);
  33. sel.value = updatedClass;
  34. if (sel.raws && sel.raws.value) {
  35. sel.raws.value = (0, _escapeCommas.default)(sel.raws.value);
  36. }
  37. });
  38. });
  39. let result = parser.processSync(selectors);
  40. return result;
  41. }
  42. function resolveArbitraryValue(modifier, validate) {
  43. if (!isArbitraryValue(modifier)) {
  44. return undefined;
  45. }
  46. let value = modifier.slice(1, -1);
  47. if (!validate(value)) {
  48. return undefined;
  49. }
  50. return (0, _dataTypes.normalize)(value);
  51. }
  52. function asNegativeValue(modifier, lookup = {}, validate) {
  53. let positiveValue = lookup[modifier];
  54. if (positiveValue !== undefined) {
  55. return (0, _negateValue.default)(positiveValue);
  56. }
  57. if (isArbitraryValue(modifier)) {
  58. let resolved = resolveArbitraryValue(modifier, validate);
  59. if (resolved === undefined) {
  60. return undefined;
  61. }
  62. return (0, _negateValue.default)(resolved);
  63. }
  64. }
  65. function asValue(modifier, options = {}, { validate =()=>true } = {}) {
  66. var ref;
  67. let value = (ref = options.values) === null || ref === void 0 ? void 0 : ref[modifier];
  68. if (value !== undefined) {
  69. return value;
  70. }
  71. if (options.supportsNegativeValues && modifier.startsWith("-")) {
  72. return asNegativeValue(modifier.slice(1), options.values, validate);
  73. }
  74. return resolveArbitraryValue(modifier, validate);
  75. }
  76. function isArbitraryValue(input) {
  77. return input.startsWith("[") && input.endsWith("]");
  78. }
  79. function splitAlpha(modifier) {
  80. let slashIdx = modifier.lastIndexOf("/");
  81. if (slashIdx === -1 || slashIdx === modifier.length - 1) {
  82. return [
  83. modifier
  84. ];
  85. }
  86. return [
  87. modifier.slice(0, slashIdx),
  88. modifier.slice(slashIdx + 1)
  89. ];
  90. }
  91. function parseColorFormat(value) {
  92. if (typeof value === "string" && value.includes("<alpha-value>")) {
  93. let oldValue = value;
  94. return ({ opacityValue =1 })=>oldValue.replace("<alpha-value>", opacityValue);
  95. }
  96. return value;
  97. }
  98. function asColor(modifier, options = {}, { tailwindConfig ={} } = {}) {
  99. var ref;
  100. if (((ref = options.values) === null || ref === void 0 ? void 0 : ref[modifier]) !== undefined) {
  101. var ref1;
  102. return parseColorFormat((ref1 = options.values) === null || ref1 === void 0 ? void 0 : ref1[modifier]);
  103. }
  104. let [color, alpha] = splitAlpha(modifier);
  105. if (alpha !== undefined) {
  106. var ref2, ref3, ref4;
  107. var ref5;
  108. let normalizedColor = (ref5 = (ref2 = options.values) === null || ref2 === void 0 ? void 0 : ref2[color]) !== null && ref5 !== void 0 ? ref5 : isArbitraryValue(color) ? color.slice(1, -1) : undefined;
  109. if (normalizedColor === undefined) {
  110. return undefined;
  111. }
  112. normalizedColor = parseColorFormat(normalizedColor);
  113. if (isArbitraryValue(alpha)) {
  114. return (0, _withAlphaVariable.withAlphaValue)(normalizedColor, alpha.slice(1, -1));
  115. }
  116. if (((ref3 = tailwindConfig.theme) === null || ref3 === void 0 ? void 0 : (ref4 = ref3.opacity) === null || ref4 === void 0 ? void 0 : ref4[alpha]) === undefined) {
  117. return undefined;
  118. }
  119. return (0, _withAlphaVariable.withAlphaValue)(normalizedColor, tailwindConfig.theme.opacity[alpha]);
  120. }
  121. return asValue(modifier, options, {
  122. validate: _dataTypes.color
  123. });
  124. }
  125. function asLookupValue(modifier, options = {}) {
  126. var ref;
  127. return (ref = options.values) === null || ref === void 0 ? void 0 : ref[modifier];
  128. }
  129. function guess(validate) {
  130. return (modifier, options)=>{
  131. return asValue(modifier, options, {
  132. validate
  133. });
  134. };
  135. }
  136. let typeMap = {
  137. any: asValue,
  138. color: asColor,
  139. url: guess(_dataTypes.url),
  140. image: guess(_dataTypes.image),
  141. length: guess(_dataTypes.length),
  142. percentage: guess(_dataTypes.percentage),
  143. position: guess(_dataTypes.position),
  144. lookup: asLookupValue,
  145. "generic-name": guess(_dataTypes.genericName),
  146. "family-name": guess(_dataTypes.familyName),
  147. number: guess(_dataTypes.number),
  148. "line-width": guess(_dataTypes.lineWidth),
  149. "absolute-size": guess(_dataTypes.absoluteSize),
  150. "relative-size": guess(_dataTypes.relativeSize),
  151. shadow: guess(_dataTypes.shadow)
  152. };
  153. let supportedTypes = Object.keys(typeMap);
  154. function splitAtFirst(input, delim) {
  155. let idx = input.indexOf(delim);
  156. if (idx === -1) return [
  157. undefined,
  158. input
  159. ];
  160. return [
  161. input.slice(0, idx),
  162. input.slice(idx + 1)
  163. ];
  164. }
  165. function coerceValue(types, modifier, options, tailwindConfig) {
  166. if (isArbitraryValue(modifier)) {
  167. let arbitraryValue = modifier.slice(1, -1);
  168. let [explicitType, value] = splitAtFirst(arbitraryValue, ":");
  169. // It could be that this resolves to `url(https` which is not a valid
  170. // identifier. We currently only support "simple" words with dashes or
  171. // underscores. E.g.: family-name
  172. if (!/^[\w-_]+$/g.test(explicitType)) {
  173. value = arbitraryValue;
  174. } else if (explicitType !== undefined && !supportedTypes.includes(explicitType)) {
  175. return [];
  176. }
  177. if (value.length > 0 && supportedTypes.includes(explicitType)) {
  178. return [
  179. asValue(`[${value}]`, options),
  180. explicitType
  181. ];
  182. }
  183. }
  184. // Find first matching type
  185. for (let type of [].concat(types)){
  186. let result = typeMap[type](modifier, options, {
  187. tailwindConfig
  188. });
  189. if (result !== undefined) return [
  190. result,
  191. type
  192. ];
  193. }
  194. return [];
  195. }