Skip to content

Commit

Permalink
dom/attribute: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 6, 2014
1 parent 05c61a7 commit 297750b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
19 changes: 7 additions & 12 deletions opal/browser/dom/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@ module Browser; module DOM
class Attribute
include Native

# Returns true if the attribute is an id.
def id?
`#@native.isId`
end

# @!attribute [r] name
# @return [String] the name of the attribute
def name
`#@native.name`
end
alias_native :name

# @!attribute [r] value
# @!attribute value
# @return [String] the value of the attribute
def value
`#@native.value`
end
alias_native :value
alias_native :value=

# Returns true if the attribute is an id.
alias_native :id?, :isId
end

end; end
49 changes: 49 additions & 0 deletions spec/dom/attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'spec_helper'

describe Browser::DOM::Attribute do
html <<-HTML
<div id="lol" class="hue" something="wat"></div>
HTML

describe '#name' do
it 'gets the right name' do
attr = $document['lol'].attribute_nodes.find { |a| a.name == :id }

expect(attr.name).to eq(:id)
end
end

describe '#value' do
it 'gets the right value' do
attr = $document['lol'].attribute_nodes.find { |a| a.name == :id }

expect(attr.name).to eq(:id)
expect(attr.value).to eq(:lol)
end
end

describe '#value=' do
it 'sets the value' do
attr = $document['lol'].attribute_nodes.find { |a| a.name == :id }

expect(attr.name).to eq(:id)
expect(attr.value).to eq(:lol)
attr.value = :omg
expect(attr.value).to eq(:omg)
end
end

describe '#id?' do
it 'is true for an id attribute' do
attr = $document['lol'].attribute_nodes.find { |a| a.name == :id }

expect(attr.id?).to be_truthy
end

it 'is false for any other attribute' do
attr = $document['lol'].attribute_nodes.find { |a| a.name == :class }

expect(attr.id?).to be_falsy
end
end
end

0 comments on commit 297750b

Please sign in to comment.