Skip to content

Commit

Permalink
Fixed bug with element contents being copied into attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Aman committed Jan 8, 2011
1 parent 66c932e commit 308a49d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/sax-machine/sax_element_config.rb
Expand Up @@ -64,8 +64,12 @@ def attrs_match?(attrs)
end
end

def has_value?
!@value.nil?
end

def has_value_and_attrs_match?(attrs)
!@value.nil? && attrs_match?(attrs)
has_value? && attrs_match?(attrs)
end

def xmlns_match?(ns)
Expand Down
4 changes: 2 additions & 2 deletions lib/sax-machine/sax_handler.rb
Expand Up @@ -56,15 +56,15 @@ def end_element(name)
reset_current_collection
elsif parsing_collection?
@collection_handler.end_element(name)
elsif characaters_captured?
elsif characters_captured? && !@element_config.has_value?
@object.send(@element_config.setter, @value)
end

reset_current_tag
@nsstack = @nsstack.pop
end

def characaters_captured?
def characters_captured?
!@value.nil? && !@value.empty?
end

Expand Down
46 changes: 46 additions & 0 deletions spec/sax-machine/sax_document_spec.rb
Expand Up @@ -229,6 +229,31 @@ def title=(val)
end
end

it "should not save a value when the attribute is missing" do
document = @klass.parse("<link />")
document.link.should == nil
end

it "should not save a value when the attribute is missing" do
document = @klass.parse("<link bar='foo' />")
document.link.should == nil
end

it "should not save a value when the attribute is missing" do
document = @klass.parse("<link></link>")
document.link.should == nil
end

it "should not save a value when the attribute is missing" do
document = @klass.parse("<link>\n</link>")
document.link.should == nil
end

it "should not save a value when the attribute is missing" do
document = @klass.parse("<link>not foo</link>")
document.link.should == nil
end

it "should save the attribute value" do
document = @klass.parse("<link foo='test'>hello</link>")
document.link.should == 'test'
Expand Down Expand Up @@ -283,6 +308,27 @@ def title=(val)
document.link_foo.should == 'test1'
document.link_bar.should == 'test2'
end

it "should parse the element and attribute values" do
document = @klass.parse("<link foo='test1'>hello</link>")
document.link.should == 'hello'
document.link_foo.should == 'test1'
document.link_bar.should == nil
end

it "should parse the element and attribute values" do
document = @klass.parse("<link bar='test2'>hello</link>")
document.link.should == 'hello'
document.link_foo.should == nil
document.link_bar.should == 'test2'
end

it "should parse the element and attribute values" do
document = @klass.parse("<link>hello</link>")
document.link.should == 'hello'
document.link_foo.should == nil
document.link_bar.should == nil
end
end

describe "when specifying namespaces" do
Expand Down

0 comments on commit 308a49d

Please sign in to comment.