Skip to content

A fourth composite pull request #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ apps << Class.new(Sinatra::Base) {
"lol"
end

post '/http-file' do
if params['lol'] == 'wut' &&
params['file'][:filename] == 'yay.txt' &&
params['file'][:tempfile].read == 'content'

"ok"
else
"fail"
end
end

get '/events' do
headers 'Content-Type' => 'text/event-stream'

Expand Down
1 change: 1 addition & 0 deletions opal/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require 'browser/version'
require 'browser/utils'
require 'browser/form_data'
require 'browser/support'

require 'browser/event'
Expand Down
83 changes: 83 additions & 0 deletions opal/browser/blob.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module Browser

class Blob
include NativeCachedWrapper

# Create a new blob from anything that Blob API supports
def self.create(from, options={})
new(`new Blob(#{Native.convert(from)}, #{options.to_n})`)
end

# @!attribute [r] size
# @return [Integer] blob size in bytes
def size
`#@native.size`
end

# @!attribute [r] type
# @return [String] blob mime type
def type
`#@native.type`
end

# Convert a blob to a UTF-8 encoded string.
#
# If block is given it will be called with a parameter once we receive
# the text. Otherwise return a {Promise} which will resolve once we
# receive it.
def text(&block)
promise = nil
unless block_given?
promise = Promise.new
block = proc { |i| promise.resolve(i) }
end
`#@native.text().then(#{block.to_n})`
promise
end

# {Buffer} view into the blob
def buffer
Buffer.new(`#@native.arrayBuffer()`)
end

# Create a new blob by slicing this blob
def slice(start, finish=nil)
Blob.new(`#@native.slice(#{start}, #{finish})`)
end

# Convert a blob to an URL that can be used to reference this blob in DOM
# eg. display some multimedia
def to_url(window=$window)
`#{window.to_n}.URL.createObjectURL(#@native)`
end

# Rename a blob and return a {File} with a new name.
#
# @return [File] a renamed blob
def rename(new_filename)
File.create([self], new_filename, type: type,
lastModified: respond_to?(:last_modified) ?
last_modified : Time.now)
end
end

class File < Blob
# Create a new file from anything that File API supports
def self.create(from, name, options={})
new(`new File(#{Native.convert(from)}, #{name}, #{options.to_n})`)
end

# @!attribute [r] last_modified
# @return [Time] last modified date of this file
def last_modified
Time.at(`#@native.lastModified`/1000.0)
end

# @!attribute [r] name
# @return [String] filename
def name
`#@native.name`
end
end

end
6 changes: 3 additions & 3 deletions opal/browser/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def initialize(document)
#
# @return [Object]
def [](name)
matches = `#@document.cookie`.scan(/#{Regexp.escape(name.encode_uri_component)}=([^;]*)/)
matches = `#@document.cookie`.scan(/#{Regexp.escape(FormData.encode(name))}=([^;]*)/)

return if matches.empty?

result = matches.flatten.map {|value|
JSON.parse(value.decode_uri_component)
JSON.parse(FormData.decode(value))
}

result.length == 1 ? result.first : result
Expand Down Expand Up @@ -119,7 +119,7 @@ def clear
def encode(key, value, options = {})
io = StringIO.new

io << key.encode_uri_component << ?= << value.encode_uri_component << '; '
io << FormData.encode(key) << ?= << FormData.encode(value) << '; '

io << 'max-age=' << options[:max_age] << '; ' if options[:max_age]
io << 'expires=' << options[:expires].utc << '; ' if options[:expires]
Expand Down
30 changes: 23 additions & 7 deletions opal/browser/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@
require 'browser/css/rule/style'

module Kernel
# Create a <style> element from a string or a block using the
# {Paggio::CSS} DSL.
# @overload CSS(document = $document, &block)
#
# @param text [String] the CSS text
# @return [Browser::DOM::Element] the create <style> element
def CSS(text = nil, &block)
style = $document.create_element(:style)
# Create a `<style>` element from a {Paggio::CSS} DSL.
#
# @param document [Browser::DOM::Document] the document instance
# we intend to use
#
# @return [Browser::DOM::Element] the created `<style>` element
#
# @overload CSS(string, document = $document)
#
# Create a `<style>` element from a string.
#
# @param document [Browser::DOM::Document] the document instance
# we intend to use
#
# @return [Browser::DOM::Element] the created `<style>` element
def CSS(*args, &block)
document = if args.length > 1 || block_given?
args.pop
end || $document

style = document.create_element(:style)
style[:type] = 'text/css'

if block
style.inner_text = Paggio.css(&block)
else
style.inner_text = text
style.inner_text = args.join("")
end

style
Expand Down
16 changes: 16 additions & 0 deletions opal/browser/delay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ def after(time, &block)
def after!(time, &block)
Delay.new(@native, time, &block)
end

# Returns a promise that will resolve after the given seconds.
#
# @param time [Float] the seconds after it gets called
#
# @return [Promise] the promise that will resolve after timeout happens
def resolve_after(time)
promise = Promise.new
Delay.new(@native, time) { promise.resolve }.start
promise
end
end

end
Expand All @@ -63,6 +74,11 @@ def after(time, &block)
def after!(time, &block)
$window.after!(time, &block)
end

# (see Browser::Window#resolve_after)
def resolve_after(time)
$window.resolve_after(time)
end
end

class Proc
Expand Down
28 changes: 26 additions & 2 deletions opal/browser/dom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,33 @@ def XML(what)
DOM(`doc`)
end

# Wrap a native element or create a DOM tree using the {Paggio::HTML} DSL.
# @overload DOM(document = $document, &block)
#
# Create a DOM tree using the {Paggio::HTML} DSL.
#
# @param document [Browser::DOM::Document] the document instance
# we intend to use
#
# @return [Browser::DOM::Node, Browser::DOM::NodeSet]
#
# @overload DOM(string, document = $document)
#
# Create a DOM tree from a HTML string.
#
# @param string [String] the HTML string
# @param document [Browser::DOM::Document] the document instance
# we intend to use
#
# @return [Browser::DOM::Node]
#
# @overload DOM(native)
#
# Wrap a native element to create a DOM tree.
#
# @param native [Native] the Native node
#
# @return [Browser::DOM::Node]
#
# @return [Browser::DOM::Node]
def DOM(*args, &block)
if block
document = args.shift || $document
Expand Down
7 changes: 7 additions & 0 deletions opal/browser/dom/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require 'browser/dom/element/button'
require 'browser/dom/element/image'
require 'browser/dom/element/form'
require 'browser/dom/element/input'
require 'browser/dom/element/select'
require 'browser/dom/element/template'
Expand Down Expand Up @@ -216,6 +217,12 @@ def css(selector)
end
end

# Click the element. it fires the element's click event.
def click
`#@native.click()`
self
end

# @overload data()
#
# Return the data for the element.
Expand Down
40 changes: 40 additions & 0 deletions opal/browser/dom/element/form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'browser/blob'

module Browser; module DOM; class Element < Node

class Form < Element
# Capture the content of this form to a new {FormData} object,
#
# @return [FormData]
def form_data
FormData.create(self)
end

# Submit a form. This will fire a submit event.
def submit
`#@native.submit()`
end

# Reset a form. This will fire a reset event.
def reset
`#@native.reset()`
end

alias_native :action
alias_native :action=
alias_native :method
alias_native :method=
alias_native :target
alias_native :target=
alias_native :name
alias_native :name=
alias_native :encoding
alias_native :encoding=

# Return a NodeSet containing all form controls belonging to this form element.
def controls
NodeSet[Native::Array.new(`#@native.elements`)]
end
end

end; end; end
2 changes: 1 addition & 1 deletion opal/browser/dom/element/iframe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def content_window
# @!attribute [r] content_document
# @return [Document] document of content of this iframe
def content_document
DOM(`#@native.contentDocument`)
DOM(`#@native.contentDocument || #@native.contentWindow.document`)
end

# Send a message to the iframe content's window.
Expand Down
8 changes: 8 additions & 0 deletions opal/browser/dom/element/input.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'browser/blob'

module Browser; module DOM; class Element < Node

class Input < Element
Expand Down Expand Up @@ -31,6 +33,12 @@ def uncheck!
def clear
`#@native.value = ''`
end

# @!attribute [r] files
# @return [Array<File>] list of files attached to this {Input}
def files
Native::Array.new(`#@native.files`).map { |f| File.new(f.to_n) }
end
end

end; end; end
6 changes: 0 additions & 6 deletions opal/browser/effects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ def focused?
`#@native.hasFocus`
end

# Click the element. it fires the element's click event.
def click
`#@native.click()`
self
end

# Queue the block to happen when currently queued animations finish or during
# the next animation frame.
def animation_queue &block
Expand Down
5 changes: 3 additions & 2 deletions opal/browser/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'browser/event/keyboard'
require 'browser/event/focus'
require 'browser/event/wheel'
require 'browser/event/data_transfer'
require 'browser/event/composition'
require 'browser/event/animation'
require 'browser/event/audio_processing'
Expand Down Expand Up @@ -55,7 +56,7 @@ def self.class_for(name)
when 'compositionend', 'compositionstart', 'compositionupdate'
Composition

when 'copy', 'cut'
when 'copy', 'cut', 'paste'
Clipboard

when 'devicelight'
Expand All @@ -70,7 +71,7 @@ def self.class_for(name)
when 'deviceproximity'
DeviceProximity

when 'drag', 'dragend', 'dragleave', 'dragover', 'dragstart', 'drop'
when 'drag', 'dragend', 'dragenter', 'dragexit', 'dragleave', 'dragover', 'dragstart', 'drop'
Drag

when 'gamepadconnected', 'gamepaddisconnected'
Expand Down
7 changes: 7 additions & 0 deletions opal/browser/event/clipboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def self.construct(name, desc)

alias_native :data
alias_native :type, :dataType

# Returns a {DataTransfer} related to this event
#
# @see https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer
def transfer
DataTransfer.new(`#@native.clipboardData`)
end
end

end; end
Loading