Ei kuvausta

pluginUtils.js 10KB

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