Last active
December 14, 2015 09:50
-
-
Save rafbm/5067714 to your computer and use it in GitHub Desktop.
Rails view helper for stripping whitespace between elements. Very useful for using `display: inline-block`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
def inline_tag(*args, &block) | |
body = Nokogiri::HTML(capture(&block)).at_css('body') | |
# First-level text nodes | |
body.xpath('text()').each do |node| | |
node.content = node.content.strip | |
end | |
content_tag(*args) { body.inner_html.strip.html_safe } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> | |
<h1>Foozle</h1> | |
<nav> | |
<%= inline_tag :ul, style: 'outline: 2px solid pink' do %> | |
<li>Foo</li> | |
<li>Baz <b>Twiz</b> Miz</li> | |
<li>Fiz</li> | |
<% end %> | |
</nav> | |
</header> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> | |
<h1>Foozle</h1> | |
<nav> | |
<ul style="outline: 2px solid pink"><li>Foo</li><li>Baz <b>Twiz</b> Miz</li><li>Fiz</li></ul> </nav> | |
</header> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment