Skip to content

Commit

Permalink
Revert "Include blank attributes in HTML output (#517)" (#541)
Browse files Browse the repository at this point in the history
This reverts commit cf59b8f.
  • Loading branch information
javierjulio committed Oct 8, 2023
1 parent 09aba77 commit b706341
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
17 changes: 11 additions & 6 deletions lib/arbre/html/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class Attributes < Hash

def to_s
flatten_hash.map do |name, value|
if value.nil?
html_escape(name)
else
"#{html_escape(name)}=\"#{html_escape(value)}\""
end
end.join ' '
next if value_empty?(value)
"#{html_escape(name)}=\"#{html_escape(value)}\""
end.compact.join ' '
end

def any?
super{ |k,v| !value_empty?(v) }
end

protected
Expand All @@ -28,6 +29,10 @@ def flatten_hash(hash = self, old_path = [], accumulator = {})
accumulator
end

def value_empty?(value)
value.respond_to?(:empty?) ? value.empty? : !value
end

def html_escape(s)
ERB::Util.html_escape(s)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/arbre/html/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def child_is_text?
end

def attributes_html
attributes.any? ? " " + attributes.to_s : nil
" #{attributes}" if attributes.any?
end

def set_for_attribute(record)
Expand Down
8 changes: 4 additions & 4 deletions spec/arbre/unit/html/tag_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
end

it "should still render attributes that are empty" do
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\" class=\"\" foo=\"\" bar></tag>\n"
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
end

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

it "should still render attributes that are empty" do
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 }

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

Expand Down

0 comments on commit b706341

Please sign in to comment.