Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option to strip attributes for empty tags #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/nori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ def self.hash_key(name, options = {})

def initialize(options = {})
defaults = {
:strip_namespaces => false,
:delete_namespace_attributes => false,
:convert_tags_to => nil,
:convert_attributes_to => nil,
:empty_tag_value => nil,
:advanced_typecasting => true,
:convert_dashes_to_underscores => true,
:scrub_xml => true,
:parser => :nokogiri
:strip_namespaces => false,
:delete_namespace_attributes => false,
:convert_tags_to => nil,
:convert_attributes_to => nil,
:empty_tag_value => nil,
:advanced_typecasting => true,
:convert_dashes_to_underscores => true,
:scrub_xml => true,
:strip_attributes_of_empty_tags => false,
:parser => :nokogiri
}

validate_options! defaults.keys, options.keys
Expand Down
4 changes: 3 additions & 1 deletion lib/nori/xml_utility_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def to_hash
out.merge!( k => v.map{|e| e.to_hash[k]})
end
end
out.merge! prefixed_attributes unless attributes.empty?
if !(@options[:strip_attributes_of_empty_tags] && @children.empty?) && attributes.any?
out.merge! prefixed_attributes
end
out = out.empty? ? @options[:empty_tag_value] : out
end

Expand Down
47 changes: 47 additions & 0 deletions spec/nori/nori_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,53 @@
expect(parse(' ')).to eq({})
end

context "with strip_attributes_of_empty_tags set to true" do
it "should strip attributes of empty tags" do
expect(parse('<foo bar="" />', strip_attributes_of_empty_tags: true)).to eq({ 'foo' => nil })
end

it "should strip attributes only of empty tags also in a nested structure" do
xml = <<-XML
<content>
<foo />
<foo bar="baz" />
<foo bar="baz" bar2="baz2"></foo>
<foo bar="baz">actual content</foo>
</content>
XML

expect(parse(xml, strip_attributes_of_empty_tags: true)).to match({
"content" => {
"foo" => [nil, nil, nil, be_a(Nori::StringWithAttributes).and(eq("actual content"))],
}
})
end
end

context "with strip_attributes_of_empty_tags set to false" do
it "should not strip any attributes" do
xml = <<-XML
<content>
<foo />
<foo bar="baz" />
<foo bar="baz" bar2="baz2"></foo>
<foo bar="baz">actual content</foo>
</content>
XML

expect(parse(xml, strip_attributes_of_empty_tags: false)).to match({
"content" => {
"foo" => [
nil,
{"@bar"=>"baz"},
{"@bar"=>"baz", "@bar2"=>"baz2"},
be_a(Nori::StringWithAttributes).and(eq("actual content"))
],
}
})
end
end

end
end

Expand Down