bowser 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ce8d7cc946f87f6ff13c2f734fe21374a41a30c5
4
- data.tar.gz: 8f5e88a218400a259cafc0fb5bf5c5c083fa3aa5
3
+ metadata.gz: c8764d8fafbc1b0bceb01df46a44ef1c97957c38
4
+ data.tar.gz: 0f92be9feadace2fad0b02138476a823e49b440f
5
5
  SHA512:
6
- metadata.gz: cbf88b08d8fde0e49c923d09eaf10a190b5bb52ecab4ab70c6ad3c6b0ff95e68d74bf054cd5d8246836a5134eda8dea1754528270240be45b6bc4eb868da4ac1
7
- data.tar.gz: 77d638123b06602ace4cb47b434906ec010aaf6ea858ef4b8821c359b890c9d5d529aeba9fb59734f00028baa0c85d4cbc06a47dd68292a6d643694f564001db
6
+ metadata.gz: 5d5e3db8ed89a6bd2fcab026ebc4fca1b705d95118c51571e3d5951d831bdbff9cdcc41a2132b2e1e86e71cd26ad4416412a78dede2dbcfc7dea1e68e78c2fc4
7
+ data.tar.gz: a1da66808e31e6ddca3b7a6de2f39146f14263f782521389eb9be80db57849585634052985e0a2ee0df1645125d674fe4d3c5d4ac0d5471c3f935dca2001e61a
data/.travis.yml CHANGED
@@ -1,13 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.5
4
- - 2.3.1
5
- - 2.4.1
6
- - jruby-9.0.5.0
7
- - jruby-9.1.1.0
3
+ - 2.2
4
+ - 2.3
5
+ - 2.4
6
+ - jruby-9.1.14.0
8
7
  before_install:
9
- - wget https://s3.amazonaws.com/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2
10
- - tar -xjf phantomjs-2.0.0-ubuntu-12.04.tar.bz2
11
- - sudo rm -rf /usr/local/phantomjs/bin/phantomjs
12
- - sudo mv phantomjs /usr/local/phantomjs/bin/phantomjs
13
8
  - gem install bundler
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## Version 0.5.0
2
+
3
+ - IndexedDB support (#22)
4
+ - ServiceWorker support (#5)
5
+ - Subscript operators (`[]` and `[]=`) to get/set HTML attributes on `Element`
6
+ - Added `HTTP::Response#ok?` to align with [`Response.ok`](https://developer.mozilla.org/en-US/docs/Web/API/Response/ok)
7
+
1
8
  ## Version 0.3.0
2
9
 
3
10
  - Fix bug with falsy JS values as element properties
@@ -1,3 +1,3 @@
1
1
  module Bowser
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
data/opal/bowser.rb CHANGED
@@ -3,6 +3,3 @@ require 'bowser/event'
3
3
  require 'bowser/event_target'
4
4
  require 'bowser/window'
5
5
  require 'bowser/document'
6
-
7
- module Bowser
8
- end
@@ -1,6 +1,14 @@
1
1
  module Bowser
2
2
  module DelegateNative
3
- # Fall back to native properties.
3
+ # Fall back to native properties. If the message sent to this element is not
4
+ # recognized, it checks to see if it is a property of the native element. It
5
+ # also checks for variations of the message name, such as:
6
+ #
7
+ # :supported? => [:supported, :isSupported]
8
+ #
9
+ # If a property with the specified message name is found and it is a
10
+ # function, that function is invoked with `args`. Otherwise, the property
11
+ # is returned as is.
4
12
  def method_missing message, *args, &block
5
13
  property_name = property_for_message(message)
6
14
  property = `#@native[#{property_name}]`
@@ -11,10 +11,13 @@ module Bowser
11
11
 
12
12
  module_function
13
13
 
14
+ # @return [Bowser::Element] the body element of the current document
14
15
  def body
15
16
  @body ||= Element.new(`#@native.body`)
16
17
  end
17
18
 
19
+ # @return [Bowser::Element?] the first element that matches the given CSS
20
+ # selector or `nil` if no elements match
18
21
  def [] css
19
22
  native = `#@native.querySelector(css)`
20
23
  if `#{native} === null`
@@ -24,6 +27,10 @@ module Bowser
24
27
  end
25
28
  end
26
29
 
30
+ # Create an element of the specified type
31
+ #
32
+ # @param type [String] the type of element to create
33
+ # @example Bowser.document.create_element('div')
27
34
  def create_element type
28
35
  Element.new(`document.createElement(type)`)
29
36
  end
@@ -31,6 +38,7 @@ module Bowser
31
38
 
32
39
  module_function
33
40
 
41
+ # @return [Document] the browser's document object
34
42
  def document
35
43
  Document
36
44
  end
@@ -3,6 +3,7 @@ require 'bowser/file_list'
3
3
  require 'bowser/delegate_native'
4
4
 
5
5
  module Bowser
6
+ # Wrap a native DOM element
6
7
  class Element
7
8
  include EventTarget
8
9
  include DelegateNative
@@ -23,23 +24,37 @@ module Bowser
23
24
  element
24
25
  end
25
26
 
27
+ # @param native [JS] The native DOM element to wrap
26
28
  def initialize native
27
29
  @native = native
28
30
  end
29
31
 
30
- def inner_dom= node
32
+ # Replace all child elements with the given element
33
+ #
34
+ # @param element [Bowser::Element] The Bowser element with which to replace
35
+ # this element's contents
36
+ def inner_dom= element
31
37
  clear
32
- append node
38
+ append element
33
39
  end
34
40
 
41
+ # The contents of this element as an HTML string
42
+ #
43
+ # @return [String] the HTML representation of this element's contents
35
44
  def inner_html
36
45
  `#@native.innerHTML`
37
46
  end
38
47
 
48
+ # Use the supplied HTML string to replace this element's contents
49
+ #
50
+ # @param html [String] the HTML with which to replace this elements contents
39
51
  def inner_html= html
40
52
  `#@native.innerHTML = html`
41
53
  end
42
54
 
55
+ # This element's direct child elements
56
+ #
57
+ # @return [Array<Bowser::Element>] list of this element's children
43
58
  def children
44
59
  elements = []
45
60
 
@@ -53,10 +68,17 @@ module Bowser
53
68
  elements
54
69
  end
55
70
 
71
+ # Determine whether this element has any contents
72
+ #
73
+ # @return [Boolean] true if the element has no children, false otherwise
56
74
  def empty?
57
75
  `#@native.children.length === 0`
58
76
  end
59
77
 
78
+ # Remove all contents from this element. After this call, `empty?` will
79
+ # return `true`.
80
+ #
81
+ # @return [Bowser::Element] self
60
82
  def clear
61
83
  if %w(input textarea).include? type
62
84
  `#@native.value = null`
@@ -69,29 +91,49 @@ module Bowser
69
91
  self
70
92
  end
71
93
 
94
+ # Remove the specified child element
95
+ #
96
+ # @param child [Bowser::Element] the child element to remove
72
97
  def remove_child child
73
98
  `#@native.removeChild(child['native'] ? child['native'] : child)`
74
99
  end
75
100
 
101
+ # This element's type. For example: "div", "span", "p"
102
+ #
103
+ # @return [String] the HTML tag name for this element
76
104
  def type
77
105
  `#@native.nodeName`.downcase
78
106
  end
79
107
 
108
+ # Append the specified element as a child element
109
+ #
110
+ # @param element [Bowser::Element, JS] the element to insert
80
111
  def append node
81
112
  `#@native.appendChild(node['native'] ? node['native'] : node)`
82
113
  self
83
114
  end
84
115
 
85
- # Form input methods
116
+ # Methods for <input /> elements
117
+
118
+ # A checkbox's checked status
119
+ #
120
+ # @return [Boolean] true if the checkbox is checked, false otherwise
86
121
  def checked?
87
122
  `!!#@native.checked`
88
123
  end
89
124
 
90
- # Convenience for when you only need a single file
125
+ # Get the currently selected file for this input. This is only useful for
126
+ # file inputs without the `multiple` property set.
127
+ #
128
+ # @return [Bowser::File] the file selected by the user
91
129
  def file
92
130
  files.first
93
131
  end
94
132
 
133
+ # Get the currently selected files for this input. This is only useful for
134
+ # file inputs with the `multiple` property set.
135
+ #
136
+ # @return [Bowser::FileList] the currently selected files for this input
95
137
  def files
96
138
  FileList.new(`#@native.files`)
97
139
  end
@@ -100,6 +142,17 @@ module Bowser
100
142
  `#@native === #{other.to_n}`
101
143
  end
102
144
 
145
+ def []= attribute, value
146
+ `#@native.setAttribute(#{attribute}, #{value})`
147
+ end
148
+
149
+ def [] attribute
150
+ `#@native.getAttribute(#{attribute})`
151
+ end
152
+
153
+ # The native representation of this element.
154
+ #
155
+ # @return [JS] the native element wrapped by this object.
103
156
  def to_n
104
157
  @native
105
158
  end
data/opal/bowser/event.rb CHANGED
@@ -1,47 +1,76 @@
1
1
  module Bowser
2
+ # Wrapper for JS events
2
3
  class Event
4
+ # @param [JS] the native event to wrap
3
5
  def initialize native
4
6
  @native = native
5
7
  end
6
8
 
9
+ # Prevent the runtime from executing this event's default behavior. For
10
+ # example, prevent navigation after clicking a link.
11
+ #
12
+ # @return [Bowser::Event] self
7
13
  def prevent
8
14
  `#@native.preventDefault()`
9
15
  self
10
16
  end
11
17
 
18
+ # Prevent the runtime from bubbling this event up the hierarchy. This is
19
+ # typically used to keep an event local to the element on which it was
20
+ # triggered, such as keeping a click event on a button from unintentionally
21
+ # triggering a handler on a parent element.
22
+ #
23
+ # @return self
12
24
  def stop_propagation
13
25
  `#@native.stopPropagation()`
14
26
  self
15
27
  end
16
28
 
29
+ # @return [Boolean] true if `prevent` has been called on this event, false
30
+ # otherwise
17
31
  def prevented?
18
32
  `#@native.defaultPrevented`
19
33
  end
20
34
 
35
+ # @return [Boolean] true if the Meta/Command/Windows key was pressed when
36
+ # this event fired, false otherwise
21
37
  def meta?
22
38
  `#@native.metaKey`
23
39
  end
24
40
 
41
+ # @return [Boolean] true if the Shift key was pressed when this event fired,
42
+ # false otherwise
25
43
  def shift?
26
44
  `#@native.shiftKey`
27
45
  end
28
46
 
47
+ # @return [Boolean] true if the Ctrl key was pressed when this event fired,
48
+ # false otherwise
29
49
  def ctrl?
30
50
  `#@native.ctrlKey`
31
51
  end
32
52
 
53
+ # @return [Boolean] true if the Alt key was pressed when this event fired,
54
+ # false otherwise
33
55
  def alt?
34
56
  `#@native.altKey`
35
57
  end
36
58
 
59
+ # The target for this event
60
+ #
61
+ # @return [Bowser::Element] the element on which this event was triggered
62
+ # @todo Handle non-DOM events here
37
63
  def target
38
64
  Element.new(`#@native.target`)
39
65
  end
40
66
 
67
+ # @return [Numeric] the key code associated with this event. Only useful for
68
+ # keyboard-based events.
41
69
  def code
42
70
  `#@native.keyCode`
43
71
  end
44
72
 
73
+ # Return properties on the event not covered by Ruby methods.
45
74
  def method_missing name, *args
46
75
  property = name.gsub(/_[a-z]/) { |match| match[-1, 1].upcase }
47
76
  value = `#@native[property]`
@@ -55,6 +84,7 @@ module Bowser
55
84
  end
56
85
  end
57
86
 
87
+ # @return [JS] the native event wrapped by this object.
58
88
  def to_n
59
89
  @native
60
90
  end
@@ -2,6 +2,12 @@ require 'bowser/event'
2
2
 
3
3
  module Bowser
4
4
  module EventTarget
5
+ # Add the block as a handler for the specified event name. Will use either
6
+ # `addEventListener` or `addListener` if they exist.
7
+ #
8
+ # @param event_name [String] the name of the event
9
+ # @return [Proc] the block to pass to `off` to remove this handler
10
+ # @yieldparam event [Bowser::Event] the event object
5
11
  def on event_name, &block
6
12
  wrapper = proc { |event| block.call Event.new(event) }
7
13
 
@@ -16,6 +22,10 @@ module Bowser
16
22
  wrapper
17
23
  end
18
24
 
25
+ # Remove an event handler
26
+ #
27
+ # @param event_name [String] the name of the event
28
+ # @block the handler to remove, as returned from `on`
19
29
  def off event_name, &block
20
30
  if `#@native.removeEventListener !== undefined`
21
31
  `#@native.removeEventListener(event_name, block)`
@@ -5,6 +5,7 @@ module Bowser
5
5
  class FileList
6
6
  include Enumerable
7
7
 
8
+ # @param native [JS] the native FileList object to wrap
8
9
  def initialize native
9
10
  @native = `#{native} || []`
10
11
  @files = length.times.each_with_object([]) { |index, array|
@@ -12,54 +13,72 @@ module Bowser
12
13
  }
13
14
  end
14
15
 
16
+ # @param index [Integer] the index of the file in the list
17
+ # @return [Bowser::FileList::File] the file at the specified index
15
18
  def [] index
16
19
  @files[index]
17
20
  end
18
21
 
22
+ # @return [Integer] the number of files in this list
19
23
  def length
20
24
  `#@native.length`
21
25
  end
22
26
  alias size length
23
27
 
28
+ # Call the given block for each file in the list
29
+ #
30
+ # @yieldparam file [Bowser::FileList::File]
24
31
  def each &block
25
32
  @files.each do |file|
26
33
  block.call file
27
34
  end
28
35
  end
29
36
 
37
+ # Convert this FileList into an array
30
38
  def to_a
31
39
  @files.dup # Don't return a value that can mutate our internal state
32
40
  end
33
41
  alias to_ary to_a
34
42
 
43
+ # @return [String] a string representation of this FileList
35
44
  def to_s
36
45
  @files.to_s
37
46
  end
38
47
 
48
+ # An individual item in a FileList
39
49
  class File
40
50
  attr_reader :data
41
51
 
52
+ # @param native [JS] the native File object to wrap
42
53
  def initialize native
43
54
  @native = native
44
55
  @data = nil
45
56
  end
46
57
 
58
+ # @return [String] the filename
47
59
  def name
48
60
  `#@native.name`
49
61
  end
50
62
 
63
+ # @return [Integer] the size of this file on disk
51
64
  def size
52
65
  `#@native.size`
53
66
  end
54
67
 
68
+ # @return [String] the MIME type of the file, detected by the browser
55
69
  def type
56
70
  `#@native.type`
57
71
  end
58
72
 
73
+ # @return [Time] the timestamp of the file
59
74
  def last_modified
60
75
  `#@native.lastModifiedDate`
61
76
  end
62
77
 
78
+ # Read the file from disk into memory
79
+ #
80
+ # @return [Promise] a promise that resolves when finished loading and
81
+ # rejects if an error occurs while loading.
63
82
  def read
64
83
  promise = Promise.new
65
84
  reader = FileReader.new
@@ -79,10 +98,16 @@ module Bowser
79
98
  promise
80
99
  end
81
100
 
101
+ # Convert to the native object
102
+ #
103
+ # @return [JS.HTMLElement] the underlying native element
82
104
  def to_n
83
105
  @native
84
106
  end
85
107
 
108
+ # The object that reads the file from disk.
109
+ #
110
+ # @api private
86
111
  class FileReader
87
112
  include EventTarget
88
113