Skip to content

Commit 8164455

Browse files
authored
Include empty attributes in HTML output (#543)
This is mostly a full revert of commit 1526789 which was a bad change. Empty attributes are valid HTML and should be supported in Arbre (e.g. boolean attributes). The bad commit was for just addressing the class attribute but that applied to any attribute in HTML when it shouldn't have, even for class. Note that if an attribute value is nil then it is removed. This follows similar output logic to Rails `tag.attributes` helper.
1 parent 70ed0b9 commit 8164455

File tree

2 files changed

+8
-17
lines changed

2 files changed

+8
-17
lines changed

lib/arbre/html/attributes.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ module HTML
55
class Attributes < Hash
66

77
def to_s
8-
flatten_hash.map do |name, value|
9-
next if value_empty?(value)
8+
flatten_hash.compact.map do |name, value|
109
"#{html_escape(name)}=\"#{html_escape(value)}\""
1110
end.compact.join ' '
1211
end
1312

14-
def any?
15-
super{ |k,v| !value_empty?(v) }
16-
end
17-
1813
protected
1914

2015
def flatten_hash(hash = self, old_path = [], accumulator = {})
@@ -29,10 +24,6 @@ def flatten_hash(hash = self, old_path = [], accumulator = {})
2924
accumulator
3025
end
3126

32-
def value_empty?(value)
33-
value.respond_to?(:empty?) ? value.empty? : !value
34-
end
35-
3627
def html_escape(s)
3728
ERB::Util.html_escape(s)
3829
end

spec/arbre/unit/html/tag_attributes_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
1919
end
2020

21-
it "shouldn't render attributes that are empty" do
21+
it "should still render attributes that are empty but not nil" do
2222
tag.class_list # initializes an empty ClassList
2323
tag.set_attribute :foo, ''
2424
tag.set_attribute :bar, nil
2525

26-
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
26+
expect(tag.to_s).to eq "<tag id=\"my_id\" class=\"\" foo=\"\"></tag>\n"
2727
end
2828

2929
context "with hyphenated attributes" do
@@ -41,28 +41,28 @@
4141
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\"></tag>\n"
4242
end
4343

44-
it "shouldn't render attributes that are empty" do
44+
it "should still render attributes that are empty but not nil" do
4545
tag.class_list # initializes an empty ClassList
4646
tag.set_attribute :foo, { bar: '' }
4747
tag.set_attribute :bar, { baz: nil }
4848

49-
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\"></tag>\n"
49+
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\" class=\"\" foo-bar=\"\"></tag>\n"
5050
end
5151
end
5252

5353
context "when there is a deeply nested attribute" do
54-
before { tag.build id: "my_id", foo: { bar: { baz: 'foozle' } } }
54+
before { tag.build id: "my_id", foo: { bar: { bat: nil, baz: 'foozle' } } }
5555

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

6161
context "when there are multiple nested attributes" do
62-
before { tag.build id: "my_id", foo: { bar: 'foozle1', baz: 'foozle2' } }
62+
before { tag.build id: "my_id", foo: { bar: 'foozle1', bat: nil, baz: '' } }
6363

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

0 commit comments

Comments
 (0)