opal-browser 0.1.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +11 -0
  5. data/README.md +106 -0
  6. data/Rakefile +5 -0
  7. data/config.ru +63 -0
  8. data/lib/opal/browser.rb +4 -0
  9. data/opal-browser.gemspec +23 -0
  10. data/opal/browser.rb +10 -0
  11. data/opal/browser/animation_frame.rb +29 -0
  12. data/opal/browser/canvas.rb +277 -0
  13. data/opal/browser/canvas/data.rb +73 -0
  14. data/opal/browser/canvas/gradient.rb +37 -0
  15. data/opal/browser/canvas/style.rb +123 -0
  16. data/opal/browser/canvas/text.rb +55 -0
  17. data/opal/browser/compatibility.rb +59 -0
  18. data/opal/browser/compatibility/animation_frame.rb +93 -0
  19. data/opal/browser/compatibility/dom/document/window.rb +15 -0
  20. data/opal/browser/compatibility/dom/element/css.rb +15 -0
  21. data/opal/browser/compatibility/dom/element/matches.rb +31 -0
  22. data/opal/browser/compatibility/dom/element/offset.rb +20 -0
  23. data/opal/browser/compatibility/dom/element/scroll.rb +25 -0
  24. data/opal/browser/compatibility/dom/element/style.rb +15 -0
  25. data/opal/browser/compatibility/dom/mutation_observer.rb +47 -0
  26. data/opal/browser/compatibility/http/request.rb +15 -0
  27. data/opal/browser/compatibility/immediate.rb +107 -0
  28. data/opal/browser/compatibility/window/scroll.rb +27 -0
  29. data/opal/browser/compatibility/window/size.rb +13 -0
  30. data/opal/browser/compatibility/window/view.rb +13 -0
  31. data/opal/browser/console.rb +137 -0
  32. data/opal/browser/cookies.rb +79 -0
  33. data/opal/browser/css.rb +24 -0
  34. data/opal/browser/css/declaration.rb +88 -0
  35. data/opal/browser/css/rule.rb +48 -0
  36. data/opal/browser/css/rule/style.rb +16 -0
  37. data/opal/browser/css/style_sheet.rb +83 -0
  38. data/opal/browser/css/unit.rb +188 -0
  39. data/opal/browser/dom.rb +95 -0
  40. data/opal/browser/dom/attribute.rb +19 -0
  41. data/opal/browser/dom/builder.rb +97 -0
  42. data/opal/browser/dom/cdata.rb +9 -0
  43. data/opal/browser/dom/character_data.rb +37 -0
  44. data/opal/browser/dom/comment.rb +9 -0
  45. data/opal/browser/dom/compatibility.rb +8 -0
  46. data/opal/browser/dom/document.rb +83 -0
  47. data/opal/browser/dom/document_fragment.rb +7 -0
  48. data/opal/browser/dom/element.rb +290 -0
  49. data/opal/browser/dom/element/input.rb +17 -0
  50. data/opal/browser/dom/element/offset.rb +67 -0
  51. data/opal/browser/dom/element/position.rb +37 -0
  52. data/opal/browser/dom/element/scroll.rb +49 -0
  53. data/opal/browser/dom/event.rb +240 -0
  54. data/opal/browser/dom/event/animation.rb +26 -0
  55. data/opal/browser/dom/event/audio_processing.rb +31 -0
  56. data/opal/browser/dom/event/base.rb +207 -0
  57. data/opal/browser/dom/event/before_unload.rb +13 -0
  58. data/opal/browser/dom/event/clipboard.rb +26 -0
  59. data/opal/browser/dom/event/close.rb +35 -0
  60. data/opal/browser/dom/event/composition.rb +38 -0
  61. data/opal/browser/dom/event/custom.rb +30 -0
  62. data/opal/browser/dom/event/device_light.rb +21 -0
  63. data/opal/browser/dom/event/device_motion.rb +38 -0
  64. data/opal/browser/dom/event/device_orientation.rb +36 -0
  65. data/opal/browser/dom/event/device_proximity.rb +31 -0
  66. data/opal/browser/dom/event/drag.rb +113 -0
  67. data/opal/browser/dom/event/focus.rb +23 -0
  68. data/opal/browser/dom/event/gamepad.rb +47 -0
  69. data/opal/browser/dom/event/hash_change.rb +26 -0
  70. data/opal/browser/dom/event/keyboard.rb +93 -0
  71. data/opal/browser/dom/event/message.rb +50 -0
  72. data/opal/browser/dom/event/mouse.rb +253 -0
  73. data/opal/browser/dom/event/page_transition.rb +21 -0
  74. data/opal/browser/dom/event/pop_state.rb +21 -0
  75. data/opal/browser/dom/event/progress.rb +31 -0
  76. data/opal/browser/dom/event/sensor.rb +13 -0
  77. data/opal/browser/dom/event/storage.rb +41 -0
  78. data/opal/browser/dom/event/touch.rb +69 -0
  79. data/opal/browser/dom/event/ui.rb +22 -0
  80. data/opal/browser/dom/event/wheel.rb +49 -0
  81. data/opal/browser/dom/mutation_observer.rb +118 -0
  82. data/opal/browser/dom/node.rb +317 -0
  83. data/opal/browser/dom/node_set.rb +88 -0
  84. data/opal/browser/dom/text.rb +21 -0
  85. data/opal/browser/effects.rb +39 -0
  86. data/opal/browser/event_source.rb +67 -0
  87. data/opal/browser/history.rb +54 -0
  88. data/opal/browser/http.rb +129 -0
  89. data/opal/browser/http/binary.rb +57 -0
  90. data/opal/browser/http/compatibility.rb +1 -0
  91. data/opal/browser/http/headers.rb +90 -0
  92. data/opal/browser/http/parameters.rb +8 -0
  93. data/opal/browser/http/request.rb +331 -0
  94. data/opal/browser/http/response.rb +115 -0
  95. data/opal/browser/immediate.rb +43 -0
  96. data/opal/browser/interval.rb +93 -0
  97. data/opal/browser/location.rb +77 -0
  98. data/opal/browser/navigator.rb +151 -0
  99. data/opal/browser/screen.rb +40 -0
  100. data/opal/browser/socket.rb +115 -0
  101. data/opal/browser/storage.rb +149 -0
  102. data/opal/browser/timeout.rb +60 -0
  103. data/opal/browser/utils.rb +56 -0
  104. data/opal/browser/version.rb +3 -0
  105. data/opal/browser/window.rb +113 -0
  106. data/opal/browser/window/compatibility.rb +3 -0
  107. data/opal/browser/window/scroll.rb +49 -0
  108. data/opal/browser/window/size.rb +35 -0
  109. data/opal/browser/window/view.rb +18 -0
  110. data/spec/dom/builder_spec.rb +69 -0
  111. data/spec/dom/document_spec.rb +40 -0
  112. data/spec/dom/element_spec.rb +46 -0
  113. data/spec/dom/event_spec.rb +127 -0
  114. data/spec/dom/mutation_observer_spec.rb +37 -0
  115. data/spec/dom/node_spec.rb +154 -0
  116. data/spec/dom_spec.rb +13 -0
  117. data/spec/event_source_spec.rb +42 -0
  118. data/spec/history_spec.rb +48 -0
  119. data/spec/http_spec.rb +87 -0
  120. data/spec/immediate_spec.rb +12 -0
  121. data/spec/json2.js +486 -0
  122. data/spec/sizzle.js +5 -0
  123. data/spec/socket_spec.rb +43 -0
  124. data/spec/spec_helper.rb +37 -0
  125. data/spec/storage_spec.rb +26 -0
  126. data/spec/window_spec.rb +10 -0
  127. metadata +240 -0
@@ -0,0 +1,43 @@
1
+ require 'browser/compatibility/immediate'
2
+
3
+ module Browser
4
+
5
+ # FIXME: drop the method_defined? checks when require order is fixed
6
+ class Immediate
7
+ def initialize(func, args, &block)
8
+ @aborted = false
9
+ @function = func
10
+ @arguments = args
11
+ @block = block
12
+ end
13
+
14
+ def dispatch
15
+ raise NotImplementedError
16
+ end unless method_defined? :dispatch
17
+
18
+ def prevent
19
+ raise NotImplementedError
20
+ end unless method_defined? :prevent
21
+
22
+ def abort
23
+ return if aborted?
24
+
25
+ @aborted = true
26
+ prevent
27
+
28
+ self
29
+ end
30
+
31
+ def aborted?
32
+ @aborted
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ class Proc
39
+ # Defer the function to be called as soon as possible.
40
+ def defer(*args, &block)
41
+ Browser::Immediate.new(self, args, &block).tap(&:dispatch)
42
+ end
43
+ end
@@ -0,0 +1,93 @@
1
+ module Browser
2
+
3
+ # This class wraps `setInterval`.
4
+ class Interval
5
+ # @!attribute [r] every
6
+ # @return [Number] the seconds every which the block is called
7
+ attr_reader :every
8
+
9
+ # Create and start an interval.
10
+ #
11
+ # @param window [Window] the window to start the interval on
12
+ # @param time [Number] seconds every which to call the block
13
+ def initialize(window, time, &block)
14
+ @window = Native.convert(window)
15
+ @every = time
16
+ @block = block
17
+
18
+ @aborted = false
19
+ @stopped = true
20
+
21
+ start
22
+ end
23
+
24
+ # Check if the interval has been stopped.
25
+ def stopped?
26
+ @stopped
27
+ end
28
+
29
+ # Check if the interval has been aborted.
30
+ def aborted?
31
+ @aborted
32
+ end
33
+
34
+ # Abort the interval, it won't be possible to start it again.
35
+ #
36
+ # @return [self]
37
+ def abort
38
+ `#@window.clearInterval(#@id)`
39
+
40
+ @aborted = true
41
+ @id = nil
42
+
43
+ self
44
+ end
45
+
46
+ # Stop the interval, it will be possible to start it again.
47
+ #
48
+ # @return [self]
49
+ def stop
50
+ `#@window.clearInterval(#@id)`
51
+
52
+ @stopped = true
53
+ @id = nil
54
+ end
55
+
56
+ # Start the interval if it has been stopped.
57
+ #
58
+ # @return [self]
59
+ def start
60
+ raise "the interval has been aborted" if aborted?
61
+
62
+ return unless stopped?
63
+
64
+ @id = `#@window.setInterval(#{@block.to_n}, #@every * 1000)`
65
+
66
+ self
67
+ end
68
+ end
69
+
70
+ class Window
71
+ # Execute the block every given seconds.
72
+ #
73
+ # @param time [Float] the seconds between every call
74
+ # @return [Interval] the object representing the interval
75
+ def every(time, &block)
76
+ Interval.new(@native, time, &block)
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ class Proc
83
+ def every(time)
84
+ $window.every(time, &self)
85
+ end
86
+ end
87
+
88
+ module Kernel
89
+ # (see Browser::Window#every)
90
+ def every(time, &block)
91
+ $window.every(time, &block)
92
+ end
93
+ end
@@ -0,0 +1,77 @@
1
+ module Browser
2
+
3
+ class Location
4
+ include Native
5
+
6
+ # Change the location.
7
+ #
8
+ # @param url [String, #to_s] the URL to go to
9
+ def assign(url)
10
+ `#@native.assign(#{url.to_s})`
11
+ end
12
+
13
+ # Replace the current URL.
14
+ #
15
+ # @param url [String, #to_s] the URL to go to
16
+ def replace(url)
17
+ `#@native.replace(#{url.to_s})`
18
+ end
19
+
20
+ # Reload the page.
21
+ #
22
+ # @param force [Boolean] whether to force the reload
23
+ def reload(force = false)
24
+ `#@native.reload(force)`
25
+ end
26
+
27
+ # Convert the location to a string.
28
+ def to_s
29
+ `#@native.toString()`
30
+ end
31
+
32
+ # @!attribute fragment
33
+ # @return [String] the hash fragment of the location URI
34
+ alias_native :fragment, :hash
35
+ alias_native :fragment=, :hash=
36
+
37
+ # @!attribute host
38
+ # @return [String] the host part of the location URI
39
+ alias_native :host
40
+ alias_native :host=
41
+
42
+ # @!attribute uri
43
+ # @return [String] the whole location URI
44
+ alias_native :uri, :href
45
+ alias_native :uri=, :href=
46
+
47
+ # @!attribute path
48
+ # @return [String] the path part of the location URI
49
+ alias_native :path, :pathname
50
+ alias_native :path=, :pathname=
51
+
52
+ # @!attribute port
53
+ # @return [Integer] the port part of the location URI
54
+ alias_native :port
55
+ alias_native :port=
56
+
57
+ # @!attribute scheme
58
+ # @return [String] the scheme part of the location URI
59
+ alias_native :scheme, :protocol
60
+ alias_native :scheme=, :protocol=
61
+
62
+ # @!attribute query
63
+ # @return [String] the query part of the location URI
64
+ alias_native :query, :search
65
+ alias_native :query=, :search=
66
+ end
67
+
68
+ class Window
69
+ # Get the {Location} object for this window.
70
+ #
71
+ # @return [Location]
72
+ def location
73
+ Location.new(`#@native.location`) if `#@native.location`
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,151 @@
1
+ module Browser
2
+
3
+ # Class that represents the browser attributes.
4
+ class Navigator
5
+ include Native
6
+
7
+ Version = Struct.new(:major, :minor, :build)
8
+ Product = Struct.new(:name, :version)
9
+ Vendor = Struct.new(:name, :version)
10
+
11
+ # Class that represents a MIME type.
12
+ class MimeType
13
+ include Native
14
+
15
+ # @!attribute [r] plugin
16
+ # @return [Plugin] the plugin for the MIME type
17
+ def plugin
18
+ Plugin.new(`#@native.enabledPlugin`)
19
+ end
20
+
21
+ # @!attribute [r] description
22
+ # @return [String] the description for the MIME type
23
+ alias_native :description
24
+
25
+ # @!attribute [r] extensions
26
+ # @return [Array<String>] the extensions for this MIME type
27
+ def extensions
28
+ `#@native.suffixes`.split(/\s*/)
29
+ end
30
+
31
+ # @!attribute [r] type
32
+ # @return [String] the MIME type
33
+ alias_native :type
34
+ end
35
+
36
+ # Class to represent a browser plugin.
37
+ class Plugin < Native::Array
38
+ def initialize(plugin)
39
+ super plugin do |m|
40
+ MimeType.new(m)
41
+ end
42
+ end
43
+
44
+ # @!attribute [r] description
45
+ # @return [String] the plugin description
46
+ alias_native :description
47
+
48
+ # @!attribute [r] file
49
+ # @return [String] the file associated with the plugin
50
+ alias_native :file, :filename
51
+
52
+ # @!attribute [r] name
53
+ # @return [String] the plugin name
54
+ alias_native :name
55
+
56
+ # @!attribute [r] version
57
+ # @return [String] the plugin version
58
+ alias_native :version
59
+ end
60
+
61
+ # @!attribute [r] code
62
+ # @return [String] the browser code name
63
+ alias_native :code, :appCodeName
64
+
65
+ # @!attribute [r] name
66
+ # @return [String] the browser name
67
+ alias_native :name, :appName
68
+
69
+ # @!attribute [r] version
70
+ # @return [Version] the browser version
71
+ def version
72
+ Version.new(`#@native.appVersion`, `#@native.appMinorVersion`, `#@native.buildID`)
73
+ end
74
+
75
+ # Check if cookies are enabled.
76
+ alias_native :cookies?, :cookieEnabled
77
+
78
+ # Check if DNT is disabled.
79
+ def track?
80
+ `!#@native.doNotTrack`
81
+ end
82
+
83
+ # @!attribute [r] language
84
+ # @return [String] the browser language
85
+ alias_native :language
86
+
87
+ # @!attribute [r] mime_types
88
+ # @return [Native::Array<MimeType>] the supported MIME types
89
+ def mime_types
90
+ Native::Array.new `#@native.mimeTypes`, get: :item, named: :namedItem do |m|
91
+ MimeType.new(m)
92
+ end
93
+ end
94
+
95
+ # Check if the browser is in offline mode.
96
+ def offline?
97
+ `!#@native.onLine`
98
+ end
99
+
100
+ # @!attribute [r] operating_system
101
+ # @return [String] the operating system the browser is running on
102
+ alias_native :operating_system, :oscpu
103
+
104
+ alias os operating_system
105
+
106
+ # @!attribute [r] platform
107
+ # @return [String] the platform the browser is running on
108
+ alias_native :platform
109
+
110
+ # @!attribute [r] plugins
111
+ # @return [Native::Array<Plugin>] the enabled plugins
112
+ def plugins
113
+ Native::Array.new `#@native.plugins` do |p|
114
+ Plugin.new(p)
115
+ end
116
+ end
117
+
118
+ # @!attribute [r] product
119
+ # @return [Product] the product name and version
120
+ def product
121
+ Product.new(`#@native.product`, `#@native.productSub`)
122
+ end
123
+
124
+ # @!attribute [r] user_agent
125
+ # @return [String] the browser's user agent
126
+ alias_native :user_agent, :userAgent
127
+
128
+ # @!attribute [r] vendor
129
+ # @return [Vendor] the vendor name and version
130
+ def vendor
131
+ Vendor.new(`#@native.vendor`, `#@native.vendorSub`)
132
+ end
133
+
134
+ # Check if Java is enabled.
135
+ def java?
136
+ `#@native.javaEnabled()`
137
+ rescue
138
+ false
139
+ end
140
+ end
141
+
142
+ class Window
143
+ # Get the {Navigator} object for this window.
144
+ #
145
+ # @return [Navigator]
146
+ def navigator
147
+ Navigator.new(`#@native.navigator`) if `#@native.navigator`
148
+ end
149
+ end
150
+
151
+ end
@@ -0,0 +1,40 @@
1
+ module Browser
2
+
3
+ class Screen
4
+ include Native
5
+ include DOM::Event::Target
6
+
7
+ target {|value|
8
+ Screen.new(value) if Native.is_a?(value, `window.Screen`)
9
+ }
10
+
11
+ alias_native :width
12
+ alias_native :height
13
+
14
+ def size
15
+ Size.new(width, height)
16
+ end
17
+
18
+ alias_native :x, :top
19
+ alias_native :y, :left
20
+
21
+ def position
22
+ Position.new(x, y)
23
+ end
24
+
25
+ alias_native :color_depth, :colorDepth
26
+ alias_native :pixel_depth, :pixelDepth
27
+
28
+ alias_native :orientation
29
+ end
30
+
31
+ class Window
32
+ # Get the {Screen} for this window.
33
+ #
34
+ # @return [Screen]
35
+ def screen
36
+ Screen.new(`#@native.screen`)
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,115 @@
1
+ module Browser
2
+
3
+ # This class wraps `WebSocket`.
4
+ class Socket
5
+ def self.supported?
6
+ defined? `window.WebSocket`
7
+ end
8
+
9
+ include Native
10
+ include IO::Writable
11
+ include DOM::Event::Target
12
+
13
+ target {|value|
14
+ Socket.new(value) if Native.is_a?(value, `window.WebSocket`)
15
+ }
16
+
17
+ # Create a connection to the given URL, optionally using the given protocol.
18
+ #
19
+ # @param url [String] the URL to connect to
20
+ # @param protocol [String] the protocol to use
21
+ # @yield if the block has no parameters it's instance_exec'd, otherwise it's
22
+ # called with self
23
+ def initialize(url, protocol = nil, &block)
24
+ if native?(url)
25
+ super(url)
26
+ elsif protocol
27
+ super(`new window.WebSocket(#{url.to_s}, #{protocol.to_n})`)
28
+ else
29
+ super(`new window.WebSocket(#{url.to_s})`)
30
+ end
31
+
32
+ if block.arity == 0
33
+ instance_exec(&block)
34
+ else
35
+ block.call(self)
36
+ end if block
37
+ end
38
+
39
+ # @!attribute [r] protocol
40
+ # @return [String] the protocol of the socket
41
+ alias_native :protocol
42
+
43
+ # @!attribute [r] url
44
+ # @return [String] the URL the socket is connected to
45
+ alias_native :url
46
+
47
+ # @!attribute [r] buffered
48
+ # @return [Integer] the amount of buffered data.
49
+ alias_native :buffered, :bufferedAmount
50
+
51
+ # @!attribute [r] type
52
+ # @return [:blob, :buffer, :string] the type of the socket
53
+ def type
54
+ %x{
55
+ switch (#@native.binaryType) {
56
+ case "blob":
57
+ return "blob";
58
+
59
+ case "arraybuffer":
60
+ return "buffer";
61
+
62
+ default:
63
+ return "string";
64
+ }
65
+ }
66
+ end
67
+
68
+ # @!attribute [r] state
69
+ # @return [:connecting, :open, :closing, :closed] the state of the socket
70
+ def state
71
+ %x{
72
+ switch (#@native.readyState) {
73
+ case window.WebSocket.CONNECTING:
74
+ return "connecting";
75
+
76
+ case window.WebSocket.OPEN:
77
+ return "open";
78
+
79
+ case window.WebSocket.CLOSING:
80
+ return "closing";
81
+
82
+ case window.WebSocket.CLOSED:
83
+ return "closed";
84
+ }
85
+ }
86
+ end
87
+
88
+ # @!attribute [r] extensions
89
+ # @return [Array<String>] the extensions used by the socket
90
+ def extensions
91
+ `#@native.extensions`.split(/\s*,\s*/)
92
+ end
93
+
94
+ # Check if the socket is alive.
95
+ def alive?
96
+ state == :open
97
+ end
98
+
99
+ # Send data to the socket.
100
+ #
101
+ # @param data [#to_n] the data to send
102
+ def write(data)
103
+ `#@native.send(#{data.to_n})`
104
+ end
105
+
106
+ # Close the socket.
107
+ #
108
+ # @param code [Integer, nil] the error code
109
+ # @param reason [String, nil] the reason for closing
110
+ def close(code = nil, reason = nil)
111
+ `#@native.close(#{code.to_n}, #{reason.to_n})`
112
+ end
113
+ end
114
+
115
+ end