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

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

Closed
davidalejandroaguilar opened this issue Dec 2, 2024 · 2 comments

Comments

@davidalejandroaguilar
Copy link

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
Copy link
Contributor

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
Copy link
Author

Ahhh thanks @stephannv, forgot about discussions!

Going to close this now then.

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

No branches or pull requests

2 participants