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,83 @@
|
|
1
|
+
module Browser; module CSS
|
2
|
+
|
3
|
+
class Declaration
|
4
|
+
include Browser::NativeCachedWrapper
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def rule
|
8
|
+
Rule.new(`#@native.parentRule`) if defined?(`#@native.parentRule`)
|
9
|
+
end
|
10
|
+
|
11
|
+
def assign(data)
|
12
|
+
data.each {|name, value|
|
13
|
+
self[name] = value
|
14
|
+
}
|
15
|
+
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def replace(string)
|
20
|
+
`#@native.cssText = #{string}`
|
21
|
+
end
|
22
|
+
|
23
|
+
def apply(&block)
|
24
|
+
Paggio::CSS::Definition.new(&block).each {|style|
|
25
|
+
if style.important
|
26
|
+
`#@native.setProperty(#{style.name}, #{style.value}, "important")`
|
27
|
+
else
|
28
|
+
`#@native.setProperty(#{style.name}, #{style.value}, "")`
|
29
|
+
end
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete(name)
|
34
|
+
`#@native.removeProperty(#{name})`
|
35
|
+
end
|
36
|
+
|
37
|
+
def [](name)
|
38
|
+
%x{
|
39
|
+
var result = #@native.getPropertyValue(#{name});
|
40
|
+
|
41
|
+
if (result == null || result === "") {
|
42
|
+
return nil;
|
43
|
+
}
|
44
|
+
|
45
|
+
return result;
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def []=(name, value)
|
50
|
+
`#@native.setProperty(#{name}, #{value.to_s}, "")`
|
51
|
+
end
|
52
|
+
|
53
|
+
def important?(name)
|
54
|
+
`#@native.getPropertyPriority(#{name}) == "important"`
|
55
|
+
end
|
56
|
+
|
57
|
+
def each(&block)
|
58
|
+
return enum_for :each unless block_given?
|
59
|
+
|
60
|
+
%x{
|
61
|
+
for (var i = 0, length = #@native.length; i < length; i++) {
|
62
|
+
var name = #@native.item(i);
|
63
|
+
|
64
|
+
#{yield `name`, self[`name`]}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
alias_native :length
|
72
|
+
alias_native :to_s, :cssText
|
73
|
+
|
74
|
+
def method_missing(name, value = nil)
|
75
|
+
if name.end_with? ?=
|
76
|
+
self[name[0 .. -2]] = value
|
77
|
+
else
|
78
|
+
self[name]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end; end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Browser; module CSS; class Rule
|
2
|
+
|
3
|
+
class Style < Rule
|
4
|
+
alias_native :selector, :selectorText
|
5
|
+
alias_native :id, :selectorText
|
6
|
+
|
7
|
+
def declaration
|
8
|
+
Declaration.new(`#@native.style`)
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(*args, &block)
|
12
|
+
declaration.__send__(*args, &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end; end; end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Browser; module CSS
|
2
|
+
|
3
|
+
class Rule
|
4
|
+
include Browser::NativeCachedWrapper
|
5
|
+
|
6
|
+
STYLE_RULE = 1
|
7
|
+
CHARSET_RULE = 2
|
8
|
+
IMPORT_RULE = 3
|
9
|
+
MEDIA_RULE = 4
|
10
|
+
FONT_FACE_RULE = 5
|
11
|
+
PAGE_RULE = 6
|
12
|
+
KEYFRAMES_RULE = 7
|
13
|
+
KEYFRAME_RULE = 8
|
14
|
+
NAMESPACE_RULE = 10
|
15
|
+
COUNTER_STYLE_RULE = 11
|
16
|
+
SUPPORTS_RULE = 12
|
17
|
+
DOCUMENT_RULE = 13
|
18
|
+
FONT_FEATURE_VALUES_RULE = 14
|
19
|
+
VIEWPORT_RULE = 15
|
20
|
+
REGION_STYLE_RULE = 16
|
21
|
+
|
22
|
+
def self.new(rule)
|
23
|
+
if self == Rule
|
24
|
+
@classes ||= [nil, Style]
|
25
|
+
|
26
|
+
if klass = @classes[`rule.type`]
|
27
|
+
klass.new(rule)
|
28
|
+
else
|
29
|
+
raise ArgumentError, 'cannot instantiate a non derived Rule object'
|
30
|
+
end
|
31
|
+
else
|
32
|
+
super(rule)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_native :text, :cssText
|
37
|
+
alias_native :to_s, :cssText
|
38
|
+
|
39
|
+
def parent
|
40
|
+
Rule.new(`#@native.parentRule`) if `#@native.parentRule != null`
|
41
|
+
end
|
42
|
+
|
43
|
+
def style_sheet
|
44
|
+
StyleSheet.new(`#@native.parentStyleSheet`) if `#@native.parentStyleSheet != null`
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end; end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Browser; module CSS
|
2
|
+
|
3
|
+
class StyleSheet
|
4
|
+
include Browser::NativeCachedWrapper
|
5
|
+
|
6
|
+
def initialize(what)
|
7
|
+
if DOM::Element === what
|
8
|
+
super(`#{what.to_n}.sheet`)
|
9
|
+
else
|
10
|
+
super(what)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_native :disabled?, :disabled
|
15
|
+
alias_native :href
|
16
|
+
alias_native :title
|
17
|
+
alias_native :type
|
18
|
+
|
19
|
+
def media
|
20
|
+
Media.new(`#@native.media`) if `#@native.media != null`
|
21
|
+
end
|
22
|
+
|
23
|
+
def owner
|
24
|
+
DOM(`#@native.ownerNode`)
|
25
|
+
end
|
26
|
+
|
27
|
+
def parent
|
28
|
+
Sheet.new(`#@native.parentStyleSheet`) if `#@native.parentStyleSheet != null`
|
29
|
+
end
|
30
|
+
|
31
|
+
def rules
|
32
|
+
Native::Array.new(`#@native.cssRules`) { |e|
|
33
|
+
Rule.new(e)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete(index)
|
38
|
+
`#@native.deleteRule(index)`
|
39
|
+
end
|
40
|
+
|
41
|
+
def insert(index, rule)
|
42
|
+
`#@native.insertRule(#{rule}, #{index})`
|
43
|
+
end
|
44
|
+
|
45
|
+
def rule(selector, body)
|
46
|
+
unless String === selector
|
47
|
+
selector = selector.join ', '
|
48
|
+
end
|
49
|
+
|
50
|
+
unless String === body
|
51
|
+
body = body.map {|name, value|
|
52
|
+
"#{name}: #{value};"
|
53
|
+
}.join "\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
insert(length, "#{selector} { #{body} }")
|
57
|
+
end
|
58
|
+
|
59
|
+
def [](id)
|
60
|
+
rules.find { |r| log r; r.id == id }
|
61
|
+
end
|
62
|
+
|
63
|
+
def method_missing(*args, &block)
|
64
|
+
rules.__send__(*args, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
class Media < Native::Array
|
68
|
+
alias_native :text, :mediaText
|
69
|
+
alias_native :to_s, :mediaText
|
70
|
+
|
71
|
+
def push(medium)
|
72
|
+
`#@native.appendMedium(#{medium})`
|
73
|
+
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def delete(medium)
|
78
|
+
`#@native.deleteMedium(#{medium})`
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end; end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
module Browser; module CSS
|
2
|
+
|
3
|
+
class Unit
|
4
|
+
COMPATIBLE = %i[in pt mm cm px pc]
|
5
|
+
|
6
|
+
attr_reader :type
|
7
|
+
|
8
|
+
def initialize(number, type)
|
9
|
+
@number = number
|
10
|
+
@type = type
|
11
|
+
end
|
12
|
+
|
13
|
+
def coerce(other)
|
14
|
+
return self, other
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
@number == convert(other, @type)
|
19
|
+
end
|
20
|
+
|
21
|
+
def ===(other)
|
22
|
+
@type == other.type && @number == other.to_f
|
23
|
+
end
|
24
|
+
|
25
|
+
alias eql? ==
|
26
|
+
|
27
|
+
def hash
|
28
|
+
[@number, @type].hash
|
29
|
+
end
|
30
|
+
|
31
|
+
%i[em ex ch rem vh vw vmin vmax px mm cm in pt pc].each {|name|
|
32
|
+
define_method name do
|
33
|
+
Unit.new(convert(self, name), name)
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
def +(other)
|
38
|
+
return Unit.new(@number + other, @type) unless Unit === other
|
39
|
+
|
40
|
+
if @type == other.type
|
41
|
+
Unit.new(@number + other.to_f, @type)
|
42
|
+
elsif compatible?(self) and compatible?(other)
|
43
|
+
Unit.new(@number + convert(other, @type), @type)
|
44
|
+
else
|
45
|
+
raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def -(other)
|
50
|
+
return Unit.new(@number - other, @type) unless Unit === other
|
51
|
+
|
52
|
+
if @type == other.type
|
53
|
+
Unit.new(@number - other.to_f, @type)
|
54
|
+
elsif compatible?(self) and compatible?(other)
|
55
|
+
Unit.new(@number - convert(other, @type), @type)
|
56
|
+
else
|
57
|
+
raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def *(other)
|
62
|
+
return Unit.new(@number * other, @type) unless Unit === other
|
63
|
+
|
64
|
+
if @type == other.type
|
65
|
+
Unit.new(@number * other.to_f, @type)
|
66
|
+
elsif compatible?(self) and compatible?(other)
|
67
|
+
Unit.new(@number * convert(other, @type), @type)
|
68
|
+
else
|
69
|
+
raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def /(other)
|
74
|
+
return Unit.new(@number / other, @type) unless Unit === other
|
75
|
+
|
76
|
+
if @type == other.type
|
77
|
+
Unit.new(@number / other.to_f, @type)
|
78
|
+
elsif compatible?(self) and compatible?(other)
|
79
|
+
Unit.new(@number / convert(other, @type), @type)
|
80
|
+
else
|
81
|
+
raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def -@
|
86
|
+
Unit.new(@number * -1, @type)
|
87
|
+
end
|
88
|
+
|
89
|
+
def +@
|
90
|
+
Unit.new(@number, @type)
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_i
|
94
|
+
@number.to_i
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_f
|
98
|
+
@number.to_f
|
99
|
+
end
|
100
|
+
|
101
|
+
def to_u
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_s
|
106
|
+
"#{@number}#{@type}"
|
107
|
+
end
|
108
|
+
|
109
|
+
alias to_str to_s
|
110
|
+
alias inspect to_s
|
111
|
+
|
112
|
+
private
|
113
|
+
def compatible?(unit)
|
114
|
+
COMPATIBLE.include?(unit.type)
|
115
|
+
end
|
116
|
+
|
117
|
+
def convert(unit, type)
|
118
|
+
value = unit.to_f
|
119
|
+
|
120
|
+
return value if unit.type == type
|
121
|
+
|
122
|
+
px = case unit.type
|
123
|
+
when :in then value * 96
|
124
|
+
when :pt then value * 4.0 / 3.0
|
125
|
+
when :pc then value / 12 * 4.0 / 3.0
|
126
|
+
when :mm then value * 3.77953
|
127
|
+
when :cm then value * 10 * 3.77953
|
128
|
+
when :px then value
|
129
|
+
end
|
130
|
+
|
131
|
+
case type
|
132
|
+
when :in then px / 96.0
|
133
|
+
when :pt then px / 4.0 / 3.0
|
134
|
+
when :pc then px * 12 / 4.0 / 3.0
|
135
|
+
when :mm then px / 3.77953
|
136
|
+
when :cm then px / 10 / 3.77953
|
137
|
+
when :px then px
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
end; end
|
143
|
+
|
144
|
+
class Numeric
|
145
|
+
Unit = Browser::CSS::Unit
|
146
|
+
|
147
|
+
%i[em ex ch rem vh vw vmin vmax px mm cm in pt pc].each {|name|
|
148
|
+
define_method name do
|
149
|
+
Unit.new(self, name)
|
150
|
+
end
|
151
|
+
}
|
152
|
+
|
153
|
+
alias old_percent %
|
154
|
+
|
155
|
+
def %(other = nil)
|
156
|
+
if other
|
157
|
+
old_percent(other)
|
158
|
+
else
|
159
|
+
Unit.new(self, :%)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def to_u
|
164
|
+
self
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
class String
|
169
|
+
def to_u
|
170
|
+
if matches = self.match(/^([\d+.]+)(.+)?$/)
|
171
|
+
value = matches[1].to_f
|
172
|
+
|
173
|
+
if unit = matches[2]
|
174
|
+
value.__send__(unit.downcase)
|
175
|
+
else
|
176
|
+
value
|
177
|
+
end
|
178
|
+
else
|
179
|
+
0
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
class NilClass
|
185
|
+
def to_u
|
186
|
+
0
|
187
|
+
end
|
188
|
+
end
|
data/opal/browser/css.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'browser/css/declaration'
|
2
|
+
require 'browser/css/style_sheet'
|
3
|
+
require 'browser/css/rule'
|
4
|
+
require 'browser/css/rule/style'
|
5
|
+
|
6
|
+
module Kernel
|
7
|
+
# @overload CSS(document = $document, &block)
|
8
|
+
#
|
9
|
+
# Create a `<style>` element from a {Paggio::CSS} DSL.
|
10
|
+
#
|
11
|
+
# @param document [Browser::DOM::Document] the document instance
|
12
|
+
# we intend to use
|
13
|
+
#
|
14
|
+
# @return [Browser::DOM::Element] the created `<style>` element
|
15
|
+
#
|
16
|
+
# @overload CSS(string, document = $document)
|
17
|
+
#
|
18
|
+
# Create a `<style>` element from a string.
|
19
|
+
#
|
20
|
+
# @param document [Browser::DOM::Document] the document instance
|
21
|
+
# we intend to use
|
22
|
+
#
|
23
|
+
# @return [Browser::DOM::Element] the created `<style>` element
|
24
|
+
def CSS(*args, &block)
|
25
|
+
document = if args.length > 1 || block_given?
|
26
|
+
args.pop
|
27
|
+
end || $document
|
28
|
+
|
29
|
+
style = document.create_element(:style)
|
30
|
+
style[:type] = 'text/css'
|
31
|
+
|
32
|
+
if block
|
33
|
+
style.inner_text = Paggio.css(&block)
|
34
|
+
else
|
35
|
+
style.inner_text = args.join("")
|
36
|
+
end
|
37
|
+
|
38
|
+
style
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module Browser; module Database
|
4
|
+
|
5
|
+
class SQL
|
6
|
+
# Check if the browser supports WebSQL.
|
7
|
+
def self.supported?
|
8
|
+
Browser.supports? 'WebSQL'
|
9
|
+
end
|
10
|
+
|
11
|
+
class Error < StandardError
|
12
|
+
def self.new(error)
|
13
|
+
return super if self != Error
|
14
|
+
|
15
|
+
[Unknown, Database, Version, TooLarge, Quota, Syntax, Constraint, Timeout] \
|
16
|
+
[`error.code`].new(`error.message`)
|
17
|
+
end
|
18
|
+
|
19
|
+
Unknown = Class.new(self)
|
20
|
+
Database = Class.new(self)
|
21
|
+
Version = Class.new(self)
|
22
|
+
TooLarge = Class.new(self)
|
23
|
+
Quota = Class.new(self)
|
24
|
+
Syntax = Class.new(self)
|
25
|
+
Constraint = Class.new(self)
|
26
|
+
Timeout = Class.new(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
include Native::Wrapper
|
30
|
+
|
31
|
+
# @return [String] the name of the database
|
32
|
+
attr_reader :name
|
33
|
+
|
34
|
+
# @return [String] the description for the database
|
35
|
+
attr_reader :description
|
36
|
+
|
37
|
+
# @return [Integer] the size constraint in bytes
|
38
|
+
attr_reader :size
|
39
|
+
|
40
|
+
# Open a database with the given name and options.
|
41
|
+
#
|
42
|
+
# @param name [String] the name for the database
|
43
|
+
# @param options [Hash] options to open the database
|
44
|
+
#
|
45
|
+
# @option options [String] :description the description for the database
|
46
|
+
# @option options [String] :version ('') the expected version of the database
|
47
|
+
# @option options [Integer] :size (5 * 1024 * 1024) the size constraint in bytes
|
48
|
+
def initialize(name, options = {})
|
49
|
+
@name = name
|
50
|
+
@description = options[:description] || name
|
51
|
+
@version = options[:version] || ''
|
52
|
+
@size = options[:size] || 2 * 1024 * 1024
|
53
|
+
|
54
|
+
super(`window.openDatabase(#{name}, #{@version}, #{@description}, #{@size})`)
|
55
|
+
end
|
56
|
+
|
57
|
+
# @overload version()
|
58
|
+
#
|
59
|
+
# Get the version of the database.
|
60
|
+
#
|
61
|
+
# @return [String]
|
62
|
+
#
|
63
|
+
# @overload version(from, to, &block)
|
64
|
+
#
|
65
|
+
# Migrate the database to a new version.
|
66
|
+
#
|
67
|
+
# @param from [String] the version you're migrating from
|
68
|
+
# @param to [String] the version you're migrating to
|
69
|
+
#
|
70
|
+
# @yieldparam transaction [Transaction] the transaction to work with
|
71
|
+
def version(from = nil, to = nil, &block)
|
72
|
+
return `#@native.version` unless block
|
73
|
+
|
74
|
+
`#@native.changeVersion(#{from}, #{to},
|
75
|
+
#{->(t) { block.call(Transaction.new(self, t)) }})`
|
76
|
+
end
|
77
|
+
|
78
|
+
# Start a transaction on the database.
|
79
|
+
#
|
80
|
+
# @yieldparam transaction [Transaction] the transaction to work on
|
81
|
+
def transaction(&block)
|
82
|
+
raise ArgumentError, 'no block given' unless block
|
83
|
+
|
84
|
+
`#@native.transaction(#{->(t) { block.call(Transaction.new(self, t)) }})`
|
85
|
+
end
|
86
|
+
|
87
|
+
# Allows you to make changes to the database or read data from it.
|
88
|
+
class Transaction
|
89
|
+
include Native::Wrapper
|
90
|
+
|
91
|
+
# @return [Database] the database the transaction has been created from
|
92
|
+
attr_reader :database
|
93
|
+
|
94
|
+
# @private
|
95
|
+
def initialize(database, transaction)
|
96
|
+
@database = database
|
97
|
+
|
98
|
+
super(transaction)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Query the database.
|
102
|
+
#
|
103
|
+
# @param query [String] the SQL query to send
|
104
|
+
# @param parameters [Array] optional bind parameters for the query
|
105
|
+
#
|
106
|
+
# @return [Promise]
|
107
|
+
def query(query, *parameters)
|
108
|
+
promise = Promise.new
|
109
|
+
|
110
|
+
`#@native.executeSql(#{query}, #{parameters},
|
111
|
+
#{->(_, r) { promise.resolve(Result.new(self, r)) }},
|
112
|
+
#{->(_, e) { promise.reject(Error.new(e)) }})`
|
113
|
+
|
114
|
+
promise
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Represents a row.
|
119
|
+
class Row < OpenStruct
|
120
|
+
# @private
|
121
|
+
def initialize(row)
|
122
|
+
super(Hash.new(row))
|
123
|
+
end
|
124
|
+
|
125
|
+
def inspect
|
126
|
+
"#<SQL::Row: #{Hash.new(@native).inspect}>"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
class Result
|
131
|
+
include Native::Wrapper
|
132
|
+
|
133
|
+
# @return [Transaction] the transaction the result came from
|
134
|
+
attr_reader :transaction
|
135
|
+
|
136
|
+
# @return [SQL] the database the result came from
|
137
|
+
attr_reader :database
|
138
|
+
|
139
|
+
# @private
|
140
|
+
def initialize(transaction, result)
|
141
|
+
@transaction = transaction
|
142
|
+
@database = transaction.database
|
143
|
+
|
144
|
+
super(result)
|
145
|
+
end
|
146
|
+
|
147
|
+
include Enumerable
|
148
|
+
|
149
|
+
# Get a row from the result.
|
150
|
+
#
|
151
|
+
# @param index [Integer] the index for the row
|
152
|
+
#
|
153
|
+
# @return [Row]
|
154
|
+
def [](index)
|
155
|
+
if index < 0
|
156
|
+
index += length
|
157
|
+
end
|
158
|
+
|
159
|
+
unless index < 0 || index >= length
|
160
|
+
Row.new(`#@native.rows.item(index)`)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
# Enumerate over the rows.
|
165
|
+
#
|
166
|
+
# @yieldparam row [Row]
|
167
|
+
#
|
168
|
+
# @return [self]
|
169
|
+
def each(&block)
|
170
|
+
return enum_for :each unless block
|
171
|
+
|
172
|
+
%x{
|
173
|
+
for (var i = 0, length = #@native.rows.length; i < length; i++) {
|
174
|
+
#{block.call(self[`i`])};
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
self
|
179
|
+
end
|
180
|
+
|
181
|
+
# @!attribute [r] length
|
182
|
+
# @return [Integer] number of rows in the result
|
183
|
+
def length
|
184
|
+
`#@native.rows.length`
|
185
|
+
end
|
186
|
+
|
187
|
+
# @!attribute [r] affected
|
188
|
+
# @return [Integer] number of affected rows
|
189
|
+
alias_native :affected, :rowsAffected
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
end; end
|