Skip to content

Commit b706341

Browse files
authored
Revert "Include blank attributes in HTML output (#517)" (#541)
This reverts commit cf59b8f.
1 parent 09aba77 commit b706341

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

lib/arbre/html/attributes.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ class Attributes < Hash
66

77
def to_s
88
flatten_hash.map do |name, value|
9-
if value.nil?
10-
html_escape(name)
11-
else
12-
"#{html_escape(name)}=\"#{html_escape(value)}\""
13-
end
14-
end.join ' '
9+
next if value_empty?(value)
10+
"#{html_escape(name)}=\"#{html_escape(value)}\""
11+
end.compact.join ' '
12+
end
13+
14+
def any?
15+
super{ |k,v| !value_empty?(v) }
1516
end
1617

1718
protected
@@ -28,6 +29,10 @@ def flatten_hash(hash = self, old_path = [], accumulator = {})
2829
accumulator
2930
end
3031

32+
def value_empty?(value)
33+
value.respond_to?(:empty?) ? value.empty? : !value
34+
end
35+
3136
def html_escape(s)
3237
ERB::Util.html_escape(s)
3338
end

lib/arbre/html/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def child_is_text?
150150
end
151151

152152
def attributes_html
153-
attributes.any? ? " " + attributes.to_s : nil
153+
" #{attributes}" if attributes.any?
154154
end
155155

156156
def set_for_attribute(record)

spec/arbre/unit/html/tag_attributes_spec.rb

Lines changed: 4 additions & 4 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 "should still render attributes that are empty" do
21+
it "shouldn't render attributes that are empty" 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\" class=\"\" foo=\"\" bar></tag>\n"
26+
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
2727
end
2828

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

44-
it "should still render attributes that are empty" do
44+
it "shouldn't render attributes that are empty" 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\" class=\"\" foo-bar=\"\" bar-baz></tag>\n"
49+
expect(tag.to_s).to eq "<tag id=\"my_id\" data-action=\"some_action\"></tag>\n"
5050
end
5151
end
5252

0 commit comments

Comments
 (0)