Skip to content

Commit

Permalink
Proc should return a Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Spone committed Feb 6, 2023
1 parent f16a4d0 commit d8ed3fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,21 +697,21 @@ You can define custom shortcuts using lambdas.
In this example we add `~` to create a shortcut with a special processing (adding a prefix) for the class attribute.

~~~ ruby
Slim::Engine.set_options shortcut: {'~' => {attr: ->(v) {["class", "\"styled-#{v}\""]}}}
Slim::Engine.set_options shortcut: {'~' => {attr: ->(v) {{class: "styled-#{v}"}}}}
~~~

We can use it in Slim code like this

~~~ slim
h1~title Hello
~text~question How are you?
~text~question.paragraph How are you?
~~~

which renders to

~~~ html
<h1 class="styled-title">Hello</h1>
<div class="styled-text styled-question">How are you?</div>
<div class="styled-text styled-question paragraph">How are you?</div>
~~~

#### ID shortcut `#` and class shortcut `.`
Expand Down
7 changes: 3 additions & 4 deletions lib/slim/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,10 @@ def parse_tag(tag)
syntax_error!('Illegal shortcut') unless shortcut = @attr_shortcut[$1]

if shortcut.is_a?(Proc)
value = shortcut.call($2)
attributes << [:html, :attr, value[0], [:dynamic, value[1]]]
values = shortcut.call($2)
values.each {|a, v| attributes << [:html, :attr, a, [:static, v]] }
else
value = [:static, $2]
shortcut.each {|a| attributes << [:html, :attr, a, value] }
shortcut.each {|a| attributes << [:html, :attr, a, [:static, $2]] }
end

if additional_attr_pairs = @additional_attrs[$1]
Expand Down
26 changes: 17 additions & 9 deletions test/core/test_html_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,32 @@ def test_render_with_custom_lambda_shortcut
~foo Hello
}
assert_html '<div class="styled-foo">Hello</div>',
source, shortcut: {'~' => {attr: ->(v) {["class", "\"styled-#{v}\""]}}}
source, shortcut: {'~' => {attr: ->(v) {{class: "styled-#{v}"}}}}
end

def test_render_with_custom_lambda_shortcut_and_multiple_values
source = %q{
~foo~bar Hello
}
assert_html '<div class="styled-foo styled-bar">Hello</div>',
source, shortcut: {'~' => {attr: ->(v) {["class", "\"styled-#{v}\""]}}}
source, shortcut: {'~' => {attr: ->(v) {{class: "styled-#{v}"}}}}
end

# def test_render_with_custom_lambda_shortcut_and_existing_class
# source = %q{
# ~foo.baz Hello
# }
# assert_html '<div class="styled-foo baz">Hello</div>',
# source, shortcut: {'~' => {attr: ->(v) {["class", "\"styled-#{v}\""]}}}
# end
def test_render_with_custom_lambda_shortcut_and_existing_class
source = %q{
~foo.baz Hello
}
assert_html '<div class="styled-foo baz">Hello</div>',
source, shortcut: {'~' => {attr: ->(v) {{class: "styled-#{v}"}}}, '.' => {attr: 'class'}}
end

def test_render_with_existing_class_and_custom_lambda_shortcut
source = %q{
.baz~foo Hello
}
assert_html '<div class="baz styled-foo">Hello</div>',
source, shortcut: {'~' => {attr: ->(v) {{class: "styled-#{v}"}}}, '.' => {attr: 'class'}}
end

def test_render_with_text_block
source = %q{
Expand Down

0 comments on commit d8ed3fa

Please sign in to comment.