phlex-ruby / phlex

A framework for building object-oriented views in Ruby.
https://beta.phlex.fun
MIT License
1.3k stars 84 forks source link

2.0.0.rc1 - Attributes passed as symbols that have underscores are dasherized (lose their underscore) #827

Closed davidalejandroaguilar closed 20 hours ago

davidalejandroaguilar commented 21 hours ago

Description

When passing attributes with underscores using symbols, such as id: :my_id, they are converted to id="my-id" on the resulting HTML.

The expected behavior is that they retain the underscore, e.g. id="my_id".

Version

This is happening on 2.0.0.rc1.

Reproduction

script.rb

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'phlex', '2.0.0.rc1'
  gem 'rspec'
end

class HelloWorld < Phlex::HTML
  def view_template
    # When passed as strings, the values retain the underscores.
    h1 id: "hello_world", class: "hello_world" do
    end

    # When passed as symbols, the values dasherized.
    h2 id: :hello_world, class: :hello_world do
    end
  end
end

RSpec.describe HelloWorld do
  subject { described_class.new }

  describe "#call" do
    it "retains underscores for attributes" do
      rendered = subject.call

      expect(rendered).to eq(%(<h1 id="hello_world" class="hello_world"></h1><h2 id="hello_world" class="hello_world"></h2>))
    end
  end
end

RSpec.configure do |config|
  config.formatter = :documentation
end

RSpec::Core::Runner.run([])

Run via:

ruby script.rb

Output:

HelloWorld
  #call
    retains underscores for attributes (FAILED - 1)

Failures:

  1) HelloWorld#call retains underscores for attributes
     Failure/Error: expect(rendered).to eq(%(<h1 id="hello_world" class="hello_world"></h1><h2 id="hello_world" class="hello_world"></h2>))

       expected: "<h1 id=\"hello_world\" class=\"hello_world\"></h1><h2 id=\"hello_world\" class=\"hello_world\"></h2>"
            got: "<h1 id=\"hello_world\" class=\"hello_world\"></h1><h2 id=\"hello-world\" class=\"hello-world\"></h2>"
stephannv commented 20 hours ago

Related: https://github.com/orgs/phlex-ruby/discussions/785

This is the intended behavior in 2.x, so you should use id: "my_id".

davidalejandroaguilar commented 20 hours ago

Ahhh thanks @stephannv, forgot about discussions!

Going to close this now then.