isomorfeus-preact 10.8.2 → 22.9.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +14 -3
  3. data/README.md +23 -39
  4. data/lib/browser/delegate_native.rb +94 -72
  5. data/lib/browser/document.rb +38 -0
  6. data/lib/browser/element.rb +160 -176
  7. data/lib/browser/event.rb +99 -94
  8. data/lib/browser/history.rb +40 -20
  9. data/lib/browser/location.rb +59 -0
  10. data/lib/browser/window.rb +58 -0
  11. data/lib/data_uri/open_uri.rb +18 -0
  12. data/lib/data_uri/uri.rb +61 -0
  13. data/lib/data_uri.rb +4 -0
  14. data/lib/isomorfeus/preact/config.rb +42 -54
  15. data/lib/isomorfeus/preact/imports.rb +5 -20
  16. data/lib/isomorfeus/preact/ssr.rb +34 -0
  17. data/lib/isomorfeus/preact/thread_local_component_cache.rb +9 -11
  18. data/lib/isomorfeus/preact/version.rb +5 -0
  19. data/lib/isomorfeus/preact/view_helper.rb +72 -0
  20. data/lib/isomorfeus/props/validator.rb +19 -11
  21. data/lib/isomorfeus/top_level.rb +55 -33
  22. data/lib/isomorfeus-preact.rb +78 -103
  23. data/lib/link.rb +52 -0
  24. data/lib/lucid_app.rb +117 -0
  25. data/lib/lucid_component.rb +154 -0
  26. data/lib/nano_css.rb +282 -465
  27. data/lib/preact/component.rb +222 -0
  28. data/lib/preact/component_resolution.rb +49 -83
  29. data/lib/preact/context.rb +68 -0
  30. data/lib/preact/elements.rb +2 -9
  31. data/lib/preact/module_component_resolution.rb +50 -0
  32. data/lib/preact/prop_declaration_mixin.rb +73 -0
  33. data/lib/preact.rb +1548 -257
  34. data/lib/redirect.rb +34 -0
  35. data/lib/route.rb +15 -0
  36. data/lib/router.rb +145 -0
  37. data/lib/switch.rb +70 -0
  38. metadata +42 -309
  39. data/lib/isomorfeus/preact/memcached_component_cache.rb +0 -19
  40. data/lib/isomorfeus/preact/redis_component_cache.rb +0 -19
  41. data/lib/isomorfeus/preact/ssr/history.rb +0 -23
  42. data/lib/isomorfeus/preact/ssr/render_core.rb +0 -117
  43. data/lib/isomorfeus/preact/ssr/top_level.rb +0 -37
  44. data/lib/isomorfeus/preact_view_helper.rb +0 -129
  45. data/lib/isomorfeus_preact/lucid_app/api.rb +0 -38
  46. data/lib/isomorfeus_preact/lucid_app/base.rb +0 -7
  47. data/lib/isomorfeus_preact/lucid_app/mixin.rb +0 -14
  48. data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +0 -101
  49. data/lib/isomorfeus_preact/lucid_component/api.rb +0 -123
  50. data/lib/isomorfeus_preact/lucid_component/app_store_proxy.rb +0 -32
  51. data/lib/isomorfeus_preact/lucid_component/base.rb +0 -7
  52. data/lib/isomorfeus_preact/lucid_component/class_store_proxy.rb +0 -37
  53. data/lib/isomorfeus_preact/lucid_component/initializer.rb +0 -9
  54. data/lib/isomorfeus_preact/lucid_component/mixin.rb +0 -13
  55. data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +0 -87
  56. data/lib/isomorfeus_preact/lucid_component/styles_wrapper.rb +0 -40
  57. data/lib/isomorfeus_preact/lucid_func/base.rb +0 -7
  58. data/lib/isomorfeus_preact/lucid_func/initializer.rb +0 -8
  59. data/lib/isomorfeus_preact/lucid_func/mixin.rb +0 -10
  60. data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +0 -56
  61. data/lib/isomorfeus_preact/preact/function_component/api.rb +0 -140
  62. data/lib/isomorfeus_preact/preact/function_component/base.rb +0 -7
  63. data/lib/isomorfeus_preact/preact/function_component/initializer.rb +0 -6
  64. data/lib/isomorfeus_preact/preact/function_component/mixin.rb +0 -8
  65. data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +0 -47
  66. data/lib/lucid_app/context.rb +0 -24
  67. data/lib/lucid_prop_declaration/mixin.rb +0 -126
  68. data/lib/preact/component/api.rb +0 -137
  69. data/lib/preact/component/base.rb +0 -7
  70. data/lib/preact/component/callbacks.rb +0 -111
  71. data/lib/preact/component/initializer.rb +0 -7
  72. data/lib/preact/component/mixin.rb +0 -11
  73. data/lib/preact/component/native_component_constructor.rb +0 -77
  74. data/lib/preact/context_wrapper.rb +0 -48
  75. data/lib/preact/native_constant_wrapper.rb +0 -29
  76. data/lib/preact/option_hooks.rb +0 -98
  77. data/lib/preact/params.rb +0 -16
  78. data/lib/preact/props.rb +0 -69
  79. data/lib/preact/ref.rb +0 -17
  80. data/lib/preact/state.rb +0 -87
  81. data/lib/preact/version.rb +0 -3
  82. data/node_modules/.package-lock.json +0 -38
  83. data/node_modules/preact/LICENSE +0 -21
  84. data/node_modules/preact/README.md +0 -188
  85. data/node_modules/preact/compat/LICENSE +0 -21
  86. data/node_modules/preact/compat/client.js +0 -19
  87. data/node_modules/preact/compat/client.mjs +0 -17
  88. data/node_modules/preact/compat/dist/compat.js +0 -2
  89. data/node_modules/preact/compat/dist/compat.js.map +0 -1
  90. data/node_modules/preact/compat/dist/compat.mjs +0 -2
  91. data/node_modules/preact/compat/dist/compat.module.js +0 -2
  92. data/node_modules/preact/compat/dist/compat.module.js.map +0 -1
  93. data/node_modules/preact/compat/dist/compat.umd.js +0 -2
  94. data/node_modules/preact/compat/dist/compat.umd.js.map +0 -1
  95. data/node_modules/preact/compat/jsx-dev-runtime.js +0 -3
  96. data/node_modules/preact/compat/jsx-dev-runtime.mjs +0 -3
  97. data/node_modules/preact/compat/jsx-runtime.js +0 -3
  98. data/node_modules/preact/compat/jsx-runtime.mjs +0 -3
  99. data/node_modules/preact/compat/package.json +0 -49
  100. data/node_modules/preact/compat/scheduler.js +0 -15
  101. data/node_modules/preact/compat/scheduler.mjs +0 -23
  102. data/node_modules/preact/compat/server.browser.js +0 -4
  103. data/node_modules/preact/compat/server.js +0 -15
  104. data/node_modules/preact/compat/server.mjs +0 -4
  105. data/node_modules/preact/compat/src/Children.js +0 -21
  106. data/node_modules/preact/compat/src/PureComponent.js +0 -15
  107. data/node_modules/preact/compat/src/forwardRef.js +0 -44
  108. data/node_modules/preact/compat/src/index.d.ts +0 -155
  109. data/node_modules/preact/compat/src/index.js +0 -187
  110. data/node_modules/preact/compat/src/internal.d.ts +0 -47
  111. data/node_modules/preact/compat/src/memo.js +0 -34
  112. data/node_modules/preact/compat/src/portals.js +0 -82
  113. data/node_modules/preact/compat/src/render.js +0 -238
  114. data/node_modules/preact/compat/src/suspense-list.d.ts +0 -14
  115. data/node_modules/preact/compat/src/suspense-list.js +0 -126
  116. data/node_modules/preact/compat/src/suspense.d.ts +0 -15
  117. data/node_modules/preact/compat/src/suspense.js +0 -270
  118. data/node_modules/preact/compat/src/util.js +0 -28
  119. data/node_modules/preact/compat/test-utils.js +0 -1
  120. data/node_modules/preact/debug/LICENSE +0 -21
  121. data/node_modules/preact/debug/dist/debug.js +0 -2
  122. data/node_modules/preact/debug/dist/debug.js.map +0 -1
  123. data/node_modules/preact/debug/dist/debug.mjs +0 -2
  124. data/node_modules/preact/debug/dist/debug.module.js +0 -2
  125. data/node_modules/preact/debug/dist/debug.module.js.map +0 -1
  126. data/node_modules/preact/debug/dist/debug.umd.js +0 -2
  127. data/node_modules/preact/debug/dist/debug.umd.js.map +0 -1
  128. data/node_modules/preact/debug/package.json +0 -26
  129. data/node_modules/preact/debug/src/check-props.js +0 -54
  130. data/node_modules/preact/debug/src/component-stack.js +0 -146
  131. data/node_modules/preact/debug/src/constants.js +0 -3
  132. data/node_modules/preact/debug/src/debug.js +0 -437
  133. data/node_modules/preact/debug/src/index.js +0 -6
  134. data/node_modules/preact/debug/src/internal.d.ts +0 -82
  135. data/node_modules/preact/debug/src/util.js +0 -11
  136. data/node_modules/preact/devtools/LICENSE +0 -21
  137. data/node_modules/preact/devtools/dist/devtools.js +0 -2
  138. data/node_modules/preact/devtools/dist/devtools.js.map +0 -1
  139. data/node_modules/preact/devtools/dist/devtools.mjs +0 -2
  140. data/node_modules/preact/devtools/dist/devtools.module.js +0 -2
  141. data/node_modules/preact/devtools/dist/devtools.module.js.map +0 -1
  142. data/node_modules/preact/devtools/dist/devtools.umd.js +0 -2
  143. data/node_modules/preact/devtools/dist/devtools.umd.js.map +0 -1
  144. data/node_modules/preact/devtools/package.json +0 -25
  145. data/node_modules/preact/devtools/src/devtools.js +0 -10
  146. data/node_modules/preact/devtools/src/index.d.ts +0 -8
  147. data/node_modules/preact/devtools/src/index.js +0 -15
  148. data/node_modules/preact/dist/preact.js +0 -2
  149. data/node_modules/preact/dist/preact.js.map +0 -1
  150. data/node_modules/preact/dist/preact.min.js +0 -2
  151. data/node_modules/preact/dist/preact.min.js.map +0 -1
  152. data/node_modules/preact/dist/preact.mjs +0 -2
  153. data/node_modules/preact/dist/preact.module.js +0 -2
  154. data/node_modules/preact/dist/preact.module.js.map +0 -1
  155. data/node_modules/preact/dist/preact.umd.js +0 -2
  156. data/node_modules/preact/dist/preact.umd.js.map +0 -1
  157. data/node_modules/preact/hooks/LICENSE +0 -21
  158. data/node_modules/preact/hooks/dist/hooks.js +0 -2
  159. data/node_modules/preact/hooks/dist/hooks.js.map +0 -1
  160. data/node_modules/preact/hooks/dist/hooks.mjs +0 -2
  161. data/node_modules/preact/hooks/dist/hooks.module.js +0 -2
  162. data/node_modules/preact/hooks/dist/hooks.module.js.map +0 -1
  163. data/node_modules/preact/hooks/dist/hooks.umd.js +0 -2
  164. data/node_modules/preact/hooks/dist/hooks.umd.js.map +0 -1
  165. data/node_modules/preact/hooks/package.json +0 -35
  166. data/node_modules/preact/hooks/src/index.d.ts +0 -139
  167. data/node_modules/preact/hooks/src/index.js +0 -417
  168. data/node_modules/preact/hooks/src/internal.d.ts +0 -78
  169. data/node_modules/preact/jsx-runtime/LICENSE +0 -21
  170. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js +0 -2
  171. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +0 -1
  172. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.mjs +0 -2
  173. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js +0 -2
  174. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +0 -1
  175. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js +0 -2
  176. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +0 -1
  177. data/node_modules/preact/jsx-runtime/package.json +0 -28
  178. data/node_modules/preact/jsx-runtime/src/index.d.ts +0 -50
  179. data/node_modules/preact/jsx-runtime/src/index.js +0 -77
  180. data/node_modules/preact/package.json +0 -304
  181. data/node_modules/preact/src/cjs.js +0 -3
  182. data/node_modules/preact/src/clone-element.js +0 -34
  183. data/node_modules/preact/src/component.js +0 -225
  184. data/node_modules/preact/src/constants.js +0 -3
  185. data/node_modules/preact/src/create-context.js +0 -68
  186. data/node_modules/preact/src/create-element.js +0 -98
  187. data/node_modules/preact/src/diff/catch-error.js +0 -40
  188. data/node_modules/preact/src/diff/children.js +0 -335
  189. data/node_modules/preact/src/diff/index.js +0 -533
  190. data/node_modules/preact/src/diff/props.js +0 -158
  191. data/node_modules/preact/src/index.d.ts +0 -317
  192. data/node_modules/preact/src/index.js +0 -13
  193. data/node_modules/preact/src/internal.d.ts +0 -155
  194. data/node_modules/preact/src/jsx.d.ts +0 -1013
  195. data/node_modules/preact/src/options.js +0 -16
  196. data/node_modules/preact/src/render.js +0 -75
  197. data/node_modules/preact/src/util.js +0 -27
  198. data/node_modules/preact/test-utils/dist/testUtils.js +0 -2
  199. data/node_modules/preact/test-utils/dist/testUtils.js.map +0 -1
  200. data/node_modules/preact/test-utils/dist/testUtils.mjs +0 -2
  201. data/node_modules/preact/test-utils/dist/testUtils.module.js +0 -2
  202. data/node_modules/preact/test-utils/dist/testUtils.module.js.map +0 -1
  203. data/node_modules/preact/test-utils/dist/testUtils.umd.js +0 -2
  204. data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +0 -1
  205. data/node_modules/preact/test-utils/package.json +0 -28
  206. data/node_modules/preact/test-utils/src/index.d.ts +0 -3
  207. data/node_modules/preact/test-utils/src/index.js +0 -118
  208. data/node_modules/preact-render-to-string/LICENSE +0 -21
  209. data/node_modules/preact-render-to-string/README.md +0 -102
  210. data/node_modules/preact-render-to-string/dist/commonjs.js +0 -2
  211. data/node_modules/preact-render-to-string/dist/commonjs.js.map +0 -1
  212. data/node_modules/preact-render-to-string/dist/index.d.ts +0 -16
  213. data/node_modules/preact-render-to-string/dist/index.js +0 -1
  214. data/node_modules/preact-render-to-string/dist/index.js.map +0 -1
  215. data/node_modules/preact-render-to-string/dist/index.mjs +0 -2
  216. data/node_modules/preact-render-to-string/dist/index.module.js +0 -2
  217. data/node_modules/preact-render-to-string/dist/index.module.js.map +0 -1
  218. data/node_modules/preact-render-to-string/dist/jsx-entry.js +0 -2
  219. data/node_modules/preact-render-to-string/dist/jsx-entry.js.map +0 -1
  220. data/node_modules/preact-render-to-string/dist/jsx.d.ts +0 -13
  221. data/node_modules/preact-render-to-string/dist/jsx.js +0 -1
  222. data/node_modules/preact-render-to-string/dist/jsx.js.map +0 -1
  223. data/node_modules/preact-render-to-string/dist/jsx.mjs +0 -2
  224. data/node_modules/preact-render-to-string/dist/jsx.modern.js +0 -2
  225. data/node_modules/preact-render-to-string/dist/jsx.modern.js.map +0 -1
  226. data/node_modules/preact-render-to-string/dist/jsx.module.js +0 -2
  227. data/node_modules/preact-render-to-string/dist/jsx.module.js.map +0 -1
  228. data/node_modules/preact-render-to-string/dist/preact-render-to-string-tests.d.ts +0 -1
  229. data/node_modules/preact-render-to-string/jsx.js +0 -1
  230. data/node_modules/preact-render-to-string/package.json +0 -142
  231. data/node_modules/preact-render-to-string/src/index.d.ts +0 -16
  232. data/node_modules/preact-render-to-string/src/index.js +0 -462
  233. data/node_modules/preact-render-to-string/src/jsx.d.ts +0 -13
  234. data/node_modules/preact-render-to-string/src/jsx.js +0 -76
  235. data/node_modules/preact-render-to-string/src/polyfills.js +0 -8
  236. data/node_modules/preact-render-to-string/src/preact-render-to-string-tests.d.ts +0 -1
  237. data/node_modules/preact-render-to-string/src/util.js +0 -78
  238. data/node_modules/preact-render-to-string/typings.json +0 -5
  239. data/node_modules/pretty-format/.npmignore +0 -3
  240. data/node_modules/pretty-format/LICENSE.md +0 -15
  241. data/node_modules/pretty-format/README.md +0 -94
  242. data/node_modules/pretty-format/index.js +0 -343
  243. data/node_modules/pretty-format/package.json +0 -26
  244. data/node_modules/pretty-format/plugins/ReactElement.js +0 -74
  245. data/node_modules/pretty-format/plugins/ReactTestComponent.js +0 -58
  246. data/node_modules/pretty-format/printString.js +0 -7
  247. data/node_modules/wouter-preact/cjs/index.js +0 -180
  248. data/node_modules/wouter-preact/cjs/matcher.js +0 -72
  249. data/node_modules/wouter-preact/cjs/package.json +0 -1
  250. data/node_modules/wouter-preact/cjs/react-deps.js +0 -75
  251. data/node_modules/wouter-preact/cjs/static-location.js +0 -21
  252. data/node_modules/wouter-preact/cjs/use-location.js +0 -94
  253. data/node_modules/wouter-preact/index.d.ts +0 -110
  254. data/node_modules/wouter-preact/index.js +0 -178
  255. data/node_modules/wouter-preact/matcher.d.ts +0 -30
  256. data/node_modules/wouter-preact/matcher.js +0 -66
  257. data/node_modules/wouter-preact/package.json +0 -33
  258. data/node_modules/wouter-preact/react-deps.js +0 -15
  259. data/node_modules/wouter-preact/static-location.d.ts +0 -16
  260. data/node_modules/wouter-preact/static-location.js +0 -17
  261. data/node_modules/wouter-preact/use-location.d.ts +0 -43
  262. data/node_modules/wouter-preact/use-location.js +0 -86
  263. data/package.json +0 -8
data/lib/nano_css.rb CHANGED
@@ -1,481 +1,298 @@
1
- module NanoCSS
2
- %x{
3
- var KEBAB_REGEX = /[A-Z]/g;
4
- var on_browser = #{on_browser?};
5
- var in_dev = #{Isomorfeus.development?};
6
-
7
- function hash(str) {
8
- var h = 5381, i = str.length;
9
- while (i) h = (h * 33) ^ str.charCodeAt(--i);
10
- return '_' + (h >>> 0).toString(36);
11
- };
12
-
13
- self.create = function (config) {
14
- config = config || {};
15
- var assign = config.assign || Object.assign;
16
- var client = typeof window === 'object';
17
-
18
- // Check if we are really in browser environment.
19
- if (on_browser && in_dev) {
20
- if (client) {
21
- if ((typeof document !== 'object') || !document.getElementsByTagName('HTML')) {
22
- console.error('nano-css detected browser environment because of "window" global, but "document" global seems to be defective.');
23
- }
24
- }
25
- }
26
-
27
- var renderer = assign({
28
- raw: '',
29
- pfx: '_',
30
- client: client,
31
- assign: assign,
32
- stringify: JSON.stringify,
33
- kebab: function (prop) {
34
- return prop.replace(KEBAB_REGEX, '-$&').toLowerCase();
35
- },
36
- decl: function (key, value) {
37
- key = renderer.kebab(key);
38
- return key + ':' + value + ';';
39
- },
40
- hash: function (obj) { return hash(renderer.stringify(obj)); },
41
- selector: function (parent, selector) {
42
- return parent + (selector[0] === ':' ? '' : ' ') + selector;
43
- },
44
- putRaw: function (rawCssRule) { renderer.raw += rawCssRule; }
45
- }, config);
46
-
47
- if (renderer.client) {
48
- if (!renderer.sh) { document.head.appendChild(renderer.sh = document.createElement('style')); }
49
-
50
- if (in_dev) {
51
- renderer.sh.setAttribute('data-nano-css-dev', '');
52
-
53
- // Test style sheet used in DEV mode to test if .insetRule() would throw.
54
- renderer.shTest = document.createElement('style');
55
- renderer.shTest.setAttribute('data-nano-css-dev-tests', '');
56
- document.head.appendChild(renderer.shTest);
57
- }
58
-
59
- renderer.putRaw = function (rawCssRule) {
60
- // .insertRule() is faster than .appendChild(), that's why we use it in PROD.
61
- // But CSS injected using .insertRule() is not displayed in Chrome Devtools
62
- var sheet = renderer.sh.sheet;
63
- if (!in_dev) {
64
- // Unknown pseudo-selectors will throw, this try/catch swallows all errors.
65
- try { sheet.insertRule(rawCssRule, sheet.cssRules.length); }
66
- catch (error) {}
67
- } else {
68
- // Test if .insertRule() works in dev mode. Unknown pseudo-selectors will throw when
69
- try {
70
- var testSheet = renderer.shTest.sheet;
71
- testSheet.insertRule(rawCssRule, testSheet.cssRules.length);
72
- testSheet.deleteRule(testSheet.cssRules.length - 1);
73
- } catch (error) {
74
- if (config.verbose) {
75
- console.error(error);
76
- }
77
- }
78
- sheet.insertRule(rawCssRule, sheet.cssRules.length);
79
- }
80
- };
81
- }
82
-
83
- renderer.put = function (selector, decls, atrule) {
84
- var str = '';
85
- var prop, value;
86
- var postponed = [];
87
-
88
- for (prop in decls) {
89
- value = decls[prop];
90
-
91
- if ((value instanceof Object) && !(value instanceof Array)) {
92
- postponed.push(prop);
93
- } else {
94
- if (in_dev && !renderer.sourcemaps) {
95
- str += ' ' + renderer.decl(prop, value, selector, atrule) + '\n';
96
- } else {
97
- str += renderer.decl(prop, value, selector, atrule);
98
- }
99
- }
100
- }
101
-
102
- if (str) {
103
- if (in_dev && !renderer.sourcemaps) {
104
- str = '\n' + selector + ' {\n' + str + '}\n';
105
- } else {
106
- str = selector + '{' + str + '}';
107
- }
108
- renderer.putRaw(atrule ? atrule + '{' + str + '}' : str);
109
- }
110
-
111
- for (var i = 0; i < postponed.length; i++) {
112
- prop = postponed[i];
113
-
114
- if (prop[0] === '@' && prop !== '@font-face') {
115
- renderer.putAt(selector, decls[prop], prop);
116
- } else {
117
- renderer.put(renderer.selector(selector, prop), decls[prop], atrule);
118
- }
119
- }
120
- };
121
-
122
- renderer.putAt = renderer.put;
123
-
124
- return renderer;
125
- };
126
- }
1
+ class NanoCSS
2
+ KEBAB_REGEX = /[A-Z]/
3
+ UNITLESS_NUMBER_PROPS = %w[animation-iteration-count border-image-outset border-image-slice
4
+ border-image-width box-flex box-flex-group box-ordinal-group column-count columns flex
5
+ flex-grow flex-positive flex-shrink flex-negative flex-order grid-row grid-row-end
6
+ grid-row-span grid-row-start grid-column grid-column-end grid-column-span grid-column-start
7
+ font-weight line-clamp line-height opacity order orphans tabSize widows z-index zoom
8
+ fill-opacity flood-opacity stop-opacity stroke-dasharray stroke-dashoffset stroke-miterlimit
9
+ stroke-opacity stroke-width] # from fill-opacity onwards is for SVG
10
+
11
+ class << self
12
+ if RUBY_ENGINE == 'opal'
13
+ def instance
14
+ @instance
15
+ end
16
+
17
+ def instance=(i)
18
+ @instance = i
19
+ end
20
+ else
21
+ def instance
22
+ i = Thread.current[:@_isomorfeus_preact_nanocss_instance]
23
+ return i if i
24
+ @global_instance
25
+ end
26
+
27
+ def instance=(i)
28
+ @global_instance = i unless @global_instance
29
+ Thread.current[:@_isomorfeus_preact_nanocss_instance] = i
30
+ end
31
+
32
+ def global_instance
33
+ @global_instance
34
+ end
35
+ end
36
+ end
37
+
38
+ @@unitless_css_properties = nil
39
+
40
+ attr_reader :renderer
41
+
42
+ def initialize(config = nil, given_renderer: nil)
43
+ config = {} unless config
44
+ if RUBY_ENGINE == 'opal'
45
+ @client = `typeof window === "object"`
46
+ else
47
+ @client = false
48
+ end
49
+
50
+ if given_renderer
51
+ @renderer = given_renderer
52
+ else
53
+ @renderer = { raw: '', pfx: '_', hydrate_force_put: false, prefixes: ['-webkit-', '-moz-', '-o-', ''] }
54
+ @renderer.merge!(config)
55
+ end
56
+
57
+ @hydrated = {}
58
+
59
+ if @client
60
+ unless @renderer.key?(:sh)
61
+ e = `document.createElement('style')`
62
+ @renderer[:sh] = e
63
+ `document.head.appendChild(e)`
64
+ end
65
+
66
+ unless @renderer.key?(:kh)
67
+ e = `document.createElement('style')`
68
+ @renderer[:ksh] = e
69
+ `document.head.appendChild(e)`
70
+ end
71
+
72
+ hydrate(@renderer[:sh])
73
+ end
74
+
75
+ unless @@unitless_css_properties
76
+ @@unitless_css_properties = {}
77
+ UNITLESS_NUMBER_PROPS.each do |prop|
78
+ @@unitless_css_properties[prop] = 1
79
+ @@unitless_css_properties['-webkit-' + prop] = 1
80
+ @@unitless_css_properties['-ms-' + prop] = 1
81
+ @@unitless_css_properties['-moz-' + prop] = 1
82
+ @@unitless_css_properties['-o-' + prop] = 1
83
+ end
84
+ end
85
+
86
+ unless given_renderer
87
+ put('', { '@keyframes fadein' => { from: { opacity: 0 }, to: { opacity: 1 }},
88
+ '.fade_in' => { animation: 'fadein .4s linear' }})
89
+ put('', { '@keyframes fadeout' => { from: { opacity: 1 }, to: { opacity: 0 }},
90
+ '.fade_out' => { animation: 'fadeout .3s linear', 'animation-fill-mode' => 'forwards' }})
91
+ end
92
+ end
93
+
94
+ def decl(key, value)
95
+ key = kebab(key)
96
+
97
+ if value.is_a?(Numeric) && !@@unitless_css_properties.key?(key)
98
+ "#{key}:#{value}px;"
99
+ else
100
+ "#{key}:#{value};"
101
+ end
102
+ end
103
+
104
+ def hash(obj)
105
+ hash_str(JSON.dump(obj))
106
+ end
107
+
108
+ def hash_str(str)
109
+ h = 5381
110
+ str.each_codepoint do |cp|
111
+ h = (h * 33) ^ cp
112
+ end
113
+ '_' + h.abs.to_s(36)
114
+ end
115
+
116
+ def kebab(prop)
117
+ prop.to_s.gsub(KEBAB_REGEX, '-$&').downcase.to_sym
118
+ end
119
+
120
+ def put(css_selector, decls, atrule = nil)
121
+ return if @client && !@renderer[:hydrate_force_put] && @hydrated.key?(css_selector)
122
+
123
+ str = ''
124
+ postponed = []
125
+
126
+ decls.each do |prop, value|
127
+ if value.is_a?(Hash) && !value.is_a?(Array)
128
+ postponed.push(prop)
129
+ else
130
+ str += decl(prop, value)
131
+ end
132
+ end
133
+
134
+ unless str.empty?
135
+ str = css_selector + '{' + str + '}'
136
+ put_raw(atrule ? atrule + '{' + str + '}' : str)
137
+ end
138
+
139
+ postponed.each do |prop|
140
+ if prop[0] === '@' && prop != '@font-face'
141
+ put_at(css_selector, decls[prop], prop)
142
+ else
143
+ put(selector(css_selector, prop), decls[prop], atrule)
144
+ end
145
+ end
146
+ end
147
+
148
+ def put_at(_, keyframes, prelude)
149
+ if prelude[1] == 'k'
150
+ str = ''
151
+ keyframes.each do |keyframe, decls|
152
+ str_decls = ''
153
+ decls.each do |prop, value|
154
+ str_decls += decl(prop, value)
155
+ end
156
+ str += "#{keyframe}{#{str_decls}}"
157
+ end
158
+
159
+ @renderer[:prefixes].each do |prefix|
160
+ raw_key_frames = prelude.sub('@keyframes', "@#{prefix}keyframes") + "{#{str}}"
161
+ if @client
162
+ ksh = @renderer[:ksh]
163
+ `ksh.appendChild(document.createTextNode(raw_key_frames))`
164
+ else
165
+ put_raw(raw_key_frames)
166
+ end
167
+ end
168
+
169
+ return
170
+ end
171
+ put(nil, keyframes, prelude)
172
+ end
173
+
174
+ def put_raw(raw_css_rule)
175
+ @client ? put_raw_client(raw_css_rule) : put_raw_ssr(raw_css_rule)
176
+ end
177
+
178
+ def put_raw_ssr(raw_css_rule)
179
+ @renderer[:raw] << raw_css_rule
180
+ end
181
+
182
+ def put_raw_client(raw_css_rule)
183
+ # .insertRule() is faster than .appendChild(), that's why we use it in PROD.
184
+ # But CSS injected using .insertRule() is not displayed in Chrome Devtools
185
+ sheet = @renderer[:sh].JS[:sheet]
186
+ # Unknown pseudo-selectors will throw, this try/catch swallows all errors.
187
+ `sheet.insertRule(raw_css_rule, sheet.cssRules.length)` rescue nil
188
+ end
127
189
 
128
190
  # addons
129
191
 
130
- %x{
131
- self.rule = function (renderer) {
132
- if (in_dev) {
133
- renderer.rule_blocks = {};
134
- }
192
+ # rule
135
193
 
136
- renderer.delete_from_rule_blocks = function (rule_name) {
137
- rule_name = rule_name + '-';
138
- for(const rule in renderer.rule_blocks) {
139
- if (rule.startsWith(rule_name)) { delete renderer.rule_blocks[rule]; }
140
- }
141
- }
194
+ def rule(css, block = nil)
195
+ block = block || hash(css)
196
+ block = @renderer[:pfx] + block
197
+ put('.' + block, css)
142
198
 
143
- renderer.rule = function (css, block) {
144
- // Warn user if CSS selectors clash.
145
- if (in_dev) {
146
- if (block) {
147
- if (typeof block !== 'string') {
148
- throw new TypeError(
149
- 'nano-css block name must be a string. ' +
150
- 'For example, use nano.rule({color: "red"}, "RedText").'
151
- );
152
- }
153
-
154
- if (renderer.rule_blocks[block]) {
155
- console.error('nano-css block name "' + block + '" used more than once.');
156
- }
157
-
158
- renderer.rule_blocks[block] = 1;
159
- }
160
- }
199
+ ' ' + block
200
+ end
161
201
 
162
- block = block || renderer.hash(css);
163
- block = renderer.pfx + block;
164
- renderer.put('.' + block, css);
165
-
166
- return ' ' + block;
167
- };
168
- };
169
- }
170
-
171
- %x{
172
- self.sheet = function (renderer) {
173
- renderer.delete_from_sheet = function(rule_name) {
174
- let selector_rule_name = "._" + rule_name + "-";
175
- if (renderer.sh && renderer.sh.sheet) {
176
- let sheet = renderer.sh.sheet;
177
- let rules = sheet.cssRules;
178
- for (let i=rules.length-1;i>=0;i--) {
179
- let rule = rules.item(i);
180
- if (rule.cssText.includes(selector_rule_name)) {
181
- sheet.deleteRule(i);
182
- }
183
- }
184
- }
185
- }
202
+ # sheet
186
203
 
187
- renderer.sheet = function (map, block) {
188
- var result = {};
189
-
190
- if (!block) {
191
- block = renderer.hash(map);
192
- }
193
-
194
- var onElementModifier = function (elementModifier) {
195
- var styles = map[elementModifier];
196
-
197
- if (in_dev && renderer.sourcemaps) {
198
- // In dev mode emit CSS immediately to generate sourcemaps.
199
- result[elementModifier] = renderer.rule(styles, block + '-' + elementModifier);
200
- } else {
201
- Object.defineProperty(result, elementModifier, {
202
- configurable: true,
203
- enumerable: true,
204
- get: function () {
205
- var classNames = renderer.rule(styles, block + '-' + elementModifier);
206
-
207
- Object.defineProperty(result, elementModifier, {
208
- value: classNames,
209
- enumerable: true
210
- });
211
-
212
- return classNames;
213
- },
214
- });
215
- }
216
- };
217
-
218
- for (var elementModifier in map) {
219
- onElementModifier(elementModifier);
220
- }
221
-
222
- return result;
223
- };
224
- };
225
- }
226
-
227
- %x{
228
- self.nesting = function (renderer) {
229
- renderer.selector = function (parentSelectors, selector) {
230
- var parents = parentSelectors.split(',');
231
- var result = [];
232
- var selectors = selector.split(',');
233
- var len1 = parents.length;
234
- var len2 = selectors.length;
235
- var i, j, sel, pos, parent, replacedSelector;
236
-
237
- for (i = 0; i < len2; i++) {
238
- sel = selectors[i];
239
- pos = sel.indexOf('&');
240
-
241
- if (pos > -1) {
242
- for (j = 0; j < len1; j++) {
243
- parent = parents[j];
244
- replacedSelector = sel.replace(/&/g, parent);
245
- result.push(replacedSelector);
246
- }
247
- } else {
248
- for (j = 0; j < len1; j++) {
249
- parent = parents[j];
250
-
251
- if (parent) {
252
- result.push(parent + ' ' + sel);
253
- } else {
254
- result.push(sel);
255
- }
256
- }
204
+ def delete_from_sheet(rule_name)
205
+ selector_rule_name = "._" + rule_name + "-"
206
+ if renderer[:sh] && renderer[:sh].JS[:sheet]
207
+ sheet = renderer[:sh].JS[:sheet]
208
+ css_rules = sheet.JS[:cssRules]
209
+ %x{
210
+ let i = 0;
211
+ for(i=0; i<css_rules.length; i++) {
212
+ if (css_rules[i].cssText.includes(selector_rule_name)) {
213
+ sheet.deleteRule(i);
257
214
  }
258
215
  }
259
-
260
- return result.join(',');
261
- };
262
- };
263
- }
264
-
265
- %x{
266
- self.hydrate = function (renderer) {
267
- var hydrated = {};
268
- renderer.hydrate_force_put = false;
269
- renderer.hydrate = function (sh) {
270
- var cssRules = sh.cssRules || sh.sheet.cssRules;
271
-
272
- for (var i = 0; i < cssRules.length; i++)
273
- hydrated[cssRules[i].selectorText] = 1;
274
- };
275
-
276
- if (renderer.client) {
277
- if (renderer.sh) renderer.hydrate(renderer.sh);
278
-
279
- var put = renderer.put;
280
-
281
- renderer.put = function (selector, css) {
282
- if (!renderer.hydrate_force_put && selector in hydrated) return;
283
-
284
- put(selector, css);
285
- };
286
216
  }
287
- };
288
- }
289
-
290
- %x{
291
- var UNITLESS_NUMBER_PROPS = [
292
- 'animation-iteration-count',
293
- 'border-image-outset',
294
- 'border-image-slice',
295
- 'border-image-width',
296
- 'box-flex',
297
- 'box-flex-group',
298
- 'box-ordinal-group',
299
- 'column-count',
300
- 'columns',
301
- 'flex',
302
- 'flex-grow',
303
- 'flex-positive',
304
- 'flex-shrink',
305
- 'flex-negative',
306
- 'flex-order',
307
- 'grid-row',
308
- 'grid-row-end',
309
- 'grid-row-span',
310
- 'grid-row-start',
311
- 'grid-column',
312
- 'grid-column-end',
313
- 'grid-column-span',
314
- 'grid-column-start',
315
- 'font-weight',
316
- 'line-clamp',
317
- 'line-height',
318
- 'opacity',
319
- 'order',
320
- 'orphans',
321
- 'tabSize',
322
- 'widows',
323
- 'z-index',
324
- 'zoom',
325
-
326
- // SVG-related properties
327
- 'fill-opacity',
328
- 'flood-opacity',
329
- 'stop-opacity',
330
- 'stroke-dasharray',
331
- 'stroke-dashoffset',
332
- 'stroke-miterlimit',
333
- 'stroke-opacity',
334
- 'stroke-width',
335
- ];
336
-
337
- var unitlessCssProperties = {};
338
-
339
- for (var i = 0; i < UNITLESS_NUMBER_PROPS.length; i++) {
340
- var prop = UNITLESS_NUMBER_PROPS[i];
341
-
342
- unitlessCssProperties[prop] = 1;
343
- unitlessCssProperties['-webkit-' + prop] = 1;
344
- unitlessCssProperties['-ms-' + prop] = 1;
345
- unitlessCssProperties['-moz-' + prop] = 1;
346
- unitlessCssProperties['-o-' + prop] = 1;
347
- }
348
-
349
- self.unitless = function (renderer) {
350
- var decl = renderer.decl;
351
-
352
- renderer.decl = function (prop, value) {
353
- var str = decl(prop, value);
354
-
355
- if (typeof value === 'number') {
356
- var pos = str.indexOf(':');
357
- var propKebab = str.substr(0, pos);
358
-
359
- if (!unitlessCssProperties[propKebab]) {
360
- return decl(prop, value + 'px');
361
- }
362
- }
363
-
364
- return str;
365
- };
366
- };
367
- }
368
-
369
- %x{
370
- self.global = function (renderer) {
371
- var selector = renderer.selector;
372
-
373
- renderer.selector = function (parent, current) {
374
- if (parent.indexOf(':global') > -1) parent = '';
375
-
376
- return selector(parent, current);
377
- };
378
-
379
- renderer.global = function (css) {
380
- return renderer.put('', css);
381
- };
382
- };
383
- }
384
-
385
- %x{
386
- self.keyframes = function (renderer, config) {
387
- config = renderer.assign({
388
- prefixes: ['-webkit-', '-moz-', '-o-', ''],
389
- }, config || {});
390
-
391
- var prefixes = config.prefixes;
392
-
393
- if (renderer.client) {
394
- // Craete @keyframe Stylesheet `ksh`.
395
- document.head.appendChild(renderer.ksh = document.createElement('style'));
217
+ end
218
+ end
219
+
220
+ def on_element_modifier(element_modifier, map, block, result)
221
+ result[element_modifier] = rule(map[element_modifier], "#{block}-#{element_modifier}")
222
+ end
223
+
224
+ def sheet(map, block = nil)
225
+ result = {}
226
+ block = hash(map) unless block
227
+ map.each_key do |element_modifier|
228
+ on_element_modifier(element_modifier, map, block, result)
229
+ end
230
+ result
231
+ end
232
+
233
+ # nesting
234
+
235
+ def selector(parent_selectors, css_selector)
236
+ parent_selectors = '' if parent_selectors.include?(':global')
237
+ parents = parent_selectors.split(',')
238
+ selectors = css_selector.split(',')
239
+ result = []
240
+
241
+ selectors.each do |sel|
242
+ pos = sel.index('&')
243
+
244
+ if pos
245
+ if parents.empty?
246
+ replaced_selector = sel.gsub(/&/, parent)
247
+ result.push(replaced_selector)
248
+ else
249
+ parents.each do |parent|
250
+ replaced_selector = sel.gsub(/&/, parent)
251
+ result.push(replaced_selector)
252
+ end
253
+ end
254
+ else
255
+ if parents.empty?
256
+ result.push(sel)
257
+ else
258
+ parents.each do |parent|
259
+ result.push(parent + ' ' + sel)
260
+ end
261
+ end
262
+ end
263
+ end
264
+
265
+ return result.join(',')
266
+ end
267
+
268
+ # hydrate
269
+
270
+ def hydrate(sh)
271
+ css_rules = sh.JS[:cssRules] || sh.JS[:sheet].JS[:cssRules]
272
+
273
+ %x{
274
+ let i = 0;
275
+ let st;
276
+ for(i=0; i<css_rules.length; i++) {
277
+ st = css_rules[i].selectorText;
278
+ if (st) { #{@hydrated[`st`] = 1}; }
396
279
  }
280
+ }
281
+ end
397
282
 
398
- var putAt = renderer.putAt;
399
-
400
- renderer.putAt = function (__, keyframes, prelude) {
401
- // @keyframes
402
- if (prelude[1] === 'k') {
403
- var str = '';
404
-
405
- for (var keyframe in keyframes) {
406
- var decls = keyframes[keyframe];
407
- var strDecls = '';
408
-
409
- for (var prop in decls)
410
- strDecls += renderer.decl(prop, decls[prop]);
411
-
412
- str += keyframe + '{' + strDecls + '}';
413
- }
414
-
415
- for (var i = 0; i < prefixes.length; i++) {
416
- var prefix = prefixes[i];
417
- var rawKeyframes = prelude.replace('@keyframes', '@' + prefix + 'keyframes') + '{' + str + '}';
418
-
419
- if (renderer.client) {
420
- renderer.ksh.appendChild(document.createTextNode(rawKeyframes));
421
- } else {
422
- renderer.putRaw(rawKeyframes);
423
- }
424
- }
425
283
 
426
- return;
427
- }
284
+ # global
428
285
 
429
- putAt(__, keyframes, prelude);
430
- };
431
-
432
- renderer.keyframes = function (keyframes, block) {
433
- if (!block) block = renderer.hash(keyframes);
434
- block = renderer.pfx + block;
435
-
436
- renderer.putAt('', keyframes, '@keyframes ' + block);
437
-
438
- return block;
439
- };
440
- };
441
- }
442
-
443
- %x{
444
- self.fadeIn = function (renderer) {
445
- renderer.put('', {
446
- '@keyframes fadeIn': {
447
- from: {
448
- opacity: 0,
449
- },
450
- to: {
451
- opacity: 1,
452
- }
453
- },
286
+ def global(css)
287
+ put('', css)
288
+ end
454
289
 
455
- '.fadeIn': {
456
- animation: 'fadeIn .4s linear',
457
- }
458
- });
459
- };
460
- }
461
-
462
- %x{
463
- self.fadeOut = function (renderer) {
464
- renderer.put('', {
465
- '@keyframes fadeOut': {
466
- from: {
467
- opacity: 1,
468
- },
469
- to: {
470
- opacity: 0,
471
- }
472
- },
290
+ # keyframes
473
291
 
474
- '.fadeOut': {
475
- animation: 'fadeOut .3s linear',
476
- 'animation-fill-mode': 'forwards',
477
- }
478
- });
479
- };
480
- }
481
- end
292
+ def keyframes(keyframes, cblock = nil)
293
+ block = hash(keyframes) unless block
294
+ block = @renderer[:pfx] + block
295
+ put_at('', keyframes, '@keyframes' + block)
296
+ block
297
+ end
298
+ end