Skip to content

Commit 2b3dded

Browse files
committedNov 1, 2014
Make Element#[] return nil for empty attributes #39
1 parent 9ce17e8 commit 2b3dded

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed
 

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## edge
22

3+
* `Element#[]` and `Element#attr` now return `nil` for empty attributes,
4+
instead of returning an empty string.
5+
36
* Add `HTTP.setup` and `HTTP.setup=` to access `$.ajaxSetup`
47

58
* Add PATCH and HEAD support to `HTTP`

‎Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
source 'https://rubygems.org'
22
gemspec
33

4-
gem 'opal', github: 'opal/opal'
5-
# gem 'opal', path: '../opal'
4+
gem 'opal', github: 'opal/opal'
5+
gem 'opal-rspec', github: 'opal/opal-rspec'

‎opal/opal-jquery/element.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,18 @@ def to_n
9090
end
9191

9292
def [](name)
93-
`self.attr(name) || ""`
93+
`self.attr(name) || nil`
9494
end
9595

96-
def add_attribute name
97-
self[name] = ''
96+
def attr(name, value=nil)
97+
if value.nil?
98+
`self.attr(name) || nil`
99+
else
100+
`self.attr(name, value)`
101+
end
98102
end
99103

100-
def has_attribute? name
104+
def has_attribute?(name)
101105
`!!self.attr(name)`
102106
end
103107

‎spec/element/attributes_spec.rb

+16-5
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,30 @@
4545
Element.find('#attr-foo')[:title].should == "Hello there!"
4646
end
4747

48-
it 'should return an empty string for an empty attribute value' do
49-
Element.find('#attr-bar')[:title].should == ""
50-
Element.find('#attr-baz')[:title].should == ""
48+
it 'should return nil for an empty attribute' do
49+
expect(Element.find('#attr-bar')[:title]).to be_nil
50+
expect(Element.find('#attr-baz')[:title]).to be_nil
51+
end
52+
end
53+
54+
describe '#attr' do
55+
it 'returns attributes from elements' do
56+
expect(Element.find('#attr-foo').attr(:title)).to eq('Hello there!')
57+
end
58+
59+
it 'returns nil for empty attributes' do
60+
expect(Element.find('#attr-bar').attr(:title)).to be_nil
61+
expect(Element.find('#attr-baz').attr(:title)).to be_nil
5162
end
5263
end
5364

5465
describe '#[]=' do
5566
it 'should set the attr value on the element' do
5667
woosh = Element.find '#attr-woosh'
57-
woosh[:title].should == ""
68+
expect(woosh[:title]).to be_nil
5869

5970
woosh[:title] = "Oranges"
60-
woosh[:title].should == "Oranges"
71+
expect(woosh[:title]).to eq('Oranges')
6172
end
6273

6374
it 'should replace the old value for the attribute' do

0 commit comments

Comments
 (0)
Please sign in to comment.