Fix using symbol on Element#attribute alwas getting nil

This commit is contained in:
PikachuEXE 2017-04-13 11:33:44 +08:00 committed by Yorick Peterse
parent e9953d4212
commit 21b5eeec4b
2 changed files with 20 additions and 10 deletions

View File

@ -64,14 +64,14 @@ module Oga
#
# @return [Oga::XML::Attribute]
def attribute(name)
if html?
ns = nil
name_str, ns = if html?
[name.to_s, nil]
else
name, ns = split_name(name)
split_name(name)
end
attributes.each do |attr|
return attr if attribute_matches?(attr, ns, name)
return attr if attribute_matches?(attr, ns, name_str)
end
return
@ -115,10 +115,10 @@ module Oga
if found
found.value = value
else
name, ns = split_name(name)
name_str, ns = split_name(name)
attr = Attribute.new(
:name => name,
:name => name_str,
:namespace_name => ns,
:value => value
)

View File

@ -107,13 +107,23 @@ describe Oga::XML::Element do
end
describe 'using an HTML document' do
it 'returns an attribute containing a namespace separator' do
attr = Oga::XML::Attribute.new(:name => 'foo:bar', :value => 'foo')
el = described_class.new(:name => 'foo', :attributes => [attr])
doc = Oga::XML::Document.new(:children => [el], :type => :html)
let!(:attr) do
Oga::XML::Attribute.new(:name => 'foo:bar', :value => 'foo')
end
let!(:el) do
described_class.new(:name => 'foo', :attributes => [attr])
end
let!(:doc) do
Oga::XML::Document.new(:children => [el], :type => :html)
end
it 'returns an attribute with a name containing a namespace separator' do
el.attribute('foo:bar').should == attr
end
it 'returns an attribute with a name containing a namespace separator when using a Symbol' do
el.attribute(:'foo:bar').should == attr
end
end
end