Skip to content

Commit

Permalink
Add option to hyphenate underscore attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscz committed Jun 10, 2020
1 parent 1f3bcf0 commit 8e9be5f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,8 @@ There are a lot of them but the good thing is, that Slim checks the configuratio
| Symbol | :format | :xhtml | HTML output format (Possible formats :html, :xhtml, :xml) |
| String | :attr_quote | '"' | Character to wrap attributes in html (can be ' or ") |
| Hash | :merge_attrs | \{'class' => ' '} | Joining character used if multiple html attributes are supplied (e.g. class="class1 class2") |
| Array<String> | :hyphen_attrs | %w(data) | Attributes which will be hyphenated if a Hash is given (e.g. data={a:1,b:2} will render as data-a="1" data-b="2") |
| Array<String> | :hyphen_attrs | %w(data) | Attributes which will be hyphenated if a Hash is given (e.g. data={a_foo:1,b:2} will render as data-a_foo="1" data-b="2") |
| Boolean | :hyphen_underscore_attrs | false | Attributes that have underscores in their names will be hyphenated (e.g. data={a_foo:1,b_bar:2} will render as data-a-foo="1" data-b-bar="2") |
| Boolean | :sort_attrs | true | Sort attributes by name |
| Symbol | :js_wrapper | nil | Wrap javascript by :comment, :cdata or :both. You can also :guess the wrapper based on :format. |
| Boolean | :pretty | false | Pretty HTML indenting, only block level tags are indented <b>(This is slower!)</b> |
Expand Down
10 changes: 8 additions & 2 deletions lib/slim/splat/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ def build_attrs

def hyphen_attr(name, escape, value)
if Hash === value
value.each do |n, v|
hyphen_attr("#{name}-#{n}", escape, v)
if @options[:hyphen_underscore_attrs]
value.each do |n, v|
hyphen_attr("#{name}-#{n.to_s.gsub('_', '-')}", escape, v)
end
else
value.each do |n, v|
hyphen_attr("#{name}-#{n}", escape, v)
end
end
else
attr(name, escape_html(value != true && escape, value))
Expand Down
3 changes: 2 additions & 1 deletion lib/slim/splat/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Splat
# @api private
class Filter < ::Slim::Filter
define_options :merge_attrs, :attr_quote, :sort_attrs, :default_tag, :format, :disable_capture,
hyphen_attrs: %w(data aria), use_html_safe: ''.respond_to?(:html_safe?)
hyphen_attrs: %w(data aria), use_html_safe: ''.respond_to?(:html_safe?),
hyphen_underscore_attrs: false

def call(exp)
@splat_options = nil
Expand Down
8 changes: 8 additions & 0 deletions test/core/test_html_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ def test_hyphenated_attribute
assert_html '<div class="alpha" data-a="alpha" data-b="beta" data-c-e="epsilon" data-c_d="gamma"></div>', source
end

def test_hyphenated_underscore_attribute
source = %{
.alpha data={a: 'alpha', b: 'beta', c_d: 'gamma', c: {e: 'epsilon'}}
}

assert_html '<div class="alpha" data-a="alpha" data-b="beta" data-c-d="gamma" data-c-e="epsilon"></div>', source, hyphen_underscore_attrs: true
end

def test_splat_without_content
source = %q{
*hash
Expand Down

0 comments on commit 8e9be5f

Please sign in to comment.