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.

cache.browser.esm.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { StyleSheet } from '@emotion/sheet';
  2. import Stylis from '@emotion/stylis';
  3. import '@emotion/weak-memoize';
  4. // https://github.com/thysultan/stylis.js/tree/master/plugins/rule-sheet
  5. // inlined to avoid umd wrapper and peerDep warnings/installing stylis
  6. // since we use stylis after closure compiler
  7. var delimiter = '/*|*/';
  8. var needle = delimiter + '}';
  9. function toSheet(block) {
  10. if (block) {
  11. Sheet.current.insert(block + '}');
  12. }
  13. }
  14. var Sheet = {
  15. current: null
  16. };
  17. var ruleSheet = function ruleSheet(context, content, selectors, parents, line, column, length, ns, depth, at) {
  18. switch (context) {
  19. // property
  20. case 1:
  21. {
  22. switch (content.charCodeAt(0)) {
  23. case 64:
  24. {
  25. // @import
  26. Sheet.current.insert(content + ';');
  27. return '';
  28. }
  29. // charcode for l
  30. case 108:
  31. {
  32. // charcode for b
  33. // this ignores label
  34. if (content.charCodeAt(2) === 98) {
  35. return '';
  36. }
  37. }
  38. }
  39. break;
  40. }
  41. // selector
  42. case 2:
  43. {
  44. if (ns === 0) return content + delimiter;
  45. break;
  46. }
  47. // at-rule
  48. case 3:
  49. {
  50. switch (ns) {
  51. // @font-face, @page
  52. case 102:
  53. case 112:
  54. {
  55. Sheet.current.insert(selectors[0] + content);
  56. return '';
  57. }
  58. default:
  59. {
  60. return content + (at === 0 ? delimiter : '');
  61. }
  62. }
  63. }
  64. case -2:
  65. {
  66. content.split(needle).forEach(toSheet);
  67. }
  68. }
  69. };
  70. var createCache = function createCache(options) {
  71. if (options === undefined) options = {};
  72. var key = options.key || 'css';
  73. var stylisOptions;
  74. if (options.prefix !== undefined) {
  75. stylisOptions = {
  76. prefix: options.prefix
  77. };
  78. }
  79. var stylis = new Stylis(stylisOptions);
  80. if (process.env.NODE_ENV !== 'production') {
  81. // $FlowFixMe
  82. if (/[^a-z-]/.test(key)) {
  83. throw new Error("Emotion key must only contain lower case alphabetical characters and - but \"" + key + "\" was passed");
  84. }
  85. }
  86. var inserted = {}; // $FlowFixMe
  87. var container;
  88. {
  89. container = options.container || document.head;
  90. var nodes = document.querySelectorAll("style[data-emotion-" + key + "]");
  91. Array.prototype.forEach.call(nodes, function (node) {
  92. var attrib = node.getAttribute("data-emotion-" + key); // $FlowFixMe
  93. attrib.split(' ').forEach(function (id) {
  94. inserted[id] = true;
  95. });
  96. if (node.parentNode !== container) {
  97. container.appendChild(node);
  98. }
  99. });
  100. }
  101. var _insert;
  102. {
  103. stylis.use(options.stylisPlugins)(ruleSheet);
  104. _insert = function insert(selector, serialized, sheet, shouldCache) {
  105. var name = serialized.name;
  106. Sheet.current = sheet;
  107. if (process.env.NODE_ENV !== 'production' && serialized.map !== undefined) {
  108. var map = serialized.map;
  109. Sheet.current = {
  110. insert: function insert(rule) {
  111. sheet.insert(rule + map);
  112. }
  113. };
  114. }
  115. stylis(selector, serialized.styles);
  116. if (shouldCache) {
  117. cache.inserted[name] = true;
  118. }
  119. };
  120. }
  121. if (process.env.NODE_ENV !== 'production') {
  122. // https://esbench.com/bench/5bf7371a4cd7e6009ef61d0a
  123. var commentStart = /\/\*/g;
  124. var commentEnd = /\*\//g;
  125. stylis.use(function (context, content) {
  126. switch (context) {
  127. case -1:
  128. {
  129. while (commentStart.test(content)) {
  130. commentEnd.lastIndex = commentStart.lastIndex;
  131. if (commentEnd.test(content)) {
  132. commentStart.lastIndex = commentEnd.lastIndex;
  133. continue;
  134. }
  135. throw new Error('Your styles have an unterminated comment ("/*" without corresponding "*/").');
  136. }
  137. commentStart.lastIndex = 0;
  138. break;
  139. }
  140. }
  141. });
  142. stylis.use(function (context, content, selectors) {
  143. switch (context) {
  144. case -1:
  145. {
  146. var flag = 'emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason';
  147. var unsafePseudoClasses = content.match(/(:first|:nth|:nth-last)-child/g);
  148. if (unsafePseudoClasses && cache.compat !== true) {
  149. unsafePseudoClasses.forEach(function (unsafePseudoClass) {
  150. var ignoreRegExp = new RegExp(unsafePseudoClass + ".*\\/\\* " + flag + " \\*\\/");
  151. var ignore = ignoreRegExp.test(content);
  152. if (unsafePseudoClass && !ignore) {
  153. console.error("The pseudo class \"" + unsafePseudoClass + "\" is potentially unsafe when doing server-side rendering. Try changing it to \"" + unsafePseudoClass.split('-child')[0] + "-of-type\".");
  154. }
  155. });
  156. }
  157. break;
  158. }
  159. }
  160. });
  161. }
  162. var cache = {
  163. key: key,
  164. sheet: new StyleSheet({
  165. key: key,
  166. container: container,
  167. nonce: options.nonce,
  168. speedy: options.speedy
  169. }),
  170. nonce: options.nonce,
  171. inserted: inserted,
  172. registered: {},
  173. insert: _insert
  174. };
  175. return cache;
  176. };
  177. export default createCache;