Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

problem with attr: 'false' #29

Closed
aaronjensen opened this Issue · 7 comments

4 participants

@aaronjensen

I've seen a couple bugs about this that say its fixed, but I can't get it working.

Haml behaves differently than hamlc in this case:

%param{ :value => 'false' }
%param{ :value => false }

In haml, this gets rendered to:

<param value='false' />
<param />

hamlc gets rendered to

<param />
<param />

There is no way that I can tell to set value to be 'false' other than to do this:

!= "<param value = 'true' />"
@netzpirat netzpirat closed this issue from a commit
@netzpirat Improve boolean conversions. (Closes #29)
Prior to this commit, code like

    %param{ :value => 'false' }
    %param{ :value => 'true' }

 would be treated like boolean values, but they should be
 passed unmodified. This behaviour was changed in the
 HAML compiler, but leads to a further change:

 The boolean logic conversion at runtime, wasn't able to
 distinguish between real booleans and boolean like text
 attributes. The workaround to this is to let the clean function
 (`$c`) mark real boolean values with a hidden unicode marker
 character.
691c6bd
@netzpirat netzpirat closed this in 691c6bd
@netzpirat
Owner

Thanks for reporting, the boolean attributes behaved in fact different than Ruby HAML. The challenge for haml-coffee is that we have two different phases for processing Boolean logic: compile time (easy) and render time (tricky). The render time conversion was not able to differentiate between real booleans and boolean-like string. I found a nifty little trick to finally be able to distinguish between these, so with version 1.1.0 haml-coffee should behave correct for both, compile and render time boolean attributes.

@coffeebite

With this update, my regular checked='true' has stopped working. I have restarted my rails server, updated my file so that it is certainly recompiled. The hidden unicode is in the hamlc javascript but not in my templates.
Do I need to specify some special charset somewhere?

@netzpirat
Owner

JavaScript supports Unicode since version 1.3, so there's no need for a special charset.

With this change, checked: 'true' is now seen a plain String literal and no boolean conversion logic is performed:

coffee> hc = require 'haml-coffee'
{ compile: [Function],
  template: [Function] }
coffee> t = hc.compile "%input{ type: 'checkbox', checked: 'true' }"
[Function]
coffee> t()
'<input type=\'checkbox\' checked=\'true\'>'

whereas checked: true will be recognized as boolean:

coffee> hc = require 'haml-coffee'
{ compile: [Function],
  template: [Function] }
coffee> t = hc.compile "%input{ type: 'checkbox', checked: true }"
[Function]
coffee> t()
'<input type=\'checkbox\' checked>'

As you see, I cannot reproduce it currently.

There are currently quite some integration tests that covers booleans: This template will be converted to this HTML5 and this XHTML result. Also the Haml Specs covers some boolean conversions.

Can you please give me some information about your runtime and provide the failing code snippet? Thanks.

@coffeebite

Hi Michael
I'm actually using haml-coffee-assets for my rails project. But I'm guessing this project is responsible for the logic of template generation?

The problem lies here in the generated template file:

$o.push("<input name='privacy' type='radio' value='onlyme' checked='" + privacy + "'>");

As you can see, single quotes are being forced around the checked value without any unicode in them.

@netzpirat
Owner

Yep, haml-coffee is the template engine, whereas haml_coffee_assets is only the Rails asset pipeline integration.

I'm still not able to reproduce it:

coffee> hc = require 'haml-coffee'
{ compile: [Function],
  template: [Function] }
coffee> t = hc.compile "- privacy = true\n%input{ name: 'privacy', type: 'radio', value: 'onlyme', checked: privacy }"
[Function]
coffee> t()
'<input name=\'privacy\' type=\'radio\' value=\'onlyme\' checked>'
coffee> t = hc.compile "- privacy = false\n%input{ name: 'privacy', type: 'radio', value: 'onlyme', checked: privacy }"
[Function]
coffee> t()
'<input name=\'privacy\' type=\'radio\' value=\'onlyme\'>'

or with a context variable:

coffee> hc = require 'haml-coffee'
{ compile: [Function],
  template: [Function] }
coffee> t = hc.compile "%input{ name: 'privacy', type: 'radio', value: 'onlyme', checked: @privacy }"
[Function]
coffee> t({ privacy: true })
'<input name=\'privacy\' type=\'radio\' value=\'onlyme\' checked>'
coffee> t({ privacy: false })
'<input name=\'privacy\' type=\'radio\' value=\'onlyme\'>'

Can you please provide the actual haml-coffee snippet instead of the generated template code?

@mrchess

@aaronjensen @coffeebite did either of you figure out how to actually resolve this issue?

@aaronjensen

I don't recall... I haven't been using hamlc recently so I won't be of much help.

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.