opal-browser 0.3.1 → 0.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a728e0d53d58c89018baa0d46dbc4731da85966b7fb5b7b312f4bb24d9dc23f
4
- data.tar.gz: 7219cd2463a99d60f4f39b19045ab3ebf22b254a62f82c4684bd3023ad7548e4
3
+ metadata.gz: 16a070e0b1c644fc54d33c3266f0c470a920cac127bc4b37ede951f55f76517f
4
+ data.tar.gz: 7895a72bebd9b3528fcd1b714a603355f83b9fb3c34ea610f3bc4157a5eb8b93
5
5
  SHA512:
6
- metadata.gz: a04d4225b59e9e3d244bfa748d931bc3954946c9de3e6f4623115494e00e54a8a5c3e90bf7fe679b71d1ebc74b7318c18f422014775059ca457bcd1cf872f250
7
- data.tar.gz: 194e860d12c4e737593bc1888cff156fccfc94d13b20faf8ec26b12ed9a1d26d43ad960b21fb88a6605a5b8c20b8b2d4c239dec4a52013b87d1ab7ec9649c372
6
+ metadata.gz: 3e08c691cfc2f4c633c3467a7ed8059690ded3bbcf3759e2c5b5333f220f67752caf4d13316dd956e87304f192732ca027434810b1c6a0f77cc7e039fa5fa871
7
+ data.tar.gz: d724afddb0f157fe632275195ca01bc776129442cce3876fb0d6aea2d30202e81d0240c4666b5fe76837f5671398efa63f648f61e518fc7b7a7017fccb1817e7
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ ## 0.3.2
2
+ * Cookie: refactor the module
3
+ * Note in documentation it's available as `$document.cookies` and it's the preferred way to access it
4
+ * Always encode a cookie with JSON, unless a new parameter `raw:` is provided
5
+
6
+ ## 0.3.1
7
+ * Element#inner_dom: Reduce flickering - first build tree, then insert it
8
+ * NodeSet#to_a to be aliased to #to_ary
data/Gemfile CHANGED
@@ -6,12 +6,12 @@ gem 'rake'
6
6
  gem 'rack'
7
7
  gem 'sinatra'
8
8
  gem 'sinatra-websocket'
9
- # For opal-rspec, a release is needed
10
- gem 'opal-rspec', github: 'hmdne/opal-rspec', branch: 'opal-1.3', submodules: true # '>= 0.8.0.alpha1'
9
+ gem 'opal-rspec'
11
10
  gem 'opal-sprockets'
12
- # Force build of eventmachine on Windows
13
- gem 'eventmachine', github: 'eventmachine/eventmachine' if RUBY_PLATFORM =~ /mingw/
14
-
11
+ # Force build of eventmachine... I wish we could find a way to not use
12
+ # this unmaintained library anymore.
13
+ gem 'eventmachine', github: 'eventmachine/eventmachine'
14
+ gem 'thin', github: 'macournoyer/thin' unless RUBY_PLATFORM =~ /mingw/
15
15
 
16
16
  # runner
17
17
  gem 'selenium-webdriver', require: false
data/README.md CHANGED
@@ -4,6 +4,9 @@ Opal-Browser - Client side web development in pure Ruby, using Opal
4
4
  [![Gem Version](https://badge.fury.io/rb/opal-browser.svg)](http://badge.fury.io/rb/opal-browser)
5
5
  [![Code Climate](https://img.shields.io/codeclimate/maintainability-percentage/opal/opal-browser.svg)](https://codeclimate.com/github/opal/opal-browser)
6
6
  [![Build Status](https://github.com/opal/opal-browser/workflows/build/badge.svg)](https://github.com/opal/opal-browser/actions?query=workflow%3Abuild)
7
+ [![Join Chat](https://img.shields.io/badge/slack-join%20chat-46BC99?logo=slack&style=flat)](https://slack.opalrb.com/)
8
+ [![Stack Overflow](https://img.shields.io/badge/stackoverflow-%23opalrb-orange.svg?style=flat)](https://stackoverflow.com/questions/ask?tags=opalrb,opal-browser)
9
+ [![Documentation](https://img.shields.io/badge/docs-updated-blue.svg)](https://rubydoc.info/gems/opal-browser)
7
10
 
8
11
  This library aims to be a full-blown wrapper for all the browser API as defined by
9
12
  HTML5.
@@ -70,7 +73,7 @@ _And load it in HTML!_
70
73
  </html>
71
74
  ```
72
75
 
73
- See the examples/integrations/ directory for various ideas on how to quickly start
76
+ See the `examples/integrations/` directory for various ideas on how to quickly start
74
77
  development using opal-browser.
75
78
 
76
79
  Features
@@ -109,6 +112,7 @@ Add an event to a given element:
109
112
  ```ruby
110
113
  $document.at_css("button").on(:click) do |e|
111
114
  e.prevent # Prevent the default action (eg. form submission)
115
+ # You can also use `e.stop` to stop propagating the event to other handlers.
112
116
  alert "Button clicked!"
113
117
  end
114
118
  ```
@@ -201,6 +205,17 @@ History
201
205
  -------
202
206
  The HTML5 History API has been fully wrapped.
203
207
 
208
+ ```ruby
209
+ current = $window.history.current
210
+ $window.history.replace("?get=params")
211
+ $window.history.push("?get=params")
212
+ $window.history.back
213
+
214
+ $window.on :popstate do |e|
215
+ p "User clicked a back button! He is now on #{$window.history.current}"
216
+ end
217
+ ```
218
+
204
219
  Storage
205
220
  -------
206
221
  The HTML5 Storage API has been wrapped and it exports a single Storage class
@@ -1,5 +1,3 @@
1
- require 'stringio'
2
-
3
1
  module Browser
4
2
 
5
3
  # Allows manipulation of browser cookies.
@@ -8,10 +6,19 @@ module Browser
8
6
  #
9
7
  # Usage:
10
8
  #
11
- # cookies = Browser::Cookies.new(`document`)
9
+ # cookies = $document.cookies
12
10
  # cookies["my-cookie"] = "monster"
13
11
  # cookies.delete("my-cookie")
14
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
15
22
  class Cookies
16
23
  # Default cookie options.
17
24
  DEFAULT = {
@@ -26,24 +33,34 @@ class Cookies
26
33
  # Create a new {Cookies} wrapper.
27
34
  #
28
35
  # @param document [native] the native document object
29
- def initialize(document)
36
+ # @param options [Hash] the default cookie options
37
+ def initialize(document, options = {})
30
38
  @document = document
31
- @options = DEFAULT.dup
39
+ @options = DEFAULT.merge(options)
32
40
  end
33
41
 
34
42
  # Access the cookie with the given name.
35
43
  #
36
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
37
48
  #
38
49
  # @return [Object]
39
- def [](name)
50
+ def [](name, options = {})
51
+ options = @options.merge(options)
52
+
40
53
  matches = `#@document.cookie`.scan(/#{Regexp.escape(FormData.encode(name))}=([^;]*)/)
41
54
 
42
55
  return if matches.empty?
43
56
 
44
- result = matches.flatten.map {|value|
45
- JSON.parse(FormData.decode(value))
46
- }
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
47
64
 
48
65
  result.length == 1 ? result.first : result
49
66
  end
@@ -54,38 +71,45 @@ class Cookies
54
71
  # @param value [Object] the data to set
55
72
  # @param options [Hash] the options for the cookie
56
73
  #
74
+ # @option options [Boolean] :raw don't encode a value with JSON
57
75
  # @option options [Integer] :max_age the max age of the cookie in seconds
58
76
  # @option options [Time] :expires the expire date
59
77
  # @option options [String] :path the path the cookie is valid on
60
78
  # @option options [String] :domain the domain the cookie is valid on
61
79
  # @option options [Boolean] :secure whether the cookie is secure or not
62
- def []=(name, value, options = {})
63
- string = value.is_a?(String) ? value : JSON.dump(value)
64
- encoded_value = encode(name, string, @options.merge(options))
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)
65
88
  `#@document.cookie = #{encoded_value}`
66
89
  end
67
90
 
68
91
  # Delete a cookie.
69
92
  #
70
93
  # @param name [String] the name of the cookie
71
- def delete(name)
94
+ def delete(name, _options = {})
72
95
  `#@document.cookie = #{encode name, '', expires: Time.now}`
73
96
  end
74
97
 
75
98
  # @!attribute [r] keys
76
99
  # @return [Array<String>] all the cookie names
77
- def keys
78
- Array(`#@document.cookie.split(/; /)`).map {|cookie|
100
+ def keys(_options = {})
101
+ Array(`#@document.cookie.split(/; /)`).map do |cookie|
79
102
  cookie.split(/\s*=\s*/).first
80
- }
103
+ end
81
104
  end
82
105
 
83
106
  # @!attribute [r] values
84
107
  # @return [Array] all the cookie values
85
- def values
86
- keys.map {|key|
87
- self[key]
88
- }
108
+ def values(options = {})
109
+ options = @options.merge(options)
110
+ keys.map do |key|
111
+ self[key, options]
112
+ end
89
113
  end
90
114
 
91
115
  # Enumerate the cookies.
@@ -93,13 +117,18 @@ class Cookies
93
117
  # @yieldparam key [String] the name of the cookie
94
118
  # @yieldparam value [String] the value of the cookie
95
119
  #
120
+ # @param options [Hash] the options for the cookie
121
+ #
122
+ # @option options [Boolean] :raw don't encode a value with JSON
123
+ #
96
124
  # @return [self]
97
- def each(&block)
98
- return enum_for :each unless block
125
+ def each(options = {}, &block)
126
+ return enum_for :each, options unless block
127
+ options = @options.merge(options)
99
128
 
100
- keys.each {|key|
101
- yield key, self[key]
102
- }
129
+ keys.each do |key|
130
+ yield key, self[key, options]
131
+ end
103
132
 
104
133
  self
105
134
  end
@@ -107,17 +136,17 @@ class Cookies
107
136
  # Delete all the cookies
108
137
  #
109
138
  # @return [self]
110
- def clear
111
- keys.each {|key|
139
+ def clear(_options = {})
140
+ keys.each do |key|
112
141
  delete key
113
- }
142
+ end
114
143
 
115
144
  self
116
145
  end
117
146
 
118
147
  protected
119
148
  def encode(key, value, options = {})
120
- io = StringIO.new
149
+ io = []
121
150
 
122
151
  io << FormData.encode(key) << ?= << FormData.encode(value) << '; '
123
152
 
@@ -127,15 +156,15 @@ protected
127
156
  io << 'domain=' << options[:domain] << '; ' if options[:domain]
128
157
  io << 'secure' if options[:secure]
129
158
 
130
- io.string
159
+ io.join
131
160
  end
132
161
  end
133
162
 
134
163
  class DOM::Document < DOM::Element
135
164
  # @!attribute [r] cookies
136
165
  # @return [Cookies] the cookies for the document
137
- def cookies
138
- Cookies.new(@native) if defined?(`#@native.cookie`)
166
+ def cookies(options = {})
167
+ Cookies.new(@native, options) if defined?(`#@native.cookie`)
139
168
  end
140
169
  end
141
170
 
@@ -1,3 +1,3 @@
1
1
  module Browser
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-browser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - meh.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-11-23 00:00:00.000000000 Z
12
+ date: 2021-12-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: opal
@@ -54,6 +54,7 @@ files:
54
54
  - ".github/workflows/build.yml"
55
55
  - ".gitignore"
56
56
  - ".yardopts"
57
+ - CHANGELOG.md
57
58
  - Gemfile
58
59
  - LICENSE
59
60
  - README.md