Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Whitespaces and CSS :empty selector #569

Open
the8472 opened this Issue · 5 comments

3 participants

the8472 Norman Clarke Paul Peganov
the8472

When i do something like

%div.message-list
   - @messages.each do |m|
     =m.body

Then @messages may be empty and the div will contain a whitespace. Therefore I can't use the CSS :empty selector (which only matches completely-empty elements). Basically I can't use the :empty selector for anything while using haml.

Since haml does not allow content on the same line and indented blocks at the same time i'm forced to add the newline after the div, which in turn causes the whitespace to always be rendered.

Adding conditionals or whitespace eaters seems quite noisy just that i can use a CSS selector on elements.

Maybe constructs like this would work:

%div.message-list=@messages.map do |m|
   =m.body

Or an option to always munch whitespaces in otherwise empty tags

Norman Clarke
Owner

Adding conditionals or whitespace eaters seems quite noisy

I agree.

Or an option to always munch whitespaces in otherwise empty tags

Perhaps this should in fact be the default behavior, it seems more intuitive than adding whitespace when there's no content. Not sure how much this might break backwards compatibility though, so we'll have to consider it carefully.

Or an option to always munch whitespaces in otherwise empty tags

Haml probably has too many configuration options as it is, and whatever the default is, there are ways to work around this in the syntax - either like it is now where you can use whitespace control, or a potential change in the tag rendering as you suggest, where you could control it by emitting either content of a newline if you want to ensure whitespace is added.

the8472

Haml probably has too many configuration options as it is

I only count 11 options in the official docs.

Convention over configuration just means providing sane default behavior, not providing no configuration at all.

Norman Clarke
Owner

I think you're confusing "ability to control the output" with "having a configuration option." You'll still have the ability to control the output, but you'll do it through Haml syntax and your document's content, just like you do now.

What I don't want to do is add another option to Haml::Options. 11 is a lot.

the8472

Not sure how much this might break backwards compatibility though, so we'll have to consider it carefully.

It would have an impact on DOM tree (firstChild would be null instead of an one-space text node) so it might affect some sloppy javascript. An empty span vs. one containing a space could also affect line-level styling/layout.

Paul Peganov

Just encountered the same issue. So... this can be solved:
1. by placing content on the same line as the container element, if possible to fit
2. if impossible to fit, render a partial inside that block

There might be implications with these solutions that I don't know just yet, and this seems like hackery. But it should work. I applied solution 1 since I was using content_for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.