atome-opal-browser 0.3.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +33 -0
- data/Gemfile.lock +122 -0
- data/LICENSE +20 -0
- data/README.md +301 -0
- data/Rakefile +63 -0
- data/bin/rake +7 -0
- data/bin/setup +8 -0
- data/config.ru +81 -0
- data/docs/polyfills.md +24 -0
- data/examples/2048/Gemfile +13 -0
- data/examples/2048/Gemfile.lock +41 -0
- data/examples/2048/README.md +13 -0
- data/examples/2048/app/application.rb +169 -0
- data/examples/2048/config.ru +9 -0
- data/examples/canvas/Gemfile +9 -0
- data/examples/canvas/README.md +9 -0
- data/examples/canvas/app/application.rb +55 -0
- data/examples/canvas/config.ru +9 -0
- data/examples/component/Gemfile +9 -0
- data/examples/component/Gemfile.lock +45 -0
- data/examples/component/README.md +10 -0
- data/examples/component/app/application.rb +66 -0
- data/examples/component/config.ru +9 -0
- data/examples/integrations/README.md +24 -0
- data/examples/integrations/dynamic-rack-opal-sprockets-server/Gemfile +7 -0
- data/examples/integrations/dynamic-rack-opal-sprockets-server/README.md +16 -0
- data/examples/integrations/dynamic-rack-opal-sprockets-server/app/application.rb +6 -0
- data/examples/integrations/dynamic-rack-opal-sprockets-server/config.ru +9 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/.gitignore +1 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/Gemfile +8 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/README.md +22 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/Rakefile +4 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/app/application.rb +6 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/app.rb +32 -0
- data/examples/integrations/dynamic-roda-roda-sprockets/config.ru +3 -0
- data/examples/integrations/dynamic-roda-tilt/.gitignore +1 -0
- data/examples/integrations/dynamic-roda-tilt/Gemfile +9 -0
- data/examples/integrations/dynamic-roda-tilt/README.md +17 -0
- data/examples/integrations/dynamic-roda-tilt/Rakefile +6 -0
- data/examples/integrations/dynamic-roda-tilt/app/application.rb +6 -0
- data/examples/integrations/dynamic-roda-tilt/app.rb +50 -0
- data/examples/integrations/dynamic-roda-tilt/config.ru +3 -0
- data/examples/integrations/dynamic-sinatra-opal-sprockets-server/Gemfile +10 -0
- data/examples/integrations/dynamic-sinatra-opal-sprockets-server/README.md +16 -0
- data/examples/integrations/dynamic-sinatra-opal-sprockets-server/app/application.rb +6 -0
- data/examples/integrations/dynamic-sinatra-opal-sprockets-server/config.ru +29 -0
- data/examples/integrations/static-bash/.gitignore +2 -0
- data/examples/integrations/static-bash/Gemfile +3 -0
- data/examples/integrations/static-bash/README.md +8 -0
- data/examples/integrations/static-bash/app/application.rb +6 -0
- data/examples/integrations/static-bash/build.sh +4 -0
- data/examples/integrations/static-bash/index.html +10 -0
- data/examples/integrations/static-bash-opal-parser/.gitignore +3 -0
- data/examples/integrations/static-bash-opal-parser/Gemfile +3 -0
- data/examples/integrations/static-bash-opal-parser/README.md +10 -0
- data/examples/integrations/static-bash-opal-parser/build.sh +4 -0
- data/examples/integrations/static-bash-opal-parser/index.html +19 -0
- data/examples/integrations/static-rake/.gitignore +1 -0
- data/examples/integrations/static-rake/Gemfile +7 -0
- data/examples/integrations/static-rake/README.md +7 -0
- data/examples/integrations/static-rake/Rakefile +10 -0
- data/examples/integrations/static-rake/app/application.rb +6 -0
- data/examples/integrations/static-rake/index.html +9 -0
- data/examples/integrations/static-rake-guard/.gitignore +1 -0
- data/examples/integrations/static-rake-guard/Gemfile +9 -0
- data/examples/integrations/static-rake-guard/Gemfile.lock +69 -0
- data/examples/integrations/static-rake-guard/Guardfile +3 -0
- data/examples/integrations/static-rake-guard/README.md +10 -0
- data/examples/integrations/static-rake-guard/Rakefile +10 -0
- data/examples/integrations/static-rake-guard/app/application.rb +6 -0
- data/examples/integrations/static-rake-guard/index.html +9 -0
- data/examples/svg/.gitignore +1 -0
- data/examples/svg/Gemfile +4 -0
- data/examples/svg/README.md +7 -0
- data/examples/svg/Rakefile +10 -0
- data/examples/svg/app/application.rb +11 -0
- data/examples/svg/index.html +17 -0
- data/examples/svg/index.svg +6 -0
- data/index.html.erb +24 -0
- data/lib/opal/browser.rb +4 -0
- data/lib/opal-browser.rb +1 -0
- data/opal/browser/animation_frame.rb +111 -0
- data/opal/browser/audio/node.rb +121 -0
- data/opal/browser/audio/param_schedule.rb +43 -0
- data/opal/browser/audio.rb +66 -0
- data/opal/browser/blob.rb +94 -0
- data/opal/browser/canvas/data.rb +63 -0
- data/opal/browser/canvas/gradient.rb +27 -0
- data/opal/browser/canvas/style.rb +115 -0
- data/opal/browser/canvas/text.rb +45 -0
- data/opal/browser/canvas.rb +335 -0
- data/opal/browser/console.rb +105 -0
- data/opal/browser/cookies.rb +171 -0
- data/opal/browser/crypto.rb +79 -0
- data/opal/browser/css/declaration.rb +83 -0
- data/opal/browser/css/rule/style.rb +16 -0
- data/opal/browser/css/rule.rb +48 -0
- data/opal/browser/css/style_sheet.rb +83 -0
- data/opal/browser/css/unit.rb +188 -0
- data/opal/browser/css.rb +40 -0
- data/opal/browser/database/sql.rb +193 -0
- data/opal/browser/delay.rb +94 -0
- data/opal/browser/dom/attribute.rb +26 -0
- data/opal/browser/dom/builder.rb +107 -0
- data/opal/browser/dom/cdata.rb +9 -0
- data/opal/browser/dom/character_data.rb +73 -0
- data/opal/browser/dom/comment.rb +9 -0
- data/opal/browser/dom/document.rb +217 -0
- data/opal/browser/dom/document_fragment.rb +25 -0
- data/opal/browser/dom/document_or_shadow_root.rb +19 -0
- data/opal/browser/dom/element/attributes.rb +111 -0
- data/opal/browser/dom/element/button.rb +31 -0
- data/opal/browser/dom/element/custom.rb +177 -0
- data/opal/browser/dom/element/data.rb +82 -0
- data/opal/browser/dom/element/editable.rb +47 -0
- data/opal/browser/dom/element/form.rb +38 -0
- data/opal/browser/dom/element/iframe.rb +37 -0
- data/opal/browser/dom/element/image.rb +25 -0
- data/opal/browser/dom/element/input.rb +64 -0
- data/opal/browser/dom/element/media.rb +43 -0
- data/opal/browser/dom/element/offset.rb +89 -0
- data/opal/browser/dom/element/position.rb +46 -0
- data/opal/browser/dom/element/scroll.rb +168 -0
- data/opal/browser/dom/element/select.rb +42 -0
- data/opal/browser/dom/element/size.rb +46 -0
- data/opal/browser/dom/element/template.rb +11 -0
- data/opal/browser/dom/element/textarea.rb +26 -0
- data/opal/browser/dom/element.rb +618 -0
- data/opal/browser/dom/mutation_observer.rb +178 -0
- data/opal/browser/dom/node.rb +504 -0
- data/opal/browser/dom/node_set.rb +121 -0
- data/opal/browser/dom/shadow_root.rb +12 -0
- data/opal/browser/dom/text.rb +36 -0
- data/opal/browser/dom.rb +124 -0
- data/opal/browser/effects.rb +216 -0
- data/opal/browser/event/all.rb +26 -0
- data/opal/browser/event/animation.rb +40 -0
- data/opal/browser/event/audio_processing.rb +35 -0
- data/opal/browser/event/base.rb +461 -0
- data/opal/browser/event/before_unload.rb +17 -0
- data/opal/browser/event/clipboard.rb +37 -0
- data/opal/browser/event/close.rb +49 -0
- data/opal/browser/event/composition.rb +52 -0
- data/opal/browser/event/custom.rb +65 -0
- data/opal/browser/event/data_transfer.rb +95 -0
- data/opal/browser/event/device_light.rb +25 -0
- data/opal/browser/event/device_motion.rb +53 -0
- data/opal/browser/event/device_orientation.rb +50 -0
- data/opal/browser/event/device_proximity.rb +35 -0
- data/opal/browser/event/drag.rb +123 -0
- data/opal/browser/event/focus.rb +41 -0
- data/opal/browser/event/gamepad.rb +62 -0
- data/opal/browser/event/hash_change.rb +30 -0
- data/opal/browser/event/keyboard.rb +128 -0
- data/opal/browser/event/message.rb +72 -0
- data/opal/browser/event/mouse.rb +258 -0
- data/opal/browser/event/page_transition.rb +25 -0
- data/opal/browser/event/pop_state.rb +35 -0
- data/opal/browser/event/progress.rb +45 -0
- data/opal/browser/event/sensor.rb +17 -0
- data/opal/browser/event/storage.rb +45 -0
- data/opal/browser/event/touch.rb +62 -0
- data/opal/browser/event/ui.rb +38 -0
- data/opal/browser/event/wheel.rb +51 -0
- data/opal/browser/event.rb +162 -0
- data/opal/browser/event_source.rb +70 -0
- data/opal/browser/form_data.rb +225 -0
- data/opal/browser/history.rb +86 -0
- data/opal/browser/http/binary.rb +58 -0
- data/opal/browser/http/headers.rb +109 -0
- data/opal/browser/http/request.rb +359 -0
- data/opal/browser/http/response.rb +119 -0
- data/opal/browser/http.rb +167 -0
- data/opal/browser/immediate.rb +161 -0
- data/opal/browser/interval.rb +111 -0
- data/opal/browser/location.rb +93 -0
- data/opal/browser/navigator.rb +274 -0
- data/opal/browser/polyfill/visual_viewport.rb +216 -0
- data/opal/browser/screen.rb +66 -0
- data/opal/browser/setup/base.rb +6 -0
- data/opal/browser/setup/full.rb +13 -0
- data/opal/browser/setup/large.rb +17 -0
- data/opal/browser/setup/mini.rb +8 -0
- data/opal/browser/setup/traditional.rb +10 -0
- data/opal/browser/socket.rb +123 -0
- data/opal/browser/storage.rb +252 -0
- data/opal/browser/support.rb +299 -0
- data/opal/browser/utils.rb +154 -0
- data/opal/browser/version.rb +3 -0
- data/opal/browser/visual_viewport.rb +39 -0
- data/opal/browser/window/size.rb +73 -0
- data/opal/browser/window/view.rb +51 -0
- data/opal/browser/window.rb +133 -0
- data/opal/browser.rb +1 -0
- data/opal/opal-browser.rb +1 -0
- data/opal-browser.gemspec +27 -0
- data/spec/database/sql_spec.rb +139 -0
- data/spec/delay_spec.rb +41 -0
- data/spec/dom/attribute_spec.rb +49 -0
- data/spec/dom/builder_spec.rb +86 -0
- data/spec/dom/document_spec.rb +62 -0
- data/spec/dom/element/attributes_spec.rb +52 -0
- data/spec/dom/element/custom_spec.rb +106 -0
- data/spec/dom/element/subclass_spec.rb +144 -0
- data/spec/dom/element_spec.rb +223 -0
- data/spec/dom/mutation_observer_spec.rb +41 -0
- data/spec/dom/node_set_spec.rb +44 -0
- data/spec/dom/node_spec.rb +214 -0
- data/spec/dom_spec.rb +23 -0
- data/spec/event_source_spec.rb +45 -0
- data/spec/event_spec.rb +156 -0
- data/spec/history_spec.rb +61 -0
- data/spec/http_spec.rb +76 -0
- data/spec/immediate_spec.rb +15 -0
- data/spec/interval_spec.rb +59 -0
- data/spec/json2.js +486 -0
- data/spec/native_cached_wrapper_spec.rb +46 -0
- data/spec/runner.rb +107 -0
- data/spec/sizzle.js +5 -0
- data/spec/socket_spec.rb +47 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/spec_helper_promise.rb.erb +25 -0
- data/spec/storage_spec.rb +26 -0
- data/spec/wgxpath.install.js +49 -0
- data/spec/window_spec.rb +10 -0
- metadata +500 -0
@@ -0,0 +1,335 @@
|
|
1
|
+
require 'browser/canvas/style'
|
2
|
+
require 'browser/canvas/text'
|
3
|
+
require 'browser/canvas/data'
|
4
|
+
require 'browser/canvas/gradient'
|
5
|
+
|
6
|
+
module Browser
|
7
|
+
|
8
|
+
class Canvas
|
9
|
+
include Native::Wrapper
|
10
|
+
|
11
|
+
attr_reader :element, :style, :text
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
if DOM::Element === args.first
|
15
|
+
element = args.shift
|
16
|
+
|
17
|
+
if DOM::Element::Image === element
|
18
|
+
@image = element
|
19
|
+
else
|
20
|
+
@element = element
|
21
|
+
end
|
22
|
+
elsif Canvas === args.first
|
23
|
+
@image = args.first
|
24
|
+
end
|
25
|
+
|
26
|
+
unless @element
|
27
|
+
@element = $document.create_element('canvas')
|
28
|
+
|
29
|
+
if @image
|
30
|
+
@element[:width] = @image.width
|
31
|
+
@element[:height] = @image.height
|
32
|
+
else
|
33
|
+
@element[:width] = args.shift
|
34
|
+
@element[:height] = args.shift
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if @element.node_name != 'CANVAS'
|
39
|
+
raise ArgumentError, "the element isn't a <canvas> element"
|
40
|
+
end
|
41
|
+
|
42
|
+
super(`#{@element.to_n}.getContext('2d')`)
|
43
|
+
|
44
|
+
@style = Style.new(self)
|
45
|
+
@text = Text.new(self)
|
46
|
+
|
47
|
+
if @image
|
48
|
+
draw_image(@image)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def width
|
53
|
+
@element[:width].to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
def height
|
57
|
+
@element[:height].to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
def width=(new_width)
|
61
|
+
@element[:width] = new_width.to_i
|
62
|
+
end
|
63
|
+
|
64
|
+
def height=(new_height)
|
65
|
+
@element[:height] = new_height.to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
def append_to(parent)
|
69
|
+
@element.append_to(parent)
|
70
|
+
end
|
71
|
+
|
72
|
+
def load(path)
|
73
|
+
promise = Promise.new
|
74
|
+
image = $document.create_element('img')
|
75
|
+
|
76
|
+
image.on :load do
|
77
|
+
promise.resolve(image)
|
78
|
+
end
|
79
|
+
|
80
|
+
image[:src] = path
|
81
|
+
|
82
|
+
promise
|
83
|
+
end
|
84
|
+
|
85
|
+
def data(x = nil, y = nil, width = nil, height = nil)
|
86
|
+
x ||= 0
|
87
|
+
y ||= 0
|
88
|
+
width ||= self.width
|
89
|
+
height ||= self.height
|
90
|
+
|
91
|
+
Data.new(self, x, y, width, height)
|
92
|
+
end
|
93
|
+
|
94
|
+
def pattern(image, type = :repeat)
|
95
|
+
`#@native.createPattern(#{DOM(image).to_n}, type)`
|
96
|
+
end
|
97
|
+
|
98
|
+
def gradient(*args, &block)
|
99
|
+
Gradient.new(self, *args, &block)
|
100
|
+
end
|
101
|
+
|
102
|
+
def clear(x = nil, y = nil, width = nil, height = nil)
|
103
|
+
x ||= 0
|
104
|
+
y ||= 0
|
105
|
+
width ||= self.width
|
106
|
+
height ||= self.height
|
107
|
+
|
108
|
+
`#@native.clearRect(x, y, width, height)`
|
109
|
+
end
|
110
|
+
|
111
|
+
def begin
|
112
|
+
`#@native.beginPath()`
|
113
|
+
|
114
|
+
self
|
115
|
+
end
|
116
|
+
|
117
|
+
def close
|
118
|
+
`#@native.closePath()`
|
119
|
+
|
120
|
+
self
|
121
|
+
end
|
122
|
+
|
123
|
+
def save
|
124
|
+
`#@native.save()`
|
125
|
+
|
126
|
+
self
|
127
|
+
end
|
128
|
+
|
129
|
+
def restore
|
130
|
+
`#@native.restore()`
|
131
|
+
|
132
|
+
self
|
133
|
+
end
|
134
|
+
|
135
|
+
def move_to(x, y)
|
136
|
+
`#@native.moveTo(x, y)`
|
137
|
+
|
138
|
+
self
|
139
|
+
end
|
140
|
+
|
141
|
+
alias move move_to
|
142
|
+
|
143
|
+
def line_to(x, y)
|
144
|
+
`#@native.lineTo(x, y)`
|
145
|
+
|
146
|
+
self
|
147
|
+
end
|
148
|
+
|
149
|
+
def line(x1, y1, x2, y2)
|
150
|
+
move_to x1, y1
|
151
|
+
line_to x2, y2
|
152
|
+
end
|
153
|
+
|
154
|
+
def rect(x, y, width, height)
|
155
|
+
`#@native.rect(x, y, width, height)`
|
156
|
+
|
157
|
+
self
|
158
|
+
end
|
159
|
+
|
160
|
+
def arc(x, y, radius, angle, clockwise = false)
|
161
|
+
`#@native.arc(x, y, radius, #{angle[:start]}, #{angle[:end]}, !clockwise)`
|
162
|
+
|
163
|
+
self
|
164
|
+
end
|
165
|
+
|
166
|
+
def quadratic_curve_to(cp1x, cp1y, x, y)
|
167
|
+
`#@native.quadraticCurveTo(cp1x, cp1y, x, y)`
|
168
|
+
|
169
|
+
self
|
170
|
+
end
|
171
|
+
|
172
|
+
def bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)
|
173
|
+
`#@native.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)`
|
174
|
+
|
175
|
+
self
|
176
|
+
end
|
177
|
+
|
178
|
+
def curve_to(*args)
|
179
|
+
case args.length
|
180
|
+
when 4 then quadratic_curve_to(*args)
|
181
|
+
when 6 then bezier_curve_to(*args)
|
182
|
+
|
183
|
+
else raise ArgumentError, "don't know where to dispatch"
|
184
|
+
end
|
185
|
+
|
186
|
+
self
|
187
|
+
end
|
188
|
+
|
189
|
+
def draw_image(image, *args)
|
190
|
+
if Canvas === image
|
191
|
+
image = image.element
|
192
|
+
else
|
193
|
+
image = DOM(image)
|
194
|
+
end
|
195
|
+
|
196
|
+
if args.first.is_a?(Hash)
|
197
|
+
source, destination = args
|
198
|
+
|
199
|
+
`#@native.drawImage(#{image.to_n}, #{source[:x]}, #{source[:y]}, #{source[:width]}, #{source[:height]}, #{destination[:x]}, #{destination[:y]}, #{destination[:width]}, #{destination[:height]})`
|
200
|
+
else
|
201
|
+
case args.length
|
202
|
+
when 0
|
203
|
+
`#@native.drawImage(#{image.to_n}, 0, 0)`
|
204
|
+
|
205
|
+
when 2
|
206
|
+
`#@native.drawImage(#{image.to_n}, #{args[0]}, #{args[1]})`
|
207
|
+
|
208
|
+
when 4
|
209
|
+
`#@native.drawImage(#{image.to_n}, #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]})`
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
self
|
214
|
+
end
|
215
|
+
|
216
|
+
def translate(x, y, &block)
|
217
|
+
if block
|
218
|
+
save
|
219
|
+
|
220
|
+
`#@native.translate(x, y)`
|
221
|
+
|
222
|
+
instance_eval(&block)
|
223
|
+
|
224
|
+
restore
|
225
|
+
else
|
226
|
+
`#@native.translate(x, y)`
|
227
|
+
end
|
228
|
+
|
229
|
+
self
|
230
|
+
end
|
231
|
+
|
232
|
+
def rotate(angle, &block)
|
233
|
+
if block
|
234
|
+
save
|
235
|
+
|
236
|
+
`#@native.rotate(angle)`
|
237
|
+
|
238
|
+
instance_eval(&block)
|
239
|
+
|
240
|
+
restore
|
241
|
+
else
|
242
|
+
`#@native.rotate(angle)`
|
243
|
+
end
|
244
|
+
|
245
|
+
self
|
246
|
+
end
|
247
|
+
|
248
|
+
def scale(x, y, &block)
|
249
|
+
if block
|
250
|
+
save
|
251
|
+
|
252
|
+
`#@native.scale(x, y)`
|
253
|
+
|
254
|
+
instance_eval(&block)
|
255
|
+
|
256
|
+
restore
|
257
|
+
else
|
258
|
+
`#@native.scale(x, y)`
|
259
|
+
end
|
260
|
+
|
261
|
+
self
|
262
|
+
end
|
263
|
+
|
264
|
+
def transform(m11, m12, m21, m22, dx, dy, &block)
|
265
|
+
if block
|
266
|
+
save
|
267
|
+
|
268
|
+
`#@native.transform(m11, m12, m21, m22, dx, dy)`
|
269
|
+
|
270
|
+
instance_eval(&block)
|
271
|
+
|
272
|
+
restore
|
273
|
+
else
|
274
|
+
`#@native.transform(m11, m12, m21, m22, dx, dy)`
|
275
|
+
end
|
276
|
+
|
277
|
+
self
|
278
|
+
end
|
279
|
+
|
280
|
+
def path(&block)
|
281
|
+
`#@native.beginPath()`
|
282
|
+
|
283
|
+
instance_eval(&block)
|
284
|
+
|
285
|
+
`#@native.closePath()`
|
286
|
+
|
287
|
+
self
|
288
|
+
end
|
289
|
+
|
290
|
+
def fill(&block)
|
291
|
+
path(&block) if block
|
292
|
+
|
293
|
+
`#@native.fill()`
|
294
|
+
|
295
|
+
self
|
296
|
+
end
|
297
|
+
|
298
|
+
def stroke(&block)
|
299
|
+
path(&block) if block
|
300
|
+
|
301
|
+
`#@native.stroke()`
|
302
|
+
|
303
|
+
self
|
304
|
+
end
|
305
|
+
|
306
|
+
def clip(&block)
|
307
|
+
path(&block) if block
|
308
|
+
|
309
|
+
`#@native.clip()`
|
310
|
+
|
311
|
+
self
|
312
|
+
end
|
313
|
+
|
314
|
+
def point_in_path?(x, y)
|
315
|
+
`#@native.isPointInPath(x, y)`
|
316
|
+
end
|
317
|
+
|
318
|
+
def to_data(type = undefined)
|
319
|
+
`#{@element.to_n}.toDataUrl(type)`
|
320
|
+
end
|
321
|
+
|
322
|
+
def to_dom(*)
|
323
|
+
@element
|
324
|
+
end
|
325
|
+
|
326
|
+
def on(*args, &block); @element.on(*args, &block); end
|
327
|
+
def one(*args, &block); @element.one(*args, &block); end
|
328
|
+
def off(*args, &block); @element.off(*args, &block); end
|
329
|
+
end
|
330
|
+
|
331
|
+
Browser::DOM::Builder.for Canvas do |b, item|
|
332
|
+
item.element
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
warn "`console' has been moved to Opal's stdlib, please `require 'console'` instead." if RUBY_ENGINE_VERSION.to_f >= 0.9
|
2
|
+
|
3
|
+
module Browser
|
4
|
+
|
5
|
+
# Manipulate the browser console.
|
6
|
+
#
|
7
|
+
# @see https://developer.mozilla.org/en-US/docs/Web/API/console
|
8
|
+
class Console
|
9
|
+
include Browser::NativeCachedWrapper
|
10
|
+
|
11
|
+
# Clear the console.
|
12
|
+
def clear
|
13
|
+
`#@native.clear()`
|
14
|
+
end
|
15
|
+
|
16
|
+
# Print a stacktrace from the call site.
|
17
|
+
def trace
|
18
|
+
`#@native.trace()`
|
19
|
+
end
|
20
|
+
|
21
|
+
# Log the passed objects based on an optional initial format.
|
22
|
+
def log(*args)
|
23
|
+
`#@native.log.apply(#@native, args)`
|
24
|
+
end
|
25
|
+
|
26
|
+
# Log the passed objects based on an optional initial format as informational
|
27
|
+
# log.
|
28
|
+
def info(*args)
|
29
|
+
`#@native.info.apply(#@native, args)`
|
30
|
+
end
|
31
|
+
|
32
|
+
# Log the passed objects based on an optional initial format as warning.
|
33
|
+
def warn(*args)
|
34
|
+
`#@native.warn.apply(#@native, args)`
|
35
|
+
end
|
36
|
+
|
37
|
+
# Log the passed objects based on an optional initial format as error.
|
38
|
+
def error(*args)
|
39
|
+
`#@native.error.apply(#@native, args)`
|
40
|
+
end
|
41
|
+
|
42
|
+
# Time the given block with the given label.
|
43
|
+
def time(label, &block)
|
44
|
+
raise ArgumentError, "no block given" unless block
|
45
|
+
|
46
|
+
`#@native.time(label)`
|
47
|
+
|
48
|
+
begin
|
49
|
+
if block.arity == 0
|
50
|
+
instance_exec(&block)
|
51
|
+
else
|
52
|
+
block.call(self)
|
53
|
+
end
|
54
|
+
ensure
|
55
|
+
`#@native.timeEnd()`
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Group the given block.
|
60
|
+
def group(*args, &block)
|
61
|
+
raise ArgumentError, "no block given" unless block
|
62
|
+
|
63
|
+
`#@native.group.apply(#@native, args)`
|
64
|
+
|
65
|
+
begin
|
66
|
+
if block.arity == 0
|
67
|
+
instance_exec(&block)
|
68
|
+
else
|
69
|
+
block.call(self)
|
70
|
+
end
|
71
|
+
ensure
|
72
|
+
`#@native.groupEnd()`
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Group the given block but collapse it.
|
77
|
+
def group!(*args, &block)
|
78
|
+
return unless block_given?
|
79
|
+
|
80
|
+
`#@native.groupCollapsed.apply(#@native, args)`
|
81
|
+
|
82
|
+
begin
|
83
|
+
if block.arity == 0
|
84
|
+
instance_exec(&block)
|
85
|
+
else
|
86
|
+
block.call(self)
|
87
|
+
end
|
88
|
+
ensure
|
89
|
+
`#@native.groupEnd()`
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Window
|
95
|
+
# Get the {Console} for this window.
|
96
|
+
#
|
97
|
+
# @return [Console]
|
98
|
+
def console
|
99
|
+
Console.new(`#@native.console`)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
$console = $window.console
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
module Browser
|
2
|
+
|
3
|
+
# Allows manipulation of browser cookies.
|
4
|
+
#
|
5
|
+
# @see https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# cookies = $document.cookies
|
10
|
+
# cookies["my-cookie"] = "monster"
|
11
|
+
# cookies.delete("my-cookie")
|
12
|
+
#
|
13
|
+
# By default, cookies are stored JSON-encoded. You can supply a `raw:` option
|
14
|
+
# whenever you need to access/write the cookies in a raw way, eg.
|
15
|
+
#
|
16
|
+
# cookies["my-other-cookie", raw: true] = 123
|
17
|
+
#
|
18
|
+
# You can also set this option while referencing $document.cookies, eg.
|
19
|
+
#
|
20
|
+
# cookies = $document.cookies(raw: true)
|
21
|
+
# cookies["my-other-cookie"] = 123
|
22
|
+
class Cookies
|
23
|
+
# Default cookie options.
|
24
|
+
DEFAULT = {
|
25
|
+
expires: Time.now + 60 * 60 * 24,
|
26
|
+
secure: false
|
27
|
+
}
|
28
|
+
|
29
|
+
include Enumerable
|
30
|
+
|
31
|
+
attr_reader :options
|
32
|
+
|
33
|
+
# Create a new {Cookies} wrapper.
|
34
|
+
#
|
35
|
+
# @param document [native] the native document object
|
36
|
+
# @param options [Hash] the default cookie options
|
37
|
+
def initialize(document, options = {})
|
38
|
+
@document = document
|
39
|
+
@options = DEFAULT.merge(options)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Access the cookie with the given name.
|
43
|
+
#
|
44
|
+
# @param name [String] the name of the cookie
|
45
|
+
# @param options [Hash] the options for the cookie
|
46
|
+
#
|
47
|
+
# @option options [Boolean] :raw get a raw cookie value, don't encode it with JSON
|
48
|
+
#
|
49
|
+
# @return [Object]
|
50
|
+
def [](name, options = {})
|
51
|
+
options = @options.merge(options)
|
52
|
+
|
53
|
+
matches = `#@document.cookie`.scan(/#{Regexp.escape(FormData.encode(name))}=([^;]*)/)
|
54
|
+
|
55
|
+
return if matches.empty?
|
56
|
+
|
57
|
+
result = matches.flatten.map do |value|
|
58
|
+
if options[:raw]
|
59
|
+
FormData.decode(value)
|
60
|
+
else
|
61
|
+
JSON.parse(FormData.decode(value))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
result.length == 1 ? result.first : result
|
66
|
+
end
|
67
|
+
|
68
|
+
# Set a cookie.
|
69
|
+
#
|
70
|
+
# @param name [String] the name of the cookie
|
71
|
+
# @param value [Object] the data to set
|
72
|
+
# @param options [Hash] the options for the cookie
|
73
|
+
#
|
74
|
+
# @option options [Boolean] :raw don't encode a value with JSON
|
75
|
+
# @option options [Integer] :max_age the max age of the cookie in seconds
|
76
|
+
# @option options [Time] :expires the expire date
|
77
|
+
# @option options [String] :path the path the cookie is valid on
|
78
|
+
# @option options [String] :domain the domain the cookie is valid on
|
79
|
+
# @option options [Boolean] :secure whether the cookie is secure or not
|
80
|
+
def []=(name, options = {}, value)
|
81
|
+
options = @options.merge(options)
|
82
|
+
if options[:raw]
|
83
|
+
string = value.to_s
|
84
|
+
else
|
85
|
+
string = JSON.dump(value)
|
86
|
+
end
|
87
|
+
encoded_value = encode(name, string, options)
|
88
|
+
`#@document.cookie = #{encoded_value}`
|
89
|
+
end
|
90
|
+
|
91
|
+
# Delete a cookie.
|
92
|
+
#
|
93
|
+
# @param name [String] the name of the cookie
|
94
|
+
def delete(name, _options = {})
|
95
|
+
`#@document.cookie = #{encode name, '', expires: Time.now}`
|
96
|
+
end
|
97
|
+
|
98
|
+
# @!attribute [r] keys
|
99
|
+
# @return [Array<String>] all the cookie names
|
100
|
+
def keys(_options = {})
|
101
|
+
Array(`#@document.cookie.split(/; /)`).map do |cookie|
|
102
|
+
cookie.split(/\s*=\s*/).first
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# @!attribute [r] values
|
107
|
+
# @return [Array] all the cookie values
|
108
|
+
def values(options = {})
|
109
|
+
options = @options.merge(options)
|
110
|
+
keys.map do |key|
|
111
|
+
self[key, options]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Enumerate the cookies.
|
116
|
+
#
|
117
|
+
# @yieldparam key [String] the name of the cookie
|
118
|
+
# @yieldparam value [String] the value of the cookie
|
119
|
+
#
|
120
|
+
# @param options [Hash] the options for the cookie
|
121
|
+
#
|
122
|
+
# @option options [Boolean] :raw don't encode a value with JSON
|
123
|
+
#
|
124
|
+
# @return [self]
|
125
|
+
def each(options = {}, &block)
|
126
|
+
return enum_for :each, options unless block
|
127
|
+
options = @options.merge(options)
|
128
|
+
|
129
|
+
keys.each do |key|
|
130
|
+
yield key, self[key, options]
|
131
|
+
end
|
132
|
+
|
133
|
+
self
|
134
|
+
end
|
135
|
+
|
136
|
+
# Delete all the cookies
|
137
|
+
#
|
138
|
+
# @return [self]
|
139
|
+
def clear(_options = {})
|
140
|
+
keys.each do |key|
|
141
|
+
delete key
|
142
|
+
end
|
143
|
+
|
144
|
+
self
|
145
|
+
end
|
146
|
+
|
147
|
+
protected
|
148
|
+
def encode(key, value, options = {})
|
149
|
+
io = []
|
150
|
+
|
151
|
+
io << FormData.encode(key) << ?= << FormData.encode(value) << '; '
|
152
|
+
|
153
|
+
io << 'max-age=' << options[:max_age] << '; ' if options[:max_age]
|
154
|
+
io << 'expires=' << options[:expires].utc << '; ' if options[:expires]
|
155
|
+
io << 'path=' << options[:path] << '; ' if options[:path]
|
156
|
+
io << 'domain=' << options[:domain] << '; ' if options[:domain]
|
157
|
+
io << 'secure' if options[:secure]
|
158
|
+
|
159
|
+
io.join
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
class DOM::Document < DOM::Element
|
164
|
+
# @!attribute [r] cookies
|
165
|
+
# @return [Cookies] the cookies for the document
|
166
|
+
def cookies(options = {})
|
167
|
+
Cookies.new(@native, options) if defined?(`#@native.cookie`)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Browser
|
2
|
+
|
3
|
+
# Implements (parts of) the web crypto interface
|
4
|
+
#
|
5
|
+
# https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API
|
6
|
+
class Crypto
|
7
|
+
include NativeCachedWrapper
|
8
|
+
|
9
|
+
class Digest
|
10
|
+
def initialize(buf)
|
11
|
+
@buffer = Buffer.new(buf)
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :buffer
|
15
|
+
|
16
|
+
# Convert a digest to a hexadecimal string
|
17
|
+
def to_hex
|
18
|
+
buffer.to_a.map { |i| "%02x" % i }.join
|
19
|
+
end
|
20
|
+
|
21
|
+
# Convert a digest to a binary string
|
22
|
+
def to_s
|
23
|
+
buffer.to_a.map { |i| "%c" % i }.join
|
24
|
+
end
|
25
|
+
|
26
|
+
# Convert a digest to a Base64-encoded string
|
27
|
+
#
|
28
|
+
# You will need to `require "base64"`
|
29
|
+
def to_b64
|
30
|
+
Base64.strict_encode64(to_s)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert a digest to a urlsafe Base64-encoded string
|
34
|
+
#
|
35
|
+
# You will need to `require "base64"`
|
36
|
+
def to_u64(padding: false)
|
37
|
+
Base64.urlsafe_encode64(to_s, padding: padding)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Compute a cryptographic digest of data (a Buffer). If block is given,
|
42
|
+
# it will call a block, otherwise it will return a Promise that will
|
43
|
+
# return once a digest is computed.
|
44
|
+
#
|
45
|
+
# Allowed values for algo: SHA-1, SHA-256 (default), SHA-368, SHA-512.
|
46
|
+
#
|
47
|
+
# The block/promise will be given an argument of type {Digest} which can
|
48
|
+
# be used to format a digest.
|
49
|
+
#
|
50
|
+
# Example:
|
51
|
+
# ```
|
52
|
+
# Browser::Blob.new(['test']).buffer.then { |b|
|
53
|
+
# $window.crypto.digest(b)
|
54
|
+
# }.then { |d|
|
55
|
+
# puts d.to_hex
|
56
|
+
# }
|
57
|
+
# ```
|
58
|
+
def digest data, algo = 'SHA-256', &block
|
59
|
+
promise = nil
|
60
|
+
unless block_given?
|
61
|
+
promise = Promise.new
|
62
|
+
block = proc { |i| promise.resolve(i) }
|
63
|
+
end
|
64
|
+
resblock = proc { |i| block.call(Digest.new(i)) }
|
65
|
+
|
66
|
+
`#@native.subtle.digest(algo, #{Native.convert(data)}).then(#{resblock.to_n})`
|
67
|
+
promise
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Window
|
72
|
+
# @!attribute [r] crypto
|
73
|
+
# @return [Crypto] the crypto interface of this window
|
74
|
+
def crypto
|
75
|
+
@crypto ||= Crypto.new(`#@native.crypto`)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|