Skip to content

Instantly share code, notes, and snippets.

@rafbm
Last active December 14, 2015 09:50
Show Gist options
  • Save rafbm/5067714 to your computer and use it in GitHub Desktop.
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`.
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
<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>
<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