opal-jquery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ module Kernel
2
+ def alert(msg)
3
+ `alert(msg)`
4
+ nil
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ module LocalStorage
2
+ def self.[](key)
3
+ %x{
4
+ var val = localStorage.getItem(key);
5
+ return val === null ? nil : val;
6
+ }
7
+ end
8
+
9
+ def self.[]=(key, value)
10
+ `localStorage.setItem(key, value)`
11
+ end
12
+
13
+ def self.clear
14
+ `localStorage.clear()`
15
+ self
16
+ end
17
+
18
+ def self.delete(key)
19
+ %x{
20
+ var val = localStorage.getItem(key);
21
+ localStorage.removeItem(key);
22
+ return val === null ? nil : val;
23
+ }
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'opal-jquery'
5
+ s.version = '0.0.1'
6
+ s.author = 'Adam Beynon'
7
+ s.email = 'adam.beynon@gmail.com'
8
+ s.homepage = 'http://opalrb.org'
9
+ s.summary = 'Opal access to jquery'
10
+ s.description = 'Opal DOM library for jquery'
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.require_paths = ['lib']
16
+ end
@@ -0,0 +1 @@
1
+ hey
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "Adam",
3
+ "age": 26
4
+ }
@@ -0,0 +1,91 @@
1
+ describe Document do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="foo" class="bar"></div>
5
+ <div class="woosh"></div>
6
+ <div class="woosh"></div>
7
+ <div class="find-foo"></div>
8
+ <div class="find-bar"></div>
9
+ <div class="find-foo"></div>
10
+ HTML
11
+
12
+ @div.append_to_body
13
+ end
14
+
15
+ after do
16
+ @div.remove
17
+ end
18
+
19
+ describe ".[]" do
20
+ it "should be able to find elements with given id" do
21
+ Document['#foo'].class_name.should == "bar"
22
+ Document['#foo'].size.should == 1
23
+ end
24
+
25
+ it "should be able to match any valid CSS selector" do
26
+ Document['.woosh'].should be_kind_of(Element)
27
+ Document['.woosh'].size.should == 2
28
+ end
29
+
30
+ it "should return an empty Elements instance when not matching any elements" do
31
+ dom = Document['.some-non-existing-class']
32
+
33
+ dom.should be_kind_of(Element)
34
+ dom.size.should == 0
35
+ end
36
+
37
+ it "should accept an HTML string and parse it into a Elements instance" do
38
+ el = Document['<div id="foo-bar-baz"></div>']
39
+
40
+ el.should be_kind_of(Element)
41
+ el.id.should == "foo-bar-baz"
42
+ el.size.should == 1
43
+ end
44
+ end
45
+
46
+ describe ".find" do
47
+ it "should find all elements matching CSS selector" do
48
+ foo = Document.find '.find-foo'
49
+ foo.should be_kind_of(Element)
50
+ foo.length.should == 2
51
+
52
+ bar = Document.find '.find-bar'
53
+ bar.should be_kind_of(Element)
54
+ bar.length.should == 1
55
+ end
56
+
57
+ it "should return an empty Element instance with length 0 when no matching" do
58
+ baz = Document.find '.find-baz'
59
+ baz.should be_kind_of(Element)
60
+ baz.length.should == 0
61
+ end
62
+ end
63
+
64
+ describe '.id' do
65
+ it "should return a new instance with the element with given id" do
66
+ Document.id('foo').should be_kind_of(Element)
67
+ Document.id('foo').id.should == 'foo'
68
+ end
69
+
70
+ it "should return nil if no element could be found" do
71
+ Document.id('bad-element-id').should be_nil
72
+ end
73
+ end
74
+
75
+ describe '.parse' do
76
+ it "should return a new instance with parsed element as single element" do
77
+ foo = Document.parse '<div id="foo" class="bar"></div>'
78
+ foo.id.should == 'foo'
79
+ foo.class_name.should == 'bar'
80
+ end
81
+ end
82
+
83
+ describe '.title and .title=' do
84
+ it 'sets/gets the page title' do
85
+ current = Document.title
86
+ Document.title = 'hello from opal'
87
+ Document.title.should == 'hello from opal'
88
+ Document.title = current
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,33 @@
1
+ describe "Element#after" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="after-spec">
5
+ <div id="some-header" class="kapow"></div>
6
+ <div id="foo" class="after-spec-first"></div>
7
+ <div id="bar" class="after-spec-first"></div>
8
+ <div id="baz"></div>
9
+ </div>
10
+ HTML
11
+
12
+ @div.append_to_body
13
+ end
14
+
15
+ after do
16
+ @div.remove
17
+ end
18
+
19
+ it "should insert the given html string after each element" do
20
+ el = Document['.after-spec-first']
21
+ el.size.should == 2
22
+
23
+ el.after '<p class="woosh"></p>'
24
+
25
+ Document.id('foo').next.class_name.should == "woosh"
26
+ Document.id('bar').next.class_name.should == "woosh"
27
+ end
28
+
29
+ it "should insert the given DOM element after this element" do
30
+ Document.id('baz').after Document.id('some-header')
31
+ Document.id('baz').next.id.should == "some-header"
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ describe "Element animation methods" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="animate-foo"></div>
5
+ HTML
6
+
7
+ @div.append_to_body
8
+
9
+ Document.id("animate-foo").css("width", "0px")
10
+ end
11
+
12
+ after do
13
+ @div.remove
14
+ end
15
+
16
+ describe "#animate" do
17
+ ### HACKY
18
+ # jQUery's animate method doesn't *always* finish on time
19
+ # so the values are being compared using greater than
20
+
21
+ it "should animate a set of properties and values" do
22
+ foo = Document.id "animate-foo"
23
+ foo.animate :width => "200px"
24
+
25
+ set_timeout 400 do
26
+ (foo.css("width").to_f > 199).should be_true
27
+ end
28
+ end
29
+
30
+ it "should allow you to set a speed in the params" do
31
+ foo = Document.id "animate-foo"
32
+ foo.animate :width => "200px", :speed => 100
33
+
34
+ set_timeout 105 do
35
+ (foo.css("width").to_f > 199).should be_true
36
+ end
37
+ end
38
+
39
+ it "should accept a block as a callback" do
40
+ foo = Document.id "animate-foo"
41
+ foo.animate :width => "200px" do
42
+ foo.add_class "finished"
43
+ end
44
+
45
+ set_timeout 405 do
46
+ foo.class_name.should equal("finished")
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,36 @@
1
+ describe "Element#append" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="append-spec">
5
+ <div id="foo" class="first-append"></div>
6
+ <div id="bar" class="first-append"></div>
7
+ <div id="baz"></div>
8
+ <div id="buz"></div>
9
+ </div>
10
+ HTML
11
+
12
+ @div.append_to_body
13
+ end
14
+
15
+ after do
16
+ @div.remove
17
+ end
18
+
19
+ it "should insert the HTML string to the end of each element" do
20
+ Document.find('.first-append').append '<p class="woosh"></p>'
21
+
22
+ Document.id('foo').children.class_name.should == "woosh"
23
+ Document.id('bar').children.class_name.should == "woosh"
24
+ end
25
+
26
+ it "should insert the given DOM node at the end of the element" do
27
+ baz = Document.id 'baz'
28
+ buz = Document.id 'buz'
29
+
30
+ baz.children.size.should == 0
31
+ baz.append buz
32
+
33
+ baz.children.size.should == 1
34
+ baz.children.id.should == "buz"
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ describe "Element#append_to" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="foo"></div>
5
+ <div id="bar"></div>
6
+ <div id="baz"></div>
7
+ HTML
8
+
9
+ @div.append_to_body
10
+ end
11
+
12
+ after do
13
+ @div.remove
14
+ end
15
+
16
+ it "should insert the receiver into the target element" do
17
+ Document.id('foo').children.size.should == 0
18
+
19
+ Document.parse('<ul class="kapow"></ul>').append_to Document.id('foo')
20
+ Document.id('foo').children.class_name.should == "kapow"
21
+
22
+ Document.id('bar').append_to Document.id('baz')
23
+ Document.id('baz').children.id.should == "bar"
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ describe "Element#at" do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="at-spec">
5
+ <div class="foo" id="blah"></div>
6
+ <div class="foo" id="bleh"></div>
7
+ <div class="foo" id="bluh"></div>
8
+ </div>
9
+ HTML
10
+
11
+ @div.append_to_body
12
+ end
13
+
14
+ after do
15
+ @div.remove
16
+ end
17
+
18
+ it "returns the element at the given index" do
19
+ foos = Document.find '.foo'
20
+ foos.length.should == 3
21
+
22
+ foos.at(0).id.should == "blah"
23
+ foos.at(1).id.should == "bleh"
24
+ foos.at(2).id.should == "bluh"
25
+ end
26
+
27
+ it "counts from the last index for negative values" do
28
+ foos = Document.find '.foo'
29
+
30
+ foos.at(-1).id.should == "bluh"
31
+ foos.at(-2).id.should == "bleh"
32
+ foos.at(-3).id.should == "blah"
33
+ end
34
+
35
+ it "returns nil for indexes outside range" do
36
+ foos = Document.find '.foo'
37
+
38
+ foos.at(-4).should == nil
39
+ foos.at(4).should == nil
40
+ end
41
+ end
@@ -0,0 +1,202 @@
1
+ describe Element do
2
+ before do
3
+ @div = Document.parse <<-HTML
4
+ <div id="attributes-spec">
5
+ <div id="foo"></div>
6
+ <div id="bar" class="apples"></div>
7
+ <div id="baz" class="lemons"></div>
8
+
9
+ <div id="attr-foo" title="Hello there!"></div>
10
+ <div id="attr-bar"></div>
11
+ <div id="attr-baz" title=""></div>
12
+ <div id="attr-woosh"></div>
13
+ <div id="attr-kapow" title="Apples"></div>
14
+
15
+ <div id="has-foo" class="apples"></div>
16
+ <div id="has-bar" class="lemons bananas"></div>
17
+
18
+ <div id="html-foo">Hey there</div>
19
+ <div id="html-bar"><p>Erm</p></div>
20
+
21
+ <div class="html-bridge">Hello</div>
22
+ <div class="html-bridge">Hello as well</div>
23
+
24
+ <div id="remove-foo"></div>
25
+
26
+ <div id="remove-bar" class="lemons"></div>
27
+ <div id="remove-baz" class="apples oranges"></div>
28
+ <div id="remove-buz" class="pineapples mangos"></div>
29
+
30
+ <div id="remove-bleh" class="fruit"></div>
31
+
32
+ <select id="value-foo">
33
+ <option selected="selected">Hello</option>
34
+ <option>World</option>
35
+ </select>
36
+
37
+ <input id="value-bar" type="text" value="Blah"></input>
38
+ <div id="value-baz"></div>
39
+
40
+ <input type="text" id="value-woosh" value=""></input>
41
+ </div>
42
+ HTML
43
+
44
+ @div.append_to_body
45
+ end
46
+
47
+ after do
48
+ @div.remove
49
+ end
50
+
51
+ describe '#[]' do
52
+ it 'should retrieve the attr value from the element' do
53
+ Document.id('attr-foo')[:title].should == "Hello there!"
54
+ end
55
+
56
+ it 'should return an empty string for an empty attribute value' do
57
+ Document.id('attr-bar')[:title].should == ""
58
+ Document.id('attr-baz')[:title].should == ""
59
+ end
60
+ end
61
+
62
+ describe '#[]=' do
63
+ it 'should set the attr value on the element' do
64
+ woosh = Document.id 'attr-woosh'
65
+ woosh[:title].should == ""
66
+
67
+ woosh[:title] = "Oranges"
68
+ woosh[:title].should == "Oranges"
69
+ end
70
+
71
+ it 'should replace the old value for the attribute' do
72
+ kapow = Document.id 'attr-kapow'
73
+ kapow[:title].should == "Apples"
74
+
75
+ kapow[:title] = "Pineapple"
76
+ kapow[:title].should == "Pineapple"
77
+ end
78
+ end
79
+
80
+ describe "#add_class" do
81
+ it "should add the given class name to the element" do
82
+ foo = Document.id 'foo'
83
+ foo.has_class?('lemons').should be_false
84
+ foo.add_class 'lemons'
85
+ foo.has_class?('lemons').should be_true
86
+ end
87
+
88
+ it "should not duplicate class names on an element" do
89
+ bar = Document.id 'bar'
90
+ bar.has_class?('apples').should be_true
91
+ bar.add_class 'apples'
92
+ bar.class_name.should == 'apples'
93
+ end
94
+
95
+ it "should return self" do
96
+ baz = Document.id 'baz'
97
+ baz.add_class('oranges').should equal(baz)
98
+ baz.add_class('oranges').should equal(baz)
99
+ end
100
+ end
101
+
102
+ describe '#has_class?' do
103
+ it "should return true if the element has the given class" do
104
+ Document.id('has-foo').has_class?("apples").should be_true
105
+ Document.id('has-foo').has_class?("oranges").should be_false
106
+ Document.id('has-bar').has_class?("lemons").should be_true
107
+ end
108
+ end
109
+
110
+ describe '#html' do
111
+ it "should return the html content of the element" do
112
+ Document.id('html-foo').html.should == "Hey there"
113
+ Document.id('html-bar').html.downcase.should == "<p>erm</p>"
114
+ end
115
+
116
+ it "should only return html for first matched element" do
117
+ Document.find('.html-bridge').html.should == "Hello"
118
+ end
119
+
120
+ it "should return empty string for empty set" do
121
+ Document.find('.html-nothing-here').html.should == ""
122
+ end
123
+ end
124
+
125
+ describe '#remove_class' do
126
+ it "should have no effect on elements without class" do
127
+ foo = Document.id 'remove-foo'
128
+ foo.class_name.should == ''
129
+ foo.remove_class 'blah'
130
+ foo.class_name.should == ''
131
+ end
132
+
133
+ it "should remove the given class from the element" do
134
+ bar = Document.id 'remove-bar'
135
+ bar.remove_class "lemons"
136
+ bar.class_name.should == ''
137
+
138
+ baz = Document.id 'remove-baz'
139
+ baz.remove_class 'lemons'
140
+ baz.class_name.should == 'apples oranges'
141
+
142
+ baz.remove_class 'apples'
143
+ baz.class_name.should == 'oranges'
144
+
145
+ buz = Document.id 'remove-buz'
146
+ buz.remove_class 'mangos'
147
+ buz.class_name.should == 'pineapples'
148
+
149
+ buz.remove_class 'pineapples'
150
+ buz.class_name.should == ''
151
+ end
152
+
153
+ it "should return self" do
154
+ bleh = Document.id 'remove-bleh'
155
+ bleh.remove_class('fruit').should equal(bleh)
156
+ bleh.remove_class('hmmmm').should equal(bleh)
157
+ end
158
+ end
159
+
160
+ describe '#toggle_class' do
161
+ it 'adds the given class name to the element if not already present' do
162
+ foo = Document['#foo']
163
+ foo.has_class?('oranges').should be_false
164
+ foo.toggle_class 'oranges'
165
+ foo.has_class?('oranges').should be_true
166
+ end
167
+
168
+ it 'removes the class if the element already has it' do
169
+ bar = Document['#bar']
170
+ bar.has_class?('apples').should be_true
171
+ bar.toggle_class 'apples'
172
+ bar.has_class?('apples').should be_false
173
+ end
174
+ end
175
+
176
+ describe "#value" do
177
+ it "should return the selected value of select elements" do
178
+ Document.id('value-foo').value.should == "Hello"
179
+ end
180
+
181
+ it "should return the value of normal input fields" do
182
+ Document.id('value-bar').value.should == "Blah"
183
+ end
184
+
185
+ it "should return an empty string for elements with no value attr" do
186
+ Document.id('value-baz').value.should == ""
187
+ end
188
+ end
189
+
190
+ describe "#value=" do
191
+ it "should set the value of the element to the given value" do
192
+ foo = Document.id 'value-woosh'
193
+ foo.value.should == ""
194
+
195
+ foo.value = "Hi"
196
+ foo.value.should == "Hi"
197
+
198
+ foo.value = "There"
199
+ foo.value.should == "There"
200
+ end
201
+ end
202
+ end