isomorfeus-preact 10.9.0 → 22.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) 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. data/opal/iso_uri.rb +29 -0
  39. data/opal/uri/common.rb +18 -0
  40. data/opal/uri/generic.rb +47 -0
  41. metadata +45 -309
  42. data/lib/isomorfeus/preact/memcached_component_cache.rb +0 -19
  43. data/lib/isomorfeus/preact/redis_component_cache.rb +0 -19
  44. data/lib/isomorfeus/preact/ssr/history.rb +0 -23
  45. data/lib/isomorfeus/preact/ssr/render_core.rb +0 -117
  46. data/lib/isomorfeus/preact/ssr/top_level.rb +0 -37
  47. data/lib/isomorfeus/preact_view_helper.rb +0 -129
  48. data/lib/isomorfeus_preact/lucid_app/api.rb +0 -38
  49. data/lib/isomorfeus_preact/lucid_app/base.rb +0 -7
  50. data/lib/isomorfeus_preact/lucid_app/mixin.rb +0 -14
  51. data/lib/isomorfeus_preact/lucid_app/native_component_constructor.rb +0 -101
  52. data/lib/isomorfeus_preact/lucid_component/api.rb +0 -123
  53. data/lib/isomorfeus_preact/lucid_component/app_store_proxy.rb +0 -32
  54. data/lib/isomorfeus_preact/lucid_component/base.rb +0 -7
  55. data/lib/isomorfeus_preact/lucid_component/class_store_proxy.rb +0 -37
  56. data/lib/isomorfeus_preact/lucid_component/initializer.rb +0 -9
  57. data/lib/isomorfeus_preact/lucid_component/mixin.rb +0 -13
  58. data/lib/isomorfeus_preact/lucid_component/native_component_constructor.rb +0 -87
  59. data/lib/isomorfeus_preact/lucid_component/styles_wrapper.rb +0 -40
  60. data/lib/isomorfeus_preact/lucid_func/base.rb +0 -7
  61. data/lib/isomorfeus_preact/lucid_func/initializer.rb +0 -8
  62. data/lib/isomorfeus_preact/lucid_func/mixin.rb +0 -10
  63. data/lib/isomorfeus_preact/lucid_func/native_component_constructor.rb +0 -56
  64. data/lib/isomorfeus_preact/preact/function_component/api.rb +0 -164
  65. data/lib/isomorfeus_preact/preact/function_component/base.rb +0 -7
  66. data/lib/isomorfeus_preact/preact/function_component/initializer.rb +0 -6
  67. data/lib/isomorfeus_preact/preact/function_component/mixin.rb +0 -8
  68. data/lib/isomorfeus_preact/preact/function_component/native_component_constructor.rb +0 -47
  69. data/lib/lucid_app/context.rb +0 -24
  70. data/lib/lucid_prop_declaration/mixin.rb +0 -126
  71. data/lib/preact/component/api.rb +0 -137
  72. data/lib/preact/component/base.rb +0 -7
  73. data/lib/preact/component/callbacks.rb +0 -111
  74. data/lib/preact/component/initializer.rb +0 -7
  75. data/lib/preact/component/mixin.rb +0 -11
  76. data/lib/preact/component/native_component_constructor.rb +0 -77
  77. data/lib/preact/context_wrapper.rb +0 -48
  78. data/lib/preact/native_constant_wrapper.rb +0 -29
  79. data/lib/preact/option_hooks.rb +0 -98
  80. data/lib/preact/params.rb +0 -16
  81. data/lib/preact/props.rb +0 -69
  82. data/lib/preact/ref.rb +0 -17
  83. data/lib/preact/state.rb +0 -87
  84. data/lib/preact/version.rb +0 -3
  85. data/node_modules/.package-lock.json +0 -38
  86. data/node_modules/preact/LICENSE +0 -21
  87. data/node_modules/preact/README.md +0 -188
  88. data/node_modules/preact/compat/LICENSE +0 -21
  89. data/node_modules/preact/compat/client.js +0 -19
  90. data/node_modules/preact/compat/client.mjs +0 -17
  91. data/node_modules/preact/compat/dist/compat.js +0 -2
  92. data/node_modules/preact/compat/dist/compat.js.map +0 -1
  93. data/node_modules/preact/compat/dist/compat.mjs +0 -2
  94. data/node_modules/preact/compat/dist/compat.module.js +0 -2
  95. data/node_modules/preact/compat/dist/compat.module.js.map +0 -1
  96. data/node_modules/preact/compat/dist/compat.umd.js +0 -2
  97. data/node_modules/preact/compat/dist/compat.umd.js.map +0 -1
  98. data/node_modules/preact/compat/jsx-dev-runtime.js +0 -3
  99. data/node_modules/preact/compat/jsx-dev-runtime.mjs +0 -3
  100. data/node_modules/preact/compat/jsx-runtime.js +0 -3
  101. data/node_modules/preact/compat/jsx-runtime.mjs +0 -3
  102. data/node_modules/preact/compat/package.json +0 -49
  103. data/node_modules/preact/compat/scheduler.js +0 -15
  104. data/node_modules/preact/compat/scheduler.mjs +0 -23
  105. data/node_modules/preact/compat/server.browser.js +0 -4
  106. data/node_modules/preact/compat/server.js +0 -15
  107. data/node_modules/preact/compat/server.mjs +0 -4
  108. data/node_modules/preact/compat/src/Children.js +0 -21
  109. data/node_modules/preact/compat/src/PureComponent.js +0 -15
  110. data/node_modules/preact/compat/src/forwardRef.js +0 -44
  111. data/node_modules/preact/compat/src/index.d.ts +0 -164
  112. data/node_modules/preact/compat/src/index.js +0 -223
  113. data/node_modules/preact/compat/src/internal.d.ts +0 -47
  114. data/node_modules/preact/compat/src/memo.js +0 -34
  115. data/node_modules/preact/compat/src/portals.js +0 -82
  116. data/node_modules/preact/compat/src/render.js +0 -238
  117. data/node_modules/preact/compat/src/suspense-list.d.ts +0 -14
  118. data/node_modules/preact/compat/src/suspense-list.js +0 -126
  119. data/node_modules/preact/compat/src/suspense.d.ts +0 -15
  120. data/node_modules/preact/compat/src/suspense.js +0 -270
  121. data/node_modules/preact/compat/src/util.js +0 -28
  122. data/node_modules/preact/compat/test-utils.js +0 -1
  123. data/node_modules/preact/debug/LICENSE +0 -21
  124. data/node_modules/preact/debug/dist/debug.js +0 -2
  125. data/node_modules/preact/debug/dist/debug.js.map +0 -1
  126. data/node_modules/preact/debug/dist/debug.mjs +0 -2
  127. data/node_modules/preact/debug/dist/debug.module.js +0 -2
  128. data/node_modules/preact/debug/dist/debug.module.js.map +0 -1
  129. data/node_modules/preact/debug/dist/debug.umd.js +0 -2
  130. data/node_modules/preact/debug/dist/debug.umd.js.map +0 -1
  131. data/node_modules/preact/debug/package.json +0 -26
  132. data/node_modules/preact/debug/src/check-props.js +0 -54
  133. data/node_modules/preact/debug/src/component-stack.js +0 -146
  134. data/node_modules/preact/debug/src/constants.js +0 -3
  135. data/node_modules/preact/debug/src/debug.js +0 -437
  136. data/node_modules/preact/debug/src/index.js +0 -6
  137. data/node_modules/preact/debug/src/internal.d.ts +0 -82
  138. data/node_modules/preact/debug/src/util.js +0 -11
  139. data/node_modules/preact/devtools/LICENSE +0 -21
  140. data/node_modules/preact/devtools/dist/devtools.js +0 -2
  141. data/node_modules/preact/devtools/dist/devtools.js.map +0 -1
  142. data/node_modules/preact/devtools/dist/devtools.mjs +0 -2
  143. data/node_modules/preact/devtools/dist/devtools.module.js +0 -2
  144. data/node_modules/preact/devtools/dist/devtools.module.js.map +0 -1
  145. data/node_modules/preact/devtools/dist/devtools.umd.js +0 -2
  146. data/node_modules/preact/devtools/dist/devtools.umd.js.map +0 -1
  147. data/node_modules/preact/devtools/package.json +0 -25
  148. data/node_modules/preact/devtools/src/devtools.js +0 -10
  149. data/node_modules/preact/devtools/src/index.d.ts +0 -8
  150. data/node_modules/preact/devtools/src/index.js +0 -15
  151. data/node_modules/preact/dist/preact.js +0 -2
  152. data/node_modules/preact/dist/preact.js.map +0 -1
  153. data/node_modules/preact/dist/preact.min.js +0 -2
  154. data/node_modules/preact/dist/preact.min.js.map +0 -1
  155. data/node_modules/preact/dist/preact.mjs +0 -2
  156. data/node_modules/preact/dist/preact.module.js +0 -2
  157. data/node_modules/preact/dist/preact.module.js.map +0 -1
  158. data/node_modules/preact/dist/preact.umd.js +0 -2
  159. data/node_modules/preact/dist/preact.umd.js.map +0 -1
  160. data/node_modules/preact/hooks/LICENSE +0 -21
  161. data/node_modules/preact/hooks/dist/hooks.js +0 -2
  162. data/node_modules/preact/hooks/dist/hooks.js.map +0 -1
  163. data/node_modules/preact/hooks/dist/hooks.mjs +0 -2
  164. data/node_modules/preact/hooks/dist/hooks.module.js +0 -2
  165. data/node_modules/preact/hooks/dist/hooks.module.js.map +0 -1
  166. data/node_modules/preact/hooks/dist/hooks.umd.js +0 -2
  167. data/node_modules/preact/hooks/dist/hooks.umd.js.map +0 -1
  168. data/node_modules/preact/hooks/package.json +0 -35
  169. data/node_modules/preact/hooks/src/index.d.ts +0 -139
  170. data/node_modules/preact/hooks/src/index.js +0 -417
  171. data/node_modules/preact/hooks/src/internal.d.ts +0 -78
  172. data/node_modules/preact/jsx-runtime/LICENSE +0 -21
  173. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js +0 -2
  174. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.js.map +0 -1
  175. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.mjs +0 -2
  176. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js +0 -2
  177. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js.map +0 -1
  178. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js +0 -2
  179. data/node_modules/preact/jsx-runtime/dist/jsxRuntime.umd.js.map +0 -1
  180. data/node_modules/preact/jsx-runtime/package.json +0 -28
  181. data/node_modules/preact/jsx-runtime/src/index.d.ts +0 -50
  182. data/node_modules/preact/jsx-runtime/src/index.js +0 -77
  183. data/node_modules/preact/package.json +0 -304
  184. data/node_modules/preact/src/cjs.js +0 -3
  185. data/node_modules/preact/src/clone-element.js +0 -34
  186. data/node_modules/preact/src/component.js +0 -225
  187. data/node_modules/preact/src/constants.js +0 -3
  188. data/node_modules/preact/src/create-context.js +0 -68
  189. data/node_modules/preact/src/create-element.js +0 -98
  190. data/node_modules/preact/src/diff/catch-error.js +0 -40
  191. data/node_modules/preact/src/diff/children.js +0 -335
  192. data/node_modules/preact/src/diff/index.js +0 -533
  193. data/node_modules/preact/src/diff/props.js +0 -158
  194. data/node_modules/preact/src/index.d.ts +0 -317
  195. data/node_modules/preact/src/index.js +0 -13
  196. data/node_modules/preact/src/internal.d.ts +0 -155
  197. data/node_modules/preact/src/jsx.d.ts +0 -1014
  198. data/node_modules/preact/src/options.js +0 -16
  199. data/node_modules/preact/src/render.js +0 -75
  200. data/node_modules/preact/src/util.js +0 -27
  201. data/node_modules/preact/test-utils/dist/testUtils.js +0 -2
  202. data/node_modules/preact/test-utils/dist/testUtils.js.map +0 -1
  203. data/node_modules/preact/test-utils/dist/testUtils.mjs +0 -2
  204. data/node_modules/preact/test-utils/dist/testUtils.module.js +0 -2
  205. data/node_modules/preact/test-utils/dist/testUtils.module.js.map +0 -1
  206. data/node_modules/preact/test-utils/dist/testUtils.umd.js +0 -2
  207. data/node_modules/preact/test-utils/dist/testUtils.umd.js.map +0 -1
  208. data/node_modules/preact/test-utils/package.json +0 -28
  209. data/node_modules/preact/test-utils/src/index.d.ts +0 -3
  210. data/node_modules/preact/test-utils/src/index.js +0 -118
  211. data/node_modules/preact-render-to-string/LICENSE +0 -21
  212. data/node_modules/preact-render-to-string/README.md +0 -102
  213. data/node_modules/preact-render-to-string/dist/commonjs.js +0 -2
  214. data/node_modules/preact-render-to-string/dist/commonjs.js.map +0 -1
  215. data/node_modules/preact-render-to-string/dist/index.d.ts +0 -16
  216. data/node_modules/preact-render-to-string/dist/index.js +0 -1
  217. data/node_modules/preact-render-to-string/dist/index.js.map +0 -1
  218. data/node_modules/preact-render-to-string/dist/index.mjs +0 -2
  219. data/node_modules/preact-render-to-string/dist/index.module.js +0 -2
  220. data/node_modules/preact-render-to-string/dist/index.module.js.map +0 -1
  221. data/node_modules/preact-render-to-string/dist/jsx-entry.js +0 -2
  222. data/node_modules/preact-render-to-string/dist/jsx-entry.js.map +0 -1
  223. data/node_modules/preact-render-to-string/dist/jsx.d.ts +0 -13
  224. data/node_modules/preact-render-to-string/dist/jsx.js +0 -1
  225. data/node_modules/preact-render-to-string/dist/jsx.js.map +0 -1
  226. data/node_modules/preact-render-to-string/dist/jsx.mjs +0 -2
  227. data/node_modules/preact-render-to-string/dist/jsx.modern.js +0 -2
  228. data/node_modules/preact-render-to-string/dist/jsx.modern.js.map +0 -1
  229. data/node_modules/preact-render-to-string/dist/jsx.module.js +0 -2
  230. data/node_modules/preact-render-to-string/dist/jsx.module.js.map +0 -1
  231. data/node_modules/preact-render-to-string/dist/preact-render-to-string-tests.d.ts +0 -1
  232. data/node_modules/preact-render-to-string/jsx.js +0 -1
  233. data/node_modules/preact-render-to-string/package.json +0 -142
  234. data/node_modules/preact-render-to-string/src/index.d.ts +0 -16
  235. data/node_modules/preact-render-to-string/src/index.js +0 -462
  236. data/node_modules/preact-render-to-string/src/jsx.d.ts +0 -13
  237. data/node_modules/preact-render-to-string/src/jsx.js +0 -76
  238. data/node_modules/preact-render-to-string/src/polyfills.js +0 -8
  239. data/node_modules/preact-render-to-string/src/preact-render-to-string-tests.d.ts +0 -1
  240. data/node_modules/preact-render-to-string/src/util.js +0 -78
  241. data/node_modules/preact-render-to-string/typings.json +0 -5
  242. data/node_modules/pretty-format/.npmignore +0 -3
  243. data/node_modules/pretty-format/LICENSE.md +0 -15
  244. data/node_modules/pretty-format/README.md +0 -94
  245. data/node_modules/pretty-format/index.js +0 -343
  246. data/node_modules/pretty-format/package.json +0 -26
  247. data/node_modules/pretty-format/plugins/ReactElement.js +0 -74
  248. data/node_modules/pretty-format/plugins/ReactTestComponent.js +0 -58
  249. data/node_modules/pretty-format/printString.js +0 -7
  250. data/node_modules/wouter-preact/cjs/index.js +0 -180
  251. data/node_modules/wouter-preact/cjs/matcher.js +0 -72
  252. data/node_modules/wouter-preact/cjs/package.json +0 -1
  253. data/node_modules/wouter-preact/cjs/react-deps.js +0 -75
  254. data/node_modules/wouter-preact/cjs/static-location.js +0 -21
  255. data/node_modules/wouter-preact/cjs/use-location.js +0 -94
  256. data/node_modules/wouter-preact/index.d.ts +0 -110
  257. data/node_modules/wouter-preact/index.js +0 -178
  258. data/node_modules/wouter-preact/matcher.d.ts +0 -30
  259. data/node_modules/wouter-preact/matcher.js +0 -66
  260. data/node_modules/wouter-preact/package.json +0 -33
  261. data/node_modules/wouter-preact/react-deps.js +0 -15
  262. data/node_modules/wouter-preact/static-location.d.ts +0 -16
  263. data/node_modules/wouter-preact/static-location.js +0 -17
  264. data/node_modules/wouter-preact/use-location.d.ts +0 -43
  265. data/node_modules/wouter-preact/use-location.js +0 -86
  266. data/package.json +0 -8
@@ -1,176 +1,160 @@
1
- module Browser
2
- autoload :FileList, 'browser/file_list'
3
- autoload :Iterable, 'browser/iterable'
4
-
5
- # Wrap a native DOM element
6
- class Element
7
- include EventTarget
8
- include DelegateNative
9
-
10
- @tags = Hash.new(self)
11
-
12
- def self.element *tags, &block
13
- tags.each do |tag|
14
- @tags
15
- .fetch(tag) { @tags[tag] = const_set(tag.capitalize, Class.new(self)) }
16
- .class_exec(&block)
17
- end
18
- end
19
-
20
- def self.new(native)
21
- element = @tags[`(#{native}.tagName || '')`.downcase].allocate
22
- element.initialize native
23
- element
24
- end
25
-
26
- # @param native [JS] The native DOM element to wrap
27
- def initialize native
28
- @native = native
29
- end
30
-
31
- # Replace all child elements with the given element
32
- #
33
- # @param element [Browser::Element] The Browser element with which to replace
34
- # this element's contents
35
- def inner_dom= element
36
- clear
37
- append element
38
- end
39
-
40
- # The contents of this element as an HTML string
41
- #
42
- # @return [String] the HTML representation of this element's contents
43
- def inner_html
44
- `#@native.innerHTML`
45
- end
46
-
47
- # Use the supplied HTML string to replace this element's contents
48
- #
49
- # @param html [String] the HTML with which to replace this elements contents
50
- def inner_html= html
51
- `#@native.innerHTML = html`
52
- end
53
-
54
- # This element's direct child elements
55
- #
56
- # @return [Array<Browser::Element>] list of this element's children
57
- def children
58
- elements = []
59
-
60
- %x{
61
- var children = #@native.children;
62
- for(var i = 0; i < children.length; i++) {
63
- elements[i] = #{Element.new(`children[i]`)};
64
- }
65
- }
66
-
67
- elements
68
- end
69
-
70
- # Determine whether this element has any contents
71
- #
72
- # @return [Boolean] true if the element has no children, false otherwise
73
- def empty?
74
- `#@native.children.length === 0`
75
- end
76
-
77
- # Remove all contents from this element. After this call, `empty?` will
78
- # return `true`.
79
- #
80
- # @return [Browser::Element] self
81
- def clear
82
- if %w(input textarea).include? type
83
- `#@native.value = null`
84
- else
85
- children.each do |child|
86
- remove_child child
87
- end
88
- end
89
-
90
- self
91
- end
92
-
93
- # Remove the specified child element
94
- #
95
- # @param child [Browser::Element] the child element to remove
96
- def remove_child child
97
- `#@native.removeChild(child['native'] ? child['native'] : child)`
98
- end
99
-
100
- # This element's type. For example: "div", "span", "p"
101
- #
102
- # @return [String] the HTML tag name for this element
103
- def type
104
- `#@native.nodeName`.downcase
105
- end
106
-
107
- # Append the specified element as a child element
108
- #
109
- # @param element [Browser::Element, JS] the element to insert
110
- def append node
111
- `#@native.appendChild(node['native'] ? node['native'] : node)`
112
- self
113
- end
114
-
115
- # Methods for <input /> elements
116
-
117
- # A checkbox's checked status
118
- #
119
- # @return [Boolean] true if the checkbox is checked, false otherwise
120
- def checked?
121
- `!!#@native.checked`
122
- end
123
-
124
- # Get the currently selected file for this input. This is only useful for
125
- # file inputs without the `multiple` property set.
126
- #
127
- # @return [Browser::File] the file selected by the user
128
- def file
129
- files.first
130
- end
131
-
132
- # Get the currently selected files for this input. This is only useful for
133
- # file inputs with the `multiple` property set.
134
- #
135
- # @return [Browser::FileList] the currently selected files for this input
136
- def files
137
- FileList.new(`#@native.files`)
138
- end
139
-
140
- # Determine whether this is the same element
141
- #
142
- # @return [boolean] true if the element is the same, false otherwise
143
- def ==(other)
144
- `#@native === #{other.to_n}`
145
- end
146
-
147
- # Set the specified attribute to the specified value
148
- def []= attribute, value
149
- `#@native.setAttribute(#{attribute}, #{value})`
150
- end
151
-
152
- # Return the specified attribute
153
- #
154
- # @return [String] the value for the specified attribute
155
- def [] attribute
156
- `#@native.getAttribute(#{attribute})`
157
- end
158
-
159
- def query_selector selector
160
- result = super
161
-
162
- Element.new(result) if result
163
- end
164
-
165
- def query_selector_all selector
166
- Iterable.new(super).map { |element| Element.new(element) }
167
- end
168
-
169
- # The native representation of this element.
170
- #
171
- # @return [JS] the native element wrapped by this object.
172
- def to_n
173
- @native
174
- end
175
- end
176
- end
1
+ module Browser
2
+ autoload :FileList, 'browser/file_list'
3
+ autoload :Iterable, 'browser/iterable'
4
+
5
+ # Wrap a native DOM element
6
+ class Element
7
+ include EventTarget
8
+ include DelegateNative
9
+
10
+ @tags = Hash.new(self)
11
+
12
+ def self.element *tags, &block
13
+ tags.each do |tag|
14
+ @tags
15
+ .fetch(tag) { @tags[tag] = const_set(tag.capitalize, Class.new(self)) }
16
+ .class_exec(&block)
17
+ end
18
+ end
19
+
20
+ def self.new(native)
21
+ element = @tags[`(#{native}.tagName || '')`.downcase].allocate
22
+ element.initialize native
23
+ element
24
+ end
25
+
26
+ # @param native [JS] The native DOM element to wrap
27
+ def initialize native
28
+ @native = native
29
+ end
30
+
31
+ # Replace all child elements with the given element
32
+ #
33
+ # @param element [Browser::Element] The Browser element with which to replace
34
+ # this element's contents
35
+ def inner_dom= element
36
+ clear
37
+ append element
38
+ end
39
+
40
+ # The contents of this element as an HTML string
41
+ #
42
+ # @return [String] the HTML representation of this element's contents
43
+ def inner_html
44
+ `#@native.innerHTML`
45
+ end
46
+
47
+ def outer_html
48
+ `#@native.outerHTML`
49
+ end
50
+
51
+ # Use the supplied HTML string to replace this element's contents
52
+ #
53
+ # @param html [String] the HTML with which to replace this elements contents
54
+ def inner_html= html
55
+ `#@native.innerHTML = html`
56
+ end
57
+
58
+ # This element's direct child elements
59
+ #
60
+ # @return [Array<Browser::Element>] list of this element's children
61
+ def children
62
+ elements = []
63
+
64
+ %x{
65
+ var children = #@native.children;
66
+ for(var i = 0; i < children.length; i++) {
67
+ elements[i] = #{Element.new(`children[i]`)};
68
+ }
69
+ }
70
+
71
+ elements
72
+ end
73
+
74
+ # Determine whether this element has any contents
75
+ #
76
+ # @return [Boolean] true if the element has no children, false otherwise
77
+ def empty?
78
+ `#@native.children.length === 0`
79
+ end
80
+
81
+ # Remove all contents from this element. After this call, `empty?` will
82
+ # return `true`.
83
+ #
84
+ # @return [Browser::Element] self
85
+ def clear
86
+ if %w(input textarea).include? type
87
+ `#@native.value = null`
88
+ else
89
+ children.each do |child|
90
+ remove_child child
91
+ end
92
+ end
93
+
94
+ self
95
+ end
96
+
97
+ # This element's type. For example: "div", "span", "p"
98
+ #
99
+ # @return [String] the HTML tag name for this element
100
+ def type
101
+ `#@native.nodeName`.downcase
102
+ end
103
+
104
+ # Methods for <input /> elements
105
+
106
+ # A checkbox's checked status
107
+ #
108
+ # @return [Boolean] true if the checkbox is checked, false otherwise
109
+ def checked?
110
+ `!!#@native.checked`
111
+ end
112
+
113
+ # Get the currently selected file for this input. This is only useful for
114
+ # file inputs without the `multiple` property set.
115
+ #
116
+ # @return [Browser::File] the file selected by the user
117
+ def file
118
+ files.first
119
+ end
120
+
121
+ # Get the currently selected files for this input. This is only useful for
122
+ # file inputs with the `multiple` property set.
123
+ #
124
+ # @return [Browser::FileList] the currently selected files for this input
125
+ def files
126
+ FileList.new(`#@native.files`)
127
+ end
128
+
129
+ # Determine whether this is the same element
130
+ #
131
+ # @return [boolean] true if the element is the same, false otherwise
132
+ def ==(other)
133
+ `#@native === #{other.to_n}`
134
+ end
135
+
136
+ # Set the specified attribute to the specified value
137
+ def []= attribute, value
138
+ `#@native.setAttribute(#{attribute}, #{value})`
139
+ nil
140
+ end
141
+
142
+ # Return the specified attribute
143
+ #
144
+ # @return [String] the value for the specified attribute
145
+ def [] attribute
146
+ %x{
147
+ let val = #@native.getAttribute(#{attribute});
148
+ if (val == null) { val = nil; }
149
+ return val;
150
+ }
151
+ end
152
+
153
+ # The native representation of this element.
154
+ #
155
+ # @return [JS] the native element wrapped by this object.
156
+ def to_n
157
+ @native
158
+ end
159
+ end
160
+ end
data/lib/browser/event.rb CHANGED
@@ -1,94 +1,99 @@
1
- module Browser
2
- # Wrapper for JS events
3
- class Event
4
- # @param [JS] the native event to wrap
5
- def initialize native
6
- @native = native
7
- end
8
-
9
- # Prevent the runtime from executing this event's default behavior. For
10
- # example, prevent navigation after clicking a link.
11
- #
12
- # @return [Browser::Event] self
13
- def prevent
14
- `#@native.preventDefault()`
15
- self
16
- end
17
- alias_method :prevent_default, :prevent
18
-
19
- # Prevent the runtime from bubbling this event up the hierarchy. This is
20
- # typically used to keep an event local to the element on which it was
21
- # triggered, such as keeping a click event on a button from unintentionally
22
- # triggering a handler on a parent element.
23
- #
24
- # @return self
25
- def stop_propagation
26
- `#@native.stopPropagation()`
27
- self
28
- end
29
-
30
- # @return [Boolean] true if `prevent` has been called on this event, false
31
- # otherwise
32
- def prevented?
33
- `#@native.defaultPrevented`
34
- end
35
- alias_method :default_prevented?, :prevented?
36
-
37
- # @return [Boolean] true if the Meta/Command/Windows key was pressed when
38
- # this event fired, false otherwise
39
- def meta?
40
- `#@native.metaKey`
41
- end
42
-
43
- # @return [Boolean] true if the Shift key was pressed when this event fired,
44
- # false otherwise
45
- def shift?
46
- `#@native.shiftKey`
47
- end
48
-
49
- # @return [Boolean] true if the Ctrl key was pressed when this event fired,
50
- # false otherwise
51
- def ctrl?
52
- `#@native.ctrlKey`
53
- end
54
-
55
- # @return [Boolean] true if the Alt key was pressed when this event fired,
56
- # false otherwise
57
- def alt?
58
- `#@native.altKey`
59
- end
60
-
61
- # The target for this event
62
- #
63
- # @return [Browser::Element] the element on which this event was triggered
64
- # @todo Handle non-DOM events here
65
- def target
66
- Element.new(`#@native.target`)
67
- end
68
-
69
- # @return [Numeric] the key code associated with this event. Only useful for
70
- # keyboard-based events.
71
- def code
72
- `#@native.keyCode`
73
- end
74
-
75
- # Return properties on the event not covered by Ruby methods.
76
- def method_missing name, *args
77
- property = name.gsub(/_[a-z]/) { |match| match[-1, 1].upcase }
78
- value = `#@native[property]`
79
-
80
- if `!!value && #{Proc === value}`
81
- value.call(*args)
82
- elsif `value == null`
83
- nil
84
- else
85
- value
86
- end
87
- end
88
-
89
- # @return [JS] the native event wrapped by this object.
90
- def to_n
91
- @native
92
- end
93
- end
94
- end
1
+ module Browser
2
+ # Wrapper for JS events
3
+ class Event
4
+ # @param [JS] the native event to wrap
5
+ def initialize native_or_string
6
+ %x{
7
+ if (typeof native_or_string === "string") {
8
+ native_or_string = new Event(native_or_string);
9
+ }
10
+ }
11
+ @native = native_or_string
12
+ end
13
+
14
+ # Prevent the runtime from executing this event's default behavior. For
15
+ # example, prevent navigation after clicking a link.
16
+ #
17
+ # @return [Browser::Event] self
18
+ def prevent
19
+ `#@native.preventDefault()`
20
+ self
21
+ end
22
+ alias_method :prevent_default, :prevent
23
+
24
+ # Prevent the runtime from bubbling this event up the hierarchy. This is
25
+ # typically used to keep an event local to the element on which it was
26
+ # triggered, such as keeping a click event on a button from unintentionally
27
+ # triggering a handler on a parent element.
28
+ #
29
+ # @return self
30
+ def stop_propagation
31
+ `#@native.stopPropagation()`
32
+ self
33
+ end
34
+
35
+ # @return [Boolean] true if `prevent` has been called on this event, false
36
+ # otherwise
37
+ def prevented?
38
+ `#@native.defaultPrevented`
39
+ end
40
+ alias_method :default_prevented?, :prevented?
41
+
42
+ # @return [Boolean] true if the Meta/Command/Windows key was pressed when
43
+ # this event fired, false otherwise
44
+ def meta?
45
+ `#@native.metaKey`
46
+ end
47
+
48
+ # @return [Boolean] true if the Shift key was pressed when this event fired,
49
+ # false otherwise
50
+ def shift?
51
+ `#@native.shiftKey`
52
+ end
53
+
54
+ # @return [Boolean] true if the Ctrl key was pressed when this event fired,
55
+ # false otherwise
56
+ def ctrl?
57
+ `#@native.ctrlKey`
58
+ end
59
+
60
+ # @return [Boolean] true if the Alt key was pressed when this event fired,
61
+ # false otherwise
62
+ def alt?
63
+ `#@native.altKey`
64
+ end
65
+
66
+ # The target for this event
67
+ #
68
+ # @return [Browser::Element] the element on which this event was triggered
69
+ # @todo Handle non-DOM events here
70
+ def target
71
+ Element.new(`#@native.target`)
72
+ end
73
+
74
+ # @return [Numeric] the key code associated with this event. Only useful for
75
+ # keyboard-based events.
76
+ def code
77
+ `#@native.keyCode`
78
+ end
79
+
80
+ # Return properties on the event not covered by Ruby methods.
81
+ def method_missing name, *args
82
+ property = name.gsub(/_[a-z]/) { |match| match[-1, 1].upcase }
83
+ value = `#@native[property]`
84
+
85
+ if `!!value && #{Proc === value}`
86
+ value.call(*args)
87
+ elsif `value == null`
88
+ nil
89
+ else
90
+ value
91
+ end
92
+ end
93
+
94
+ # @return [JS] the native event wrapped by this object.
95
+ def to_n
96
+ @native
97
+ end
98
+ end
99
+ end
@@ -1,32 +1,52 @@
1
1
  module Browser
2
2
  class History
3
- include Native::Wrapper
3
+ if RUBY_ENGINE == 'opal'
4
+ include Native::Wrapper
4
5
 
5
- alias_native :back
6
- alias_native :forward
7
- alias_native :go
6
+ alias_native :back
7
+ alias_native :forward
8
+ alias_native :go
8
9
 
9
- native_reader :length
10
- alias :size :length
10
+ native_reader :length
11
+ alias :size :length
11
12
 
12
- def push_state(state, title = '', url = `null`)
13
- `#@native.pushState(#{state.to_n}, #{title}, #{url})`
14
- end
13
+ def push_state(state, title = '', url = `null`)
14
+ `#@native.pushState(#{state.to_n}, #{title}, #{url})`
15
+ end
15
16
 
16
- def replace_state(state, title = '', url = `null`)
17
- `#@native.replaceState(#{state.to_n}, #{title}, #{url})`
18
- end
17
+ def replace_state(state, title = '', url = `null`)
18
+ `#@native.replaceState(#{state.to_n}, #{title}, #{url})`
19
+ end
19
20
 
20
- def scroll_restoration
21
- `#@native.scrollRestoration`
22
- end
21
+ def scroll_restoration
22
+ `#@native.scrollRestoration`
23
+ end
23
24
 
24
- def scroll_restoration=(s)
25
- `#@native.scrollRestoration = #{s}`
26
- end
25
+ def scroll_restoration=(s)
26
+ `#@native.scrollRestoration = #{s}`
27
+ end
28
+
29
+ def state
30
+ ::Hash.new(`#@native.state`)
31
+ end
32
+ else
33
+ def back; end
34
+ def forward; end
35
+ def go(_); end
36
+
37
+ def length
38
+ 0
39
+ end
40
+ alias :size :length
41
+
42
+ def push_state(state, title = '', url = nil); end
43
+ def replace_state(state, title = '', url = nil); end
44
+ def scroll_restoration; end
45
+ def scroll_restoration=(s); end
27
46
 
28
- def state
29
- ::Hash.new(`#@native.state`)
47
+ def state
48
+ {}
49
+ end
30
50
  end
31
51
  end
32
52
  end
@@ -0,0 +1,59 @@
1
+ module Browser
2
+ class Location
3
+ if RUBY_ENGINE == 'opal'
4
+ include Native::Wrapper
5
+
6
+ alias_native :hash
7
+ alias_native :host
8
+ alias_native :hostname
9
+ alias_native :href
10
+ alias_native :origin
11
+ alias_native :pathname
12
+ alias_native :port
13
+ alias_native :protocol
14
+ alias_native :search
15
+ else
16
+ def initialize(location_string)
17
+ @location = URI(location_string)
18
+ rescue
19
+ @location = URI('http://localhost/')
20
+ end
21
+
22
+ def hash
23
+ @location.hash
24
+ end
25
+
26
+ def host
27
+ @location.host
28
+ end
29
+
30
+ def hostname
31
+ @location.hostname
32
+ end
33
+
34
+ def href
35
+ @location.to_s
36
+ end
37
+
38
+ def origin
39
+ @location.origin
40
+ end
41
+
42
+ def pathname
43
+ @location.path
44
+ end
45
+
46
+ def port
47
+ @location.port
48
+ end
49
+
50
+ def protocol
51
+ @location.scheme
52
+ end
53
+
54
+ def search
55
+ @location.query
56
+ end
57
+ end
58
+ end
59
+ end