Skip to content

Commit

Permalink
Merge pull request #50 from davydovanton/link-to-in-html
Browse files Browse the repository at this point in the history
Link to in html
  • Loading branch information
Trung Lê committed Feb 4, 2016
2 parents 70d064e + fb76a9a commit cae1e74
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 13 deletions.
76 changes: 76 additions & 0 deletions lib/hanami/helpers/html_helper/html_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ def empty_tag(name, attributes = nil)
#
# @return [self]
#
# @since 0.1.0
# @api public
#
# @see Hanami::Helpers::HtmlHelper
# @see Hanami::Helpers::HtmlHelper::TextNode
#
Expand All @@ -308,6 +311,56 @@ def text(content)
self
end

# Returns self
#
# @return [self]
#
# @since x.x.x
# @api public
#
# @see Hanami::Helpers::HtmlHelper
# @see Hanami::Helpers::HtmlHelper::TextNode
#
# @example
#
# html.label do
# p 'hello'
# html.span 'hanami'
# end
#
# # <label>
# # <p>hello</p>
# # <span>hanami</span>
# # </label>
def html
self
end

# Alias for link_to helper
#
# @return [self]
#
# @since x.x.x
# @api public
#
# @see Hanami::Helpers::LinkToHelper
#
# @example
#
# html.label do
# p 'hello'
# link_to('Posts', 'posts')
# end
#
# # <label>
# # <p>hello</p>
# # <a href=\"posts\">Posts</a>
# # </label>
def link_to(content, url = nil, options = {}, &blk)
link_to_builder(content, url, options, &blk)
self
end

# @since 0.2.5
# @api private
alias_method :+, :text
Expand Down Expand Up @@ -346,6 +399,29 @@ def nested?
@nodes.any?
end

# Simple builder helper with returns HtmlBuilder
# instance with a tag.
#
# @return [self] the result of the check
#
# @since x.x.x
# @api private
def link_to_builder(content, url, options, &blk)
if block_given?
options = url || {}
url = content
content = nil
end

begin
options[:href] = url or raise ArgumentError
rescue TypeError
raise ArgumentError
end

a(blk || content, options)
end

# Resolve the context for nested contents
#
# @since 0.1.0
Expand Down
14 changes: 1 addition & 13 deletions lib/hanami/helpers/link_to_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,7 @@ module LinkToHelper
# <%= link_to 'Home' %>
# # => ArgumentError
def link_to(content, url = nil, options = {}, &blk)
if block_given?
options = url || {}
url = content
content = nil
end

begin
options[:href] = url or raise ArgumentError
rescue TypeError
raise ArgumentError
end

html.a(blk || content, options).to_s
html.link_to_builder(content, url, options, &blk).to_s
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,29 @@ def link_to_with_content_html_content_id_and_class
end

end

class HtmlAndHtml
include Hanami::Helpers::HtmlHelper
include Hanami::Helpers::LinkToHelper

def two_links_to_in_div
html.div do
link_to('Comments', 'comments')
link_to('Posts', 'posts')
end
end

def span_and_link_to_in_div
html.div do
span 'hello'
link_to('Posts', 'posts')
end
end

def two_spans_in_div
html.div do
span 'hello'
html.span 'world'
end
end
end
18 changes: 18 additions & 0 deletions test/html_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,22 @@
it 'autoescapes nested blocks' do
@view.evil_nested_block_content.to_s.must_equal %(<div>\n<p>&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;</p>\n</div>)
end

describe 'with link_to helper' do
before do
@view = HtmlAndHtml.new
end

it 'returns two links in div' do
@view.two_links_to_in_div.to_s.must_equal %(<div>\n<a href=\"comments\">Comments</a>\n<a href=\"posts\">Posts</a>\n</div>)
end

it 'returns span and link in div' do
@view.span_and_link_to_in_div.to_s.must_equal %(<div>\n<span>hello</span>\n<a href=\"posts\">Posts</a>\n</div>)
end

it 'returns two spans in div' do
@view.two_spans_in_div.to_s.must_equal %(<div>\n<span>hello</span>\n<span>world</span>\n</div>)
end
end
end

0 comments on commit cae1e74

Please sign in to comment.