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

Support nested attribute hashes rendered as hyphenated attributes #451

Merged
merged 2 commits into from
Mar 18, 2023
Merged
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
14 changes: 13 additions & 1 deletion lib/arbre/html/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module HTML
class Attributes < Hash

def to_s
map do |name, value|
flatten_hash.map do |name, value|
next if value_empty?(value)
"#{html_escape(name)}=\"#{html_escape(value)}\""
end.compact.join ' '
Expand All @@ -17,6 +17,18 @@ def any?

protected

def flatten_hash(hash = self, old_path = [], accumulator = {})
hash.each do |key, value|
path = old_path + [key]
if value.is_a? Hash
flatten_hash(value, path, accumulator)
else
accumulator[path.join('-')] = value
end
end
accumulator
end

def value_empty?(value)
value.respond_to?(:empty?) ? value.empty? : !value
end
Expand Down
50 changes: 42 additions & 8 deletions spec/arbre/unit/html/tag_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,50 @@
expect(tag.attributes).to eq({id: "my_id"})
end

it "should render the attributes to html" do
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
end
describe "#to_s" do
it "should render the attributes to html" do
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
end

it "shouldn't render attributes that are empty" do
tag.class_list # initializes an empty ClassList
tag.set_attribute :foo, ''
tag.set_attribute :bar, nil

expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
end

context "when there is a nested attribute" do
before { tag.build id: "my_id", data: { action: 'some_action' } }

it "should flatten the attributes when rendering to html" do
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\"></tag>\n"
end

it "shouldn't render attributes that are empty" do
tag.class_list # initializes an empty ClassList
tag.set_attribute :foo, { bar: '' }
tag.set_attribute :bar, { baz: nil }

it "shouldn't render attributes that are empty" do
tag.class_list # initializes an empty ClassList
tag.set_attribute :foo, ''
tag.set_attribute :bar, nil
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\"></tag>\n"
end
end

context "when there is a deeply nested attribute" do
before { tag.build id: "my_id", foo: { bar: { baz: 'foozle' } } }

expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
it "should flatten the attributes when rendering to html" do
expect(tag.to_s).to eq "<tag id=\"my_id\" foo-bar-baz=\"foozle\"></tag>\n"
end
end

context "when there are multiple nested attributes" do
before { tag.build id: "my_id", foo: { bar: 'foozle1', baz: 'foozle2' } }

it "should flatten the attributes when rendering to html" do
expect(tag.to_s).to eq "<tag id=\"my_id\" foo-bar=\"foozle1\" foo-baz=\"foozle2\"></tag>\n"
end
end
end

it "should get an attribute value" do
Expand Down