Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class-attribute is reordered when using .foo and (class="foo") #306

Closed
mrcljx opened this issue Oct 18, 2010 · 4 comments
Closed

class-attribute is reordered when using .foo and (class="foo") #306

mrcljx opened this issue Oct 18, 2010 · 4 comments

Comments

@mrcljx
Copy link

mrcljx commented Oct 18, 2010

I was confused by this output when I tried to use jquery-tmpl conditions in the class:

# .a(class="{{if x}}{{/if}}")
Haml::Engine.new('.a(class="{{if x}}{{/if}}")').to_html
#=> "<div class='a x}}{{/if}} {{if'></div>\n"

# %div(class="a {{if x}}{{/if}}")
Haml::Engine.new('%div(class="a {{if x}}{{/if}}")').to_html
#=> "<div class='a {{if x}}{{/if}}'></div>\n"

The current behavior prevents duplicate classes (.foo(class="foo")) but it would be better just to leave the class-attribute order untouched.

@nex3
Copy link
Collaborator

nex3 commented Oct 19, 2010

I'm tentatively okay with this, but it's important that the classes are emitted in a deterministic order for testing purposes. Internally, Haml does array1 | array2 to ensure uniqueness of classes; if you can show that this maintains a deterministic order back through Ruby 1.8.6 I'll remove the sorting.

@k0kubun
Copy link
Member

k0kubun commented Mar 28, 2017

Closing because it passed 7 years without progress. Reopening because now we have progress.

@topherfangio
Copy link

For anyone else hitting this problem and needing a quick solution, I have hacked together some quick code (based on #882) that you can require to fix this.

# Override Haml to NOT sort class attributes.
#
# This is required because Semantic/Fomantic care about class ordering :facepalm:
#
# @see https://github.com/haml/haml/issues/306
module Haml
  module AttributeBuilder
    class << self
      alias_method :original_merge_value, :merge_value

      def merge_value(key, to, from)

        if key == 'class'
          merged_class = filter_and_join(from, ' ')
          if to && merged_class
            merged_class = (to.split(' ') | merged_class.split(' ')).join(' ')
          elsif to || merged_class
            merged_class ||= to
          end
          merged_class
        else
          original_merge_value(key, to, from)
        end

      end
    end
  end
end

I have not seen any problems yet, but please let me know if you find anything!

HamptonMakes pushed a commit that referenced this issue Jul 14, 2020
Coming into alignment with the haml-spec and based off of the decision made at haml/haml-spec#15 and #306
@HamptonMakes
Copy link
Member

This has been implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants