Dashboard sipadu mbip
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.

serialize.browser.cjs.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  4. var hashString = _interopDefault(require('@emotion/hash'));
  5. var unitless = _interopDefault(require('@emotion/unitless'));
  6. var memoize = _interopDefault(require('@emotion/memoize'));
  7. var ILLEGAL_ESCAPE_SEQUENCE_ERROR = "You have illegal escape sequence in your template literal, most likely inside content's property value.\nBecause you write your CSS inside a JavaScript string you actually have to do double escaping, so for example \"content: '\\00d7';\" should become \"content: '\\\\00d7';\".\nYou can read more about this here:\nhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences";
  8. var UNDEFINED_AS_OBJECT_KEY_ERROR = "You have passed in falsy value as style object's key (can happen when in example you pass unexported component as computed key).";
  9. var hyphenateRegex = /[A-Z]|^ms/g;
  10. var animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g;
  11. var isCustomProperty = function isCustomProperty(property) {
  12. return property.charCodeAt(1) === 45;
  13. };
  14. var isProcessableValue = function isProcessableValue(value) {
  15. return value != null && typeof value !== 'boolean';
  16. };
  17. var processStyleName = memoize(function (styleName) {
  18. return isCustomProperty(styleName) ? styleName : styleName.replace(hyphenateRegex, '-$&').toLowerCase();
  19. });
  20. var processStyleValue = function processStyleValue(key, value) {
  21. switch (key) {
  22. case 'animation':
  23. case 'animationName':
  24. {
  25. if (typeof value === 'string') {
  26. return value.replace(animationRegex, function (match, p1, p2) {
  27. cursor = {
  28. name: p1,
  29. styles: p2,
  30. next: cursor
  31. };
  32. return p1;
  33. });
  34. }
  35. }
  36. }
  37. if (unitless[key] !== 1 && !isCustomProperty(key) && typeof value === 'number' && value !== 0) {
  38. return value + 'px';
  39. }
  40. return value;
  41. };
  42. if (process.env.NODE_ENV !== 'production') {
  43. var contentValuePattern = /(attr|calc|counters?|url)\(/;
  44. var contentValues = ['normal', 'none', 'counter', 'open-quote', 'close-quote', 'no-open-quote', 'no-close-quote', 'initial', 'inherit', 'unset'];
  45. var oldProcessStyleValue = processStyleValue;
  46. var msPattern = /^-ms-/;
  47. var hyphenPattern = /-(.)/g;
  48. var hyphenatedCache = {};
  49. processStyleValue = function processStyleValue(key, value) {
  50. if (key === 'content') {
  51. if (typeof value !== 'string' || contentValues.indexOf(value) === -1 && !contentValuePattern.test(value) && (value.charAt(0) !== value.charAt(value.length - 1) || value.charAt(0) !== '"' && value.charAt(0) !== "'")) {
  52. console.error("You seem to be using a value for 'content' without quotes, try replacing it with `content: '\"" + value + "\"'`");
  53. }
  54. }
  55. var processed = oldProcessStyleValue(key, value);
  56. if (processed !== '' && !isCustomProperty(key) && key.indexOf('-') !== -1 && hyphenatedCache[key] === undefined) {
  57. hyphenatedCache[key] = true;
  58. console.error("Using kebab-case for css properties in objects is not supported. Did you mean " + key.replace(msPattern, 'ms-').replace(hyphenPattern, function (str, _char) {
  59. return _char.toUpperCase();
  60. }) + "?");
  61. }
  62. return processed;
  63. };
  64. }
  65. var shouldWarnAboutInterpolatingClassNameFromCss = true;
  66. function handleInterpolation(mergedProps, registered, interpolation, couldBeSelectorInterpolation) {
  67. if (interpolation == null) {
  68. return '';
  69. }
  70. if (interpolation.__emotion_styles !== undefined) {
  71. if (process.env.NODE_ENV !== 'production' && interpolation.toString() === 'NO_COMPONENT_SELECTOR') {
  72. throw new Error('Component selectors can only be used in conjunction with babel-plugin-emotion.');
  73. }
  74. return interpolation;
  75. }
  76. switch (typeof interpolation) {
  77. case 'boolean':
  78. {
  79. return '';
  80. }
  81. case 'object':
  82. {
  83. if (interpolation.anim === 1) {
  84. cursor = {
  85. name: interpolation.name,
  86. styles: interpolation.styles,
  87. next: cursor
  88. };
  89. return interpolation.name;
  90. }
  91. if (interpolation.styles !== undefined) {
  92. var next = interpolation.next;
  93. if (next !== undefined) {
  94. // not the most efficient thing ever but this is a pretty rare case
  95. // and there will be very few iterations of this generally
  96. while (next !== undefined) {
  97. cursor = {
  98. name: next.name,
  99. styles: next.styles,
  100. next: cursor
  101. };
  102. next = next.next;
  103. }
  104. }
  105. var styles = interpolation.styles + ";";
  106. if (process.env.NODE_ENV !== 'production' && interpolation.map !== undefined) {
  107. styles += interpolation.map;
  108. }
  109. return styles;
  110. }
  111. return createStringFromObject(mergedProps, registered, interpolation);
  112. }
  113. case 'function':
  114. {
  115. if (mergedProps !== undefined) {
  116. var previousCursor = cursor;
  117. var result = interpolation(mergedProps);
  118. cursor = previousCursor;
  119. return handleInterpolation(mergedProps, registered, result, couldBeSelectorInterpolation);
  120. } else if (process.env.NODE_ENV !== 'production') {
  121. console.error('Functions that are interpolated in css calls will be stringified.\n' + 'If you want to have a css call based on props, create a function that returns a css call like this\n' + 'let dynamicStyle = (props) => css`color: ${props.color}`\n' + 'It can be called directly with props or interpolated in a styled call like this\n' + "let SomeComponent = styled('div')`${dynamicStyle}`");
  122. }
  123. break;
  124. }
  125. case 'string':
  126. if (process.env.NODE_ENV !== 'production') {
  127. var matched = [];
  128. var replaced = interpolation.replace(animationRegex, function (match, p1, p2) {
  129. var fakeVarName = "animation" + matched.length;
  130. matched.push("const " + fakeVarName + " = keyframes`" + p2.replace(/^@keyframes animation-\w+/, '') + "`");
  131. return "${" + fakeVarName + "}";
  132. });
  133. if (matched.length) {
  134. console.error('`keyframes` output got interpolated into plain string, please wrap it with `css`.\n\n' + 'Instead of doing this:\n\n' + [].concat(matched, ["`" + replaced + "`"]).join('\n') + '\n\nYou should wrap it with `css` like this:\n\n' + ("css`" + replaced + "`"));
  135. }
  136. }
  137. break;
  138. } // finalize string values (regular strings and functions interpolated into css calls)
  139. if (registered == null) {
  140. return interpolation;
  141. }
  142. var cached = registered[interpolation];
  143. if (process.env.NODE_ENV !== 'production' && couldBeSelectorInterpolation && shouldWarnAboutInterpolatingClassNameFromCss && cached !== undefined) {
  144. console.error('Interpolating a className from css`` is not recommended and will cause problems with composition.\n' + 'Interpolating a className from css`` will be completely unsupported in a future major version of Emotion');
  145. shouldWarnAboutInterpolatingClassNameFromCss = false;
  146. }
  147. return cached !== undefined && !couldBeSelectorInterpolation ? cached : interpolation;
  148. }
  149. function createStringFromObject(mergedProps, registered, obj) {
  150. var string = '';
  151. if (Array.isArray(obj)) {
  152. for (var i = 0; i < obj.length; i++) {
  153. string += handleInterpolation(mergedProps, registered, obj[i], false);
  154. }
  155. } else {
  156. for (var _key in obj) {
  157. var value = obj[_key];
  158. if (typeof value !== 'object') {
  159. if (registered != null && registered[value] !== undefined) {
  160. string += _key + "{" + registered[value] + "}";
  161. } else if (isProcessableValue(value)) {
  162. string += processStyleName(_key) + ":" + processStyleValue(_key, value) + ";";
  163. }
  164. } else {
  165. if (_key === 'NO_COMPONENT_SELECTOR' && process.env.NODE_ENV !== 'production') {
  166. throw new Error('Component selectors can only be used in conjunction with babel-plugin-emotion.');
  167. }
  168. if (Array.isArray(value) && typeof value[0] === 'string' && (registered == null || registered[value[0]] === undefined)) {
  169. for (var _i = 0; _i < value.length; _i++) {
  170. if (isProcessableValue(value[_i])) {
  171. string += processStyleName(_key) + ":" + processStyleValue(_key, value[_i]) + ";";
  172. }
  173. }
  174. } else {
  175. var interpolated = handleInterpolation(mergedProps, registered, value, false);
  176. switch (_key) {
  177. case 'animation':
  178. case 'animationName':
  179. {
  180. string += processStyleName(_key) + ":" + interpolated + ";";
  181. break;
  182. }
  183. default:
  184. {
  185. if (process.env.NODE_ENV !== 'production' && _key === 'undefined') {
  186. console.error(UNDEFINED_AS_OBJECT_KEY_ERROR);
  187. }
  188. string += _key + "{" + interpolated + "}";
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. return string;
  196. }
  197. var labelPattern = /label:\s*([^\s;\n{]+)\s*;/g;
  198. var sourceMapPattern;
  199. if (process.env.NODE_ENV !== 'production') {
  200. sourceMapPattern = /\/\*#\ssourceMappingURL=data:application\/json;\S+\s+\*\//;
  201. } // this is the cursor for keyframes
  202. // keyframes are stored on the SerializedStyles object as a linked list
  203. var cursor;
  204. var serializeStyles = function serializeStyles(args, registered, mergedProps) {
  205. if (args.length === 1 && typeof args[0] === 'object' && args[0] !== null && args[0].styles !== undefined) {
  206. return args[0];
  207. }
  208. var stringMode = true;
  209. var styles = '';
  210. cursor = undefined;
  211. var strings = args[0];
  212. if (strings == null || strings.raw === undefined) {
  213. stringMode = false;
  214. styles += handleInterpolation(mergedProps, registered, strings, false);
  215. } else {
  216. if (process.env.NODE_ENV !== 'production' && strings[0] === undefined) {
  217. console.error(ILLEGAL_ESCAPE_SEQUENCE_ERROR);
  218. }
  219. styles += strings[0];
  220. } // we start at 1 since we've already handled the first arg
  221. for (var i = 1; i < args.length; i++) {
  222. styles += handleInterpolation(mergedProps, registered, args[i], styles.charCodeAt(styles.length - 1) === 46);
  223. if (stringMode) {
  224. if (process.env.NODE_ENV !== 'production' && strings[i] === undefined) {
  225. console.error(ILLEGAL_ESCAPE_SEQUENCE_ERROR);
  226. }
  227. styles += strings[i];
  228. }
  229. }
  230. var sourceMap;
  231. if (process.env.NODE_ENV !== 'production') {
  232. styles = styles.replace(sourceMapPattern, function (match) {
  233. sourceMap = match;
  234. return '';
  235. });
  236. } // using a global regex with .exec is stateful so lastIndex has to be reset each time
  237. labelPattern.lastIndex = 0;
  238. var identifierName = '';
  239. var match; // https://esbench.com/bench/5b809c2cf2949800a0f61fb5
  240. while ((match = labelPattern.exec(styles)) !== null) {
  241. identifierName += '-' + // $FlowFixMe we know it's not null
  242. match[1];
  243. }
  244. var name = hashString(styles) + identifierName;
  245. if (process.env.NODE_ENV !== 'production') {
  246. // $FlowFixMe SerializedStyles type doesn't have toString property (and we don't want to add it)
  247. return {
  248. name: name,
  249. styles: styles,
  250. map: sourceMap,
  251. next: cursor,
  252. toString: function toString() {
  253. return "You have tried to stringify object returned from `css` function. It isn't supposed to be used directly (e.g. as value of the `className` prop), but rather handed to emotion so it can handle it (e.g. as value of `css` prop).";
  254. }
  255. };
  256. }
  257. return {
  258. name: name,
  259. styles: styles,
  260. next: cursor
  261. };
  262. };
  263. exports.serializeStyles = serializeStyles;