Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal-jquery
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d1e607b270e5
Choose a base ref
...
head repository: opal/opal-jquery
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7b5c30053364
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on May 18, 2015

  1. Fix specs for Element#[]

    elia committed May 18, 2015

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    8fb6553 View commit details
  2. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    7b5c300 View commit details
Showing with 48 additions and 19 deletions.
  1. +6 −0 CHANGELOG.md
  2. +22 −13 lib/opal/jquery/element.rb
  3. +20 −6 spec/element/attributes_spec.rb
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## EDGE

* `Element#[]=` now removes the attribute when the assigned value is nil.

* `Element#attr` now better follows jQuery's behaviour by checking the number of arguments passed. Also it's now just a proxy to `Element#[]` and `Element#[]=`

* `Element#[]` now returns `nil` only for attributes that are missing, better following jQuery behaviour (which is to return `undefined` on such attributes). `Element#has_attribute?` has been updated accordingly.

* Add `Element#prepend` method.

## 0.3.0 2015-02-03
35 changes: 22 additions & 13 deletions lib/opal/jquery/element.rb
Original file line number Diff line number Diff line change
@@ -301,13 +301,6 @@ def self.expose(*methods)
alias succ next
alias << append

# @!method []=(attr, value)
#
# Set the given attribute `attr` on each element in this collection.
#
# @see http://api.jquery.com/attr/
alias_native :[]=, :attr

# @!method add_class(class_name)
alias_native :add_class, :addClass

@@ -405,12 +398,28 @@ def [](name)
}
end

def attr(name, value=nil)
if value.nil?
`self.attr(name) || nil`
else
`self.attr(name, value)`
end
# Set the given attribute `attr` on each element in this collection.
#
# @see http://api.jquery.com/attr/
def []=(name, value)
`return self.removeAttr(name)` if value.nil?
`self.attr(name, value)`
end

def attr(*args)
%x{
var size = args.length;
switch (size) {
case 1:
return #{self[`args[0]`]};
break;
case 2:
return #{self[`args[0]`] = `args[1]`};
break;
default:
#{raise ArgumentError, '#attr only accepts 1 or 2 arguments'}
}
}
end

def has_attribute?(name)
26 changes: 20 additions & 6 deletions spec/element/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@
<div id="attr-baz" title=""></div>
<div id="attr-woosh"></div>
<div id="attr-kapow" title="Apples"></div>
<div id="attr-empty" attr-empty-value=""></div>
<div id="attr-missing" attr-auto-value></div>
<div id="has-foo" class="apples"></div>
<div id="has-bar" class="lemons bananas"></div>
@@ -45,9 +47,15 @@
Element.find('#attr-foo')[:title].should == "Hello there!"
end

it 'should return nil for an empty attribute' do
expect(Element.find('#attr-bar')[:title]).to be_nil
expect(Element.find('#attr-baz')[:title]).to be_nil
it 'should return nil for a missing attribute' do
expect(Element.find('#attr-missing')['attr-missing-value']).to be_nil
end

it 'should return "" for an attribute with empty value' do
expect(Element.find('#attr-empty')['attr-empty-value']).to eq("")

# Not sure if this is browser dependant
expect(Element.find('#attr-missing')['attr-auto-value']).to eq("")
end
end

@@ -56,9 +64,15 @@
expect(Element.find('#attr-foo').attr(:title)).to eq('Hello there!')
end

it 'returns nil for empty attributes' do
expect(Element.find('#attr-bar').attr(:title)).to be_nil
expect(Element.find('#attr-baz').attr(:title)).to be_nil
it 'should return nil for a missing attribute' do
expect(Element.find('#attr-missing').attr('attr-missing-value')).to be_nil
end

it 'should return "" for an attribute with empty value' do
expect(Element.find('#attr-empty').attr('attr-empty-value')).to eq("")

# Not sure if this is browser dependant
expect(Element.find('#attr-missing').attr('attr-auto-value')).to eq("")
end
end