pluginUtils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. typeMap: ()=>typeMap,
  18. coerceValue: ()=>coerceValue,
  19. getMatchingTypes: ()=>getMatchingTypes
  20. });
  21. const _escapeCommas = /*#__PURE__*/ _interopRequireDefault(require("./escapeCommas"));
  22. const _withAlphaVariable = require("./withAlphaVariable");
  23. const _dataTypes = require("./dataTypes");
  24. const _negateValue = /*#__PURE__*/ _interopRequireDefault(require("./negateValue"));
  25. const _validateFormalSyntax = require("./validateFormalSyntax");
  26. const _featureFlagsJs = require("../featureFlags.js");
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {
  29. default: obj
  30. };
  31. }
  32. function updateAllClasses(selectors, updateClass) {
  33. selectors.walkClasses((sel)=>{
  34. sel.value = updateClass(sel.value);
  35. if (sel.raws && sel.raws.value) {
  36. sel.raws.value = (0, _escapeCommas.default)(sel.raws.value);
  37. }
  38. });
  39. }
  40. function resolveArbitraryValue(modifier, validate) {
  41. if (!isArbitraryValue(modifier)) {
  42. return undefined;
  43. }
  44. let value = modifier.slice(1, -1);
  45. if (!validate(value)) {
  46. return undefined;
  47. }
  48. return (0, _dataTypes.normalize)(value);
  49. }
  50. function asNegativeValue(modifier, lookup = {}, validate) {
  51. let positiveValue = lookup[modifier];
  52. if (positiveValue !== undefined) {
  53. return (0, _negateValue.default)(positiveValue);
  54. }
  55. if (isArbitraryValue(modifier)) {
  56. let resolved = resolveArbitraryValue(modifier, validate);
  57. if (resolved === undefined) {
  58. return undefined;
  59. }
  60. return (0, _negateValue.default)(resolved);
  61. }
  62. }
  63. function asValue(modifier, options = {}, { validate =()=>true } = {}) {
  64. var _options_values;
  65. let value = (_options_values = options.values) === null || _options_values === void 0 ? void 0 : _options_values[modifier];
  66. if (value !== undefined) {
  67. return value;
  68. }
  69. if (options.supportsNegativeValues && modifier.startsWith("-")) {
  70. return asNegativeValue(modifier.slice(1), options.values, validate);
  71. }
  72. return resolveArbitraryValue(modifier, validate);
  73. }
  74. function isArbitraryValue(input) {
  75. return input.startsWith("[") && input.endsWith("]");
  76. }
  77. function splitUtilityModifier(modifier) {
  78. let slashIdx = modifier.lastIndexOf("/");
  79. if (slashIdx === -1 || slashIdx === modifier.length - 1) {
  80. return [
  81. modifier,
  82. undefined
  83. ];
  84. }
  85. let arbitrary = isArbitraryValue(modifier);
  86. // The modifier could be of the form `[foo]/[bar]`
  87. // We want to handle this case properly
  88. // without affecting `[foo/bar]`
  89. if (arbitrary && !modifier.includes("]/[")) {
  90. return [
  91. modifier,
  92. undefined
  93. ];
  94. }
  95. return [
  96. modifier.slice(0, slashIdx),
  97. modifier.slice(slashIdx + 1)
  98. ];
  99. }
  100. function parseColorFormat(value) {
  101. if (typeof value === "string" && value.includes("<alpha-value>")) {
  102. let oldValue = value;
  103. return ({ opacityValue =1 })=>oldValue.replace("<alpha-value>", opacityValue);
  104. }
  105. return value;
  106. }
  107. function unwrapArbitraryModifier(modifier) {
  108. modifier = modifier.slice(1, -1);
  109. if (modifier.startsWith("--")) {
  110. modifier = `var(${modifier})`;
  111. }
  112. return modifier;
  113. }
  114. function asColor(modifier, options = {}, { tailwindConfig ={} } = {}) {
  115. var _options_values;
  116. if (((_options_values = options.values) === null || _options_values === void 0 ? void 0 : _options_values[modifier]) !== undefined) {
  117. var _options_values1;
  118. return parseColorFormat((_options_values1 = options.values) === null || _options_values1 === void 0 ? void 0 : _options_values1[modifier]);
  119. }
  120. // TODO: Hoist this up to getMatchingTypes or something
  121. // We do this here because we need the alpha value (if any)
  122. let [color, alpha] = splitUtilityModifier(modifier);
  123. if (alpha !== undefined) {
  124. var _options_values2, _tailwindConfig_theme, _tailwindConfig_theme_opacity;
  125. var _options_values_color;
  126. let normalizedColor = (_options_values_color = (_options_values2 = options.values) === null || _options_values2 === void 0 ? void 0 : _options_values2[color]) !== null && _options_values_color !== void 0 ? _options_values_color : isArbitraryValue(color) ? color.slice(1, -1) : undefined;
  127. if (normalizedColor === undefined) {
  128. return undefined;
  129. }
  130. normalizedColor = parseColorFormat(normalizedColor);
  131. if (isArbitraryValue(alpha)) {
  132. return (0, _withAlphaVariable.withAlphaValue)(normalizedColor, unwrapArbitraryModifier(alpha));
  133. }
  134. if (((_tailwindConfig_theme = tailwindConfig.theme) === null || _tailwindConfig_theme === void 0 ? void 0 : (_tailwindConfig_theme_opacity = _tailwindConfig_theme.opacity) === null || _tailwindConfig_theme_opacity === void 0 ? void 0 : _tailwindConfig_theme_opacity[alpha]) === undefined) {
  135. return undefined;
  136. }
  137. return (0, _withAlphaVariable.withAlphaValue)(normalizedColor, tailwindConfig.theme.opacity[alpha]);
  138. }
  139. return asValue(modifier, options, {
  140. validate: _dataTypes.color
  141. });
  142. }
  143. function asLookupValue(modifier, options = {}) {
  144. var _options_values;
  145. return (_options_values = options.values) === null || _options_values === void 0 ? void 0 : _options_values[modifier];
  146. }
  147. function guess(validate) {
  148. return (modifier, options)=>{
  149. return asValue(modifier, options, {
  150. validate
  151. });
  152. };
  153. }
  154. let typeMap = {
  155. any: asValue,
  156. color: asColor,
  157. url: guess(_dataTypes.url),
  158. image: guess(_dataTypes.image),
  159. length: guess(_dataTypes.length),
  160. percentage: guess(_dataTypes.percentage),
  161. position: guess(_dataTypes.position),
  162. lookup: asLookupValue,
  163. "generic-name": guess(_dataTypes.genericName),
  164. "family-name": guess(_dataTypes.familyName),
  165. number: guess(_dataTypes.number),
  166. "line-width": guess(_dataTypes.lineWidth),
  167. "absolute-size": guess(_dataTypes.absoluteSize),
  168. "relative-size": guess(_dataTypes.relativeSize),
  169. shadow: guess(_dataTypes.shadow),
  170. size: guess(_validateFormalSyntax.backgroundSize)
  171. };
  172. let supportedTypes = Object.keys(typeMap);
  173. function splitAtFirst(input, delim) {
  174. let idx = input.indexOf(delim);
  175. if (idx === -1) return [
  176. undefined,
  177. input
  178. ];
  179. return [
  180. input.slice(0, idx),
  181. input.slice(idx + 1)
  182. ];
  183. }
  184. function coerceValue(types, modifier, options, tailwindConfig) {
  185. if (options.values && modifier in options.values) {
  186. for (let { type } of types !== null && types !== void 0 ? types : []){
  187. let result = typeMap[type](modifier, options, {
  188. tailwindConfig
  189. });
  190. if (result === undefined) {
  191. continue;
  192. }
  193. return [
  194. result,
  195. type,
  196. null
  197. ];
  198. }
  199. }
  200. if (isArbitraryValue(modifier)) {
  201. let arbitraryValue = modifier.slice(1, -1);
  202. let [explicitType, value] = splitAtFirst(arbitraryValue, ":");
  203. // It could be that this resolves to `url(https` which is not a valid
  204. // identifier. We currently only support "simple" words with dashes or
  205. // underscores. E.g.: family-name
  206. if (!/^[\w-_]+$/g.test(explicitType)) {
  207. value = arbitraryValue;
  208. } else if (explicitType !== undefined && !supportedTypes.includes(explicitType)) {
  209. return [];
  210. }
  211. if (value.length > 0 && supportedTypes.includes(explicitType)) {
  212. return [
  213. asValue(`[${value}]`, options),
  214. explicitType,
  215. null
  216. ];
  217. }
  218. }
  219. let matches = getMatchingTypes(types, modifier, options, tailwindConfig);
  220. // Find first matching type
  221. for (let match of matches){
  222. return match;
  223. }
  224. return [];
  225. }
  226. function* getMatchingTypes(types, rawModifier, options, tailwindConfig) {
  227. let modifiersEnabled = (0, _featureFlagsJs.flagEnabled)(tailwindConfig, "generalizedModifiers");
  228. let [modifier, utilityModifier] = splitUtilityModifier(rawModifier);
  229. let canUseUtilityModifier = modifiersEnabled && options.modifiers != null && (options.modifiers === "any" || typeof options.modifiers === "object" && (utilityModifier && isArbitraryValue(utilityModifier) || utilityModifier in options.modifiers));
  230. if (!canUseUtilityModifier) {
  231. modifier = rawModifier;
  232. utilityModifier = undefined;
  233. }
  234. if (utilityModifier !== undefined && modifier === "") {
  235. modifier = "DEFAULT";
  236. }
  237. // Check the full value first
  238. // TODO: Move to asValue… somehow
  239. if (utilityModifier !== undefined) {
  240. if (typeof options.modifiers === "object") {
  241. var _options_modifiers;
  242. var _options_modifiers_utilityModifier;
  243. let configValue = (_options_modifiers_utilityModifier = (_options_modifiers = options.modifiers) === null || _options_modifiers === void 0 ? void 0 : _options_modifiers[utilityModifier]) !== null && _options_modifiers_utilityModifier !== void 0 ? _options_modifiers_utilityModifier : null;
  244. if (configValue !== null) {
  245. utilityModifier = configValue;
  246. } else if (isArbitraryValue(utilityModifier)) {
  247. utilityModifier = unwrapArbitraryModifier(utilityModifier);
  248. }
  249. }
  250. }
  251. for (let { type } of types !== null && types !== void 0 ? types : []){
  252. let result = typeMap[type](modifier, options, {
  253. tailwindConfig
  254. });
  255. if (result === undefined) {
  256. continue;
  257. }
  258. yield [
  259. result,
  260. type,
  261. utilityModifier !== null && utilityModifier !== void 0 ? utilityModifier : null
  262. ];
  263. }
  264. }